github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"testing"
    21  	"time"
    22  
    23  	"github.com/stefanmcshane/helm/pkg/chart"
    24  	"github.com/stefanmcshane/helm/pkg/release"
    25  	helmtime "github.com/stefanmcshane/helm/pkg/time"
    26  )
    27  
    28  func TestStatusCmd(t *testing.T) {
    29  	releasesMockWithStatus := func(info *release.Info, hooks ...*release.Hook) []*release.Release {
    30  		info.LastDeployed = helmtime.Unix(1452902400, 0).UTC()
    31  		return []*release.Release{{
    32  			Name:      "flummoxed-chickadee",
    33  			Namespace: "default",
    34  			Info:      info,
    35  			Chart:     &chart.Chart{},
    36  			Hooks:     hooks,
    37  		}}
    38  	}
    39  
    40  	tests := []cmdTestCase{{
    41  		name:   "get status of a deployed release",
    42  		cmd:    "status flummoxed-chickadee",
    43  		golden: "output/status.txt",
    44  		rels: releasesMockWithStatus(&release.Info{
    45  			Status: release.StatusDeployed,
    46  		}),
    47  	}, {
    48  		name:   "get status of a deployed release, with desc",
    49  		cmd:    "status --show-desc flummoxed-chickadee",
    50  		golden: "output/status-with-desc.txt",
    51  		rels: releasesMockWithStatus(&release.Info{
    52  			Status:      release.StatusDeployed,
    53  			Description: "Mock description",
    54  		}),
    55  	}, {
    56  		name:   "get status of a deployed release with notes",
    57  		cmd:    "status flummoxed-chickadee",
    58  		golden: "output/status-with-notes.txt",
    59  		rels: releasesMockWithStatus(&release.Info{
    60  			Status: release.StatusDeployed,
    61  			Notes:  "release notes",
    62  		}),
    63  	}, {
    64  		name:   "get status of a deployed release with notes in json",
    65  		cmd:    "status flummoxed-chickadee -o json",
    66  		golden: "output/status.json",
    67  		rels: releasesMockWithStatus(&release.Info{
    68  			Status: release.StatusDeployed,
    69  			Notes:  "release notes",
    70  		}),
    71  	}, {
    72  		name:   "get status of a deployed release with test suite",
    73  		cmd:    "status flummoxed-chickadee",
    74  		golden: "output/status-with-test-suite.txt",
    75  		rels: releasesMockWithStatus(
    76  			&release.Info{
    77  				Status: release.StatusDeployed,
    78  			},
    79  			&release.Hook{
    80  				Name:   "never-run-test",
    81  				Events: []release.HookEvent{release.HookTest},
    82  			},
    83  			&release.Hook{
    84  				Name:   "passing-test",
    85  				Events: []release.HookEvent{release.HookTest},
    86  				LastRun: release.HookExecution{
    87  					StartedAt:   mustParseTime("2006-01-02T15:04:05Z"),
    88  					CompletedAt: mustParseTime("2006-01-02T15:04:07Z"),
    89  					Phase:       release.HookPhaseSucceeded,
    90  				},
    91  			},
    92  			&release.Hook{
    93  				Name:   "failing-test",
    94  				Events: []release.HookEvent{release.HookTest},
    95  				LastRun: release.HookExecution{
    96  					StartedAt:   mustParseTime("2006-01-02T15:10:05Z"),
    97  					CompletedAt: mustParseTime("2006-01-02T15:10:07Z"),
    98  					Phase:       release.HookPhaseFailed,
    99  				},
   100  			},
   101  			&release.Hook{
   102  				Name:   "passing-pre-install",
   103  				Events: []release.HookEvent{release.HookPreInstall},
   104  				LastRun: release.HookExecution{
   105  					StartedAt:   mustParseTime("2006-01-02T15:00:05Z"),
   106  					CompletedAt: mustParseTime("2006-01-02T15:00:07Z"),
   107  					Phase:       release.HookPhaseSucceeded,
   108  				},
   109  			},
   110  		),
   111  	}}
   112  	runTestCmd(t, tests)
   113  }
   114  
   115  func mustParseTime(t string) helmtime.Time {
   116  	res, _ := helmtime.Parse(time.RFC3339, t)
   117  	return res
   118  }
   119  
   120  func TestStatusCompletion(t *testing.T) {
   121  	rels := []*release.Release{
   122  		{
   123  			Name:      "athos",
   124  			Namespace: "default",
   125  			Info: &release.Info{
   126  				Status: release.StatusDeployed,
   127  			},
   128  			Chart: &chart.Chart{
   129  				Metadata: &chart.Metadata{
   130  					Name:    "Athos-chart",
   131  					Version: "1.2.3",
   132  				},
   133  			},
   134  		}, {
   135  			Name:      "porthos",
   136  			Namespace: "default",
   137  			Info: &release.Info{
   138  				Status: release.StatusFailed,
   139  			},
   140  			Chart: &chart.Chart{
   141  				Metadata: &chart.Metadata{
   142  					Name:    "Porthos-chart",
   143  					Version: "111.222.333",
   144  				},
   145  			},
   146  		}, {
   147  			Name:      "aramis",
   148  			Namespace: "default",
   149  			Info: &release.Info{
   150  				Status: release.StatusUninstalled,
   151  			},
   152  			Chart: &chart.Chart{
   153  				Metadata: &chart.Metadata{
   154  					Name:    "Aramis-chart",
   155  					Version: "0.0.0",
   156  				},
   157  			},
   158  		}, {
   159  			Name:      "dartagnan",
   160  			Namespace: "gascony",
   161  			Info: &release.Info{
   162  				Status: release.StatusUnknown,
   163  			},
   164  			Chart: &chart.Chart{
   165  				Metadata: &chart.Metadata{
   166  					Name:    "Dartagnan-chart",
   167  					Version: "1.2.3-prerelease",
   168  				},
   169  			},
   170  		}}
   171  
   172  	tests := []cmdTestCase{{
   173  		name:   "completion for status",
   174  		cmd:    "__complete status a",
   175  		golden: "output/status-comp.txt",
   176  		rels:   rels,
   177  	}, {
   178  		name:   "completion for status with too many arguments",
   179  		cmd:    "__complete status dartagnan ''",
   180  		golden: "output/status-wrong-args-comp.txt",
   181  		rels:   rels,
   182  	}, {
   183  		name:   "completion for status with global flag",
   184  		cmd:    "__complete status --debug a",
   185  		golden: "output/status-comp.txt",
   186  		rels:   rels,
   187  	}}
   188  	runTestCmd(t, tests)
   189  }
   190  
   191  func TestStatusRevisionCompletion(t *testing.T) {
   192  	revisionFlagCompletionTest(t, "status")
   193  }
   194  
   195  func TestStatusOutputCompletion(t *testing.T) {
   196  	outputFlagCompletionTest(t, "status")
   197  }
   198  
   199  func TestStatusFileCompletion(t *testing.T) {
   200  	checkFileCompletion(t, "status", false)
   201  	checkFileCompletion(t, "status myrelease", false)
   202  }