github.com/sunilganatra/helm@v3.0.0-beta.3+incompatible/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  	"fmt"
    21  	"io/ioutil"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"helm.sh/helm/internal/test/ensure"
    26  	"helm.sh/helm/pkg/repo"
    27  	"helm.sh/helm/pkg/repo/repotest"
    28  )
    29  
    30  func TestRepoAddCmd(t *testing.T) {
    31  	srv, err := repotest.NewTempServer("testdata/testserver/*.*")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	defer srv.Stop()
    36  
    37  	repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
    38  
    39  	tests := []cmdTestCase{{
    40  		name:   "add a repository",
    41  		cmd:    fmt.Sprintf("repo add test-name %s --repository-config %s", srv.URL(), repoFile),
    42  		golden: "output/repo-add.txt",
    43  	}}
    44  
    45  	runTestCmd(t, tests)
    46  }
    47  
    48  func TestRepoAdd(t *testing.T) {
    49  	ts, err := repotest.NewTempServer("testdata/testserver/*.*")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	defer ts.Stop()
    54  
    55  	repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
    56  
    57  	const testRepoName = "test-name"
    58  
    59  	o := &repoAddOptions{
    60  		name:     testRepoName,
    61  		url:      ts.URL(),
    62  		noUpdate: true,
    63  		repoFile: repoFile,
    64  	}
    65  
    66  	if err := o.run(ioutil.Discard); err != nil {
    67  		t.Error(err)
    68  	}
    69  
    70  	f, err := repo.LoadFile(repoFile)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	if !f.Has(testRepoName) {
    76  		t.Errorf("%s was not successfully inserted into %s", testRepoName, repoFile)
    77  	}
    78  
    79  	o.noUpdate = false
    80  
    81  	if err := o.run(ioutil.Discard); err != nil {
    82  		t.Errorf("Repository was not updated: %s", err)
    83  	}
    84  
    85  	if err := o.run(ioutil.Discard); err != nil {
    86  		t.Errorf("Duplicate repository name was added")
    87  	}
    88  }