github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+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/proto/hapi/release"
    30  	"k8s.io/helm/pkg/timeconv"
    31  )
    32  
    33  var (
    34  	date       = timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
    35  	dateString = timeconv.String(&date)
    36  )
    37  
    38  // statusCase describes a test case dealing with the status of a release
    39  type statusCase struct {
    40  	name     string
    41  	args     []string
    42  	flags    []string
    43  	expected string
    44  	err      bool
    45  	rel      *release.Release
    46  }
    47  
    48  func TestStatusCmd(t *testing.T) {
    49  	tests := []statusCase{
    50  		{
    51  			name:     "get status of a deployed release",
    52  			args:     []string{"flummoxed-chickadee"},
    53  			expected: outputWithStatus("DEPLOYED\n\n"),
    54  			rel: releaseMockWithStatus(&release.Status{
    55  				Code: release.Status_DEPLOYED,
    56  			}),
    57  		},
    58  		{
    59  			name:     "get status of a deployed release with notes",
    60  			args:     []string{"flummoxed-chickadee"},
    61  			expected: outputWithStatus("DEPLOYED\n\nNOTES:\nrelease notes\n"),
    62  			rel: releaseMockWithStatus(&release.Status{
    63  				Code:  release.Status_DEPLOYED,
    64  				Notes: "release notes",
    65  			}),
    66  		},
    67  		{
    68  			name:     "get status of a deployed release with resources",
    69  			args:     []string{"flummoxed-chickadee"},
    70  			expected: outputWithStatus("DEPLOYED\n\nRESOURCES:\nresource A\nresource B\n\n"),
    71  			rel: releaseMockWithStatus(&release.Status{
    72  				Code:      release.Status_DEPLOYED,
    73  				Resources: "resource A\nresource B\n",
    74  			}),
    75  		},
    76  		{
    77  			name: "get status of a deployed release with test suite",
    78  			args: []string{"flummoxed-chickadee"},
    79  			expected: outputWithStatus(
    80  				fmt.Sprintf("DEPLOYED\n\nTEST SUITE:\nLast Started: %s\nLast Completed: %s\n\n", dateString, dateString) +
    81  					fmt.Sprint("TEST \tSTATUS \tINFO \tSTARTED \tCOMPLETED \n") +
    82  					fmt.Sprintf("test run 1\tSUCCESS \textra info\t%s\t%s\n", dateString, dateString) +
    83  					fmt.Sprintf("test run 2\tFAILURE \t \t%s\t%s\n", dateString, dateString)),
    84  			rel: releaseMockWithStatus(&release.Status{
    85  				Code: release.Status_DEPLOYED,
    86  				LastTestSuiteRun: &release.TestSuite{
    87  					StartedAt:   &date,
    88  					CompletedAt: &date,
    89  					Results: []*release.TestRun{
    90  						{
    91  							Name:        "test run 1",
    92  							Status:      release.TestRun_SUCCESS,
    93  							Info:        "extra info",
    94  							StartedAt:   &date,
    95  							CompletedAt: &date,
    96  						},
    97  						{
    98  							Name:        "test run 2",
    99  							Status:      release.TestRun_FAILURE,
   100  							StartedAt:   &date,
   101  							CompletedAt: &date,
   102  						},
   103  					},
   104  				},
   105  			}),
   106  		},
   107  	}
   108  
   109  	scmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
   110  		return newStatusCmd(c, out)
   111  	}
   112  
   113  	var buf bytes.Buffer
   114  	for _, tt := range tests {
   115  		c := &fakeReleaseClient{
   116  			rels: []*release.Release{tt.rel},
   117  		}
   118  		cmd := scmd(c, &buf)
   119  		cmd.ParseFlags(tt.flags)
   120  		err := cmd.RunE(cmd, tt.args)
   121  		if (err != nil) != tt.err {
   122  			t.Errorf("%q. expected error, got '%v'", tt.name, err)
   123  		}
   124  
   125  		expected := strings.Replace(tt.expected, " ", "", -1)
   126  		got := strings.Replace(buf.String(), " ", "", -1)
   127  		if expected != got {
   128  			t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, expected, got)
   129  		}
   130  		buf.Reset()
   131  	}
   132  }
   133  
   134  func outputWithStatus(status string) string {
   135  	return fmt.Sprintf("LAST DEPLOYED: %s\nNAMESPACE: \nSTATUS: %s",
   136  		dateString,
   137  		status)
   138  }
   139  
   140  func releaseMockWithStatus(status *release.Status) *release.Release {
   141  	return &release.Release{
   142  		Name: "flummoxed-chickadee",
   143  		Info: &release.Info{
   144  			FirstDeployed: &date,
   145  			LastDeployed:  &date,
   146  			Status:        status,
   147  		},
   148  	}
   149  }