github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+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/cmd/helm/helmpath"
    25  	"k8s.io/helm/pkg/repo"
    26  	"k8s.io/helm/pkg/repo/repotest"
    27  )
    28  
    29  var testName = "test-name"
    30  
    31  func TestRepoAddCmd(t *testing.T) {
    32  	srv, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	oldhome := homePath()
    38  	helmHome = thome
    39  	defer func() {
    40  		srv.Stop()
    41  		helmHome = oldhome
    42  		os.Remove(thome)
    43  	}()
    44  	if err := ensureTestHome(helmpath.Home(thome), t); err != nil {
    45  		t.Fatal(err)
    46  	}
    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  	oldhome := homePath()
    72  	helmHome = thome
    73  	hh := helmpath.Home(thome)
    74  	defer func() {
    75  		ts.Stop()
    76  		helmHome = oldhome
    77  		os.Remove(thome)
    78  	}()
    79  	if err := ensureTestHome(hh, t); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	if err := addRepository(testName, ts.URL(), hh, "", "", "", true); err != nil {
    84  		t.Error(err)
    85  	}
    86  
    87  	f, err := repo.LoadRepositoriesFile(hh.RepositoryFile())
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  
    92  	if !f.Has(testName) {
    93  		t.Errorf("%s was not successfully inserted into %s", testName, hh.RepositoryFile())
    94  	}
    95  
    96  	if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil {
    97  		t.Errorf("Repository was not updated: %s", err)
    98  	}
    99  
   100  	if err := addRepository(testName, ts.URL(), hh, "", "", "", false); err != nil {
   101  		t.Errorf("Duplicate repository name was added")
   102  	}
   103  }