github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/action/uninstall_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  
    25  	kubefake "github.com/stefanmcshane/helm/pkg/kube/fake"
    26  	"github.com/stefanmcshane/helm/pkg/release"
    27  )
    28  
    29  func uninstallAction(t *testing.T) *Uninstall {
    30  	config := actionConfigFixture(t)
    31  	unAction := NewUninstall(config)
    32  	return unAction
    33  }
    34  
    35  func TestUninstallRelease_deleteRelease(t *testing.T) {
    36  	is := assert.New(t)
    37  
    38  	unAction := uninstallAction(t)
    39  	unAction.DisableHooks = true
    40  	unAction.DryRun = false
    41  	unAction.KeepHistory = true
    42  
    43  	rel := releaseStub()
    44  	rel.Name = "keep-secret"
    45  	rel.Manifest = `{
    46  		"apiVersion": "v1",
    47  		"kind": "Secret",
    48  		"metadata": {
    49  		  "name": "secret",
    50  		  "annotations": {
    51  			"helm.sh/resource-policy": "keep"
    52  		  }
    53  		},
    54  		"type": "Opaque",
    55  		"data": {
    56  		  "password": "password"
    57  		}
    58  	}`
    59  	unAction.cfg.Releases.Create(rel)
    60  	res, err := unAction.Run(rel.Name)
    61  	is.NoError(err)
    62  	expected := `These resources were kept due to the resource policy:
    63  [Secret] secret
    64  `
    65  	is.Contains(res.Info, expected)
    66  }
    67  
    68  func TestUninstallRelease_Wait(t *testing.T) {
    69  	is := assert.New(t)
    70  
    71  	unAction := uninstallAction(t)
    72  	unAction.DisableHooks = true
    73  	unAction.DryRun = false
    74  	unAction.Wait = true
    75  
    76  	rel := releaseStub()
    77  	rel.Name = "come-fail-away"
    78  	rel.Manifest = `{
    79  		"apiVersion": "v1",
    80  		"kind": "Secret",
    81  		"metadata": {
    82  		  "name": "secret"
    83  		},
    84  		"type": "Opaque",
    85  		"data": {
    86  		  "password": "password"
    87  		}
    88  	}`
    89  	unAction.cfg.Releases.Create(rel)
    90  	failer := unAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
    91  	failer.WaitError = fmt.Errorf("U timed out")
    92  	unAction.cfg.KubeClient = failer
    93  	res, err := unAction.Run(rel.Name)
    94  	is.Error(err)
    95  	is.Contains(err.Error(), "U timed out")
    96  	is.Equal(res.Release.Info.Status, release.StatusUninstalled)
    97  }