github.com/Beeketing/helm@v2.12.1+incompatible/cmd/helm/status_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  	"fmt"
    21  	"io"
    22  	"testing"
    23  
    24  	"github.com/golang/protobuf/ptypes/timestamp"
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/helm/pkg/helm"
    28  	"k8s.io/helm/pkg/proto/hapi/release"
    29  	"k8s.io/helm/pkg/timeconv"
    30  )
    31  
    32  var (
    33  	date       = timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
    34  	dateString = timeconv.String(&date)
    35  )
    36  
    37  func TestStatusCmd(t *testing.T) {
    38  	tests := []releaseCase{
    39  		{
    40  			name:     "get status of a deployed release",
    41  			args:     []string{"flummoxed-chickadee"},
    42  			expected: outputWithStatus("DEPLOYED\n\n"),
    43  			rels: []*release.Release{
    44  				releaseMockWithStatus(&release.Status{
    45  					Code: release.Status_DEPLOYED,
    46  				}),
    47  			},
    48  		},
    49  		{
    50  			name:     "get status of a deployed release with notes",
    51  			args:     []string{"flummoxed-chickadee"},
    52  			expected: outputWithStatus("DEPLOYED\n\nNOTES:\nrelease notes\n"),
    53  			rels: []*release.Release{
    54  				releaseMockWithStatus(&release.Status{
    55  					Code:  release.Status_DEPLOYED,
    56  					Notes: "release notes",
    57  				}),
    58  			},
    59  		},
    60  		{
    61  			name:     "get status of a deployed release with notes in json",
    62  			args:     []string{"flummoxed-chickadee"},
    63  			flags:    []string{"-o", "json"},
    64  			expected: `{"name":"flummoxed-chickadee","info":{"status":{"code":1,"notes":"release notes"},"first_deployed":{"seconds":242085845},"last_deployed":{"seconds":242085845}}}`,
    65  			rels: []*release.Release{
    66  				releaseMockWithStatus(&release.Status{
    67  					Code:  release.Status_DEPLOYED,
    68  					Notes: "release notes",
    69  				}),
    70  			},
    71  		},
    72  		{
    73  			name:     "get status of a deployed release with resources",
    74  			args:     []string{"flummoxed-chickadee"},
    75  			expected: outputWithStatus("DEPLOYED\n\nRESOURCES:\nresource A\nresource B\n\n"),
    76  			rels: []*release.Release{
    77  				releaseMockWithStatus(&release.Status{
    78  					Code:      release.Status_DEPLOYED,
    79  					Resources: "resource A\nresource B\n",
    80  				}),
    81  			},
    82  		},
    83  		{
    84  			name:     "get status of a deployed release with resources in YAML",
    85  			args:     []string{"flummoxed-chickadee"},
    86  			flags:    []string{"-o", "yaml"},
    87  			expected: "info:\n (.*)first_deployed:\n (.*)seconds: 242085845\n (.*)last_deployed:\n (.*)seconds: 242085845\n (.*)status:\n code: 1\n (.*)resources: |\n (.*)resource A\n (.*)resource B\nname: flummoxed-chickadee\n",
    88  			rels: []*release.Release{
    89  				releaseMockWithStatus(&release.Status{
    90  					Code:      release.Status_DEPLOYED,
    91  					Resources: "resource A\nresource B\n",
    92  				}),
    93  			},
    94  		},
    95  		{
    96  			name: "get status of a deployed release with test suite",
    97  			args: []string{"flummoxed-chickadee"},
    98  			expected: outputWithStatus(
    99  				fmt.Sprintf("DEPLOYED\n\nTEST SUITE:\nLast Started: %s\nLast Completed: %s\n\n", dateString, dateString) +
   100  					"TEST      \tSTATUS (.*)\tINFO (.*)\tSTARTED (.*)\tCOMPLETED (.*)\n" +
   101  					fmt.Sprintf("test run 1\tSUCCESS (.*)\textra info\t%s\t%s\n", dateString, dateString) +
   102  					fmt.Sprintf("test run 2\tFAILURE (.*)\t (.*)\t%s\t%s\n", dateString, dateString)),
   103  			rels: []*release.Release{
   104  				releaseMockWithStatus(&release.Status{
   105  					Code: release.Status_DEPLOYED,
   106  					LastTestSuiteRun: &release.TestSuite{
   107  						StartedAt:   &date,
   108  						CompletedAt: &date,
   109  						Results: []*release.TestRun{
   110  							{
   111  								Name:        "test run 1",
   112  								Status:      release.TestRun_SUCCESS,
   113  								Info:        "extra info",
   114  								StartedAt:   &date,
   115  								CompletedAt: &date,
   116  							},
   117  							{
   118  								Name:        "test run 2",
   119  								Status:      release.TestRun_FAILURE,
   120  								StartedAt:   &date,
   121  								CompletedAt: &date,
   122  							},
   123  						},
   124  					},
   125  				}),
   126  			},
   127  		},
   128  	}
   129  
   130  	runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   131  		return newStatusCmd(c, out)
   132  	})
   133  
   134  }
   135  
   136  func outputWithStatus(status string) string {
   137  	return fmt.Sprintf("LAST DEPLOYED: %s\nNAMESPACE: \nSTATUS: %s",
   138  		dateString,
   139  		status)
   140  }
   141  
   142  func releaseMockWithStatus(status *release.Status) *release.Release {
   143  	return &release.Release{
   144  		Name: "flummoxed-chickadee",
   145  		Info: &release.Info{
   146  			FirstDeployed: &date,
   147  			LastDeployed:  &date,
   148  			Status:        status,
   149  		},
   150  	}
   151  }