github.com/x-helm/helm@v3.0.0-beta.3+incompatible/pkg/action/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 action
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	kubefake "helm.sh/helm/pkg/kube/fake"
    27  	"helm.sh/helm/pkg/release"
    28  )
    29  
    30  func upgradeAction(t *testing.T) *Upgrade {
    31  	config := actionConfigFixture(t)
    32  	upAction := NewUpgrade(config)
    33  	upAction.Namespace = "spaced"
    34  
    35  	return upAction
    36  }
    37  
    38  func TestUpgradeRelease_Wait(t *testing.T) {
    39  	is := assert.New(t)
    40  	req := require.New(t)
    41  
    42  	upAction := upgradeAction(t)
    43  	rel := releaseStub()
    44  	rel.Name = "come-fail-away"
    45  	rel.Info.Status = release.StatusDeployed
    46  	upAction.cfg.Releases.Create(rel)
    47  
    48  	failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
    49  	failer.WaitError = fmt.Errorf("I timed out")
    50  	upAction.cfg.KubeClient = failer
    51  	upAction.Wait = true
    52  	vals := map[string]interface{}{}
    53  
    54  	res, err := upAction.Run(rel.Name, buildChart(), vals)
    55  	req.Error(err)
    56  	is.Contains(res.Info.Description, "I timed out")
    57  	is.Equal(res.Info.Status, release.StatusFailed)
    58  }
    59  
    60  func TestUpgradeRelease_Atomic(t *testing.T) {
    61  	is := assert.New(t)
    62  	req := require.New(t)
    63  
    64  	t.Run("atomic rollback succeeds", func(t *testing.T) {
    65  		upAction := upgradeAction(t)
    66  
    67  		rel := releaseStub()
    68  		rel.Name = "nuketown"
    69  		rel.Info.Status = release.StatusDeployed
    70  		upAction.cfg.Releases.Create(rel)
    71  
    72  		failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
    73  		// We can't make Update error because then the rollback won't work
    74  		failer.WatchUntilReadyError = fmt.Errorf("arming key removed")
    75  		upAction.cfg.KubeClient = failer
    76  		upAction.Atomic = true
    77  		vals := map[string]interface{}{}
    78  
    79  		res, err := upAction.Run(rel.Name, buildChart(), vals)
    80  		req.Error(err)
    81  		is.Contains(err.Error(), "arming key removed")
    82  		is.Contains(err.Error(), "atomic")
    83  
    84  		// Now make sure it is actually upgraded
    85  		updatedRes, err := upAction.cfg.Releases.Get(res.Name, 3)
    86  		is.NoError(err)
    87  		// Should have rolled back to the previous
    88  		is.Equal(updatedRes.Info.Status, release.StatusDeployed)
    89  	})
    90  
    91  	t.Run("atomic uninstall fails", func(t *testing.T) {
    92  		upAction := upgradeAction(t)
    93  		rel := releaseStub()
    94  		rel.Name = "fallout"
    95  		rel.Info.Status = release.StatusDeployed
    96  		upAction.cfg.Releases.Create(rel)
    97  
    98  		failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
    99  		failer.UpdateError = fmt.Errorf("update fail")
   100  		upAction.cfg.KubeClient = failer
   101  		upAction.Atomic = true
   102  		vals := map[string]interface{}{}
   103  
   104  		_, err := upAction.Run(rel.Name, buildChart(), vals)
   105  		req.Error(err)
   106  		is.Contains(err.Error(), "update fail")
   107  		is.Contains(err.Error(), "an error occurred while rolling back the release")
   108  	})
   109  }
   110  
   111  func TestUpgradeRelease_ReuseValues(t *testing.T) {
   112  	is := assert.New(t)
   113  
   114  	t.Run("reuse values should work with values", func(t *testing.T) {
   115  		upAction := upgradeAction(t)
   116  
   117  		existingValues := map[string]interface{}{
   118  			"name":        "value",
   119  			"maxHeapSize": "128m",
   120  			"replicas":    2,
   121  		}
   122  		newValues := map[string]interface{}{
   123  			"name":        "newValue",
   124  			"maxHeapSize": "512m",
   125  			"cpu":         "12m",
   126  		}
   127  		expectedValues := map[string]interface{}{
   128  			"name":        "newValue",
   129  			"maxHeapSize": "512m",
   130  			"cpu":         "12m",
   131  			"replicas":    2,
   132  		}
   133  
   134  		rel := releaseStub()
   135  		rel.Name = "nuketown"
   136  		rel.Info.Status = release.StatusDeployed
   137  		rel.Config = existingValues
   138  
   139  		err := upAction.cfg.Releases.Create(rel)
   140  		is.NoError(err)
   141  
   142  		upAction.ReuseValues = true
   143  		// setting newValues and upgrading
   144  		res, err := upAction.Run(rel.Name, buildChart(), newValues)
   145  		is.NoError(err)
   146  
   147  		// Now make sure it is actually upgraded
   148  		updatedRes, err := upAction.cfg.Releases.Get(res.Name, 2)
   149  		is.NoError(err)
   150  
   151  		if updatedRes == nil {
   152  			is.Fail("Updated Release is nil")
   153  			return
   154  		}
   155  		is.Equal(release.StatusDeployed, updatedRes.Info.Status)
   156  		is.Equal(expectedValues, updatedRes.Config)
   157  	})
   158  }