github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+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 upAction.rawValues = map[string]interface{}{} 53 54 res, err := upAction.Run(rel.Name, buildChart()) 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 upAction.rawValues = map[string]interface{}{} 78 79 res, err := upAction.Run(rel.Name, buildChart()) 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 upAction.rawValues = map[string]interface{}{} 103 104 _, err := upAction.Run(rel.Name, buildChart()) 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 }