github.com/werf/3p-helm@v2.8.1+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 notes in json",
    70  			args:     []string{"flummoxed-chickadee"},
    71  			flags:    []string{"-o", "json"},
    72  			expected: `{"name":"flummoxed-chickadee","info":{"status":{"code":1,"notes":"release notes"},"first_deployed":{"seconds":242085845},"last_deployed":{"seconds":242085845}}}`,
    73  			rel: releaseMockWithStatus(&release.Status{
    74  				Code:  release.Status_DEPLOYED,
    75  				Notes: "release notes",
    76  			}),
    77  		},
    78  		{
    79  			name:     "get status of a deployed release with resources",
    80  			args:     []string{"flummoxed-chickadee"},
    81  			expected: outputWithStatus("DEPLOYED\n\nRESOURCES:\nresource A\nresource B\n\n"),
    82  			rel: releaseMockWithStatus(&release.Status{
    83  				Code:      release.Status_DEPLOYED,
    84  				Resources: "resource A\nresource B\n",
    85  			}),
    86  		},
    87  		{
    88  			name:     "get status of a deployed release with resources in YAML",
    89  			args:     []string{"flummoxed-chickadee"},
    90  			flags:    []string{"-o", "yaml"},
    91  			expected: "info:\nfirst_deployed:\nseconds:242085845\nlast_deployed:\nseconds:242085845\nstatus:\ncode:1\nresources:|\nresourceA\nresourceB\nname:flummoxed-chickadee\n",
    92  			rel: releaseMockWithStatus(&release.Status{
    93  				Code:      release.Status_DEPLOYED,
    94  				Resources: "resource A\nresource B\n",
    95  			}),
    96  		},
    97  		{
    98  			name: "get status of a deployed release with test suite",
    99  			args: []string{"flummoxed-chickadee"},
   100  			expected: outputWithStatus(
   101  				fmt.Sprintf("DEPLOYED\n\nTEST SUITE:\nLast Started: %s\nLast Completed: %s\n\n", dateString, dateString) +
   102  					"TEST \tSTATUS \tINFO \tSTARTED \tCOMPLETED \n" +
   103  					fmt.Sprintf("test run 1\tSUCCESS \textra info\t%s\t%s\n", dateString, dateString) +
   104  					fmt.Sprintf("test run 2\tFAILURE \t \t%s\t%s\n", dateString, dateString)),
   105  			rel: releaseMockWithStatus(&release.Status{
   106  				Code: release.Status_DEPLOYED,
   107  				LastTestSuiteRun: &release.TestSuite{
   108  					StartedAt:   &date,
   109  					CompletedAt: &date,
   110  					Results: []*release.TestRun{
   111  						{
   112  							Name:        "test run 1",
   113  							Status:      release.TestRun_SUCCESS,
   114  							Info:        "extra info",
   115  							StartedAt:   &date,
   116  							CompletedAt: &date,
   117  						},
   118  						{
   119  							Name:        "test run 2",
   120  							Status:      release.TestRun_FAILURE,
   121  							StartedAt:   &date,
   122  							CompletedAt: &date,
   123  						},
   124  					},
   125  				},
   126  			}),
   127  		},
   128  	}
   129  
   130  	scmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   131  		return newStatusCmd(c, out)
   132  	}
   133  
   134  	var buf bytes.Buffer
   135  	for _, tt := range tests {
   136  		c := &helm.FakeClient{
   137  			Rels: []*release.Release{tt.rel},
   138  		}
   139  		cmd := scmd(c, &buf)
   140  		cmd.ParseFlags(tt.flags)
   141  		err := cmd.RunE(cmd, tt.args)
   142  		if (err != nil) != tt.err {
   143  			t.Errorf("%q. expected error, got '%v'", tt.name, err)
   144  		}
   145  
   146  		expected := strings.Replace(tt.expected, " ", "", -1)
   147  		got := strings.Replace(buf.String(), " ", "", -1)
   148  		if expected != got {
   149  			t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, expected, got)
   150  		}
   151  		buf.Reset()
   152  	}
   153  }
   154  
   155  func outputWithStatus(status string) string {
   156  	return fmt.Sprintf("LAST DEPLOYED: %s\nNAMESPACE: \nSTATUS: %s",
   157  		dateString,
   158  		status)
   159  }
   160  
   161  func releaseMockWithStatus(status *release.Status) *release.Release {
   162  	return &release.Release{
   163  		Name: "flummoxed-chickadee",
   164  		Info: &release.Info{
   165  			FirstDeployed: &date,
   166  			LastDeployed:  &date,
   167  			Status:        status,
   168  		},
   169  	}
   170  }