github.com/y-taka-23/helm@v2.8.0+incompatible/cmd/helm/upgrade_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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. Happy Helming!\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. Happy Helming!\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. Happy Helming!\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. Happy Helming!\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 --install'", 128 args: []string{"zany-bunny", chartPath}, 129 flags: []string{"-i"}, 130 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "zany-bunny", Version: 1, Chart: ch}), 131 expected: "Release \"zany-bunny\" has been upgraded. Happy Helming!\n", 132 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "zany-bunny", Version: 1, Chart: ch})}, 133 }, 134 { 135 name: "install a release with 'upgrade --install' and timeout", 136 args: []string{"crazy-bunny", chartPath}, 137 flags: []string{"-i", "--timeout", "120"}, 138 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch}), 139 expected: "Release \"crazy-bunny\" has been upgraded. Happy Helming!\n", 140 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch})}, 141 }, 142 { 143 name: "upgrade a release with wait", 144 args: []string{"crazy-bunny", chartPath}, 145 flags: []string{"--wait"}, 146 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2}), 147 expected: "Release \"crazy-bunny\" has been upgraded. Happy Helming!\n", 148 rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2})}, 149 }, 150 { 151 name: "upgrade a release with missing dependencies", 152 args: []string{"bonkers-bunny", missingDepsPath}, 153 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}), 154 err: true, 155 }, 156 { 157 name: "upgrade a release with bad dependencies", 158 args: []string{"bonkers-bunny", badDepsPath}, 159 resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}), 160 err: true, 161 }, 162 } 163 164 cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command { 165 return newUpgradeCmd(c, out) 166 } 167 168 runReleaseCases(t, tests, cmd) 169 170 }