github.com/Beeketing/helm@v2.12.1+incompatible/pkg/chartutil/load_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 chartutil 18 19 import ( 20 "path" 21 "testing" 22 23 "k8s.io/helm/pkg/proto/hapi/chart" 24 ) 25 26 func TestLoadDir(t *testing.T) { 27 c, err := Load("testdata/frobnitz") 28 if err != nil { 29 t.Fatalf("Failed to load testdata: %s", err) 30 } 31 verifyFrobnitz(t, c) 32 verifyChart(t, c) 33 verifyRequirements(t, c) 34 } 35 36 func TestLoadFile(t *testing.T) { 37 c, err := Load("testdata/frobnitz-1.2.3.tgz") 38 if err != nil { 39 t.Fatalf("Failed to load testdata: %s", err) 40 } 41 verifyFrobnitz(t, c) 42 verifyChart(t, c) 43 verifyRequirements(t, c) 44 } 45 46 func TestLoadFiles(t *testing.T) { 47 goodFiles := []*BufferedFile{ 48 { 49 Name: ChartfileName, 50 Data: []byte(`apiVersion: v1 51 name: frobnitz 52 description: This is a frobnitz. 53 version: "1.2.3" 54 keywords: 55 - frobnitz 56 - sprocket 57 - dodad 58 maintainers: 59 - name: The Helm Team 60 email: helm@example.com 61 - name: Someone Else 62 email: nobody@example.com 63 sources: 64 - https://example.com/foo/bar 65 home: http://example.com 66 icon: https://example.com/64x64.png 67 `), 68 }, 69 { 70 Name: ValuesfileName, 71 Data: []byte(defaultValues), 72 }, 73 { 74 Name: path.Join("templates", DeploymentName), 75 Data: []byte(defaultDeployment), 76 }, 77 { 78 Name: path.Join("templates", ServiceName), 79 Data: []byte(defaultService), 80 }, 81 } 82 83 c, err := LoadFiles(goodFiles) 84 if err != nil { 85 t.Errorf("Expected good files to be loaded, got %v", err) 86 } 87 88 if c.Metadata.Name != "frobnitz" { 89 t.Errorf("Expected chart name to be 'frobnitz', got %s", c.Metadata.Name) 90 } 91 92 if c.Values.Raw != defaultValues { 93 t.Error("Expected chart values to be populated with default values") 94 } 95 96 if len(c.Templates) != 2 { 97 t.Errorf("Expected number of templates == 2, got %d", len(c.Templates)) 98 } 99 100 c, err = LoadFiles([]*BufferedFile{}) 101 if err == nil { 102 t.Fatal("Expected err to be non-nil") 103 } 104 if err.Error() != "chart metadata (Chart.yaml) missing" { 105 t.Errorf("Expected chart metadata missing error, got '%s'", err.Error()) 106 } 107 108 // legacy check 109 c, err = LoadFiles([]*BufferedFile{ 110 { 111 Name: "values.toml", 112 Data: []byte{}, 113 }, 114 }) 115 if err == nil { 116 t.Fatal("Expected err to be non-nil") 117 } 118 if err.Error() != "values.toml is illegal as of 2.0.0-alpha.2" { 119 t.Errorf("Expected values.toml to be illegal, got '%s'", err.Error()) 120 } 121 } 122 123 // Packaging the chart on a Windows machine will produce an 124 // archive that has \\ as delimiters. Test that we support these archives 125 func TestLoadFileBackslash(t *testing.T) { 126 c, err := Load("testdata/frobnitz_backslash-1.2.3.tgz") 127 if err != nil { 128 t.Fatalf("Failed to load testdata: %s", err) 129 } 130 verifyChartFileAndTemplate(t, c, "frobnitz_backslash") 131 verifyChart(t, c) 132 verifyRequirements(t, c) 133 } 134 135 func verifyChart(t *testing.T, c *chart.Chart) { 136 if c.Metadata.Name == "" { 137 t.Fatalf("No chart metadata found on %v", c) 138 } 139 t.Logf("Verifying chart %s", c.Metadata.Name) 140 if len(c.Templates) != 1 { 141 t.Errorf("Expected 1 template, got %d", len(c.Templates)) 142 } 143 144 numfiles := 8 145 if len(c.Files) != numfiles { 146 t.Errorf("Expected %d extra files, got %d", numfiles, len(c.Files)) 147 for _, n := range c.Files { 148 t.Logf("\t%s", n.TypeUrl) 149 } 150 } 151 152 if len(c.Dependencies) != 2 { 153 t.Errorf("Expected 2 dependencies, got %d (%v)", len(c.Dependencies), c.Dependencies) 154 for _, d := range c.Dependencies { 155 t.Logf("\tSubchart: %s\n", d.Metadata.Name) 156 } 157 } 158 159 expect := map[string]map[string]string{ 160 "alpine": { 161 "version": "0.1.0", 162 }, 163 "mariner": { 164 "version": "4.3.2", 165 }, 166 } 167 168 for _, dep := range c.Dependencies { 169 if dep.Metadata == nil { 170 t.Fatalf("expected metadata on dependency: %v", dep) 171 } 172 exp, ok := expect[dep.Metadata.Name] 173 if !ok { 174 t.Fatalf("Unknown dependency %s", dep.Metadata.Name) 175 } 176 if exp["version"] != dep.Metadata.Version { 177 t.Errorf("Expected %s version %s, got %s", dep.Metadata.Name, exp["version"], dep.Metadata.Version) 178 } 179 } 180 181 } 182 183 func verifyRequirements(t *testing.T, c *chart.Chart) { 184 r, err := LoadRequirements(c) 185 if err != nil { 186 t.Fatal(err) 187 } 188 if len(r.Dependencies) != 2 { 189 t.Errorf("Expected 2 requirements, got %d", len(r.Dependencies)) 190 } 191 tests := []*Dependency{ 192 {Name: "alpine", Version: "0.1.0", Repository: "https://example.com/charts"}, 193 {Name: "mariner", Version: "4.3.2", Repository: "https://example.com/charts"}, 194 } 195 for i, tt := range tests { 196 d := r.Dependencies[i] 197 if d.Name != tt.Name { 198 t.Errorf("Expected dependency named %q, got %q", tt.Name, d.Name) 199 } 200 if d.Version != tt.Version { 201 t.Errorf("Expected dependency named %q to have version %q, got %q", tt.Name, tt.Version, d.Version) 202 } 203 if d.Repository != tt.Repository { 204 t.Errorf("Expected dependency named %q to have repository %q, got %q", tt.Name, tt.Repository, d.Repository) 205 } 206 } 207 } 208 func verifyRequirementsLock(t *testing.T, c *chart.Chart) { 209 r, err := LoadRequirementsLock(c) 210 if err != nil { 211 t.Fatal(err) 212 } 213 if len(r.Dependencies) != 2 { 214 t.Errorf("Expected 2 requirements, got %d", len(r.Dependencies)) 215 } 216 tests := []*Dependency{ 217 {Name: "alpine", Version: "0.1.0", Repository: "https://example.com/charts"}, 218 {Name: "mariner", Version: "4.3.2", Repository: "https://example.com/charts"}, 219 } 220 for i, tt := range tests { 221 d := r.Dependencies[i] 222 if d.Name != tt.Name { 223 t.Errorf("Expected dependency named %q, got %q", tt.Name, d.Name) 224 } 225 if d.Version != tt.Version { 226 t.Errorf("Expected dependency named %q to have version %q, got %q", tt.Name, tt.Version, d.Version) 227 } 228 if d.Repository != tt.Repository { 229 t.Errorf("Expected dependency named %q to have repository %q, got %q", tt.Name, tt.Repository, d.Repository) 230 } 231 } 232 } 233 234 func verifyFrobnitz(t *testing.T, c *chart.Chart) { 235 verifyChartFileAndTemplate(t, c, "frobnitz") 236 } 237 238 func verifyChartFileAndTemplate(t *testing.T, c *chart.Chart, name string) { 239 240 verifyChartfile(t, c.Metadata, name) 241 242 if len(c.Templates) != 1 { 243 t.Fatalf("Expected 1 template, got %d", len(c.Templates)) 244 } 245 246 if c.Templates[0].Name != "templates/template.tpl" { 247 t.Errorf("Unexpected template: %s", c.Templates[0].Name) 248 } 249 250 if len(c.Templates[0].Data) == 0 { 251 t.Error("No template data.") 252 } 253 }