github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/tiller/hooks_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 tiller 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/ghodss/yaml" 24 25 "k8s.io/helm/pkg/chartutil" 26 "k8s.io/helm/pkg/proto/hapi/release" 27 util "k8s.io/helm/pkg/releaseutil" 28 ) 29 30 func TestSortManifests(t *testing.T) { 31 32 data := []struct { 33 name []string 34 path string 35 kind []string 36 hooks map[string][]release.Hook_Event 37 manifest string 38 }{ 39 { 40 name: []string{"first"}, 41 path: "one", 42 kind: []string{"Job"}, 43 hooks: map[string][]release.Hook_Event{"first": {release.Hook_PRE_INSTALL}}, 44 manifest: `apiVersion: v1 45 kind: Job 46 metadata: 47 name: first 48 labels: 49 doesnot: matter 50 annotations: 51 "helm.sh/hook": pre-install 52 `, 53 }, 54 { 55 name: []string{"second"}, 56 path: "two", 57 kind: []string{"ReplicaSet"}, 58 hooks: map[string][]release.Hook_Event{"second": {release.Hook_POST_INSTALL}}, 59 manifest: `kind: ReplicaSet 60 apiVersion: v1beta1 61 metadata: 62 name: second 63 annotations: 64 "helm.sh/hook": post-install 65 `, 66 }, { 67 name: []string{"third"}, 68 path: "three", 69 kind: []string{"ReplicaSet"}, 70 hooks: map[string][]release.Hook_Event{"third": nil}, 71 manifest: `kind: ReplicaSet 72 apiVersion: v1beta1 73 metadata: 74 name: third 75 annotations: 76 "helm.sh/hook": no-such-hook 77 `, 78 }, { 79 name: []string{"fourth"}, 80 path: "four", 81 kind: []string{"Pod"}, 82 hooks: map[string][]release.Hook_Event{"fourth": nil}, 83 manifest: `kind: Pod 84 apiVersion: v1 85 metadata: 86 name: fourth 87 annotations: 88 nothing: here`, 89 }, { 90 name: []string{"fifth"}, 91 path: "five", 92 kind: []string{"ReplicaSet"}, 93 hooks: map[string][]release.Hook_Event{"fifth": {release.Hook_POST_DELETE, release.Hook_POST_INSTALL}}, 94 manifest: `kind: ReplicaSet 95 apiVersion: v1beta1 96 metadata: 97 name: fifth 98 annotations: 99 "helm.sh/hook": post-delete, post-install 100 `, 101 }, { 102 // Regression test: files with an underscore in the base name should be skipped. 103 name: []string{"sixth"}, 104 path: "six/_six", 105 kind: []string{"ReplicaSet"}, 106 hooks: map[string][]release.Hook_Event{"sixth": nil}, 107 manifest: `invalid manifest`, // This will fail if partial is not skipped. 108 }, { 109 // Regression test: files with no content should be skipped. 110 name: []string{"seventh"}, 111 path: "seven", 112 kind: []string{"ReplicaSet"}, 113 hooks: map[string][]release.Hook_Event{"seventh": nil}, 114 manifest: "", 115 }, 116 { 117 name: []string{"eighth", "example-test"}, 118 path: "eight", 119 kind: []string{"ConfigMap", "Pod"}, 120 hooks: map[string][]release.Hook_Event{"eighth": nil, "example-test": {release.Hook_RELEASE_TEST_SUCCESS}}, 121 manifest: `kind: ConfigMap 122 apiVersion: v1 123 metadata: 124 name: eighth 125 data: 126 name: value 127 --- 128 apiVersion: v1 129 kind: Pod 130 metadata: 131 name: example-test 132 annotations: 133 "helm.sh/hook": test-success 134 `, 135 }, 136 } 137 138 manifests := make(map[string]string, len(data)) 139 for _, o := range data { 140 manifests[o.path] = o.manifest 141 } 142 143 hs, generic, err := sortManifests(manifests, chartutil.NewVersionSet("v1", "v1beta1"), InstallOrder) 144 if err != nil { 145 t.Fatalf("Unexpected error: %s", err) 146 } 147 148 // This test will fail if 'six' or 'seven' was added. 149 if len(generic) != 2 { 150 t.Errorf("Expected 2 generic manifests, got %d", len(generic)) 151 } 152 153 if len(hs) != 4 { 154 t.Errorf("Expected 4 hooks, got %d", len(hs)) 155 } 156 157 for _, out := range hs { 158 found := false 159 for _, expect := range data { 160 if out.Path == expect.path { 161 found = true 162 if out.Path != expect.path { 163 t.Errorf("Expected path %s, got %s", expect.path, out.Path) 164 } 165 nameFound := false 166 for _, expectedName := range expect.name { 167 if out.Name == expectedName { 168 nameFound = true 169 } 170 } 171 if !nameFound { 172 t.Errorf("Got unexpected name %s", out.Name) 173 } 174 kindFound := false 175 for _, expectedKind := range expect.kind { 176 if out.Kind == expectedKind { 177 kindFound = true 178 } 179 } 180 if !kindFound { 181 t.Errorf("Got unexpected kind %s", out.Kind) 182 } 183 184 expectedHooks := expect.hooks[out.Name] 185 if !reflect.DeepEqual(expectedHooks, out.Events) { 186 t.Errorf("expected events: %v but got: %v", expectedHooks, out.Events) 187 } 188 189 } 190 } 191 if !found { 192 t.Errorf("Result not found: %v", out) 193 } 194 } 195 196 // Verify the sort order 197 sorted := []Manifest{} 198 for _, s := range data { 199 manifests := util.SplitManifests(s.manifest) 200 201 for _, m := range manifests { 202 var sh util.SimpleHead 203 err := yaml.Unmarshal([]byte(m), &sh) 204 if err != nil { 205 // This is expected for manifests that are corrupt or empty. 206 t.Log(err) 207 continue 208 } 209 210 name := sh.Metadata.Name 211 212 //only keep track of non-hook manifests 213 if err == nil && s.hooks[name] == nil { 214 another := Manifest{ 215 Content: m, 216 Name: name, 217 Head: &sh, 218 } 219 sorted = append(sorted, another) 220 } 221 } 222 } 223 224 sorted = sortByKind(sorted, InstallOrder) 225 for i, m := range generic { 226 if m.Content != sorted[i].Content { 227 t.Errorf("Expected %q, got %q", m.Content, sorted[i].Content) 228 } 229 } 230 } 231 232 func TestVersionSet(t *testing.T) { 233 vs := chartutil.NewVersionSet("v1", "v1beta1", "extensions/alpha5", "batch/v1") 234 235 if l := len(vs); l != 4 { 236 t.Errorf("Expected 4, got %d", l) 237 } 238 239 if !vs.Has("extensions/alpha5") { 240 t.Error("No match for alpha5") 241 } 242 243 if vs.Has("nosuch/extension") { 244 t.Error("Found nonexistent extension") 245 } 246 }