...

Source file src/helm.sh/helm/v3/pkg/repo/repo_test.go

Documentation: helm.sh/helm/v3/pkg/repo

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

View as plain text