github.com/felipejfc/helm@v2.1.2+incompatible/pkg/repo/repo_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"os"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	"k8s.io/helm/pkg/proto/hapi/chart"
    27  )
    28  
    29  const testRepositoriesFile = "testdata/repositories.yaml"
    30  const testRepository = "testdata/repository"
    31  const testURL = "http://example-charts.com"
    32  
    33  func TestRepoFile(t *testing.T) {
    34  	rf := NewRepoFile()
    35  	rf.Add(
    36  		&Entry{
    37  			Name:  "stable",
    38  			URL:   "https://example.com/stable/charts",
    39  			Cache: "stable-index.yaml",
    40  		},
    41  		&Entry{
    42  			Name:  "incubator",
    43  			URL:   "https://example.com/incubator",
    44  			Cache: "incubator-index.yaml",
    45  		},
    46  	)
    47  
    48  	if len(rf.Repositories) != 2 {
    49  		t.Fatal("Expected 2 repositories")
    50  	}
    51  
    52  	if rf.Has("nosuchrepo") {
    53  		t.Error("Found nonexistent repo")
    54  	}
    55  	if !rf.Has("incubator") {
    56  		t.Error("incubator repo is missing")
    57  	}
    58  
    59  	stable := rf.Repositories[0]
    60  	if stable.Name != "stable" {
    61  		t.Error("stable is not named stable")
    62  	}
    63  	if stable.URL != "https://example.com/stable/charts" {
    64  		t.Error("Wrong URL for stable")
    65  	}
    66  	if stable.Cache != "stable-index.yaml" {
    67  		t.Error("Wrong cache name for stable")
    68  	}
    69  }
    70  
    71  func TestLoadRepositoriesFile(t *testing.T) {
    72  	expects := NewRepoFile()
    73  	expects.Add(
    74  		&Entry{
    75  			Name:  "stable",
    76  			URL:   "https://example.com/stable/charts",
    77  			Cache: "stable-index.yaml",
    78  		},
    79  		&Entry{
    80  			Name:  "incubator",
    81  			URL:   "https://example.com/incubator",
    82  			Cache: "incubator-index.yaml",
    83  		},
    84  	)
    85  
    86  	repofile, err := LoadRepositoriesFile(testRepositoriesFile)
    87  	if err != nil {
    88  		t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err)
    89  	}
    90  
    91  	if len(expects.Repositories) != len(repofile.Repositories) {
    92  		t.Fatalf("Unexpected repo data: %#v", repofile.Repositories)
    93  	}
    94  
    95  	for i, expect := range expects.Repositories {
    96  		got := repofile.Repositories[i]
    97  		if expect.Name != got.Name {
    98  			t.Errorf("Expected name %q, got %q", expect.Name, got.Name)
    99  		}
   100  		if expect.URL != got.URL {
   101  			t.Errorf("Expected url %q, got %q", expect.URL, got.URL)
   102  		}
   103  		if expect.Cache != got.Cache {
   104  			t.Errorf("Expected cache %q, got %q", expect.Cache, got.Cache)
   105  		}
   106  	}
   107  }
   108  
   109  func TestLoadPreV1RepositoriesFile(t *testing.T) {
   110  	r, err := LoadRepositoriesFile("testdata/old-repositories.yaml")
   111  	if err != nil && err != ErrRepoOutOfDate {
   112  		t.Fatal(err)
   113  	}
   114  	if len(r.Repositories) != 3 {
   115  		t.Fatalf("Expected 3 repos: %#v", r)
   116  	}
   117  
   118  	// Because they are parsed as a map, we lose ordering.
   119  	found := false
   120  	for _, rr := range r.Repositories {
   121  		if rr.Name == "best-charts-ever" {
   122  			found = true
   123  		}
   124  	}
   125  	if !found {
   126  		t.Errorf("expected the best charts ever. Got %#v", r.Repositories)
   127  	}
   128  }
   129  
   130  func TestLoadChartRepository(t *testing.T) {
   131  	cr, err := LoadChartRepository(testRepository, testURL)
   132  	if err != nil {
   133  		t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
   134  	}
   135  
   136  	paths := []string{filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), filepath.Join(testRepository, "sprocket-1.1.0.tgz"), filepath.Join(testRepository, "sprocket-1.2.0.tgz")}
   137  
   138  	if cr.RootPath != testRepository {
   139  		t.Errorf("Expected %s as RootPath but got %s", testRepository, cr.RootPath)
   140  	}
   141  
   142  	if !reflect.DeepEqual(cr.ChartPaths, paths) {
   143  		t.Errorf("Expected %#v but got %#v\n", paths, cr.ChartPaths)
   144  	}
   145  
   146  	if cr.URL != testURL {
   147  		t.Errorf("Expected url for chart repository to be %s but got %s", testURL, cr.URL)
   148  	}
   149  }
   150  
   151  func TestIndex(t *testing.T) {
   152  	cr, err := LoadChartRepository(testRepository, testURL)
   153  	if err != nil {
   154  		t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
   155  	}
   156  
   157  	err = cr.Index()
   158  	if err != nil {
   159  		t.Errorf("Error performing index: %v\n", err)
   160  	}
   161  
   162  	tempIndexPath := filepath.Join(testRepository, indexPath)
   163  	actual, err := LoadIndexFile(tempIndexPath)
   164  	defer os.Remove(tempIndexPath) // clean up
   165  	if err != nil {
   166  		t.Errorf("Error loading index file %v", err)
   167  	}
   168  	verifyIndex(t, actual)
   169  
   170  	// Re-index and test again.
   171  	err = cr.Index()
   172  	if err != nil {
   173  		t.Errorf("Error performing re-index: %s\n", err)
   174  	}
   175  	second, err := LoadIndexFile(tempIndexPath)
   176  	if err != nil {
   177  		t.Errorf("Error re-loading index file %v", err)
   178  	}
   179  	verifyIndex(t, second)
   180  }
   181  
   182  func verifyIndex(t *testing.T, actual *IndexFile) {
   183  
   184  	var empty time.Time
   185  	if actual.Generated == empty {
   186  		t.Errorf("Generated should be greater than 0: %s", actual.Generated)
   187  	}
   188  
   189  	if actual.APIVersion != APIVersionV1 {
   190  		t.Error("Expected v1 API")
   191  	}
   192  
   193  	entries := actual.Entries
   194  	if numEntries := len(entries); numEntries != 2 {
   195  		t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries)
   196  	}
   197  
   198  	expects := map[string]ChartVersions{
   199  		"frobnitz": {
   200  			{
   201  				Metadata: &chart.Metadata{
   202  					Name:    "frobnitz",
   203  					Version: "1.2.3",
   204  				},
   205  			},
   206  		},
   207  		"sprocket": {
   208  			{
   209  				Metadata: &chart.Metadata{
   210  					Name:    "sprocket",
   211  					Version: "1.2.0",
   212  				},
   213  			},
   214  			{
   215  				Metadata: &chart.Metadata{
   216  					Name:    "sprocket",
   217  					Version: "1.1.0",
   218  				},
   219  			},
   220  		},
   221  	}
   222  
   223  	for name, versions := range expects {
   224  		got, ok := entries[name]
   225  		if !ok {
   226  			t.Errorf("Could not find %q entry", name)
   227  			continue
   228  		}
   229  		if len(versions) != len(got) {
   230  			t.Errorf("Expected %d versions, got %d", len(versions), len(got))
   231  			continue
   232  		}
   233  		for i, e := range versions {
   234  			g := got[i]
   235  			if e.Name != g.Name {
   236  				t.Errorf("Expected %q, got %q", e.Name, g.Name)
   237  			}
   238  			if e.Version != g.Version {
   239  				t.Errorf("Expected %q, got %q", e.Version, g.Version)
   240  			}
   241  			if len(g.Keywords) != 3 {
   242  				t.Error("Expected 3 keyrwords.")
   243  			}
   244  			if len(g.Maintainers) != 2 {
   245  				t.Error("Expected 2 maintainers.")
   246  			}
   247  			if g.Created == empty {
   248  				t.Error("Expected created to be non-empty")
   249  			}
   250  			if g.Description == "" {
   251  				t.Error("Expected description to be non-empty")
   252  			}
   253  			if g.Home == "" {
   254  				t.Error("Expected home to be non-empty")
   255  			}
   256  			if g.Digest == "" {
   257  				t.Error("Expected digest to be non-empty")
   258  			}
   259  			if len(g.URLs) != 1 {
   260  				t.Error("Expected exactly 1 URL")
   261  			}
   262  		}
   263  	}
   264  }