github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/repo_remove_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 main
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/helm/helmpath"
    27  	"k8s.io/helm/pkg/repo"
    28  	"k8s.io/helm/pkg/repo/repotest"
    29  )
    30  
    31  func TestRepoRemove(t *testing.T) {
    32  	ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	hh := helmpath.Home(thome)
    38  	cleanup := resetEnv()
    39  	defer func() {
    40  		ts.Stop()
    41  		os.RemoveAll(thome.String())
    42  		cleanup()
    43  	}()
    44  	if err := ensureTestHome(hh, t); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	settings.Home = thome
    49  
    50  	b := bytes.NewBuffer(nil)
    51  
    52  	if err := removeRepoLine(b, testName, hh); err == nil {
    53  		t.Errorf("Expected error removing %s, but did not get one.", testName)
    54  	}
    55  	if err := addRepository(testName, ts.URL(), "", "", hh, "", "", "", true); err != nil {
    56  		t.Error(err)
    57  	}
    58  
    59  	mf, _ := os.Create(hh.CacheIndex(testName))
    60  	mf.Close()
    61  
    62  	b.Reset()
    63  	if err := removeRepoLine(b, testName, hh); err != nil {
    64  		t.Errorf("Error removing %s from repositories", testName)
    65  	}
    66  	if !strings.Contains(b.String(), "has been removed") {
    67  		t.Errorf("Unexpected output: %s", b.String())
    68  	}
    69  
    70  	if _, err := os.Stat(hh.CacheIndex(testName)); err == nil {
    71  		t.Errorf("Error cache file was not removed for repository %s", testName)
    72  	}
    73  
    74  	f, err := repo.LoadRepositoriesFile(hh.RepositoryFile())
    75  	if err != nil {
    76  		t.Error(err)
    77  	}
    78  
    79  	if f.Has(testName) {
    80  		t.Errorf("%s was not successfully removed from repositories list", testName)
    81  	}
    82  }
    83  
    84  func TestRepoRemove_NoArguments(t *testing.T) {
    85  	cmd := newRepoRemoveCmd(ioutil.Discard)
    86  	if err := cmd.RunE(cmd, []string{}); err == nil {
    87  		t.Errorf("Expected an error since no repo names were provided")
    88  	}
    89  }
    90  
    91  func TestRepoRemove_MultipleRepos(t *testing.T) {
    92  	ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	hh := helmpath.Home(thome)
    98  	cleanup := resetEnv()
    99  	defer func() {
   100  		ts.Stop()
   101  		os.RemoveAll(thome.String())
   102  		cleanup()
   103  	}()
   104  	if err := ensureTestHome(hh, t); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	settings.Home = thome
   109  
   110  	repoFoo := testName + "foo"
   111  	repoBar := testName + "bar"
   112  
   113  	if err := addRepository(repoFoo, ts.URL(), "", "", hh, "", "", "", true); err != nil {
   114  		t.Error(err)
   115  	}
   116  	if err := addRepository(repoBar, ts.URL(), "", "", hh, "", "", "", true); err != nil {
   117  		t.Error(err)
   118  	}
   119  
   120  	b := bytes.NewBuffer(nil)
   121  
   122  	cmd := newRepoRemoveCmd(b)
   123  	if err := cmd.RunE(cmd, []string{repoFoo, repoBar}); err != nil {
   124  		t.Error(err)
   125  	}
   126  
   127  	if !strings.Contains(b.String(), repoFoo) {
   128  		t.Errorf("Expected %q in output, found: %q", repoFoo, b.String())
   129  	}
   130  	if !strings.Contains(b.String(), repoBar) {
   131  		t.Errorf("Expected %q in output, found: %q", repoBar, b.String())
   132  	}
   133  
   134  	f, err := repo.LoadRepositoriesFile(hh.RepositoryFile())
   135  	if err != nil {
   136  		t.Error(err)
   137  	}
   138  
   139  	if f.Has(repoFoo) {
   140  		t.Errorf("%s was not successfully removed from repositories list", repoFoo)
   141  	}
   142  	if f.Has(repoBar) {
   143  		t.Errorf("%s was not successfully removed from repositories list", repoBar)
   144  	}
   145  }