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