github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/stefanmcshane/helm/internal/test/ensure"
    28  	"github.com/stefanmcshane/helm/pkg/helmpath"
    29  	"github.com/stefanmcshane/helm/pkg/repo"
    30  	"github.com/stefanmcshane/helm/pkg/repo/repotest"
    31  )
    32  
    33  func TestRepoRemove(t *testing.T) {
    34  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer ts.Stop()
    39  
    40  	rootDir := ensure.TempDir(t)
    41  	repoFile := filepath.Join(rootDir, "repositories.yaml")
    42  
    43  	const testRepoName = "test-name"
    44  
    45  	b := bytes.NewBuffer(nil)
    46  
    47  	rmOpts := repoRemoveOptions{
    48  		names:     []string{testRepoName},
    49  		repoFile:  repoFile,
    50  		repoCache: rootDir,
    51  	}
    52  
    53  	if err := rmOpts.run(os.Stderr); err == nil {
    54  		t.Errorf("Expected error removing %s, but did not get one.", testRepoName)
    55  	}
    56  	o := &repoAddOptions{
    57  		name:     testRepoName,
    58  		url:      ts.URL(),
    59  		repoFile: repoFile,
    60  	}
    61  
    62  	if err := o.run(os.Stderr); err != nil {
    63  		t.Error(err)
    64  	}
    65  
    66  	cacheIndexFile, cacheChartsFile := createCacheFiles(rootDir, testRepoName)
    67  
    68  	// Reset the buffer before running repo remove
    69  	b.Reset()
    70  
    71  	if err := rmOpts.run(b); err != nil {
    72  		t.Errorf("Error removing %s from repositories", testRepoName)
    73  	}
    74  	if !strings.Contains(b.String(), "has been removed") {
    75  		t.Errorf("Unexpected output: %s", b.String())
    76  	}
    77  
    78  	testCacheFiles(t, cacheIndexFile, cacheChartsFile, testRepoName)
    79  
    80  	f, err := repo.LoadFile(repoFile)
    81  	if err != nil {
    82  		t.Error(err)
    83  	}
    84  
    85  	if f.Has(testRepoName) {
    86  		t.Errorf("%s was not successfully removed from repositories list", testRepoName)
    87  	}
    88  
    89  	// Test removal of multiple repos in one go
    90  	var testRepoNames = []string{"foo", "bar", "baz"}
    91  	cacheFiles := make(map[string][]string, len(testRepoNames))
    92  
    93  	// Add test repos
    94  	for _, repoName := range testRepoNames {
    95  		o := &repoAddOptions{
    96  			name:     repoName,
    97  			url:      ts.URL(),
    98  			repoFile: repoFile,
    99  		}
   100  
   101  		if err := o.run(os.Stderr); err != nil {
   102  			t.Error(err)
   103  		}
   104  
   105  		cacheIndex, cacheChart := createCacheFiles(rootDir, repoName)
   106  		cacheFiles[repoName] = []string{cacheIndex, cacheChart}
   107  
   108  	}
   109  
   110  	// Create repo remove command
   111  	multiRmOpts := repoRemoveOptions{
   112  		names:     testRepoNames,
   113  		repoFile:  repoFile,
   114  		repoCache: rootDir,
   115  	}
   116  
   117  	// Reset the buffer before running repo remove
   118  	b.Reset()
   119  
   120  	// Run repo remove command
   121  	if err := multiRmOpts.run(b); err != nil {
   122  		t.Errorf("Error removing list of repos from repositories: %q", testRepoNames)
   123  	}
   124  
   125  	// Check that stuff were removed
   126  	if !strings.Contains(b.String(), "has been removed") {
   127  		t.Errorf("Unexpected output: %s", b.String())
   128  	}
   129  
   130  	for _, repoName := range testRepoNames {
   131  		f, err := repo.LoadFile(repoFile)
   132  		if err != nil {
   133  			t.Error(err)
   134  		}
   135  		if f.Has(repoName) {
   136  			t.Errorf("%s was not successfully removed from repositories list", repoName)
   137  		}
   138  		cacheIndex := cacheFiles[repoName][0]
   139  		cacheChart := cacheFiles[repoName][1]
   140  		testCacheFiles(t, cacheIndex, cacheChart, repoName)
   141  	}
   142  }
   143  
   144  func createCacheFiles(rootDir string, repoName string) (cacheIndexFile string, cacheChartsFile string) {
   145  	cacheIndexFile = filepath.Join(rootDir, helmpath.CacheIndexFile(repoName))
   146  	mf, _ := os.Create(cacheIndexFile)
   147  	mf.Close()
   148  
   149  	cacheChartsFile = filepath.Join(rootDir, helmpath.CacheChartsFile(repoName))
   150  	mf, _ = os.Create(cacheChartsFile)
   151  	mf.Close()
   152  
   153  	return cacheIndexFile, cacheChartsFile
   154  }
   155  
   156  func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, repoName string) {
   157  	if _, err := os.Stat(cacheIndexFile); err == nil {
   158  		t.Errorf("Error cache index file was not removed for repository %s", repoName)
   159  	}
   160  	if _, err := os.Stat(cacheChartsFile); err == nil {
   161  		t.Errorf("Error cache chart file was not removed for repository %s", repoName)
   162  	}
   163  }
   164  
   165  func TestRepoRemoveCompletion(t *testing.T) {
   166  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	defer ts.Stop()
   171  
   172  	rootDir := ensure.TempDir(t)
   173  	repoFile := filepath.Join(rootDir, "repositories.yaml")
   174  	repoCache := filepath.Join(rootDir, "cache/")
   175  
   176  	var testRepoNames = []string{"foo", "bar", "baz"}
   177  
   178  	// Add test repos
   179  	for _, repoName := range testRepoNames {
   180  		o := &repoAddOptions{
   181  			name:     repoName,
   182  			url:      ts.URL(),
   183  			repoFile: repoFile,
   184  		}
   185  
   186  		if err := o.run(os.Stderr); err != nil {
   187  			t.Error(err)
   188  		}
   189  	}
   190  
   191  	repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache)
   192  
   193  	// In the following tests, we turn off descriptions for completions by using __completeNoDesc.
   194  	// We have to do this because the description will contain the port used by the webserver,
   195  	// and that port changes each time we run the test.
   196  	tests := []cmdTestCase{{
   197  		name:   "completion for repo remove",
   198  		cmd:    fmt.Sprintf("%s __completeNoDesc repo remove ''", repoSetup),
   199  		golden: "output/repo_list_comp.txt",
   200  	}, {
   201  		name:   "completion for repo remove, no filter",
   202  		cmd:    fmt.Sprintf("%s __completeNoDesc repo remove fo", repoSetup),
   203  		golden: "output/repo_list_comp.txt",
   204  	}, {
   205  		name:   "completion for repo remove repetition",
   206  		cmd:    fmt.Sprintf("%s __completeNoDesc repo remove foo ''", repoSetup),
   207  		golden: "output/repo_repeat_comp.txt",
   208  	}}
   209  	for _, test := range tests {
   210  		runTestCmd(t, []cmdTestCase{test})
   211  	}
   212  }
   213  
   214  func TestRepoRemoveFileCompletion(t *testing.T) {
   215  	checkFileCompletion(t, "repo remove", false)
   216  	checkFileCompletion(t, "repo remove repo1", false)
   217  }