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	"fmt"
21	"testing"
22
23	"helm.sh/helm/v3/pkg/release"
24)
25
26func TestHistoryCmd(t *testing.T) {
27	mk := func(name string, vers int, status release.Status) *release.Release {
28		return release.Mock(&release.MockReleaseOptions{
29			Name:    name,
30			Version: vers,
31			Status:  status,
32		})
33	}
34
35	tests := []cmdTestCase{{
36		name: "get history for release",
37		cmd:  "history angry-bird",
38		rels: []*release.Release{
39			mk("angry-bird", 4, release.StatusDeployed),
40			mk("angry-bird", 3, release.StatusSuperseded),
41			mk("angry-bird", 2, release.StatusSuperseded),
42			mk("angry-bird", 1, release.StatusSuperseded),
43		},
44		golden: "output/history.txt",
45	}, {
46		name: "get history with max limit set",
47		cmd:  "history angry-bird --max 2",
48		rels: []*release.Release{
49			mk("angry-bird", 4, release.StatusDeployed),
50			mk("angry-bird", 3, release.StatusSuperseded),
51		},
52		golden: "output/history-limit.txt",
53	}, {
54		name: "get history with yaml output format",
55		cmd:  "history angry-bird --output yaml",
56		rels: []*release.Release{
57			mk("angry-bird", 4, release.StatusDeployed),
58			mk("angry-bird", 3, release.StatusSuperseded),
59		},
60		golden: "output/history.yaml",
61	}, {
62		name: "get history with json output format",
63		cmd:  "history angry-bird --output json",
64		rels: []*release.Release{
65			mk("angry-bird", 4, release.StatusDeployed),
66			mk("angry-bird", 3, release.StatusSuperseded),
67		},
68		golden: "output/history.json",
69	}}
70	runTestCmd(t, tests)
71}
72
73func TestHistoryOutputCompletion(t *testing.T) {
74	outputFlagCompletionTest(t, "history")
75}
76
77func revisionFlagCompletionTest(t *testing.T, cmdName string) {
78	mk := func(name string, vers int, status release.Status) *release.Release {
79		return release.Mock(&release.MockReleaseOptions{
80			Name:    name,
81			Version: vers,
82			Status:  status,
83		})
84	}
85
86	releases := []*release.Release{
87		mk("musketeers", 11, release.StatusDeployed),
88		mk("musketeers", 10, release.StatusSuperseded),
89		mk("musketeers", 9, release.StatusSuperseded),
90		mk("musketeers", 8, release.StatusSuperseded),
91	}
92
93	tests := []cmdTestCase{{
94		name:   "completion for revision flag",
95		cmd:    fmt.Sprintf("__complete %s musketeers --revision ''", cmdName),
96		rels:   releases,
97		golden: "output/revision-comp.txt",
98	}, {
99		name:   "completion for revision flag with too few args",
100		cmd:    fmt.Sprintf("__complete %s --revision ''", cmdName),
101		rels:   releases,
102		golden: "output/revision-wrong-args-comp.txt",
103	}, {
104		name:   "completion for revision flag with too many args",
105		cmd:    fmt.Sprintf("__complete %s three musketeers --revision ''", cmdName),
106		rels:   releases,
107		golden: "output/revision-wrong-args-comp.txt",
108	}}
109	runTestCmd(t, tests)
110}
111
112func TestHistoryFileCompletion(t *testing.T) {
113	checkFileCompletion(t, "history", false)
114	checkFileCompletion(t, "history myrelease", false)
115}
116