github.com/x-helm/helm@v3.0.0-beta.3+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 TestFile(t *testing.T) {
    29  	rf := NewFile()
    30  	rf.Add(
    31  		&Entry{
    32  			Name: "stable",
    33  			URL:  "https://example.com/stable/charts",
    34  		},
    35  		&Entry{
    36  			Name: "incubator",
    37  			URL:  "https://example.com/incubator",
    38  		},
    39  	)
    40  
    41  	if len(rf.Repositories) != 2 {
    42  		t.Fatal("Expected 2 repositories")
    43  	}
    44  
    45  	if rf.Has("nosuchrepo") {
    46  		t.Error("Found nonexistent repo")
    47  	}
    48  	if !rf.Has("incubator") {
    49  		t.Error("incubator repo is missing")
    50  	}
    51  
    52  	stable := rf.Repositories[0]
    53  	if stable.Name != "stable" {
    54  		t.Error("stable is not named stable")
    55  	}
    56  	if stable.URL != "https://example.com/stable/charts" {
    57  		t.Error("Wrong URL for stable")
    58  	}
    59  }
    60  
    61  func TestNewFile(t *testing.T) {
    62  	expects := NewFile()
    63  	expects.Add(
    64  		&Entry{
    65  			Name: "stable",
    66  			URL:  "https://example.com/stable/charts",
    67  		},
    68  		&Entry{
    69  			Name: "incubator",
    70  			URL:  "https://example.com/incubator",
    71  		},
    72  	)
    73  
    74  	file, err := LoadFile(testRepositoriesFile)
    75  	if err != nil {
    76  		t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err)
    77  	}
    78  
    79  	if len(expects.Repositories) != len(file.Repositories) {
    80  		t.Fatalf("Unexpected repo data: %#v", file.Repositories)
    81  	}
    82  
    83  	for i, expect := range expects.Repositories {
    84  		got := file.Repositories[i]
    85  		if expect.Name != got.Name {
    86  			t.Errorf("Expected name %q, got %q", expect.Name, got.Name)
    87  		}
    88  		if expect.URL != got.URL {
    89  			t.Errorf("Expected url %q, got %q", expect.URL, got.URL)
    90  		}
    91  	}
    92  }
    93  
    94  func TestRemoveRepository(t *testing.T) {
    95  	sampleRepository := NewFile()
    96  	sampleRepository.Add(
    97  		&Entry{
    98  			Name: "stable",
    99  			URL:  "https://example.com/stable/charts",
   100  		},
   101  		&Entry{
   102  			Name: "incubator",
   103  			URL:  "https://example.com/incubator",
   104  		},
   105  	)
   106  
   107  	removeRepository := "stable"
   108  	found := sampleRepository.Remove(removeRepository)
   109  	if !found {
   110  		t.Errorf("expected repository %s not found", removeRepository)
   111  	}
   112  
   113  	found = sampleRepository.Has(removeRepository)
   114  	if found {
   115  		t.Errorf("repository %s not deleted", removeRepository)
   116  	}
   117  }
   118  
   119  func TestUpdateRepository(t *testing.T) {
   120  	sampleRepository := NewFile()
   121  	sampleRepository.Add(
   122  		&Entry{
   123  			Name: "stable",
   124  			URL:  "https://example.com/stable/charts",
   125  		},
   126  		&Entry{
   127  			Name: "incubator",
   128  			URL:  "https://example.com/incubator",
   129  		},
   130  	)
   131  	newRepoName := "sample"
   132  	sampleRepository.Update(&Entry{Name: newRepoName,
   133  		URL: "https://example.com/sample",
   134  	})
   135  
   136  	if !sampleRepository.Has(newRepoName) {
   137  		t.Errorf("expected repository %s not found", newRepoName)
   138  	}
   139  	repoCount := len(sampleRepository.Repositories)
   140  
   141  	sampleRepository.Update(&Entry{Name: newRepoName,
   142  		URL: "https://example.com/sample",
   143  	})
   144  
   145  	if repoCount != len(sampleRepository.Repositories) {
   146  		t.Errorf("invalid number of repositories found %d, expected number of repositories %d", len(sampleRepository.Repositories), repoCount)
   147  	}
   148  }
   149  
   150  func TestWriteFile(t *testing.T) {
   151  	sampleRepository := NewFile()
   152  	sampleRepository.Add(
   153  		&Entry{
   154  			Name: "stable",
   155  			URL:  "https://example.com/stable/charts",
   156  		},
   157  		&Entry{
   158  			Name: "incubator",
   159  			URL:  "https://example.com/incubator",
   160  		},
   161  	)
   162  
   163  	file, err := ioutil.TempFile("", "helm-repo")
   164  	if err != nil {
   165  		t.Errorf("failed to create test-file (%v)", err)
   166  	}
   167  	defer os.Remove(file.Name())
   168  	if err := sampleRepository.WriteFile(file.Name(), 0644); err != nil {
   169  		t.Errorf("failed to write file (%v)", err)
   170  	}
   171  
   172  	repos, err := LoadFile(file.Name())
   173  	if err != nil {
   174  		t.Errorf("failed to load file (%v)", err)
   175  	}
   176  	for _, repo := range sampleRepository.Repositories {
   177  		if !repos.Has(repo.Name) {
   178  			t.Errorf("expected repository %s not found", repo.Name)
   179  		}
   180  	}
   181  }
   182  
   183  func TestRepoNotExists(t *testing.T) {
   184  	if _, err := LoadFile("/this/path/does/not/exist.yaml"); err == nil {
   185  		t.Errorf("expected err to be non-nil when path does not exist")
   186  	} else if !strings.Contains(err.Error(), "couldn't load repositories file") {
   187  		t.Errorf("expected prompt `couldn't load repositories file`")
   188  	}
   189  }