github.com/koderover/helm@v2.17.0+incompatible/pkg/repo/repo_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  	"strings"
    23  	"testing"
    24  )
    25  
    26  const testRepositoriesFile = "testdata/repositories.yaml"
    27  
    28  func TestRepoFile(t *testing.T) {
    29  	rf := NewRepoFile()
    30  	rf.Add(
    31  		&Entry{
    32  			Name:  "stable",
    33  			URL:   "https://example.com/stable/charts",
    34  			Cache: "stable-index.yaml",
    35  		},
    36  		&Entry{
    37  			Name:  "incubator",
    38  			URL:   "https://example.com/incubator",
    39  			Cache: "incubator-index.yaml",
    40  		},
    41  	)
    42  
    43  	if len(rf.Repositories) != 2 {
    44  		t.Fatal("Expected 2 repositories")
    45  	}
    46  
    47  	if rf.Has("nosuchrepo") {
    48  		t.Error("Found nonexistent repo")
    49  	}
    50  	if !rf.Has("incubator") {
    51  		t.Error("incubator repo is missing")
    52  	}
    53  
    54  	stable := rf.Repositories[0]
    55  	if stable.Name != "stable" {
    56  		t.Error("stable is not named stable")
    57  	}
    58  	if stable.URL != "https://example.com/stable/charts" {
    59  		t.Error("Wrong URL for stable")
    60  	}
    61  	if stable.Cache != "stable-index.yaml" {
    62  		t.Error("Wrong cache name for stable")
    63  	}
    64  }
    65  
    66  func TestNewRepositoriesFile(t *testing.T) {
    67  	expects := NewRepoFile()
    68  	expects.Add(
    69  		&Entry{
    70  			Name:  "stable",
    71  			URL:   "https://example.com/stable/charts",
    72  			Cache: "stable-index.yaml",
    73  		},
    74  		&Entry{
    75  			Name:  "incubator",
    76  			URL:   "https://example.com/incubator",
    77  			Cache: "incubator-index.yaml",
    78  		},
    79  	)
    80  
    81  	repofile, err := LoadRepositoriesFile(testRepositoriesFile)
    82  	if err != nil {
    83  		t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err)
    84  	}
    85  
    86  	if len(expects.Repositories) != len(repofile.Repositories) {
    87  		t.Fatalf("Unexpected repo data: %#v", repofile.Repositories)
    88  	}
    89  
    90  	for i, expect := range expects.Repositories {
    91  		got := repofile.Repositories[i]
    92  		if expect.Name != got.Name {
    93  			t.Errorf("Expected name %q, got %q", expect.Name, got.Name)
    94  		}
    95  		if expect.URL != got.URL {
    96  			t.Errorf("Expected url %q, got %q", expect.URL, got.URL)
    97  		}
    98  		if expect.Cache != got.Cache {
    99  			t.Errorf("Expected cache %q, got %q", expect.Cache, got.Cache)
   100  		}
   101  	}
   102  }
   103  
   104  func TestNewPreV1RepositoriesFile(t *testing.T) {
   105  	r, err := LoadRepositoriesFile("testdata/old-repositories.yaml")
   106  	if err != nil && err != ErrRepoOutOfDate {
   107  		t.Fatal(err)
   108  	}
   109  	if len(r.Repositories) != 3 {
   110  		t.Fatalf("Expected 3 repos: %#v", r)
   111  	}
   112  
   113  	// Because they are parsed as a map, we lose ordering.
   114  	found := false
   115  	for _, rr := range r.Repositories {
   116  		if rr.Name == "best-charts-ever" {
   117  			found = true
   118  		}
   119  	}
   120  	if !found {
   121  		t.Errorf("expected the best charts ever. Got %#v", r.Repositories)
   122  	}
   123  }
   124  
   125  func TestRepoFile_Get(t *testing.T) {
   126  	repo := NewRepoFile()
   127  	repo.Add(
   128  		&Entry{
   129  			Name:  "first",
   130  			URL:   "https://example.com/first",
   131  			Cache: "first-index.yaml",
   132  		},
   133  		&Entry{
   134  			Name:  "second",
   135  			URL:   "https://example.com/second",
   136  			Cache: "second-index.yaml",
   137  		},
   138  		&Entry{
   139  			Name:  "third",
   140  			URL:   "https://example.com/third",
   141  			Cache: "third-index.yaml",
   142  		},
   143  		&Entry{
   144  			Name:  "fourth",
   145  			URL:   "https://example.com/fourth",
   146  			Cache: "fourth-index.yaml",
   147  		},
   148  	)
   149  
   150  	name := "second"
   151  
   152  	entry, ok := repo.Get(name)
   153  	if !ok {
   154  		t.Fatalf("Expected repo entry %q to be found", name)
   155  	}
   156  
   157  	if entry.URL != "https://example.com/second" {
   158  		t.Fatalf("Expected repo URL to be %q but got %q", "https://example.com/second", entry.URL)
   159  	}
   160  }
   161  
   162  func TestRemoveRepository(t *testing.T) {
   163  	sampleRepository := NewRepoFile()
   164  	sampleRepository.Add(
   165  		&Entry{
   166  			Name:  "stable",
   167  			URL:   "https://example.com/stable/charts",
   168  			Cache: "stable-index.yaml",
   169  		},
   170  		&Entry{
   171  			Name:  "incubator",
   172  			URL:   "https://example.com/incubator",
   173  			Cache: "incubator-index.yaml",
   174  		},
   175  	)
   176  
   177  	removeRepository := "stable"
   178  	found := sampleRepository.Remove(removeRepository)
   179  	if !found {
   180  		t.Errorf("expected repository %s not found", removeRepository)
   181  	}
   182  
   183  	found = sampleRepository.Has(removeRepository)
   184  	if found {
   185  		t.Errorf("repository %s not deleted", removeRepository)
   186  	}
   187  }
   188  
   189  func TestUpdateRepository(t *testing.T) {
   190  	sampleRepository := NewRepoFile()
   191  	sampleRepository.Add(
   192  		&Entry{
   193  			Name:  "stable",
   194  			URL:   "https://example.com/stable/charts",
   195  			Cache: "stable-index.yaml",
   196  		},
   197  		&Entry{
   198  			Name:  "incubator",
   199  			URL:   "https://example.com/incubator",
   200  			Cache: "incubator-index.yaml",
   201  		},
   202  	)
   203  	newRepoName := "sample"
   204  	sampleRepository.Update(&Entry{Name: newRepoName,
   205  		URL:   "https://example.com/sample",
   206  		Cache: "sample-index.yaml",
   207  	})
   208  
   209  	if !sampleRepository.Has(newRepoName) {
   210  		t.Errorf("expected repository %s not found", newRepoName)
   211  	}
   212  	repoCount := len(sampleRepository.Repositories)
   213  
   214  	sampleRepository.Update(&Entry{Name: newRepoName,
   215  		URL:   "https://example.com/sample",
   216  		Cache: "sample-index.yaml",
   217  	})
   218  
   219  	if repoCount != len(sampleRepository.Repositories) {
   220  		t.Errorf("invalid number of repositories found %d, expected number of repositories %d", len(sampleRepository.Repositories), repoCount)
   221  	}
   222  }
   223  
   224  func TestWriteFile(t *testing.T) {
   225  	sampleRepository := NewRepoFile()
   226  	sampleRepository.Add(
   227  		&Entry{
   228  			Name:  "stable",
   229  			URL:   "https://example.com/stable/charts",
   230  			Cache: "stable-index.yaml",
   231  		},
   232  		&Entry{
   233  			Name:  "incubator",
   234  			URL:   "https://example.com/incubator",
   235  			Cache: "incubator-index.yaml",
   236  		},
   237  	)
   238  
   239  	repoFile, err := ioutil.TempFile("", "helm-repo")
   240  	if err != nil {
   241  		t.Errorf("failed to create test-file (%v)", err)
   242  	}
   243  	defer os.Remove(repoFile.Name())
   244  	if err := sampleRepository.WriteFile(repoFile.Name(), 0744); err != nil {
   245  		t.Errorf("failed to write file (%v)", err)
   246  	}
   247  
   248  	repos, err := LoadRepositoriesFile(repoFile.Name())
   249  	if err != nil {
   250  		t.Errorf("failed to load file (%v)", err)
   251  	}
   252  	for _, repo := range sampleRepository.Repositories {
   253  		if !repos.Has(repo.Name) {
   254  			t.Errorf("expected repository %s not found", repo.Name)
   255  		}
   256  	}
   257  }
   258  
   259  func TestRepoNotExists(t *testing.T) {
   260  	_, err := LoadRepositoriesFile("/this/path/does/not/exist.yaml")
   261  	if err == nil {
   262  		t.Errorf("expected err to be non-nil when path does not exist")
   263  	} else if !strings.Contains(err.Error(), "You might need to run `helm init`") {
   264  		t.Errorf("expected prompt to run `helm init` when repositories file does not exist")
   265  	}
   266  }