github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/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. 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:     "install a release with 'upgrade --install' and custom description",
   144  			args:     []string{"crazy-bunny", chartPath},
   145  			flags:    []string{"-i", "--description", "foo"},
   146  			resp:     helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch, Description: "foo"}),
   147  			expected: "Release \"crazy-bunny\" has been upgraded. Happy Helming!\n",
   148  			rels:     []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 1, Chart: ch, Description: "foo"})},
   149  		},
   150  		{
   151  			name:     "upgrade a release with wait",
   152  			args:     []string{"crazy-bunny", chartPath},
   153  			flags:    []string{"--wait"},
   154  			resp:     helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2}),
   155  			expected: "Release \"crazy-bunny\" has been upgraded. Happy Helming!\n",
   156  			rels:     []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2})},
   157  		},
   158  		{
   159  			name:     "upgrade a release with description",
   160  			args:     []string{"crazy-bunny", chartPath},
   161  			flags:    []string{"--description", "foo"},
   162  			resp:     helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2}),
   163  			expected: "Release \"crazy-bunny\" has been upgraded. Happy Helming!\n",
   164  			rels:     []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-bunny", Version: 2, Chart: ch2, Description: "foo"})},
   165  		},
   166  		{
   167  			name: "upgrade a release with missing dependencies",
   168  			args: []string{"bonkers-bunny", missingDepsPath},
   169  			resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}),
   170  			err:  true,
   171  		},
   172  		{
   173  			name: "upgrade a release with bad dependencies",
   174  			args: []string{"bonkers-bunny", badDepsPath},
   175  			resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "bonkers-bunny", Version: 1, Chart: ch3}),
   176  			err:  true,
   177  		},
   178  	}
   179  
   180  	cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   181  		return newUpgradeCmd(c, out)
   182  	}
   183  
   184  	runReleaseCases(t, tests, cmd)
   185  
   186  }