2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
21 package helmmanagement
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
34 "helm.sh/helm/v3/pkg/cli"
35 "helm.sh/helm/v3/pkg/getter"
36 "helm.sh/helm/v3/pkg/repo"
37 "helm.sh/helm/v3/pkg/time"
38 "oransc.org/nonrtric/capifcore/internal/helmmanagement/mocks"
41 func TestSetUpRepo_repoShouldBeAddedToReposFile(t *testing.T) {
42 settings := createReposFile(t)
44 managerUnderTest := NewHelmManager(settings)
46 repoName := "repoName"
47 repoURL := "http://url"
48 managerUnderTest.repo = getChartRepo(settings)
50 res := managerUnderTest.SetUpRepo(repoName, repoURL)
53 assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
56 func createReposFile(t *testing.T) *cli.EnvSettings {
57 reposDir, err := os.MkdirTemp("", "helm")
59 t.Errorf("Unable to create temporary directory for repos due to: %v", err)
62 os.RemoveAll(reposDir)
65 reposFile := reposDir + "/repositories.yaml"
66 settings := &cli.EnvSettings{
67 RepositoryConfig: reposFile,
70 repoData := repo.File{
71 Generated: time.Now().Time,
72 Repositories: []*repo.Entry{},
74 data, err := yaml.Marshal(&repoData)
76 assert.Fail(t, "Unable to marshal repo config yaml")
78 err2 := os.WriteFile(settings.RepositoryConfig, data, 0666)
80 assert.Fail(t, "Unable to write repo config file")
85 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
86 repoURL := "http://repoURL"
91 r, _ := repo.NewChartRepository(&c, getter.All(settings))
92 r.Client = getCLientMock(repoURL)
96 func getCLientMock(repoURL string) *mocks.Getter {
97 clientMock := &mocks.Getter{}
98 b := []byte("apiVersion: v1\nentries: {}\ngenerated: \"2022-10-28T09:14:08+02:00\"\nserverInfo: {}\n")
99 z := bytes.NewBuffer(b)
100 clientMock.On("Get", repoURL+"/index.yaml", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(z, nil)
104 func containsRepo(repoFile, repoName string) bool {
105 file, err := os.Open(repoFile)
107 log.Fatalf("Error opening repository file: %v", err)
109 defer func(file *os.File) {
113 scanner := bufio.NewScanner(file)
115 row := scanner.Text()
116 if strings.Contains(row, repoName) {