Migrate to use Helm v3 libraries
[multicloud/k8s.git] / src / k8splugin / internal / helm / helm_test.go
index a5bcd9c..29d446f 100644 (file)
@@ -1,7 +1,6 @@
-// +build unit
-
 /*
  * Copyright 2018 Intel Corporation, Inc
+ * Copyright 2020,2021 Samsung Electronics, Modifications
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +20,7 @@ package helm
 import (
        "crypto/sha256"
        "fmt"
+       "gopkg.in/yaml.v2"
        "io/ioutil"
        "path/filepath"
        "strings"
@@ -29,7 +29,7 @@ import (
 
 func TestProcessValues(t *testing.T) {
 
-       chartDir := "../../mock_files/mock_charts/testchart1"
+       chartDir := "../../mock_files/mock_charts/testchart2"
        profileDir := "../../mock_files/mock_profiles/profile1"
 
        testCases := []struct {
@@ -46,7 +46,7 @@ func TestProcessValues(t *testing.T) {
                                filepath.Join(profileDir, "override_values.yaml"),
                        },
                        //Hash of a combined values.yaml file that is expected
-                       expectedHash:  "c18a70f426933de3c051c996dc34fd537d0131b2d13a2112a2ecff674db6c2f9",
+                       expectedHash:  "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
                        expectedError: "",
                },
                {
@@ -59,7 +59,7 @@ func TestProcessValues(t *testing.T) {
                                "service.externalPort=82",
                        },
                        //Hash of a combined values.yaml file that is expected
-                       expectedHash:  "028a3521fc9f8777ea7e67a6de0c51f2c875b88ca91734999657f0ca924ddb7a",
+                       expectedHash:  "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
                        expectedError: "",
                },
                {
@@ -74,9 +74,18 @@ func TestProcessValues(t *testing.T) {
                                "service.externalPort=82",
                        },
                        //Hash of a combined values.yaml file that is expected
-                       expectedHash:  "516fab4ab7b76ba2ff35a97c2a79b74302543f532857b945f2fe25e717e755be",
+                       expectedHash:  "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
                        expectedError: "",
                },
+               {
+                       label: "Process complex Pair Override",
+                       values: []string{
+                               "name={a,b,c}",
+                               "servers[0].port=80",
+                       },
+                       expectedError: "",
+                       expectedHash:  "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+               },
        }
 
        h := sha256.New()
@@ -94,11 +103,11 @@ func TestProcessValues(t *testing.T) {
                                }
                        } else {
                                //Compute the hash of returned data and compare
-                               h.Write(out)
                                gotHash := fmt.Sprintf("%x", h.Sum(nil))
                                h.Reset()
                                if gotHash != testCase.expectedHash {
-                                       t.Fatalf("Got unexpected values.yaml %s", out)
+                                       mout, _ := yaml.Marshal(&out)
+                                       t.Fatalf("Got unexpected hash '%s' of values.yaml:\n%v", gotHash, string(mout))
                                }
                        }
                })
@@ -107,7 +116,7 @@ func TestProcessValues(t *testing.T) {
 
 func TestGenerateKubernetesArtifacts(t *testing.T) {
 
-       chartDir := "../../mock_files/mock_charts/testchart1"
+       chartDir := "../../mock_files/mock_charts/testchart2"
        profileDir := "../../mock_files/mock_profiles/profile1"
 
        testCases := []struct {
@@ -125,9 +134,9 @@ func TestGenerateKubernetesArtifacts(t *testing.T) {
                        values:     []string{},
                        //sha256 hash of the evaluated templates in each chart
                        expectedHashMap: map[string]string{
-                               "testchart1/templates/service.yaml": "bbd7257d1f6ab958680e642a8fbbbea2002ebbaa9276fb51fbd71b4b66a772cc",
-                               "subcharta/templates/service.yaml":  "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0",
-                               "subchartb/templates/service.yaml":  "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a",
+                               "manifest-0": "fcc1083ace82b633e3a0a687d50f532c07e1212b7a42b2c178b65e5768fffcfe",
+                               "manifest-2": "eefeac6ff5430a16a32ae3974857cbe5ff516a1a68566e5edcddd410d60397c0",
+                               "manifest-1": "b88aa963ee3afb9676e9930519d7caa103df1251da48a9351ab4ac0c5730d2af",
                        },
                        expectedError: "",
                },
@@ -142,12 +151,50 @@ func TestGenerateKubernetesArtifacts(t *testing.T) {
                        },
                        //sha256 hash of the evaluated templates in each chart
                        expectedHashMap: map[string]string{
-                               "testchart1/templates/service.yaml": "4c5aa5d38b763fe4730fc31a759c40566a99a9c51f5e0fc7f93473c9affc2ca8",
-                               "subcharta/templates/service.yaml":  "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0",
-                               "subchartb/templates/service.yaml":  "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a",
+                               "manifest-0": "fcc1083ace82b633e3a0a687d50f532c07e1212b7a42b2c178b65e5768fffcfe",
+                               "manifest-2": "03ae530e49071d005be78f581b7c06c59119f91f572b28c0c0c06ced8e37bf6e",
+                               "manifest-1": "b88aa963ee3afb9676e9930519d7caa103df1251da48a9351ab4ac0c5730d2af",
+                       },
+                       expectedError: "",
+               },
+               {
+                       label:      "Generate artifacts from multi-template and empty files v1",
+                       chartPath:  "../../mock_files/mock_charts/testchart3",
+                       valueFiles: []string{},
+                       values: []string{
+                               "goingEmpty=false",
+                       },
+                       expectedHashMap: map[string]string{
+                               "manifest-0": "666e8d114981a4b5d13fb799be060aa57e0e48904bba4a410f87a2e827a57ddb",
+                               "manifest-2": "6a5af22538c273b9d4a3156e3b6bb538c655041eae31e93db21a9e178f73ecf0",
                        },
                        expectedError: "",
                },
+               {
+                       label:      "Generate artifacts from multi-template and empty files v2",
+                       chartPath:  "../../mock_files/mock_charts/testchart3",
+                       valueFiles: []string{},
+                       values: []string{
+                               "goingEmpty=true",
+                       },
+                       expectedHashMap: map[string]string{
+                               "manifest-0": "666e8d114981a4b5d13fb799be060aa57e0e48904bba4a410f87a2e827a57ddb",
+                               "manifest-1": "8613e7e7cc0186516b13be37ec7fc321ff89e3abaed0a841773a4eba2d77ce2a",
+                               "manifest-2": "3543ae9563fe62ce4a7446d72e1cd23140d8cc5495f0221430d70e94845c1408",
+                       },
+                       expectedError: "",
+               },
+               {
+                       label:         "Test simple v3 helm charts support",
+                       chartPath:     "../../mock_files/mock_charts/mockv3",
+                       valueFiles:    []string{},
+                       values:        []string{},
+                       expectedError: "",
+                       expectedHashMap: map[string]string{
+                               "manifest-0": "94975ff704b9cc00a7988fe7fc865665495655ec2584d3e9de2f7e5294c7eb0d",
+                               "dummy-test": "b50bb5f818fe0be332f09401104ae9cea59442e2dabe1a16b4ce21b753177a80",
+                       },
+               },
        }
 
        h := sha256.New()
@@ -155,7 +202,7 @@ func TestGenerateKubernetesArtifacts(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename")
-                       out, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles,
+                       out, hooks, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles,
                                testCase.values)
                        if err != nil {
                                if testCase.expectedError == "" {
@@ -164,30 +211,54 @@ func TestGenerateKubernetesArtifacts(t *testing.T) {
                                if strings.Contains(err.Error(), testCase.expectedError) == false {
                                        t.Fatalf("Got unexpected error message %s", err)
                                }
+                       } else if len(testCase.expectedHashMap) != len(out)+len(hooks) {
+                               t.Fatalf("Mismatch of expected files (%d) and returned resources (%d)",
+                                       len(testCase.expectedHashMap), len(out)+len(hooks))
                        } else {
                                //Compute the hash of returned data and compare
                                for _, v := range out {
-                                       for _, f := range v {
-                                               data, err := ioutil.ReadFile(f)
-                                               if err != nil {
-                                                       t.Errorf("Unable to read file %s", v)
-                                               }
-                                               h.Write(data)
-                                               gotHash := fmt.Sprintf("%x", h.Sum(nil))
-                                               h.Reset()
+                                       f := v.FilePath
+                                       data, err := ioutil.ReadFile(f)
+                                       if err != nil {
+                                               t.Fatalf("Unable to read file %s", v)
+                                       }
+                                       h.Write(data)
+                                       gotHash := fmt.Sprintf("%x", h.Sum(nil))
+                                       h.Reset()
 
-                                               //Find the right hash from expectedHashMap
-                                               expectedHash := ""
-                                               for k1, v1 := range testCase.expectedHashMap {
-                                                       if strings.Contains(f, k1) == true {
-                                                               expectedHash = v1
-                                                               break
-                                                       }
+                                       //Find the right hash from expectedHashMap
+                                       expectedHash := ""
+                                       for k1, v1 := range testCase.expectedHashMap {
+                                               if strings.Contains(f, k1) == true {
+                                                       expectedHash = v1
+                                                       break
                                                }
-                                               if gotHash != expectedHash {
-                                                       t.Fatalf("Got unexpected hash for %s", f)
+                                       }
+                                       if gotHash != expectedHash {
+                                               t.Fatalf("Got unexpected hash for %s: '%s'; expected: '%s'", f, gotHash, expectedHash)
+                                       }
+                               }
+                               for _, v := range hooks {
+                                       f := v.KRT.FilePath
+                                       data, err := ioutil.ReadFile(f)
+                                       if err != nil {
+                                               t.Fatalf("Unable to read file %+v", v)
+                                       }
+                                       h.Write(data)
+                                       gotHash := fmt.Sprintf("%x", h.Sum(nil))
+                                       h.Reset()
+
+                                       //Find the right hash from expectedHashMap
+                                       expectedHash := ""
+                                       for k1, v1 := range testCase.expectedHashMap {
+                                               if strings.Contains(f, k1) == true {
+                                                       expectedHash = v1
+                                                       break
                                                }
                                        }
+                                       if gotHash != expectedHash {
+                                               t.Fatalf("Got unexpected hash for %s: '%s'; expected: '%s'", f, gotHash, expectedHash)
+                                       }
                                }
                        }
                })