gitlab.azmi.pl/azmi-open-source/helm@v3.0.0-beta.3+incompatible/pkg/chart/loader/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 loader
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"helm.sh/helm/pkg/chart"
    24  )
    25  
    26  func TestLoadDir(t *testing.T) {
    27  	l, err := Loader("testdata/frobnitz")
    28  	if err != nil {
    29  		t.Fatalf("Failed to load testdata: %s", err)
    30  	}
    31  	c, err := l.Load()
    32  	if err != nil {
    33  		t.Fatalf("Failed to load testdata: %s", err)
    34  	}
    35  	verifyFrobnitz(t, c)
    36  	verifyChart(t, c)
    37  	verifyDependencies(t, c)
    38  	verifyDependenciesLock(t, c)
    39  }
    40  
    41  func TestLoadV1(t *testing.T) {
    42  	l, err := Loader("testdata/frobnitz.v1")
    43  	if err != nil {
    44  		t.Fatalf("Failed to load testdata: %s", err)
    45  	}
    46  	c, err := l.Load()
    47  	if err != nil {
    48  		t.Fatalf("Failed to load testdata: %s", err)
    49  	}
    50  	verifyDependencies(t, c)
    51  	verifyDependenciesLock(t, c)
    52  }
    53  
    54  func TestLoadFile(t *testing.T) {
    55  	l, err := Loader("testdata/frobnitz-1.2.3.tgz")
    56  	if err != nil {
    57  		t.Fatalf("Failed to load testdata: %s", err)
    58  	}
    59  	c, err := l.Load()
    60  	if err != nil {
    61  		t.Fatalf("Failed to load testdata: %s", err)
    62  	}
    63  	verifyFrobnitz(t, c)
    64  	verifyChart(t, c)
    65  	verifyDependencies(t, c)
    66  }
    67  
    68  func TestLoadFiles(t *testing.T) {
    69  	goodFiles := []*BufferedFile{
    70  		{
    71  			Name: "Chart.yaml",
    72  			Data: []byte(`apiVersion: v1
    73  name: frobnitz
    74  description: This is a frobnitz.
    75  version: "1.2.3"
    76  keywords:
    77    - frobnitz
    78    - sprocket
    79    - dodad
    80  maintainers:
    81    - name: The Helm Team
    82      email: helm@example.com
    83    - name: Someone Else
    84      email: nobody@example.com
    85  sources:
    86    - https://example.com/foo/bar
    87  home: http://example.com
    88  icon: https://example.com/64x64.png
    89  `),
    90  		},
    91  		{
    92  			Name: "values.yaml",
    93  			Data: []byte("var: some values"),
    94  		},
    95  		{
    96  			Name: "values.schema.json",
    97  			Data: []byte("type: Values"),
    98  		},
    99  		{
   100  			Name: "templates/deployment.yaml",
   101  			Data: []byte("some deployment"),
   102  		},
   103  		{
   104  			Name: "templates/service.yaml",
   105  			Data: []byte("some service"),
   106  		},
   107  	}
   108  
   109  	c, err := LoadFiles(goodFiles)
   110  	if err != nil {
   111  		t.Errorf("Expected good files to be loaded, got %v", err)
   112  	}
   113  
   114  	if c.Name() != "frobnitz" {
   115  		t.Errorf("Expected chart name to be 'frobnitz', got %s", c.Name())
   116  	}
   117  
   118  	if c.Values["var"] != "some values" {
   119  		t.Error("Expected chart values to be populated with default values")
   120  	}
   121  
   122  	if !bytes.Equal(c.Schema, []byte("type: Values")) {
   123  		t.Error("Expected chart schema to be populated with default values")
   124  	}
   125  
   126  	if len(c.Templates) != 2 {
   127  		t.Errorf("Expected number of templates == 2, got %d", len(c.Templates))
   128  	}
   129  
   130  	if _, err = LoadFiles([]*BufferedFile{}); err == nil {
   131  		t.Fatal("Expected err to be non-nil")
   132  	}
   133  	if err.Error() != "validation: chart.metadata is required" {
   134  		t.Errorf("Expected chart metadata missing error, got '%s'", err.Error())
   135  	}
   136  }
   137  
   138  // Packaging the chart on a Windows machine will produce an
   139  // archive that has \\ as delimiters. Test that we support these archives
   140  func TestLoadFileBackslash(t *testing.T) {
   141  	c, err := Load("testdata/frobnitz_backslash-1.2.3.tgz")
   142  	if err != nil {
   143  		t.Fatalf("Failed to load testdata: %s", err)
   144  	}
   145  	verifyChartFileAndTemplate(t, c, "frobnitz_backslash")
   146  	verifyChart(t, c)
   147  	verifyDependencies(t, c)
   148  }
   149  
   150  func TestLoadV2WithReqs(t *testing.T) {
   151  	l, err := Loader("testdata/frobnitz.v2.reqs")
   152  	if err != nil {
   153  		t.Fatalf("Failed to load testdata: %s", err)
   154  	}
   155  	c, err := l.Load()
   156  	if err != nil {
   157  		t.Fatalf("Failed to load testdata: %s", err)
   158  	}
   159  	verifyDependencies(t, c)
   160  	verifyDependenciesLock(t, c)
   161  }
   162  
   163  func verifyChart(t *testing.T, c *chart.Chart) {
   164  	t.Helper()
   165  	if c.Name() == "" {
   166  		t.Fatalf("No chart metadata found on %v", c)
   167  	}
   168  	t.Logf("Verifying chart %s", c.Name())
   169  	if len(c.Templates) != 1 {
   170  		t.Errorf("Expected 1 template, got %d", len(c.Templates))
   171  	}
   172  
   173  	numfiles := 6
   174  	if len(c.Files) != numfiles {
   175  		t.Errorf("Expected %d extra files, got %d", numfiles, len(c.Files))
   176  		for _, n := range c.Files {
   177  			t.Logf("\t%s", n.Name)
   178  		}
   179  	}
   180  
   181  	if len(c.Dependencies()) != 2 {
   182  		t.Errorf("Expected 2 dependencies, got %d (%v)", len(c.Dependencies()), c.Dependencies())
   183  		for _, d := range c.Dependencies() {
   184  			t.Logf("\tSubchart: %s\n", d.Name())
   185  		}
   186  	}
   187  
   188  	expect := map[string]map[string]string{
   189  		"alpine": {
   190  			"version": "0.1.0",
   191  		},
   192  		"mariner": {
   193  			"version": "4.3.2",
   194  		},
   195  	}
   196  
   197  	for _, dep := range c.Dependencies() {
   198  		if dep.Metadata == nil {
   199  			t.Fatalf("expected metadata on dependency: %v", dep)
   200  		}
   201  		exp, ok := expect[dep.Name()]
   202  		if !ok {
   203  			t.Fatalf("Unknown dependency %s", dep.Name())
   204  		}
   205  		if exp["version"] != dep.Metadata.Version {
   206  			t.Errorf("Expected %s version %s, got %s", dep.Name(), exp["version"], dep.Metadata.Version)
   207  		}
   208  	}
   209  
   210  }
   211  
   212  func verifyDependencies(t *testing.T, c *chart.Chart) {
   213  	if len(c.Metadata.Dependencies) != 2 {
   214  		t.Errorf("Expected 2 dependencies, got %d", len(c.Metadata.Dependencies))
   215  	}
   216  	tests := []*chart.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 := c.Metadata.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 verifyDependenciesLock(t *testing.T, c *chart.Chart) {
   235  	if len(c.Metadata.Dependencies) != 2 {
   236  		t.Errorf("Expected 2 dependencies, got %d", len(c.Metadata.Dependencies))
   237  	}
   238  	tests := []*chart.Dependency{
   239  		{Name: "alpine", Version: "0.1.0", Repository: "https://example.com/charts"},
   240  		{Name: "mariner", Version: "4.3.2", Repository: "https://example.com/charts"},
   241  	}
   242  	for i, tt := range tests {
   243  		d := c.Metadata.Dependencies[i]
   244  		if d.Name != tt.Name {
   245  			t.Errorf("Expected dependency named %q, got %q", tt.Name, d.Name)
   246  		}
   247  		if d.Version != tt.Version {
   248  			t.Errorf("Expected dependency named %q to have version %q, got %q", tt.Name, tt.Version, d.Version)
   249  		}
   250  		if d.Repository != tt.Repository {
   251  			t.Errorf("Expected dependency named %q to have repository %q, got %q", tt.Name, tt.Repository, d.Repository)
   252  		}
   253  	}
   254  }
   255  
   256  func verifyFrobnitz(t *testing.T, c *chart.Chart) {
   257  	verifyChartFileAndTemplate(t, c, "frobnitz")
   258  }
   259  
   260  func verifyChartFileAndTemplate(t *testing.T, c *chart.Chart, name string) {
   261  	if c.Metadata == nil {
   262  		t.Fatal("Metadata is nil")
   263  	}
   264  	if c.Name() != name {
   265  		t.Errorf("Expected %s, got %s", name, c.Name())
   266  	}
   267  	if len(c.Templates) != 1 {
   268  		t.Fatalf("Expected 1 template, got %d", len(c.Templates))
   269  	}
   270  	if c.Templates[0].Name != "templates/template.tpl" {
   271  		t.Errorf("Unexpected template: %s", c.Templates[0].Name)
   272  	}
   273  	if len(c.Templates[0].Data) == 0 {
   274  		t.Error("No template data.")
   275  	}
   276  	if len(c.Files) != 6 {
   277  		t.Fatalf("Expected 6 Files, got %d", len(c.Files))
   278  	}
   279  	if len(c.Dependencies()) != 2 {
   280  		t.Fatalf("Expected 2 Dependency, got %d", len(c.Dependencies()))
   281  	}
   282  	if len(c.Metadata.Dependencies) != 2 {
   283  		t.Fatalf("Expected 2 Dependencies.Dependency, got %d", len(c.Metadata.Dependencies))
   284  	}
   285  	if len(c.Lock.Dependencies) != 2 {
   286  		t.Fatalf("Expected 2 Lock.Dependency, got %d", len(c.Lock.Dependencies))
   287  	}
   288  
   289  	for _, dep := range c.Dependencies() {
   290  		switch dep.Name() {
   291  		case "mariner":
   292  		case "alpine":
   293  			if len(dep.Templates) != 1 {
   294  				t.Fatalf("Expected 1 template, got %d", len(dep.Templates))
   295  			}
   296  			if dep.Templates[0].Name != "templates/alpine-pod.yaml" {
   297  				t.Errorf("Unexpected template: %s", dep.Templates[0].Name)
   298  			}
   299  			if len(dep.Templates[0].Data) == 0 {
   300  				t.Error("No template data.")
   301  			}
   302  			if len(dep.Files) != 1 {
   303  				t.Fatalf("Expected 1 Files, got %d", len(dep.Files))
   304  			}
   305  			if len(dep.Dependencies()) != 2 {
   306  				t.Fatalf("Expected 2 Dependency, got %d", len(dep.Dependencies()))
   307  			}
   308  		default:
   309  			t.Errorf("Unexpected dependeny %s", dep.Name())
   310  		}
   311  	}
   312  }