github.com/azlyth/helm@v2.8.2+incompatible/cmd/helm/repo_add_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "testing" 23 24 "k8s.io/helm/pkg/repo" 25 "k8s.io/helm/pkg/repo/repotest" 26 ) 27 28 var testName = "test-name" 29 30 func TestRepoAddCmd(t *testing.T) { 31 srv, thome, err := repotest.NewTempServer("testdata/testserver/*.*") 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 cleanup := resetEnv() 37 defer func() { 38 srv.Stop() 39 os.Remove(thome.String()) 40 cleanup() 41 }() 42 if err := ensureTestHome(thome, t); err != nil { 43 t.Fatal(err) 44 } 45 46 settings.Home = thome 47 48 tests := []releaseCase{ 49 { 50 name: "add a repository", 51 args: []string{testName, srv.URL()}, 52 expected: testName + " has been added to your repositories", 53 }, 54 } 55 56 for _, tt := range tests { 57 buf := bytes.NewBuffer(nil) 58 c := newRepoAddCmd(buf) 59 if err := c.RunE(c, tt.args); err != nil { 60 t.Errorf("%q: expected %q, got %q", tt.name, tt.expected, err) 61 } 62 } 63 } 64 65 func TestRepoAdd(t *testing.T) { 66 ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*") 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 cleanup := resetEnv() 72 hh := thome 73 defer func() { 74 ts.Stop() 75 os.Remove(thome.String()) 76 cleanup() 77 }() 78 if err := ensureTestHome(hh, t); err != nil { 79 t.Fatal(err) 80 } 81 82 settings.Home = thome 83 84 if err := addRepository(testName, ts.URL(), hh, "", "", "", true); err != nil { 85 t.Error(err) 86 } 87 88 f, err := repo.LoadRepositoriesFile(hh.RepositoryFile()) 89 if err != nil { 90 t.Error(err) 91 } 92 93 if !f.Has(testName) { 94 t.Errorf("%s was not successfully inserted into %s", testName, hh.RepositoryFile()) 95 } 96 97 if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil { 98 t.Errorf("Repository was not updated: %s", err) 99 } 100 101 if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil { 102 t.Errorf("Duplicate repository name was added") 103 } 104 }