Merge "Ignore 404 when deleting non-existent project in emco.sh"
[multicloud/k8s.git] / src / k8splugin / internal / helm / helm_test.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package helm
18
19 import (
20         "crypto/sha256"
21         "fmt"
22         "io/ioutil"
23         "path/filepath"
24         "strings"
25         "testing"
26 )
27
28 func TestProcessValues(t *testing.T) {
29
30         chartDir := "../../mock_files/mock_charts/testchart2"
31         profileDir := "../../mock_files/mock_profiles/profile1"
32
33         testCases := []struct {
34                 label         string
35                 valueFiles    []string
36                 values        []string
37                 expectedHash  string
38                 expectedError string
39         }{
40                 {
41                         label: "Process Values with Value Files Override",
42                         valueFiles: []string{
43                                 filepath.Join(chartDir, "values.yaml"),
44                                 filepath.Join(profileDir, "override_values.yaml"),
45                         },
46                         //Hash of a combined values.yaml file that is expected
47                         expectedHash:  "c18a70f426933de3c051c996dc34fd537d0131b2d13a2112a2ecff674db6c2f9",
48                         expectedError: "",
49                 },
50                 {
51                         label: "Process Values with Values Pair Override",
52                         valueFiles: []string{
53                                 filepath.Join(chartDir, "values.yaml"),
54                         },
55                         //Use the same convention as specified in helm template --set
56                         values: []string{
57                                 "service.externalPort=82",
58                         },
59                         //Hash of a combined values.yaml file that is expected
60                         expectedHash:  "028a3521fc9f8777ea7e67a6de0c51f2c875b88ca91734999657f0ca924ddb7a",
61                         expectedError: "",
62                 },
63                 {
64                         label: "Process Values with Both Overrides",
65                         valueFiles: []string{
66                                 filepath.Join(chartDir, "values.yaml"),
67                                 filepath.Join(profileDir, "override_values.yaml"),
68                         },
69                         //Use the same convention as specified in helm template --set
70                         //Key takes precedence over the value from override_values.yaml
71                         values: []string{
72                                 "service.externalPort=82",
73                         },
74                         //Hash of a combined values.yaml file that is expected
75                         expectedHash:  "516fab4ab7b76ba2ff35a97c2a79b74302543f532857b945f2fe25e717e755be",
76                         expectedError: "",
77                 },
78                 {
79                         label: "Process complex Pair Override",
80                         values: []string{
81                                 "name={a,b,c}",
82                                 "servers[0].port=80",
83                         },
84                         expectedError: "",
85                         expectedHash:  "50d9401b003f65c1ccfd1c5155106fff88c8201ab8b7d66bd6ffa4fe2883bead",
86                 },
87         }
88
89         h := sha256.New()
90
91         for _, testCase := range testCases {
92                 t.Run(testCase.label, func(t *testing.T) {
93                         tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename")
94                         out, err := tc.processValues(testCase.valueFiles, testCase.values)
95                         if err != nil {
96                                 if testCase.expectedError == "" {
97                                         t.Fatalf("Got an error %s", err)
98                                 }
99                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
100                                         t.Fatalf("Got unexpected error message %s", err)
101                                 }
102                         } else {
103                                 //Compute the hash of returned data and compare
104                                 h.Write(out)
105                                 gotHash := fmt.Sprintf("%x", h.Sum(nil))
106                                 h.Reset()
107                                 if gotHash != testCase.expectedHash {
108                                         t.Fatalf("Got unexpected hash '%s' of values.yaml:\n%s", gotHash, out)
109                                 }
110                         }
111                 })
112         }
113 }
114
115 func TestGenerateKubernetesArtifacts(t *testing.T) {
116
117         chartDir := "../../mock_files/mock_charts/testchart2"
118         profileDir := "../../mock_files/mock_profiles/profile1"
119
120         testCases := []struct {
121                 label           string
122                 chartPath       string
123                 valueFiles      []string
124                 values          []string
125                 expectedHashMap map[string]string
126                 expectedError   string
127         }{
128                 {
129                         label:      "Generate artifacts without any overrides",
130                         chartPath:  chartDir,
131                         valueFiles: []string{},
132                         values:     []string{},
133                         //sha256 hash of the evaluated templates in each chart
134                         expectedHashMap: map[string]string{
135                                 "testchart2/templates/service.yaml": "fdd6a2b6795486f0dd1d8c44379afb5ffe4072c09f9cf6594738e8ded4dd872d",
136                                 "subcharta/templates/service.yaml":  "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0",
137                                 "subchartb/templates/service.yaml":  "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a",
138                         },
139                         expectedError: "",
140                 },
141                 {
142                         label:     "Generate artifacts with overrides",
143                         chartPath: chartDir,
144                         valueFiles: []string{
145                                 filepath.Join(profileDir, "override_values.yaml"),
146                         },
147                         values: []string{
148                                 "service.externalPort=82",
149                         },
150                         //sha256 hash of the evaluated templates in each chart
151                         expectedHashMap: map[string]string{
152                                 "testchart2/templates/service.yaml": "2bb96e791ecb6a3404bc5de3f6c4182aed881630269e2aa6766df38b0f852724",
153                                 "subcharta/templates/service.yaml":  "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0",
154                                 "subchartb/templates/service.yaml":  "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a",
155                         },
156                         expectedError: "",
157                 },
158                 {
159                         label:      "Generate artifacts from multi-template and empty files v1",
160                         chartPath:  "../../mock_files/mock_charts/testchart3",
161                         valueFiles: []string{},
162                         values: []string{
163                                 "goingEmpty=false",
164                         },
165                         expectedHashMap: map[string]string{
166                                 "testchart3/templates/multi.yaml-2": "e24cbbefac2c2f700880b8fd041838f2dd48bbc1e099e7c1d2485ae7feb3da0d",
167                                 "testchart3/templates/multi.yaml-3": "592a8e5b2c35b8469aa45703a835bc00657bfe36b51eb08427a46e7d22fb1525",
168                         },
169                         expectedError: "",
170                 },
171                 {
172                         label:      "Generate artifacts from multi-template and empty files v2",
173                         chartPath:  "../../mock_files/mock_charts/testchart3",
174                         valueFiles: []string{},
175                         values: []string{
176                                 "goingEmpty=true",
177                         },
178                         expectedHashMap: map[string]string{
179                                 "testchart3/templates/multi.yaml-3": "e24cbbefac2c2f700880b8fd041838f2dd48bbc1e099e7c1d2485ae7feb3da0d",
180                                 "testchart3/templates/multi.yaml-4": "0bea01e65148584609ede5000c024241ba1c35b440b32ec0a4f7013015715bfe",
181                                 "testchart3/templates/multi.yaml-5": "6a5af22538c273b9d4a3156e3b6bb538c655041eae31e93db21a9e178f73ecf0",
182                         },
183                         expectedError: "",
184                 },
185         }
186
187         h := sha256.New()
188
189         for _, testCase := range testCases {
190                 t.Run(testCase.label, func(t *testing.T) {
191                         tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename")
192                         out, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles,
193                                 testCase.values)
194                         if err != nil {
195                                 if testCase.expectedError == "" {
196                                         t.Fatalf("Got an error %s", err)
197                                 }
198                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
199                                         t.Fatalf("Got unexpected error message %s", err)
200                                 }
201                         } else {
202                                 //Compute the hash of returned data and compare
203                                 for _, v := range out {
204                                         f := v.FilePath
205                                         data, err := ioutil.ReadFile(f)
206                                         if err != nil {
207                                                 t.Errorf("Unable to read file %s", v)
208                                         }
209                                         h.Write(data)
210                                         gotHash := fmt.Sprintf("%x", h.Sum(nil))
211                                         h.Reset()
212
213                                         //Find the right hash from expectedHashMap
214                                         expectedHash := ""
215                                         for k1, v1 := range testCase.expectedHashMap {
216                                                 if strings.Contains(f, k1) == true {
217                                                         expectedHash = v1
218                                                         break
219                                                 }
220                                         }
221                                         if gotHash != expectedHash {
222                                                 t.Fatalf("Got unexpected hash for %s: '%s'; expected: '%s'", f, gotHash, expectedHash)
223                                         }
224                                 }
225                         }
226                 })
227         }
228 }