6f89c539a7c2eab30991db082eddf5867b713e7a
[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 TestSetUpRepo_repoShouldBeAddedToReposFile(t *testing.T) {
42         settings := createReposFile(t)
43
44         managerUnderTest := NewHelmManager(settings)
45
46         repoName := "repoName"
47         repoURL := "http://url"
48         managerUnderTest.repo = getChartRepo(settings)
49
50         res := managerUnderTest.SetUpRepo(repoName, repoURL)
51
52         assert.Nil(t, res)
53         assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
54 }
55
56 func createReposFile(t *testing.T) *cli.EnvSettings {
57         reposDir, err := os.MkdirTemp("", "helm")
58         if err != nil {
59                 t.Errorf("Unable to create temporary directory for repos due to: %v", err)
60         }
61         t.Cleanup(func() {
62                 os.RemoveAll(reposDir)
63         })
64
65         reposFile := reposDir + "/repositories.yaml"
66         settings := &cli.EnvSettings{
67                 RepositoryConfig: reposFile,
68         }
69
70         repoData := repo.File{
71                 Generated:    time.Now().Time,
72                 Repositories: []*repo.Entry{},
73         }
74         data, err := yaml.Marshal(&repoData)
75         if err != nil {
76                 assert.Fail(t, "Unable to marshal repo config yaml")
77         }
78         err2 := os.WriteFile(settings.RepositoryConfig, data, 0666)
79         if err2 != nil {
80                 assert.Fail(t, "Unable to write repo config file")
81         }
82         return settings
83 }
84
85 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
86         repoURL := "http://repoURL"
87         c := repo.Entry{
88                 Name: "",
89                 URL:  repoURL,
90         }
91         r, _ := repo.NewChartRepository(&c, getter.All(settings))
92         r.Client = getCLientMock(repoURL)
93         return r
94 }
95
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)
101         return clientMock
102 }
103
104 func containsRepo(repoFile, repoName string) bool {
105         file, err := os.Open(repoFile)
106         if err != nil {
107                 log.Fatalf("Error opening repository file: %v", err)
108         }
109         defer func(file *os.File) {
110                 _ = file.Close()
111         }(file)
112
113         scanner := bufio.NewScanner(file)
114         for scanner.Scan() {
115                 row := scanner.Text()
116                 if strings.Contains(row, repoName) {
117                         return true
118                 }
119         }
120         return false
121 }