github.com/amy/helm@v2.7.2+incompatible/cmd/helm/status_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/golang/protobuf/ptypes/timestamp"
    27  	"github.com/spf13/cobra"
    28  
    29  	"k8s.io/helm/pkg/helm"
    30  	"k8s.io/helm/pkg/proto/hapi/release"
    31  	"k8s.io/helm/pkg/timeconv"
    32  )
    33  
    34  var (
    35  	date       = timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
    36  	dateString = timeconv.String(&date)
    37  )
    38  
    39  // statusCase describes a test case dealing with the status of a release
    40  type statusCase struct {
    41  	name     string
    42  	args     []string
    43  	flags    []string
    44  	expected string
    45  	err      bool
    46  	rel      *release.Release
    47  }
    48  
    49  func TestStatusCmd(t *testing.T) {
    50  	tests := []statusCase{
    51  		{
    52  			name:     "get status of a deployed release",
    53  			args:     []string{"flummoxed-chickadee"},
    54  			expected: outputWithStatus("DEPLOYED\n\n"),
    55  			rel: releaseMockWithStatus(&release.Status{
    56  				Code: release.Status_DEPLOYED,
    57  			}),
    58  		},
    59  		{
    60  			name:     "get status of a deployed release with notes",
    61  			args:     []string{"flummoxed-chickadee"},
    62  			expected: outputWithStatus("DEPLOYED\n\nNOTES:\nrelease notes\n"),
    63  			rel: releaseMockWithStatus(&release.Status{
    64  				Code:  release.Status_DEPLOYED,
    65  				Notes: "release notes",
    66  			}),
    67  		},
    68  		{
    69  			name:     "get status of a deployed release with resources",
    70  			args:     []string{"flummoxed-chickadee"},
    71  			expected: outputWithStatus("DEPLOYED\n\nRESOURCES:\nresource A\nresource B\n\n"),
    72  			rel: releaseMockWithStatus(&release.Status{
    73  				Code:      release.Status_DEPLOYED,
    74  				Resources: "resource A\nresource B\n",
    75  			}),
    76  		},
    77  		{
    78  			name: "get status of a deployed release with test suite",
    79  			args: []string{"flummoxed-chickadee"},
    80  			expected: outputWithStatus(
    81  				fmt.Sprintf("DEPLOYED\n\nTEST SUITE:\nLast Started: %s\nLast Completed: %s\n\n", dateString, dateString) +
    82  					"TEST \tSTATUS \tINFO \tSTARTED \tCOMPLETED \n" +
    83  					fmt.Sprintf("test run 1\tSUCCESS \textra info\t%s\t%s\n", dateString, dateString) +
    84  					fmt.Sprintf("test run 2\tFAILURE \t \t%s\t%s\n", dateString, dateString)),
    85  			rel: releaseMockWithStatus(&release.Status{
    86  				Code: release.Status_DEPLOYED,
    87  				LastTestSuiteRun: &release.TestSuite{
    88  					StartedAt:   &date,
    89  					CompletedAt: &date,
    90  					Results: []*release.TestRun{
    91  						{
    92  							Name:        "test run 1",
    93  							Status:      release.TestRun_SUCCESS,
    94  							Info:        "extra info",
    95  							StartedAt:   &date,
    96  							CompletedAt: &date,
    97  						},
    98  						{
    99  							Name:        "test run 2",
   100  							Status:      release.TestRun_FAILURE,
   101  							StartedAt:   &date,
   102  							CompletedAt: &date,
   103  						},
   104  					},
   105  				},
   106  			}),
   107  		},
   108  	}
   109  
   110  	scmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   111  		return newStatusCmd(c, out)
   112  	}
   113  
   114  	var buf bytes.Buffer
   115  	for _, tt := range tests {
   116  		c := &helm.FakeClient{
   117  			Rels: []*release.Release{tt.rel},
   118  		}
   119  		cmd := scmd(c, &buf)
   120  		cmd.ParseFlags(tt.flags)
   121  		err := cmd.RunE(cmd, tt.args)
   122  		if (err != nil) != tt.err {
   123  			t.Errorf("%q. expected error, got '%v'", tt.name, err)
   124  		}
   125  
   126  		expected := strings.Replace(tt.expected, " ", "", -1)
   127  		got := strings.Replace(buf.String(), " ", "", -1)
   128  		if expected != got {
   129  			t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, expected, got)
   130  		}
   131  		buf.Reset()
   132  	}
   133  }
   134  
   135  func outputWithStatus(status string) string {
   136  	return fmt.Sprintf("LAST DEPLOYED: %s\nNAMESPACE: \nSTATUS: %s",
   137  		dateString,
   138  		status)
   139  }
   140  
   141  func releaseMockWithStatus(status *release.Status) *release.Release {
   142  	return &release.Release{
   143  		Name: "flummoxed-chickadee",
   144  		Info: &release.Info{
   145  			FirstDeployed: &date,
   146  			LastDeployed:  &date,
   147  			Status:        status,
   148  		},
   149  	}
   150  }