github.com/latiif/helm@v2.15.0+incompatible/cmd/helm/upgrade_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 "io" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/spf13/cobra" 27 28 "k8s.io/helm/pkg/chartutil" 29 "k8s.io/helm/pkg/helm" 30 "k8s.io/helm/pkg/proto/hapi/chart" 31 "k8s.io/helm/pkg/proto/hapi/release" 32 ) 33 34 func TestUpgradeCmd(t *testing.T) { 35 tmpChart, _ := ioutil.TempDir("testdata", "tmp") 36 defer os.RemoveAll(tmpChart) 37 cfile := &chart.Metadata{ 38 Name: "testUpgradeChart", 39 Description: "A Helm chart for Kubernetes", 40 Version: "0.1.0", 41 } 42 chartPath, err := chartutil.Create(cfile, tmpChart) 43 if err != nil { 44 t.Errorf("Error creating chart for upgrade: %v", err) 45 } 46 ch, _ := chartutil.Load(chartPath) 47 _ = helm.ReleaseMock(&helm.MockReleaseOptions{ 48 Name: "funny-bunny", 49 Chart: ch, 50 }) 51 52 // update chart version 53 cfile = &chart.Metadata{ 54 Name: "testUpgradeChart", 55 Description: "A Helm chart for Kubernetes", 56 Version: "0.1.2", 57 } 58 59 chartPath, err = chartutil.Create(cfile, tmpChart) 60 if err != nil { 61 t.Errorf("Error creating chart: %v", err) 62 } 63 ch, err = chartutil.Load(chartPath) 64 if err != nil { 65 t.Errorf("Error loading updated chart: %v", err) 66 } 67 68 // update chart version again 69 cfile = &chart.Metadata{ 70 Name: "testUpgradeChart", 71 Description: "A Helm chart for Kubernetes", 72 Version: "0.1.3", 73 } 74 75 chartPath, err = chartutil.Create(cfile, tmpChart) 76 if err != nil { 77 t.Errorf("Error creating chart: %v", err) 78 } 79 var ch2 *chart.Chart 80 ch2, err = chartutil.Load(chartPath) 81 if err != nil { 82 t.Errorf("Error loading updated chart: %v", err) 83 } 84 85 originalDepsPath := filepath.Join("testdata/testcharts/reqtest") 86 missingDepsPath := filepath.Join("testdata/testcharts/chart-missing-deps") 87 badDepsPath := filepath.Join("testdata/testcharts/chart-bad-requirements") 88 var ch3 *chart.Chart 89 ch3, err = chartutil.Load(originalDepsPath) 90 if err != nil { 91 t.Errorf("Error loading chart with missing dependencies: %v", err) 92 } 93 94 tests := []releaseCase{ 95 { 96 name: "upgrade a release", 97 args: []string{"funny-bunny", chartPath}, 98 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 2, Chart: ch}), 99 expected: "Release \"funny-bunny\" has been upgraded.\n", 100 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 2, Chart: ch})}, 101 }, 102 { 103 name: "upgrade a release with timeout", 104 args: []string{"funny-bunny", chartPath}, 105 flags: []string{"--timeout", "120"}, 106 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 3, Chart: ch2}), 107 expected: "Release \"funny-bunny\" has been upgraded.\n", 108 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 3, Chart: ch2})}, 109 }, 110 { 111 name: "upgrade a release with --reset-values", 112 args: []string{"funny-bunny", chartPath}, 113 flags: []string{"--reset-values", "true"}, 114 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 4, Chart: ch2}), 115 expected: "Release \"funny-bunny\" has been upgraded.\n", 116 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 4, Chart: ch2})}, 117 }, 118 { 119 name: "upgrade a release with --reuse-values", 120 args: []string{"funny-bunny", chartPath}, 121 flags: []string{"--reuse-values", "true"}, 122 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 5, Chart: ch2}), 123 expected: "Release \"funny-bunny\" has been upgraded.\n", 124 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 5, Chart: ch2})}, 125 }, 126 { 127 name: "install a release with 'upgrade --atomic'", 128 args: []string{"funny-bunny", chartPath}, 129 flags: []string{"--atomic"}, 130 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 6, Chart: ch}), 131 expected: "Release \"funny-bunny\" has been upgraded.\n", 132 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "funny-bunny", Version: 6, Chart: ch})}, 133 }, 134 { 135 name: "install a release with 'upgrade --install'", 136 args: []string{"zany-bunny", chartPath}, 137 flags: []string{"-i"}, 138 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "zany-bunny", Version: 1, Chart: ch}), 139 expected: "Release \"zany-bunny\" has been upgraded.\n", 140 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "zany-bunny", Version: 1, Chart: ch})}, 141 }, 142 { 143 name: "install a release with 'upgrade --install' and timeout", 144 args: []string{"crazy-bunny", chartPath}, 145 flags: []string{"-i", "--timeout", "120"}, 146 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch}), 147 expected: "Release \"crazy-bunny\" has been upgraded.\n", 148 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch})}, 149 }, 150 { 151 name: "install a release with 'upgrade --install' and custom description", 152 args: []string{"crazy-bunny", chartPath}, 153 flags: []string{"-i", "--description", "foo"}, 154 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch, Description: "foo"}), 155 expected: "Release \"crazy-bunny\" has been upgraded.\n", 156 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch, Description: "foo"})}, 157 }, 158 { 159 name: "upgrade a release with wait", 160 args: []string{"crazy-bunny", chartPath}, 161 flags: []string{"--wait"}, 162 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2}), 163 expected: "Release \"crazy-bunny\" has been upgraded.\n", 164 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2})}, 165 }, 166 { 167 name: "upgrade a release with description", 168 args: []string{"crazy-bunny", chartPath}, 169 flags: []string{"--description", "foo"}, 170 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2}), 171 expected: "Release \"crazy-bunny\" has been upgraded.\n", 172 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2, Description: "foo"})}, 173 }, 174 { 175 name: "upgrade a release with missing dependencies", 176 args: []string{"bonkers-bunny", missingDepsPath}, 177 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}), 178 err: true, 179 }, 180 { 181 name: "upgrade a release with bad dependencies", 182 args: []string{"bonkers-bunny", badDepsPath}, 183 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}), 184 err: true, 185 }, 186 } 187 188 cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command { 189 return newUpgradeCmd(c, out) 190 } 191 192 runReleaseCases(t, tests, cmd) 193 194 }