github.com/sri09kanth/helm@v3.0.0-beta.3+incompatible/pkg/repo/index_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 repo
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  
    24  	"helm.sh/helm/pkg/chart"
    25  	"helm.sh/helm/pkg/cli"
    26  	"helm.sh/helm/pkg/getter"
    27  )
    28  
    29  const (
    30  	testfile          = "testdata/local-index.yaml"
    31  	unorderedTestfile = "testdata/local-index-unordered.yaml"
    32  	testRepo          = "test-repo"
    33  )
    34  
    35  func TestIndexFile(t *testing.T) {
    36  	i := NewIndexFile()
    37  	i.Add(&chart.Metadata{Name: "clipper", Version: "0.1.0"}, "clipper-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890")
    38  	i.Add(&chart.Metadata{Name: "cutter", Version: "0.1.1"}, "cutter-0.1.1.tgz", "http://example.com/charts", "sha256:1234567890abc")
    39  	i.Add(&chart.Metadata{Name: "cutter", Version: "0.1.0"}, "cutter-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890abc")
    40  	i.Add(&chart.Metadata{Name: "cutter", Version: "0.2.0"}, "cutter-0.2.0.tgz", "http://example.com/charts", "sha256:1234567890abc")
    41  	i.SortEntries()
    42  
    43  	if i.APIVersion != APIVersionV1 {
    44  		t.Error("Expected API version v1")
    45  	}
    46  
    47  	if len(i.Entries) != 2 {
    48  		t.Errorf("Expected 2 charts. Got %d", len(i.Entries))
    49  	}
    50  
    51  	if i.Entries["clipper"][0].Name != "clipper" {
    52  		t.Errorf("Expected clipper, got %s", i.Entries["clipper"][0].Name)
    53  	}
    54  
    55  	if len(i.Entries["cutter"]) != 3 {
    56  		t.Error("Expected two cutters.")
    57  	}
    58  
    59  	// Test that the sort worked. 0.2 should be at the first index for Cutter.
    60  	if v := i.Entries["cutter"][0].Version; v != "0.2.0" {
    61  		t.Errorf("Unexpected first version: %s", v)
    62  	}
    63  }
    64  
    65  func TestLoadIndex(t *testing.T) {
    66  	b, err := ioutil.ReadFile(testfile)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	i, err := loadIndex(b)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	verifyLocalIndex(t, i)
    75  }
    76  
    77  func TestLoadIndexFile(t *testing.T) {
    78  	i, err := LoadIndexFile(testfile)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	verifyLocalIndex(t, i)
    83  }
    84  
    85  func TestLoadUnorderedIndex(t *testing.T) {
    86  	b, err := ioutil.ReadFile(unorderedTestfile)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	i, err := loadIndex(b)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	verifyLocalIndex(t, i)
    95  }
    96  
    97  func TestMerge(t *testing.T) {
    98  	ind1 := NewIndexFile()
    99  	ind1.Add(&chart.Metadata{
   100  		Name:    "dreadnought",
   101  		Version: "0.1.0",
   102  	}, "dreadnought-0.1.0.tgz", "http://example.com", "aaaa")
   103  
   104  	ind2 := NewIndexFile()
   105  	ind2.Add(&chart.Metadata{
   106  		Name:    "dreadnought",
   107  		Version: "0.2.0",
   108  	}, "dreadnought-0.2.0.tgz", "http://example.com", "aaaabbbb")
   109  	ind2.Add(&chart.Metadata{
   110  		Name:    "doughnut",
   111  		Version: "0.2.0",
   112  	}, "doughnut-0.2.0.tgz", "http://example.com", "ccccbbbb")
   113  
   114  	ind1.Merge(ind2)
   115  
   116  	if len(ind1.Entries) != 2 {
   117  		t.Errorf("Expected 2 entries, got %d", len(ind1.Entries))
   118  		vs := ind1.Entries["dreadnaught"]
   119  		if len(vs) != 2 {
   120  			t.Errorf("Expected 2 versions, got %d", len(vs))
   121  		}
   122  		v := vs[0]
   123  		if v.Version != "0.2.0" {
   124  			t.Errorf("Expected %q version to be 0.2.0, got %s", v.Name, v.Version)
   125  		}
   126  	}
   127  
   128  }
   129  
   130  func TestDownloadIndexFile(t *testing.T) {
   131  	srv, err := startLocalServerForTests(nil)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	defer srv.Close()
   136  
   137  	r, err := NewChartRepository(&Entry{
   138  		Name: testRepo,
   139  		URL:  srv.URL,
   140  	}, getter.All(&cli.EnvSettings{}))
   141  	if err != nil {
   142  		t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
   143  	}
   144  
   145  	idx, err := r.DownloadIndexFile()
   146  	if err != nil {
   147  		t.Fatalf("Failed to download index file to %s: %#v", idx, err)
   148  	}
   149  
   150  	if _, err := os.Stat(idx); err != nil {
   151  		t.Fatalf("error finding created index file: %#v", err)
   152  	}
   153  
   154  	b, err := ioutil.ReadFile(idx)
   155  	if err != nil {
   156  		t.Fatalf("error reading index file: %#v", err)
   157  	}
   158  
   159  	i, err := loadIndex(b)
   160  	if err != nil {
   161  		t.Fatalf("Index %q failed to parse: %s", testfile, err)
   162  	}
   163  	verifyLocalIndex(t, i)
   164  }
   165  
   166  func verifyLocalIndex(t *testing.T, i *IndexFile) {
   167  	numEntries := len(i.Entries)
   168  	if numEntries != 3 {
   169  		t.Errorf("Expected 3 entries in index file but got %d", numEntries)
   170  	}
   171  
   172  	alpine, ok := i.Entries["alpine"]
   173  	if !ok {
   174  		t.Fatalf("'alpine' section not found.")
   175  	}
   176  
   177  	if l := len(alpine); l != 1 {
   178  		t.Fatalf("'alpine' should have 1 chart, got %d", l)
   179  	}
   180  
   181  	nginx, ok := i.Entries["nginx"]
   182  	if !ok || len(nginx) != 2 {
   183  		t.Fatalf("Expected 2 nginx entries")
   184  	}
   185  
   186  	expects := []*ChartVersion{
   187  		{
   188  			Metadata: &chart.Metadata{
   189  				Name:        "alpine",
   190  				Description: "string",
   191  				Version:     "1.0.0",
   192  				Keywords:    []string{"linux", "alpine", "small", "sumtin"},
   193  				Home:        "https://github.com/something",
   194  			},
   195  			URLs: []string{
   196  				"https://kubernetes-charts.storage.googleapis.com/alpine-1.0.0.tgz",
   197  				"http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz",
   198  			},
   199  			Digest: "sha256:1234567890abcdef",
   200  		},
   201  		{
   202  			Metadata: &chart.Metadata{
   203  				Name:        "nginx",
   204  				Description: "string",
   205  				Version:     "0.2.0",
   206  				Keywords:    []string{"popular", "web server", "proxy"},
   207  				Home:        "https://github.com/something/else",
   208  			},
   209  			URLs: []string{
   210  				"https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz",
   211  			},
   212  			Digest: "sha256:1234567890abcdef",
   213  		},
   214  		{
   215  			Metadata: &chart.Metadata{
   216  				Name:        "nginx",
   217  				Description: "string",
   218  				Version:     "0.1.0",
   219  				Keywords:    []string{"popular", "web server", "proxy"},
   220  				Home:        "https://github.com/something",
   221  			},
   222  			URLs: []string{
   223  				"https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz",
   224  			},
   225  			Digest: "sha256:1234567890abcdef",
   226  		},
   227  	}
   228  	tests := []*ChartVersion{alpine[0], nginx[0], nginx[1]}
   229  
   230  	for i, tt := range tests {
   231  		expect := expects[i]
   232  		if tt.Name != expect.Name {
   233  			t.Errorf("Expected name %q, got %q", expect.Name, tt.Name)
   234  		}
   235  		if tt.Description != expect.Description {
   236  			t.Errorf("Expected description %q, got %q", expect.Description, tt.Description)
   237  		}
   238  		if tt.Version != expect.Version {
   239  			t.Errorf("Expected version %q, got %q", expect.Version, tt.Version)
   240  		}
   241  		if tt.Digest != expect.Digest {
   242  			t.Errorf("Expected digest %q, got %q", expect.Digest, tt.Digest)
   243  		}
   244  		if tt.Home != expect.Home {
   245  			t.Errorf("Expected home %q, got %q", expect.Home, tt.Home)
   246  		}
   247  
   248  		for i, url := range tt.URLs {
   249  			if url != expect.URLs[i] {
   250  				t.Errorf("Expected URL %q, got %q", expect.URLs[i], url)
   251  			}
   252  		}
   253  		for i, kw := range tt.Keywords {
   254  			if kw != expect.Keywords[i] {
   255  				t.Errorf("Expected keywords %q, got %q", expect.Keywords[i], kw)
   256  			}
   257  		}
   258  	}
   259  }
   260  
   261  func TestIndexDirectory(t *testing.T) {
   262  	dir := "testdata/repository"
   263  	index, err := IndexDirectory(dir, "http://localhost:8080")
   264  	if err != nil {
   265  		t.Fatal(err)
   266  	}
   267  
   268  	if l := len(index.Entries); l != 3 {
   269  		t.Fatalf("Expected 3 entries, got %d", l)
   270  	}
   271  
   272  	// Other things test the entry generation more thoroughly. We just test a
   273  	// few fields.
   274  
   275  	corpus := []struct{ chartName, downloadLink string }{
   276  		{"frobnitz", "http://localhost:8080/frobnitz-1.2.3.tgz"},
   277  		{"zarthal", "http://localhost:8080/universe/zarthal-1.0.0.tgz"},
   278  	}
   279  
   280  	for _, test := range corpus {
   281  		cname := test.chartName
   282  		frobs, ok := index.Entries[cname]
   283  		if !ok {
   284  			t.Fatalf("Could not read chart %s", cname)
   285  		}
   286  
   287  		frob := frobs[0]
   288  		if frob.Digest == "" {
   289  			t.Errorf("Missing digest of file %s.", frob.Name)
   290  		}
   291  		if frob.URLs[0] != test.downloadLink {
   292  			t.Errorf("Unexpected URLs: %v", frob.URLs)
   293  		}
   294  		if frob.Name != cname {
   295  			t.Errorf("Expected %q, got %q", cname, frob.Name)
   296  		}
   297  	}
   298  }
   299  
   300  func TestLoadUnversionedIndex(t *testing.T) {
   301  	data, err := ioutil.ReadFile("testdata/unversioned-index.yaml")
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  
   306  	ind, err := loadUnversionedIndex(data)
   307  	if err != nil {
   308  		t.Fatal(err)
   309  	}
   310  
   311  	if l := len(ind.Entries); l != 2 {
   312  		t.Fatalf("Expected 2 entries, got %d", l)
   313  	}
   314  
   315  	if l := len(ind.Entries["mysql"]); l != 3 {
   316  		t.Fatalf("Expected 3 mysql versions, got %d", l)
   317  	}
   318  }
   319  
   320  func TestIndexAdd(t *testing.T) {
   321  	i := NewIndexFile()
   322  	i.Add(&chart.Metadata{Name: "clipper", Version: "0.1.0"}, "clipper-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890")
   323  
   324  	if i.Entries["clipper"][0].URLs[0] != "http://example.com/charts/clipper-0.1.0.tgz" {
   325  		t.Errorf("Expected http://example.com/charts/clipper-0.1.0.tgz, got %s", i.Entries["clipper"][0].URLs[0])
   326  	}
   327  
   328  	i.Add(&chart.Metadata{Name: "alpine", Version: "0.1.0"}, "/home/charts/alpine-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890")
   329  
   330  	if i.Entries["alpine"][0].URLs[0] != "http://example.com/charts/alpine-0.1.0.tgz" {
   331  		t.Errorf("Expected http://example.com/charts/alpine-0.1.0.tgz, got %s", i.Entries["alpine"][0].URLs[0])
   332  	}
   333  
   334  	i.Add(&chart.Metadata{Name: "deis", Version: "0.1.0"}, "/home/charts/deis-0.1.0.tgz", "http://example.com/charts/", "sha256:1234567890")
   335  
   336  	if i.Entries["deis"][0].URLs[0] != "http://example.com/charts/deis-0.1.0.tgz" {
   337  		t.Errorf("Expected http://example.com/charts/deis-0.1.0.tgz, got %s", i.Entries["deis"][0].URLs[0])
   338  	}
   339  }