github.com/zoumo/helm@v2.5.0+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 oldhome := settings.Home 37 settings.Home = thome 38 defer func() { 39 srv.Stop() 40 settings.Home = oldhome 41 os.Remove(thome.String()) 42 }() 43 if err := ensureTestHome(thome, t); err != nil { 44 t.Fatal(err) 45 } 46 47 tests := []releaseCase{ 48 { 49 name: "add a repository", 50 args: []string{testName, srv.URL()}, 51 expected: testName + " has been added to your repositories", 52 }, 53 } 54 55 for _, tt := range tests { 56 buf := bytes.NewBuffer(nil) 57 c := newRepoAddCmd(buf) 58 if err := c.RunE(c, tt.args); err != nil { 59 t.Errorf("%q: expected %q, got %q", tt.name, tt.expected, err) 60 } 61 } 62 } 63 64 func TestRepoAdd(t *testing.T) { 65 ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*") 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 oldhome := settings.Home 71 settings.Home = thome 72 hh := thome 73 defer func() { 74 ts.Stop() 75 settings.Home = oldhome 76 os.Remove(thome.String()) 77 }() 78 if err := ensureTestHome(hh, t); err != nil { 79 t.Fatal(err) 80 } 81 82 if err := addRepository(testName, ts.URL(), hh, "", "", "", true); err != nil { 83 t.Error(err) 84 } 85 86 f, err := repo.LoadRepositoriesFile(hh.RepositoryFile()) 87 if err != nil { 88 t.Error(err) 89 } 90 91 if !f.Has(testName) { 92 t.Errorf("%s was not successfully inserted into %s", testName, hh.RepositoryFile()) 93 } 94 95 if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil { 96 t.Errorf("Repository was not updated: %s", err) 97 } 98 99 if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil { 100 t.Errorf("Duplicate repository name was added") 101 } 102 }