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 TestNoChartURL_reoNotSetUp(t *testing.T) {
42 managerUnderTest := NewHelmManager(nil)
44 res := managerUnderTest.SetUpRepo("repoName", "")
47 assert.False(t, managerUnderTest.setUp)
50 // func TestSetUpRepo_repoShouldBeAddedToReposFile(t *testing.T) {
51 // settings := createReposFile(t)
53 // managerUnderTest := NewHelmManager(settings)
55 // repoName := "repoName"
56 // repoURL := "http://url"
57 // managerUnderTest.repo = getChartRepo(settings)
59 // res := managerUnderTest.SetUpRepo(repoName, repoURL)
62 // assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
63 // assert.True(t, managerUnderTest.setUp)
66 func TestSetUpRepoFail_shouldNotBeSetUp(t *testing.T) {
67 settings := createReposFile(t)
69 managerUnderTest := NewHelmManager(settings)
71 res := managerUnderTest.SetUpRepo("repoName", "repoURL")
74 assert.False(t, managerUnderTest.setUp)
77 func createReposFile(t *testing.T) *cli.EnvSettings {
78 reposDir, err := os.MkdirTemp("", "helm")
80 t.Errorf("Unable to create temporary directory for repos due to: %v", err)
83 os.RemoveAll(reposDir)
86 reposFile := reposDir + "/repositories.yaml"
87 settings := &cli.EnvSettings{
88 RepositoryConfig: reposFile,
91 repoData := repo.File{
92 Generated: time.Now().Time,
93 Repositories: []*repo.Entry{},
95 data, err := yaml.Marshal(&repoData)
97 assert.Fail(t, "Unable to marshal repo config yaml")
99 err2 := os.WriteFile(settings.RepositoryConfig, data, 0666)
101 assert.Fail(t, "Unable to write repo config file")
106 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
107 repoURL := "http://repoURL"
112 r, _ := repo.NewChartRepository(&c, getter.All(settings))
113 r.Client = getCLientMock(repoURL)
117 func getCLientMock(repoURL string) *mocks.Getter {
118 clientMock := &mocks.Getter{}
119 b := []byte("apiVersion: v1\nentries: {}\ngenerated: \"2022-10-28T09:14:08+02:00\"\nserverInfo: {}\n")
120 z := bytes.NewBuffer(b)
121 clientMock.On("Get", repoURL+"/index.yaml", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(z, nil)
125 func containsRepo(repoFile, repoName string) bool {
126 file, err := os.Open(repoFile)
128 log.Fatalf("Error opening repository file: %v", err)
130 defer func(file *os.File) {
134 scanner := bufio.NewScanner(file)
136 row := scanner.Text()
137 if strings.Contains(row, repoName) {