github.com/felipejfc/helm@v2.1.2+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 "testing" 21 22 "github.com/ghodss/yaml" 23 24 "k8s.io/helm/pkg/proto/hapi/release" 25 ) 26 27 func TestSortManifests(t *testing.T) { 28 29 data := []struct { 30 name string 31 path string 32 kind string 33 hooks []release.Hook_Event 34 manifest string 35 }{ 36 { 37 name: "first", 38 path: "one", 39 kind: "Job", 40 hooks: []release.Hook_Event{release.Hook_PRE_INSTALL}, 41 manifest: `apiVersion: v1 42 kind: Job 43 metadata: 44 name: first 45 labels: 46 doesnot: matter 47 annotations: 48 "helm.sh/hook": pre-install 49 `, 50 }, 51 { 52 name: "second", 53 path: "two", 54 kind: "ReplicaSet", 55 hooks: []release.Hook_Event{release.Hook_POST_INSTALL}, 56 manifest: `kind: ReplicaSet 57 apiVersion: v1beta1 58 metadata: 59 name: second 60 annotations: 61 "helm.sh/hook": post-install 62 `, 63 }, { 64 name: "third", 65 path: "three", 66 kind: "ReplicaSet", 67 hooks: []release.Hook_Event{}, 68 manifest: `kind: ReplicaSet 69 apiVersion: v1beta1 70 metadata: 71 name: third 72 annotations: 73 "helm.sh/hook": no-such-hook 74 `, 75 }, { 76 name: "fourth", 77 path: "four", 78 kind: "Pod", 79 hooks: []release.Hook_Event{}, 80 manifest: `kind: Pod 81 apiVersion: v1 82 metadata: 83 name: fourth 84 annotations: 85 nothing: here 86 `, 87 }, { 88 name: "fifth", 89 path: "five", 90 kind: "ReplicaSet", 91 hooks: []release.Hook_Event{release.Hook_POST_DELETE, release.Hook_POST_INSTALL}, 92 manifest: `kind: ReplicaSet 93 apiVersion: v1beta1 94 metadata: 95 name: fifth 96 annotations: 97 "helm.sh/hook": post-delete, post-install 98 `, 99 }, { 100 // Regression test: files with an underscore in the base name should be skipped. 101 name: "sixth", 102 path: "six/_six", 103 kind: "ReplicaSet", 104 hooks: []release.Hook_Event{}, 105 manifest: `invalid manifest`, // This will fail if partial is not skipped. 106 }, { 107 // Regression test: files with no content should be skipped. 108 name: "seventh", 109 path: "seven", 110 kind: "ReplicaSet", 111 hooks: []release.Hook_Event{}, 112 manifest: "", 113 }, 114 } 115 116 manifests := make(map[string]string, len(data)) 117 for _, o := range data { 118 manifests[o.path] = o.manifest 119 } 120 121 hs, generic, err := sortManifests(manifests, newVersionSet("v1", "v1beta1"), InstallOrder) 122 if err != nil { 123 t.Fatalf("Unexpected error: %s", err) 124 } 125 126 // This test will fail if 'six' or 'seven' was added. 127 if len(generic) != 1 { 128 t.Errorf("Expected 1 generic manifest, got %d", len(generic)) 129 } 130 131 if len(hs) != 3 { 132 t.Errorf("Expected 3 hooks, got %d", len(hs)) 133 } 134 135 for _, out := range hs { 136 found := false 137 for _, expect := range data { 138 if out.Path == expect.path { 139 found = true 140 if out.Path != expect.path { 141 t.Errorf("Expected path %s, got %s", expect.path, out.Path) 142 } 143 if out.Name != expect.name { 144 t.Errorf("Expected name %s, got %s", expect.name, out.Name) 145 } 146 if out.Kind != expect.kind { 147 t.Errorf("Expected kind %s, got %s", expect.kind, out.Kind) 148 } 149 for i := 0; i < len(out.Events); i++ { 150 if out.Events[i] != expect.hooks[i] { 151 t.Errorf("Expected event %d, got %d", expect.hooks[i], out.Events[i]) 152 } 153 } 154 } 155 } 156 if !found { 157 t.Errorf("Result not found: %v", out) 158 } 159 } 160 161 // Verify the sort order 162 sorted := make([]manifest, len(data)) 163 for i, s := range data { 164 var sh simpleHead 165 err := yaml.Unmarshal([]byte(s.manifest), &sh) 166 if err != nil { 167 // This is expected for manifests that are corrupt or empty. 168 t.Log(err) 169 } 170 sorted[i] = manifest{ 171 content: s.manifest, 172 name: s.name, 173 head: &sh, 174 } 175 } 176 sorted = sortByKind(sorted, InstallOrder) 177 for i, m := range generic { 178 if m.content != sorted[i].content { 179 t.Errorf("Expected %q, got %q", m.content, sorted[i].content) 180 } 181 } 182 183 } 184 185 func TestVersionSet(t *testing.T) { 186 vs := newVersionSet("v1", "v1beta1", "extensions/alpha5", "batch/v1") 187 188 if l := len(vs); l != 4 { 189 t.Errorf("Expected 4, got %d", l) 190 } 191 192 if !vs.Has("extensions/alpha5") { 193 t.Error("No match for alpha5") 194 } 195 196 if vs.Has("nosuch/extension") { 197 t.Error("Found nonexistent extension") 198 } 199 }