github.com/cloudposse/helm@v2.2.3+incompatible/pkg/chartutil/requirements_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 package chartutil 16 17 import ( 18 "sort" 19 "testing" 20 21 "k8s.io/helm/pkg/proto/hapi/chart" 22 ) 23 24 func TestLoadRequirements(t *testing.T) { 25 c, err := Load("testdata/frobnitz") 26 if err != nil { 27 t.Fatalf("Failed to load testdata: %s", err) 28 } 29 verifyRequirements(t, c) 30 } 31 32 func TestLoadRequirementsLock(t *testing.T) { 33 c, err := Load("testdata/frobnitz") 34 if err != nil { 35 t.Fatalf("Failed to load testdata: %s", err) 36 } 37 verifyRequirementsLock(t, c) 38 } 39 func TestRequirementsTagsNonValue(t *testing.T) { 40 c, err := Load("testdata/subpop") 41 if err != nil { 42 t.Fatalf("Failed to load testdata: %s", err) 43 } 44 // tags with no effect 45 v := &chart.Config{Raw: "tags:\n nothinguseful: false\n\n"} 46 // expected charts including duplicates in alphanumeric order 47 e := []string{"parentchart", "subchart1", "subcharta", "subchartb"} 48 49 verifyRequirementsEnabled(t, c, v, e) 50 } 51 func TestRequirementsTagsDisabledL1(t *testing.T) { 52 c, err := Load("testdata/subpop") 53 if err != nil { 54 t.Fatalf("Failed to load testdata: %s", err) 55 } 56 // tags disabling a group 57 v := &chart.Config{Raw: "tags:\n front-end: false\n\n"} 58 // expected charts including duplicates in alphanumeric order 59 e := []string{"parentchart"} 60 61 verifyRequirementsEnabled(t, c, v, e) 62 } 63 func TestRequirementsTagsEnabledL1(t *testing.T) { 64 c, err := Load("testdata/subpop") 65 if err != nil { 66 t.Fatalf("Failed to load testdata: %s", err) 67 } 68 // tags disabling a group and enabling a different group 69 v := &chart.Config{Raw: "tags:\n front-end: false\n\n back-end: true\n"} 70 // expected charts including duplicates in alphanumeric order 71 e := []string{"parentchart", "subchart2", "subchartb", "subchartc"} 72 73 verifyRequirementsEnabled(t, c, v, e) 74 } 75 func TestRequirementsTagsDisabledL2(t *testing.T) { 76 c, err := Load("testdata/subpop") 77 if err != nil { 78 t.Fatalf("Failed to load testdata: %s", err) 79 } 80 // tags disabling only children 81 v := &chart.Config{Raw: "tags:\n subcharta: false\n\n subchartb: false\n"} 82 // expected charts including duplicates in alphanumeric order 83 e := []string{"parentchart", "subchart1"} 84 85 verifyRequirementsEnabled(t, c, v, e) 86 } 87 func TestRequirementsTagsDisabledL1Mixed(t *testing.T) { 88 c, err := Load("testdata/subpop") 89 if err != nil { 90 t.Fatalf("Failed to load testdata: %s", err) 91 } 92 // tags disabling all parents/children with additional tag re-enabling a parent 93 v := &chart.Config{Raw: "tags:\n front-end: false\n\n subchart1: true\n\n back-end: false\n"} 94 // expected charts including duplicates in alphanumeric order 95 e := []string{"parentchart", "subchart1"} 96 97 verifyRequirementsEnabled(t, c, v, e) 98 } 99 func TestRequirementsConditionsNonValue(t *testing.T) { 100 c, err := Load("testdata/subpop") 101 if err != nil { 102 t.Fatalf("Failed to load testdata: %s", err) 103 } 104 // tags with no effect 105 v := &chart.Config{Raw: "subchart1:\n nothinguseful: false\n\n"} 106 // expected charts including duplicates in alphanumeric order 107 e := []string{"parentchart", "subchart1", "subcharta", "subchartb"} 108 109 verifyRequirementsEnabled(t, c, v, e) 110 } 111 func TestRequirementsConditionsEnabledL1Both(t *testing.T) { 112 c, err := Load("testdata/subpop") 113 if err != nil { 114 t.Fatalf("Failed to load testdata: %s", err) 115 } 116 // conditions enabling the parent charts, effectively enabling children 117 v := &chart.Config{Raw: "subchart1:\n enabled: true\nsubchart2:\n enabled: true\n"} 118 // expected charts including duplicates in alphanumeric order 119 e := []string{"parentchart", "subchart1", "subchart2", "subcharta", "subchartb", "subchartb", "subchartc"} 120 121 verifyRequirementsEnabled(t, c, v, e) 122 } 123 func TestRequirementsConditionsDisabledL1Both(t *testing.T) { 124 c, err := Load("testdata/subpop") 125 if err != nil { 126 t.Fatalf("Failed to load testdata: %s", err) 127 } 128 // conditions disabling the parent charts, effectively disabling children 129 v := &chart.Config{Raw: "subchart1:\n enabled: false\nsubchart2:\n enabled: false\n"} 130 // expected charts including duplicates in alphanumeric order 131 e := []string{"parentchart"} 132 133 verifyRequirementsEnabled(t, c, v, e) 134 } 135 136 func TestRequirementsConditionsSecond(t *testing.T) { 137 c, err := Load("testdata/subpop") 138 if err != nil { 139 t.Fatalf("Failed to load testdata: %s", err) 140 } 141 // conditions a child using the second condition path of child's condition 142 v := &chart.Config{Raw: "subchart1:\n subcharta:\n enabled: false\n"} 143 // expected charts including duplicates in alphanumeric order 144 e := []string{"parentchart", "subchart1", "subchartb"} 145 146 verifyRequirementsEnabled(t, c, v, e) 147 } 148 func TestRequirementsCombinedDisabledL2(t *testing.T) { 149 c, err := Load("testdata/subpop") 150 if err != nil { 151 t.Fatalf("Failed to load testdata: %s", err) 152 } 153 // tags enabling a parent/child group with condition disabling one child 154 v := &chart.Config{Raw: "subchartc:\n enabled: false\ntags:\n back-end: true\n"} 155 // expected charts including duplicates in alphanumeric order 156 e := []string{"parentchart", "subchart1", "subchart2", "subcharta", "subchartb", "subchartb"} 157 158 verifyRequirementsEnabled(t, c, v, e) 159 } 160 func TestRequirementsCombinedDisabledL1(t *testing.T) { 161 c, err := Load("testdata/subpop") 162 if err != nil { 163 t.Fatalf("Failed to load testdata: %s", err) 164 } 165 // tags will not enable a child if parent is explicitly disabled with condition 166 v := &chart.Config{Raw: "subchart1:\n enabled: false\ntags:\n front-end: true\n"} 167 // expected charts including duplicates in alphanumeric order 168 e := []string{"parentchart"} 169 170 verifyRequirementsEnabled(t, c, v, e) 171 } 172 173 func verifyRequirementsEnabled(t *testing.T, c *chart.Chart, v *chart.Config, e []string) { 174 out := []*chart.Chart{} 175 err := ProcessRequirementsEnabled(c, v) 176 if err != nil { 177 t.Errorf("Error processing enabled requirements %v", err) 178 } 179 out = extractCharts(c, out) 180 // build list of chart names 181 p := []string{} 182 for _, r := range out { 183 p = append(p, r.Metadata.Name) 184 } 185 //sort alphanumeric and compare to expectations 186 sort.Strings(p) 187 if len(p) != len(e) { 188 t.Errorf("Error slice lengths do not match got %v, expected %v", len(p), len(e)) 189 return 190 } 191 for i := range p { 192 if p[i] != e[i] { 193 t.Errorf("Error slice values do not match got %v, expected %v", p[i], e[i]) 194 } 195 } 196 } 197 198 // extractCharts recursively searches chart dependencies returning all charts found 199 func extractCharts(c *chart.Chart, out []*chart.Chart) []*chart.Chart { 200 201 if len(c.Metadata.Name) > 0 { 202 out = append(out, c) 203 } 204 for _, d := range c.Dependencies { 205 out = extractCharts(d, out) 206 } 207 return out 208 }