github.com/cloudposse/helm@v2.2.3+incompatible/cmd/helm/installer/install_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 installer // import "k8s.io/helm/cmd/helm/installer" 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/ghodss/yaml" 24 "k8s.io/kubernetes/pkg/api" 25 "k8s.io/kubernetes/pkg/api/errors" 26 "k8s.io/kubernetes/pkg/apis/extensions" 27 "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" 28 testcore "k8s.io/kubernetes/pkg/client/testing/core" 29 "k8s.io/kubernetes/pkg/runtime" 30 31 "k8s.io/helm/pkg/version" 32 ) 33 34 func TestDeploymentManifest(t *testing.T) { 35 36 tests := []struct { 37 name string 38 image string 39 canary bool 40 expect string 41 }{ 42 {"default", "", false, "gcr.io/kubernetes-helm/tiller:" + version.Version}, 43 {"canary", "example.com/tiller", true, "gcr.io/kubernetes-helm/tiller:canary"}, 44 {"custom", "example.com/tiller:latest", false, "example.com/tiller:latest"}, 45 } 46 47 for _, tt := range tests { 48 49 o, err := DeploymentManifest(api.NamespaceDefault, tt.image, tt.canary) 50 if err != nil { 51 t.Fatalf("%s: error %q", tt.name, err) 52 } 53 var dep extensions.Deployment 54 if err := yaml.Unmarshal([]byte(o), &dep); err != nil { 55 t.Fatalf("%s: error %q", tt.name, err) 56 } 57 58 if got := dep.Spec.Template.Spec.Containers[0].Image; got != tt.expect { 59 t.Errorf("%s: expected image %q, got %q", tt.name, tt.expect, got) 60 } 61 62 if got := dep.Spec.Template.Spec.Containers[0].Env[0].Value; got != api.NamespaceDefault { 63 t.Errorf("%s: expected namespace %q, got %q", tt.name, api.NamespaceDefault, got) 64 } 65 } 66 } 67 68 func TestServiceManifest(t *testing.T) { 69 o, err := ServiceManifest(api.NamespaceDefault) 70 if err != nil { 71 t.Fatalf("error %q", err) 72 } 73 var svc api.Service 74 if err := yaml.Unmarshal([]byte(o), &svc); err != nil { 75 t.Fatalf("error %q", err) 76 } 77 78 if got := svc.ObjectMeta.Namespace; got != api.NamespaceDefault { 79 t.Errorf("expected namespace %s, got %s", api.NamespaceDefault, got) 80 } 81 } 82 83 func TestInstall(t *testing.T) { 84 image := "gcr.io/kubernetes-helm/tiller:v2.0.0" 85 86 fc := &fake.Clientset{} 87 fc.AddReactor("create", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 88 obj := action.(testcore.CreateAction).GetObject().(*extensions.Deployment) 89 l := obj.GetLabels() 90 if reflect.DeepEqual(l, map[string]string{"app": "helm"}) { 91 t.Errorf("expected labels = '', got '%s'", l) 92 } 93 i := obj.Spec.Template.Spec.Containers[0].Image 94 if i != image { 95 t.Errorf("expected image = '%s', got '%s'", image, i) 96 } 97 return true, obj, nil 98 }) 99 fc.AddReactor("create", "services", func(action testcore.Action) (bool, runtime.Object, error) { 100 obj := action.(testcore.CreateAction).GetObject().(*api.Service) 101 l := obj.GetLabels() 102 if reflect.DeepEqual(l, map[string]string{"app": "helm"}) { 103 t.Errorf("expected labels = '', got '%s'", l) 104 } 105 n := obj.ObjectMeta.Namespace 106 if n != api.NamespaceDefault { 107 t.Errorf("expected namespace = '%s', got '%s'", api.NamespaceDefault, n) 108 } 109 return true, obj, nil 110 }) 111 112 if err := Install(fc, api.NamespaceDefault, image, false, false); err != nil { 113 t.Errorf("unexpected error: %#+v", err) 114 } 115 116 if actions := fc.Actions(); len(actions) != 2 { 117 t.Errorf("unexpected actions: %v, expected 2 actions got %d", actions, len(actions)) 118 } 119 } 120 121 func TestInstall_canary(t *testing.T) { 122 fc := &fake.Clientset{} 123 fc.AddReactor("create", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 124 obj := action.(testcore.CreateAction).GetObject().(*extensions.Deployment) 125 i := obj.Spec.Template.Spec.Containers[0].Image 126 if i != "gcr.io/kubernetes-helm/tiller:canary" { 127 t.Errorf("expected canary image, got '%s'", i) 128 } 129 return true, obj, nil 130 }) 131 fc.AddReactor("create", "services", func(action testcore.Action) (bool, runtime.Object, error) { 132 obj := action.(testcore.CreateAction).GetObject().(*api.Service) 133 return true, obj, nil 134 }) 135 136 if err := Install(fc, api.NamespaceDefault, "", true, false); err != nil { 137 t.Errorf("unexpected error: %#+v", err) 138 } 139 140 if actions := fc.Actions(); len(actions) != 2 { 141 t.Errorf("unexpected actions: %v, expected 2 actions got %d", actions, len(actions)) 142 } 143 } 144 145 func TestUpgrade(t *testing.T) { 146 image := "gcr.io/kubernetes-helm/tiller:v2.0.0" 147 148 existingDeployment := deployment(api.NamespaceDefault, "imageToReplace", false) 149 existingService := service(api.NamespaceDefault) 150 151 fc := &fake.Clientset{} 152 fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 153 return true, existingDeployment, nil 154 }) 155 fc.AddReactor("update", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 156 obj := action.(testcore.UpdateAction).GetObject().(*extensions.Deployment) 157 i := obj.Spec.Template.Spec.Containers[0].Image 158 if i != image { 159 t.Errorf("expected image = '%s', got '%s'", image, i) 160 } 161 return true, obj, nil 162 }) 163 fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) { 164 return true, existingService, nil 165 }) 166 167 if err := Upgrade(fc, api.NamespaceDefault, image, false); err != nil { 168 t.Errorf("unexpected error: %#+v", err) 169 } 170 171 if actions := fc.Actions(); len(actions) != 3 { 172 t.Errorf("unexpected actions: %v, expected 3 actions got %d", actions, len(actions)) 173 } 174 } 175 176 func TestUpgrade_serviceNotFound(t *testing.T) { 177 image := "gcr.io/kubernetes-helm/tiller:v2.0.0" 178 179 existingDeployment := deployment(api.NamespaceDefault, "imageToReplace", false) 180 181 fc := &fake.Clientset{} 182 fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 183 return true, existingDeployment, nil 184 }) 185 fc.AddReactor("update", "deployments", func(action testcore.Action) (bool, runtime.Object, error) { 186 obj := action.(testcore.UpdateAction).GetObject().(*extensions.Deployment) 187 i := obj.Spec.Template.Spec.Containers[0].Image 188 if i != image { 189 t.Errorf("expected image = '%s', got '%s'", image, i) 190 } 191 return true, obj, nil 192 }) 193 fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) { 194 return true, nil, errors.NewNotFound(api.Resource("services"), "1") 195 }) 196 fc.AddReactor("create", "services", func(action testcore.Action) (bool, runtime.Object, error) { 197 obj := action.(testcore.CreateAction).GetObject().(*api.Service) 198 n := obj.ObjectMeta.Namespace 199 if n != api.NamespaceDefault { 200 t.Errorf("expected namespace = '%s', got '%s'", api.NamespaceDefault, n) 201 } 202 return true, obj, nil 203 }) 204 205 if err := Upgrade(fc, api.NamespaceDefault, image, false); err != nil { 206 t.Errorf("unexpected error: %#+v", err) 207 } 208 209 if actions := fc.Actions(); len(actions) != 4 { 210 t.Errorf("unexpected actions: %v, expected 4 actions got %d", actions, len(actions)) 211 } 212 }