github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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  	"os"
    23  	"path/filepath"
    24  	"sync"
    25  	"testing"
    26  
    27  	"sigs.k8s.io/yaml"
    28  
    29  	"github.com/codefresh-io/kcfi/pkg/helm-internal/test/ensure"
    30  	"helm.sh/helm/v3/pkg/helmpath"
    31  	"helm.sh/helm/v3/pkg/helmpath/xdg"
    32  	"helm.sh/helm/v3/pkg/repo"
    33  	"helm.sh/helm/v3/pkg/repo/repotest"
    34  )
    35  
    36  func TestRepoAddCmd(t *testing.T) {
    37  	srv, err := repotest.NewTempServer("testdata/testserver/*.*")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer srv.Stop()
    42  
    43  	repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
    44  
    45  	tests := []cmdTestCase{{
    46  		name:   "add a repository",
    47  		cmd:    fmt.Sprintf("repo add test-name %s --repository-config %s", srv.URL(), repoFile),
    48  		golden: "output/repo-add.txt",
    49  	}}
    50  
    51  	runTestCmd(t, tests)
    52  }
    53  
    54  func TestRepoAdd(t *testing.T) {
    55  	ts, err := repotest.NewTempServer("testdata/testserver/*.*")
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	defer ts.Stop()
    60  
    61  	rootDir := ensure.TempDir(t)
    62  	repoFile := filepath.Join(rootDir, "repositories.yaml")
    63  
    64  	const testRepoName = "test-name"
    65  
    66  	o := &repoAddOptions{
    67  		name:     testRepoName,
    68  		url:      ts.URL(),
    69  		noUpdate: true,
    70  		repoFile: repoFile,
    71  	}
    72  	os.Setenv(xdg.CacheHomeEnvVar, rootDir)
    73  
    74  	if err := o.run(ioutil.Discard); err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	f, err := repo.LoadFile(repoFile)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	if !f.Has(testRepoName) {
    84  		t.Errorf("%s was not successfully inserted into %s", testRepoName, repoFile)
    85  	}
    86  
    87  	idx := filepath.Join(helmpath.CachePath("repository"), helmpath.CacheIndexFile(testRepoName))
    88  	if _, err := os.Stat(idx); os.IsNotExist(err) {
    89  		t.Errorf("Error cache index file was not created for repository %s", testRepoName)
    90  	}
    91  	idx = filepath.Join(helmpath.CachePath("repository"), helmpath.CacheChartsFile(testRepoName))
    92  	if _, err := os.Stat(idx); os.IsNotExist(err) {
    93  		t.Errorf("Error cache charts file was not created for repository %s", testRepoName)
    94  	}
    95  
    96  	o.noUpdate = false
    97  
    98  	if err := o.run(ioutil.Discard); err != nil {
    99  		t.Errorf("Repository was not updated: %s", err)
   100  	}
   101  
   102  	if err := o.run(ioutil.Discard); err != nil {
   103  		t.Errorf("Duplicate repository name was added")
   104  	}
   105  }
   106  
   107  func TestRepoAddConcurrentGoRoutines(t *testing.T) {
   108  	const testName = "test-name"
   109  	repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
   110  	repoAddConcurrent(t, testName, repoFile)
   111  }
   112  
   113  func TestRepoAddConcurrentDirNotExist(t *testing.T) {
   114  	const testName = "test-name-2"
   115  	repoFile := filepath.Join(ensure.TempDir(t), "foo", "repositories.yaml")
   116  	repoAddConcurrent(t, testName, repoFile)
   117  }
   118  
   119  func repoAddConcurrent(t *testing.T, testName, repoFile string) {
   120  	ts, err := repotest.NewTempServer("testdata/testserver/*.*")
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	defer ts.Stop()
   125  
   126  	var wg sync.WaitGroup
   127  	wg.Add(3)
   128  	for i := 0; i < 3; i++ {
   129  		go func(name string) {
   130  			defer wg.Done()
   131  			o := &repoAddOptions{
   132  				name:     name,
   133  				url:      ts.URL(),
   134  				noUpdate: true,
   135  				repoFile: repoFile,
   136  			}
   137  			if err := o.run(ioutil.Discard); err != nil {
   138  				t.Error(err)
   139  			}
   140  		}(fmt.Sprintf("%s-%d", testName, i))
   141  	}
   142  	wg.Wait()
   143  
   144  	b, err := ioutil.ReadFile(repoFile)
   145  	if err != nil {
   146  		t.Error(err)
   147  	}
   148  
   149  	var f repo.File
   150  	if err := yaml.Unmarshal(b, &f); err != nil {
   151  		t.Error(err)
   152  	}
   153  
   154  	var name string
   155  	for i := 0; i < 3; i++ {
   156  		name = fmt.Sprintf("%s-%d", testName, i)
   157  		if !f.Has(name) {
   158  			t.Errorf("%s was not successfully inserted into %s: %s", name, repoFile, f.Repositories[0])
   159  		}
   160  	}
   161  }