Refactor Helm management
[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         "strings"
29         "testing"
30
31         "github.com/stretchr/testify/assert"
32         "github.com/stretchr/testify/mock"
33         "gopkg.in/yaml.v2"
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"
39 )
40
41 func TestNoChartURL_reoNotSetUp(t *testing.T) {
42         managerUnderTest := NewHelmManager(nil)
43
44         res := managerUnderTest.SetUpRepo("repoName", "")
45
46         assert.Nil(t, res)
47         assert.False(t, managerUnderTest.setUp)
48 }
49
50 // func TestSetUpRepo_repoShouldBeAddedToReposFile(t *testing.T) {
51 //      settings := createReposFile(t)
52
53 //      managerUnderTest := NewHelmManager(settings)
54
55 //      repoName := "repoName"
56 //      repoURL := "http://url"
57 //      managerUnderTest.repo = getChartRepo(settings)
58
59 //      res := managerUnderTest.SetUpRepo(repoName, repoURL)
60
61 //      assert.Nil(t, res)
62 //      assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
63 //      assert.True(t, managerUnderTest.setUp)
64 // }
65
66 func TestSetUpRepoFail_shouldNotBeSetUp(t *testing.T) {
67         settings := createReposFile(t)
68
69         managerUnderTest := NewHelmManager(settings)
70
71         res := managerUnderTest.SetUpRepo("repoName", "repoURL")
72
73         assert.NotNil(t, res)
74         assert.False(t, managerUnderTest.setUp)
75 }
76
77 func createReposFile(t *testing.T) *cli.EnvSettings {
78         reposDir, err := os.MkdirTemp("", "helm")
79         if err != nil {
80                 t.Errorf("Unable to create temporary directory for repos due to: %v", err)
81         }
82         t.Cleanup(func() {
83                 os.RemoveAll(reposDir)
84         })
85
86         reposFile := reposDir + "/repositories.yaml"
87         settings := &cli.EnvSettings{
88                 RepositoryConfig: reposFile,
89         }
90
91         repoData := repo.File{
92                 Generated:    time.Now().Time,
93                 Repositories: []*repo.Entry{},
94         }
95         data, err := yaml.Marshal(&repoData)
96         if err != nil {
97                 assert.Fail(t, "Unable to marshal repo config yaml")
98         }
99         err2 := os.WriteFile(settings.RepositoryConfig, data, 0666)
100         if err2 != nil {
101                 assert.Fail(t, "Unable to write repo config file")
102         }
103         return settings
104 }
105
106 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
107         repoURL := "http://repoURL"
108         c := repo.Entry{
109                 Name: "",
110                 URL:  repoURL,
111         }
112         r, _ := repo.NewChartRepository(&c, getter.All(settings))
113         r.Client = getCLientMock(repoURL)
114         return r
115 }
116
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)
122         return clientMock
123 }
124
125 func containsRepo(repoFile, repoName string) bool {
126         file, err := os.Open(repoFile)
127         if err != nil {
128                 log.Fatalf("Error opening repository file: %v", err)
129         }
130         defer func(file *os.File) {
131                 _ = file.Close()
132         }(file)
133
134         scanner := bufio.NewScanner(file)
135         for scanner.Scan() {
136                 row := scanner.Text()
137                 if strings.Contains(row, repoName) {
138                         return true
139                 }
140         }
141         return false
142 }