Fix helmmanager
[nonrtric/plt/sme.git] / capifcore / internal / helmmanagement / helm_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package helmmanagement
22
23 import (
24         "bufio"
25         "bytes"
26         "log"
27         "os"
28         "path/filepath"
29         "strings"
30         "testing"
31
32         "github.com/stretchr/testify/assert"
33         "github.com/stretchr/testify/mock"
34         "gopkg.in/yaml.v2"
35         "helm.sh/helm/v3/pkg/cli"
36         "helm.sh/helm/v3/pkg/getter"
37         "helm.sh/helm/v3/pkg/repo"
38         "helm.sh/helm/v3/pkg/time"
39         "oransc.org/nonrtric/capifcore/internal/helmmanagement/mocks"
40 )
41
42 func TestNoChartURL_repoNotSetUp(t *testing.T) {
43         managerUnderTest := NewHelmManager(nil)
44
45         res := managerUnderTest.SetUpRepo("repoName", "")
46
47         assert.Nil(t, res)
48         assert.False(t, managerUnderTest.setUp)
49 }
50
51 func TestSetUpRepoExistingRepoFile_repoShouldBeAddedToReposFile(t *testing.T) {
52         settings := createReposFile(t)
53
54         managerUnderTest := NewHelmManager(settings)
55
56         repoName := filepath.Dir(settings.RepositoryConfig)
57         repoURL := "http://url"
58         managerUnderTest.repo = getChartRepo(settings)
59
60         res := managerUnderTest.SetUpRepo(repoName, repoURL)
61
62         assert.Nil(t, res)
63         assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
64         assert.True(t, managerUnderTest.setUp)
65 }
66
67 func TestSetUpRepoFail_shouldNotBeSetUp(t *testing.T) {
68         settings := createReposFile(t)
69
70         managerUnderTest := NewHelmManager(settings)
71
72         res := managerUnderTest.SetUpRepo("repoName", "repoURL")
73
74         assert.NotNil(t, res)
75         assert.False(t, managerUnderTest.setUp)
76 }
77
78 func createReposFile(t *testing.T) *cli.EnvSettings {
79         reposDir, err := os.MkdirTemp("", "helm")
80         if err != nil {
81                 t.Errorf("Unable to create temporary directory for repos due to: %v", err)
82         }
83         t.Cleanup(func() {
84                 os.RemoveAll(reposDir)
85         })
86
87         reposFile := reposDir + "/index.yaml"
88         settings := &cli.EnvSettings{
89                 RepositoryConfig: reposFile,
90         }
91
92         repoData := repo.File{
93                 Generated:    time.Now().Time,
94                 Repositories: []*repo.Entry{},
95         }
96         data, err := yaml.Marshal(&repoData)
97         if err != nil {
98                 assert.Fail(t, "Unable to marshal repo config yaml")
99         }
100         err2 := os.WriteFile(settings.RepositoryConfig, data, 0666)
101         if err2 != nil {
102                 assert.Fail(t, "Unable to write repo config file")
103         }
104         return settings
105 }
106
107 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
108         repoURL := "http://repoURL"
109         c := repo.Entry{
110                 Name: filepath.Dir(settings.RepositoryConfig),
111                 URL:  repoURL,
112         }
113         r, _ := repo.NewChartRepository(&c, getter.All(settings))
114         r.Client = getCLientMock(repoURL)
115         return r
116 }
117
118 func getCLientMock(repoURL string) *mocks.Getter {
119         clientMock := &mocks.Getter{}
120         b := []byte("apiVersion: v1\nentries: {}\ngenerated: \"2022-10-28T09:14:08+02:00\"\nserverInfo: {}\n")
121         z := bytes.NewBuffer(b)
122         clientMock.On("Get", repoURL+"/index.yaml", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(z, nil)
123         return clientMock
124 }
125
126 func containsRepo(repoFile, repoName string) bool {
127         file, err := os.Open(repoFile)
128         if err != nil {
129                 log.Fatalf("Error opening repository file: %v", err)
130         }
131         defer func(file *os.File) {
132                 _ = file.Close()
133         }(file)
134
135         scanner := bufio.NewScanner(file)
136         for scanner.Scan() {
137                 row := scanner.Text()
138                 if strings.Contains(row, repoName) {
139                         return true
140                 }
141         }
142         return false
143 }