1/*
2Copyright The Helm Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package main
18
19import (
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
28func 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 notes",
49		cmd:    "status flummoxed-chickadee",
50		golden: "output/status-with-notes.txt",
51		rels: releasesMockWithStatus(&release.Info{
52			Status: release.StatusDeployed,
53			Notes:  "release notes",
54		}),
55	}, {
56		name:   "get status of a deployed release with notes in json",
57		cmd:    "status flummoxed-chickadee -o json",
58		golden: "output/status.json",
59		rels: releasesMockWithStatus(&release.Info{
60			Status: release.StatusDeployed,
61			Notes:  "release notes",
62		}),
63	}, {
64		name:   "get status of a deployed release with test suite",
65		cmd:    "status flummoxed-chickadee",
66		golden: "output/status-with-test-suite.txt",
67		rels: releasesMockWithStatus(
68			&release.Info{
69				Status: release.StatusDeployed,
70			},
71			&release.Hook{
72				Name:   "never-run-test",
73				Events: []release.HookEvent{release.HookTest},
74			},
75			&release.Hook{
76				Name:   "passing-test",
77				Events: []release.HookEvent{release.HookTest},
78				LastRun: release.HookExecution{
79					StartedAt:   mustParseTime("2006-01-02T15:04:05Z"),
80					CompletedAt: mustParseTime("2006-01-02T15:04:07Z"),
81					Phase:       release.HookPhaseSucceeded,
82				},
83			},
84			&release.Hook{
85				Name:   "failing-test",
86				Events: []release.HookEvent{release.HookTest},
87				LastRun: release.HookExecution{
88					StartedAt:   mustParseTime("2006-01-02T15:10:05Z"),
89					CompletedAt: mustParseTime("2006-01-02T15:10:07Z"),
90					Phase:       release.HookPhaseFailed,
91				},
92			},
93			&release.Hook{
94				Name:   "passing-pre-install",
95				Events: []release.HookEvent{release.HookPreInstall},
96				LastRun: release.HookExecution{
97					StartedAt:   mustParseTime("2006-01-02T15:00:05Z"),
98					CompletedAt: mustParseTime("2006-01-02T15:00:07Z"),
99					Phase:       release.HookPhaseSucceeded,
100				},
101			},
102		),
103	}}
104	runTestCmd(t, tests)
105}
106
107func mustParseTime(t string) helmtime.Time {
108	res, _ := helmtime.Parse(time.RFC3339, t)
109	return res
110}
111
112func TestStatusCompletion(t *testing.T) {
113	releasesMockWithStatus := func(info *release.Info, hooks ...*release.Hook) []*release.Release {
114		info.LastDeployed = helmtime.Unix(1452902400, 0).UTC()
115		return []*release.Release{{
116			Name:      "athos",
117			Namespace: "default",
118			Info:      info,
119			Chart:     &chart.Chart{},
120			Hooks:     hooks,
121		}, {
122			Name:      "porthos",
123			Namespace: "default",
124			Info:      info,
125			Chart:     &chart.Chart{},
126			Hooks:     hooks,
127		}, {
128			Name:      "aramis",
129			Namespace: "default",
130			Info:      info,
131			Chart:     &chart.Chart{},
132			Hooks:     hooks,
133		}, {
134			Name:      "dartagnan",
135			Namespace: "gascony",
136			Info:      info,
137			Chart:     &chart.Chart{},
138			Hooks:     hooks,
139		}}
140	}
141
142	tests := []cmdTestCase{{
143		name:   "completion for status",
144		cmd:    "__complete status a",
145		golden: "output/status-comp.txt",
146		rels: releasesMockWithStatus(&release.Info{
147			Status: release.StatusDeployed,
148		}),
149	}, {
150		name:   "completion for status with too many arguments",
151		cmd:    "__complete status dartagnan ''",
152		golden: "output/status-wrong-args-comp.txt",
153		rels: releasesMockWithStatus(&release.Info{
154			Status: release.StatusDeployed,
155		}),
156	}, {
157		name:   "completion for status with too many arguments",
158		cmd:    "__complete status --debug a",
159		golden: "output/status-comp.txt",
160		rels: releasesMockWithStatus(&release.Info{
161			Status: release.StatusDeployed,
162		}),
163	}}
164	runTestCmd(t, tests)
165}
166
167func TestStatusRevisionCompletion(t *testing.T) {
168	revisionFlagCompletionTest(t, "status")
169}
170
171func TestStatusOutputCompletion(t *testing.T) {
172	outputFlagCompletionTest(t, "status")
173}
174