github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/init_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 "bytes" 21 "io/ioutil" 22 "os" 23 "strings" 24 "testing" 25 26 "github.com/ghodss/yaml" 27 28 "k8s.io/kubernetes/pkg/api" 29 "k8s.io/kubernetes/pkg/api/errors" 30 "k8s.io/kubernetes/pkg/apis/extensions" 31 "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" 32 testcore "k8s.io/kubernetes/pkg/client/testing/core" 33 "k8s.io/kubernetes/pkg/runtime" 34 35 "k8s.io/helm/cmd/helm/helmpath" 36 ) 37 38 func TestInitCmd(t *testing.T) { 39 home, err := ioutil.TempDir("", "helm_home") 40 if err != nil { 41 t.Fatal(err) 42 } 43 defer os.Remove(home) 44 45 var buf bytes.Buffer 46 fc := fake.NewSimpleClientset() 47 cmd := &initCmd{ 48 out: &buf, 49 home: helmpath.Home(home), 50 kubeClient: fc, 51 namespace: api.NamespaceDefault, 52 } 53 if err := cmd.run(); err != nil { 54 t.Errorf("expected error: %v", err) 55 } 56 actions := fc.Actions() 57 if len(actions) != 2 { 58 t.Errorf("Expected 2 actions, got %d", len(actions)) 59 } 60 if !actions[0].Matches("create", "deployments") { 61 t.Errorf("unexpected action: %v, expected create deployment", actions[0]) 62 } 63 if !actions[1].Matches("create", "services") { 64 t.Errorf("unexpected action: %v, expected create service", actions[1]) 65 } 66 expected := "Tiller (the helm server side component) has been installed into your Kubernetes Cluster." 67 if !strings.Contains(buf.String(), expected) { 68 t.Errorf("expected %q, got %q", expected, buf.String()) 69 } 70 } 71 72 func TestInitCmd_exists(t *testing.T) { 73 home, err := ioutil.TempDir("", "helm_home") 74 if err != nil { 75 t.Fatal(err) 76 } 77 defer os.Remove(home) 78 79 var buf bytes.Buffer 80 fc := fake.NewSimpleClientset(&extensions.Deployment{ 81 ObjectMeta: api.ObjectMeta{ 82 Namespace: api.NamespaceDefault, 83 Name: "tiller-deploy", 84 }, 85 }) 86 fc.PrependReactor("*", "*", func(action testcore.Action) (bool, runtime.Object, error) { 87 return true, nil, errors.NewAlreadyExists(api.Resource("deployments"), "1") 88 }) 89 cmd := &initCmd{ 90 out: &buf, 91 home: helmpath.Home(home), 92 kubeClient: fc, 93 namespace: api.NamespaceDefault, 94 } 95 if err := cmd.run(); err != nil { 96 t.Errorf("expected error: %v", err) 97 } 98 expected := "Warning: Tiller is already installed in the cluster.\n" + 99 "(Use --client-only to suppress this message, or --upgrade to upgrade Tiller to the current version.)" 100 if !strings.Contains(buf.String(), expected) { 101 t.Errorf("expected %q, got %q", expected, buf.String()) 102 } 103 } 104 105 func TestInitCmd_clientOnly(t *testing.T) { 106 home, err := ioutil.TempDir("", "helm_home") 107 if err != nil { 108 t.Fatal(err) 109 } 110 defer os.Remove(home) 111 112 var buf bytes.Buffer 113 fc := fake.NewSimpleClientset() 114 cmd := &initCmd{ 115 out: &buf, 116 home: helmpath.Home(home), 117 kubeClient: fc, 118 clientOnly: true, 119 namespace: api.NamespaceDefault, 120 } 121 if err := cmd.run(); err != nil { 122 t.Errorf("unexpected error: %v", err) 123 } 124 if len(fc.Actions()) != 0 { 125 t.Error("expected client call") 126 } 127 expected := "Not installing tiller due to 'client-only' flag having been set" 128 if !strings.Contains(buf.String(), expected) { 129 t.Errorf("expected %q, got %q", expected, buf.String()) 130 } 131 } 132 133 func TestInitCmd_dryRun(t *testing.T) { 134 // This is purely defensive in this case. 135 home, err := ioutil.TempDir("", "helm_home") 136 if err != nil { 137 t.Fatal(err) 138 } 139 dbg := flagDebug 140 flagDebug = true 141 defer func() { 142 os.Remove(home) 143 flagDebug = dbg 144 }() 145 146 var buf bytes.Buffer 147 fc := fake.NewSimpleClientset() 148 cmd := &initCmd{ 149 out: &buf, 150 home: helmpath.Home(home), 151 kubeClient: fc, 152 clientOnly: true, 153 dryRun: true, 154 namespace: api.NamespaceDefault, 155 } 156 if err := cmd.run(); err != nil { 157 t.Fatal(err) 158 } 159 if len(fc.Actions()) != 0 { 160 t.Error("expected no server calls") 161 } 162 163 var y map[string]interface{} 164 if err := yaml.Unmarshal(buf.Bytes(), &y); err != nil { 165 t.Errorf("Expected parseable YAML, got %q\n\t%s", buf.String(), err) 166 } 167 } 168 169 func TestEnsureHome(t *testing.T) { 170 home, err := ioutil.TempDir("", "helm_home") 171 if err != nil { 172 t.Fatal(err) 173 } 174 defer os.Remove(home) 175 176 b := bytes.NewBuffer(nil) 177 hh := helmpath.Home(home) 178 helmHome = home 179 if err := ensureDirectories(hh, b); err != nil { 180 t.Error(err) 181 } 182 if err := ensureDefaultRepos(hh, b); err != nil { 183 t.Error(err) 184 } 185 if err := ensureRepoFileFormat(hh.RepositoryFile(), b); err != nil { 186 t.Error(err) 187 } 188 189 expectedDirs := []string{hh.String(), hh.Repository(), hh.Cache(), hh.LocalRepository()} 190 for _, dir := range expectedDirs { 191 if fi, err := os.Stat(dir); err != nil { 192 t.Errorf("%s", err) 193 } else if !fi.IsDir() { 194 t.Errorf("%s is not a directory", fi) 195 } 196 } 197 198 if fi, err := os.Stat(hh.RepositoryFile()); err != nil { 199 t.Error(err) 200 } else if fi.IsDir() { 201 t.Errorf("%s should not be a directory", fi) 202 } 203 204 if fi, err := os.Stat(hh.LocalRepository(localRepoIndexFilePath)); err != nil { 205 t.Errorf("%s", err) 206 } else if fi.IsDir() { 207 t.Errorf("%s should not be a directory", fi) 208 } 209 }