github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/repo_add_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  	"io"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/helm"
    27  	"k8s.io/helm/pkg/repo"
    28  	"k8s.io/helm/pkg/repo/repotest"
    29  )
    30  
    31  var testName = "test-name"
    32  
    33  func TestRepoAddCmd(t *testing.T) {
    34  	srv, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	cleanup := resetEnv()
    40  	defer func() {
    41  		srv.Stop()
    42  		os.RemoveAll(thome.String())
    43  		cleanup()
    44  	}()
    45  	if err := ensureTestHome(thome, t); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	settings.Home = thome
    50  
    51  	tests := []releaseCase{
    52  		{
    53  			name:     "add a repository",
    54  			args:     []string{testName, srv.URL()},
    55  			expected: "\"" + testName + "\" has been added to your repositories",
    56  		},
    57  	}
    58  
    59  	runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
    60  		return newRepoAddCmd(out)
    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  	cleanup := resetEnv()
    71  	hh := thome
    72  	defer func() {
    73  		ts.Stop()
    74  		os.RemoveAll(thome.String())
    75  		cleanup()
    76  	}()
    77  	if err := ensureTestHome(hh, t); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	settings.Home = thome
    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  }