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