github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/codefresh-io/kcfi/pkg/helm-internal/test/ensure" 27 "helm.sh/helm/v3/pkg/helmpath" 28 "helm.sh/helm/v3/pkg/repo" 29 "helm.sh/helm/v3/pkg/repo/repotest" 30 ) 31 32 func TestRepoRemove(t *testing.T) { 33 ts, err := repotest.NewTempServer("testdata/testserver/*.*") 34 if err != nil { 35 t.Fatal(err) 36 } 37 defer ts.Stop() 38 39 rootDir := ensure.TempDir(t) 40 repoFile := filepath.Join(rootDir, "repositories.yaml") 41 42 const testRepoName = "test-name" 43 44 b := bytes.NewBuffer(nil) 45 46 rmOpts := repoRemoveOptions{ 47 names: []string{testRepoName}, 48 repoFile: repoFile, 49 repoCache: rootDir, 50 } 51 52 if err := rmOpts.run(os.Stderr); err == nil { 53 t.Errorf("Expected error removing %s, but did not get one.", testRepoName) 54 } 55 o := &repoAddOptions{ 56 name: testRepoName, 57 url: ts.URL(), 58 repoFile: repoFile, 59 } 60 61 if err := o.run(os.Stderr); err != nil { 62 t.Error(err) 63 } 64 65 cacheIndexFile, cacheChartsFile := createCacheFiles(rootDir, testRepoName) 66 67 // Reset the buffer before running repo remove 68 b.Reset() 69 70 if err := rmOpts.run(b); err != nil { 71 t.Errorf("Error removing %s from repositories", testRepoName) 72 } 73 if !strings.Contains(b.String(), "has been removed") { 74 t.Errorf("Unexpected output: %s", b.String()) 75 } 76 77 testCacheFiles(t, cacheIndexFile, cacheChartsFile, testRepoName) 78 79 f, err := repo.LoadFile(repoFile) 80 if err != nil { 81 t.Error(err) 82 } 83 84 if f.Has(testRepoName) { 85 t.Errorf("%s was not successfully removed from repositories list", testRepoName) 86 } 87 88 // Test removal of multiple repos in one go 89 var testRepoNames = []string{"foo", "bar", "baz"} 90 cacheFiles := make(map[string][]string, len(testRepoNames)) 91 92 // Add test repos 93 for _, repoName := range testRepoNames { 94 o := &repoAddOptions{ 95 name: repoName, 96 url: ts.URL(), 97 repoFile: repoFile, 98 } 99 100 if err := o.run(os.Stderr); err != nil { 101 t.Error(err) 102 } 103 104 cacheIndex, cacheChart := createCacheFiles(rootDir, repoName) 105 cacheFiles[repoName] = []string{cacheIndex, cacheChart} 106 107 } 108 109 // Create repo remove command 110 multiRmOpts := repoRemoveOptions{ 111 names: testRepoNames, 112 repoFile: repoFile, 113 repoCache: rootDir, 114 } 115 116 // Reset the buffer before running repo remove 117 b.Reset() 118 119 // Run repo remove command 120 if err := multiRmOpts.run(b); err != nil { 121 t.Errorf("Error removing list of repos from repositories: %q", testRepoNames) 122 } 123 124 // Check that stuff were removed 125 if !strings.Contains(b.String(), "has been removed") { 126 t.Errorf("Unexpected output: %s", b.String()) 127 } 128 129 for _, repoName := range testRepoNames { 130 f, err := repo.LoadFile(repoFile) 131 if err != nil { 132 t.Error(err) 133 } 134 if f.Has(repoName) { 135 t.Errorf("%s was not successfully removed from repositories list", repoName) 136 } 137 cacheIndex := cacheFiles[repoName][0] 138 cacheChart := cacheFiles[repoName][1] 139 testCacheFiles(t, cacheIndex, cacheChart, repoName) 140 } 141 } 142 143 func createCacheFiles(rootDir string, repoName string) (cacheIndexFile string, cacheChartsFile string) { 144 cacheIndexFile = filepath.Join(rootDir, helmpath.CacheIndexFile(repoName)) 145 mf, _ := os.Create(cacheIndexFile) 146 mf.Close() 147 148 cacheChartsFile = filepath.Join(rootDir, helmpath.CacheChartsFile(repoName)) 149 mf, _ = os.Create(cacheChartsFile) 150 mf.Close() 151 152 return cacheIndexFile, cacheChartsFile 153 } 154 155 func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, repoName string) { 156 if _, err := os.Stat(cacheIndexFile); err == nil { 157 t.Errorf("Error cache index file was not removed for repository %s", repoName) 158 } 159 if _, err := os.Stat(cacheChartsFile); err == nil { 160 t.Errorf("Error cache chart file was not removed for repository %s", repoName) 161 } 162 }