github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"helm.sh/helm/v3/pkg/chart"
    24  	"helm.sh/helm/v3/pkg/release"
    25  	helmtime "helm.sh/helm/v3/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 resources",
    73  		cmd:    "status --show-resources flummoxed-chickadee",
    74  		golden: "output/status-with-resources.txt",
    75  		rels: releasesMockWithStatus(
    76  			&release.Info{
    77  				Status: release.StatusDeployed,
    78  			},
    79  		),
    80  	}, {
    81  		name:   "get status of a deployed release with resources in json",
    82  		cmd:    "status --show-resources flummoxed-chickadee -o json",
    83  		golden: "output/status-with-resources.json",
    84  		rels: releasesMockWithStatus(
    85  			&release.Info{
    86  				Status: release.StatusDeployed,
    87  			},
    88  		),
    89  	}, {
    90  		name:   "get status of a deployed release with test suite",
    91  		cmd:    "status flummoxed-chickadee",
    92  		golden: "output/status-with-test-suite.txt",
    93  		rels: releasesMockWithStatus(
    94  			&release.Info{
    95  				Status: release.StatusDeployed,
    96  			},
    97  			&release.Hook{
    98  				Name:   "never-run-test",
    99  				Events: []release.HookEvent{release.HookTest},
   100  			},
   101  			&release.Hook{
   102  				Name:   "passing-test",
   103  				Events: []release.HookEvent{release.HookTest},
   104  				LastRun: release.HookExecution{
   105  					StartedAt:   mustParseTime("2006-01-02T15:04:05Z"),
   106  					CompletedAt: mustParseTime("2006-01-02T15:04:07Z"),
   107  					Phase:       release.HookPhaseSucceeded,
   108  				},
   109  			},
   110  			&release.Hook{
   111  				Name:   "failing-test",
   112  				Events: []release.HookEvent{release.HookTest},
   113  				LastRun: release.HookExecution{
   114  					StartedAt:   mustParseTime("2006-01-02T15:10:05Z"),
   115  					CompletedAt: mustParseTime("2006-01-02T15:10:07Z"),
   116  					Phase:       release.HookPhaseFailed,
   117  				},
   118  			},
   119  			&release.Hook{
   120  				Name:   "passing-pre-install",
   121  				Events: []release.HookEvent{release.HookPreInstall},
   122  				LastRun: release.HookExecution{
   123  					StartedAt:   mustParseTime("2006-01-02T15:00:05Z"),
   124  					CompletedAt: mustParseTime("2006-01-02T15:00:07Z"),
   125  					Phase:       release.HookPhaseSucceeded,
   126  				},
   127  			},
   128  		),
   129  	}}
   130  	runTestCmd(t, tests)
   131  }
   132  
   133  func mustParseTime(t string) helmtime.Time {
   134  	res, _ := helmtime.Parse(time.RFC3339, t)
   135  	return res
   136  }
   137  
   138  func TestStatusCompletion(t *testing.T) {
   139  	rels := []*release.Release{
   140  		{
   141  			Name:      "athos",
   142  			Namespace: "default",
   143  			Info: &release.Info{
   144  				Status: release.StatusDeployed,
   145  			},
   146  			Chart: &chart.Chart{
   147  				Metadata: &chart.Metadata{
   148  					Name:    "Athos-chart",
   149  					Version: "1.2.3",
   150  				},
   151  			},
   152  		}, {
   153  			Name:      "porthos",
   154  			Namespace: "default",
   155  			Info: &release.Info{
   156  				Status: release.StatusFailed,
   157  			},
   158  			Chart: &chart.Chart{
   159  				Metadata: &chart.Metadata{
   160  					Name:    "Porthos-chart",
   161  					Version: "111.222.333",
   162  				},
   163  			},
   164  		}, {
   165  			Name:      "aramis",
   166  			Namespace: "default",
   167  			Info: &release.Info{
   168  				Status: release.StatusUninstalled,
   169  			},
   170  			Chart: &chart.Chart{
   171  				Metadata: &chart.Metadata{
   172  					Name:    "Aramis-chart",
   173  					Version: "0.0.0",
   174  				},
   175  			},
   176  		}, {
   177  			Name:      "dartagnan",
   178  			Namespace: "gascony",
   179  			Info: &release.Info{
   180  				Status: release.StatusUnknown,
   181  			},
   182  			Chart: &chart.Chart{
   183  				Metadata: &chart.Metadata{
   184  					Name:    "Dartagnan-chart",
   185  					Version: "1.2.3-prerelease",
   186  				},
   187  			},
   188  		}}
   189  
   190  	tests := []cmdTestCase{{
   191  		name:   "completion for status",
   192  		cmd:    "__complete status a",
   193  		golden: "output/status-comp.txt",
   194  		rels:   rels,
   195  	}, {
   196  		name:   "completion for status with too many arguments",
   197  		cmd:    "__complete status dartagnan ''",
   198  		golden: "output/status-wrong-args-comp.txt",
   199  		rels:   rels,
   200  	}, {
   201  		name:   "completion for status with global flag",
   202  		cmd:    "__complete status --debug a",
   203  		golden: "output/status-comp.txt",
   204  		rels:   rels,
   205  	}}
   206  	runTestCmd(t, tests)
   207  }
   208  
   209  func TestStatusRevisionCompletion(t *testing.T) {
   210  	revisionFlagCompletionTest(t, "status")
   211  }
   212  
   213  func TestStatusOutputCompletion(t *testing.T) {
   214  	outputFlagCompletionTest(t, "status")
   215  }
   216  
   217  func TestStatusFileCompletion(t *testing.T) {
   218  	checkFileCompletion(t, "status", false)
   219  	checkFileCompletion(t, "status myrelease", false)
   220  }