github.com/Beeketing/helm@v2.12.1+incompatible/cmd/helm/reset_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 "bytes" 21 "io/ioutil" 22 "os" 23 "strings" 24 "testing" 25 26 "k8s.io/api/core/v1" 27 "k8s.io/client-go/kubernetes/fake" 28 29 "k8s.io/helm/pkg/helm" 30 "k8s.io/helm/pkg/helm/helmpath" 31 "k8s.io/helm/pkg/proto/hapi/release" 32 ) 33 34 type resetCase struct { 35 name string 36 err bool 37 resp []*release.Release 38 removeHelmHome bool 39 force bool 40 expectedActions int 41 expectedOutput string 42 } 43 44 func TestResetCmd(t *testing.T) { 45 46 verifyResetCmd(t, resetCase{ 47 name: "test reset command", 48 expectedActions: 3, 49 expectedOutput: "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster.", 50 }) 51 } 52 53 func TestResetCmd_removeHelmHome(t *testing.T) { 54 verifyResetCmd(t, resetCase{ 55 name: "test reset command - remove helm home", 56 removeHelmHome: true, 57 expectedActions: 3, 58 expectedOutput: "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster.", 59 }) 60 } 61 62 func TestReset_deployedReleases(t *testing.T) { 63 verifyResetCmd(t, resetCase{ 64 name: "test reset command - deployed releases", 65 resp: []*release.Release{ 66 helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}), 67 }, 68 err: true, 69 expectedOutput: "there are still 1 deployed releases (Tip: use --force to remove Tiller. Releases will not be deleted.)", 70 }) 71 } 72 73 func TestReset_forceFlag(t *testing.T) { 74 verifyResetCmd(t, resetCase{ 75 name: "test reset command - force flag", 76 force: true, 77 resp: []*release.Release{ 78 helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}), 79 }, 80 expectedActions: 3, 81 expectedOutput: "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster.", 82 }) 83 } 84 85 func verifyResetCmd(t *testing.T, tc resetCase) { 86 home, err := ioutil.TempDir("", "helm_home") 87 if err != nil { 88 t.Fatal(err) 89 } 90 defer os.Remove(home) 91 92 var buf bytes.Buffer 93 c := &helm.FakeClient{ 94 Rels: tc.resp, 95 } 96 fc := fake.NewSimpleClientset() 97 cmd := &resetCmd{ 98 removeHelmHome: tc.removeHelmHome, 99 force: tc.force, 100 out: &buf, 101 home: helmpath.Home(home), 102 client: c, 103 kubeClient: fc, 104 namespace: v1.NamespaceDefault, 105 } 106 107 err = cmd.run() 108 if !tc.err && err != nil { 109 t.Errorf("unexpected error: %v", err) 110 } 111 112 got := buf.String() 113 if tc.err { 114 got = err.Error() 115 } 116 117 actions := fc.Actions() 118 if tc.expectedActions > 0 && len(actions) != tc.expectedActions { 119 t.Errorf("Expected %d actions, got %d", tc.expectedActions, len(actions)) 120 } 121 if !strings.Contains(got, tc.expectedOutput) { 122 t.Errorf("expected %q, got %q", tc.expectedOutput, got) 123 } 124 _, err = os.Stat(home) 125 if !tc.removeHelmHome && err != nil { 126 t.Errorf("Helm home directory %s does not exist", home) 127 } 128 if tc.removeHelmHome && err == nil { 129 t.Errorf("Helm home directory %s exists", home) 130 } 131 }