From 8fbc3a769b6709b5321b6445798a6860aa62f359 Mon Sep 17 00:00:00 2001 From: Akash Shrivastava Date: Thu, 11 Jan 2024 21:31:57 +0530 Subject: [PATCH] feat: [ISSUE-1401]: Added test cases and fixes Signed-off-by: Akash Shrivastava --- pkg/collect/helm.go | 22 +++++++++++++------ .../support-bundle/helm_collector_e2e_test.go | 3 ++- test/e2e/support-bundle/spec/helm.yaml | 4 +++- .../support-bundle/testdata/helm-values.yaml | 4 ++++ 4 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 test/e2e/support-bundle/testdata/helm-values.yaml diff --git a/pkg/collect/helm.go b/pkg/collect/helm.go index 8a9f13142..b9a463d30 100644 --- a/pkg/collect/helm.go +++ b/pkg/collect/helm.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "strconv" + "strings" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -99,7 +100,7 @@ func helmReleaseHistoryCollector(releaseName string, namespace string, collectVa if err != nil { return nil, []error{err} } - versionInfo, err := getVersionInfo(actionConfig, r.Name, collectValues) + versionInfo, err := getVersionInfo(actionConfig, r.Name, r.Namespace, collectValues) results = append(results, ReleaseInfo{ ReleaseName: r.Name, Chart: r.Chart.Metadata.Name, @@ -121,7 +122,7 @@ func helmReleaseHistoryCollector(releaseName string, namespace string, collectVa } for _, r := range releases { - versionInfo, err := getVersionInfo(actionConfig, r.Name, collectValues) + versionInfo, err := getVersionInfo(actionConfig, r.Name, r.Namespace, collectValues) if err != nil { error_list = append(error_list, err) } @@ -140,7 +141,7 @@ func helmReleaseHistoryCollector(releaseName string, namespace string, collectVa return results, nil } -func getVersionInfo(actionConfig *action.Configuration, releaseName string, collectValues bool) ([]VersionInfo, error) { +func getVersionInfo(actionConfig *action.Configuration, releaseName, namespace string, collectValues bool) ([]VersionInfo, error) { versionCollect := []VersionInfo{} error_list := []error{} @@ -153,7 +154,7 @@ func getVersionInfo(actionConfig *action.Configuration, releaseName string, coll for _, release := range history { values := map[string]interface{}{} if collectValues { - values, err = getHelmValues(actionConfig, releaseName, release.Version) + values, err = getHelmValues(releaseName, namespace, release.Version) if err != nil { error_list = append(error_list, err) } @@ -168,8 +169,11 @@ func getVersionInfo(actionConfig *action.Configuration, releaseName string, coll }) } if len(error_list) > 0 { - errs, _ := json.MarshalIndent(error_list, "", " ") - return nil, errors.New(string(errs)) + errs := []string{} + for _, e := range error_list { + errs = append(errs, e.Error()) + } + return nil, errors.New(strings.Join(errs, "\n")) } return versionCollect, nil } @@ -184,7 +188,11 @@ func helmReleaseInfoByNamespaces(releaseInfo []ReleaseInfo) map[string][]Release return releaseInfoByNamespace } -func getHelmValues(actionConfig *action.Configuration, releaseName string, revision int) (map[string]interface{}, error) { +func getHelmValues(releaseName, namespace string, revision int) (map[string]interface{}, error) { + actionConfig := new(action.Configuration) + if err := actionConfig.Init(nil, namespace, "", klog.V(2).Infof); err != nil { + return nil, err + } getAction := action.NewGetValues(actionConfig) getAction.AllValues = true getAction.Version = revision diff --git a/test/e2e/support-bundle/helm_collector_e2e_test.go b/test/e2e/support-bundle/helm_collector_e2e_test.go index 0475a574b..cdf67af9e 100644 --- a/test/e2e/support-bundle/helm_collector_e2e_test.go +++ b/test/e2e/support-bundle/helm_collector_e2e_test.go @@ -27,7 +27,7 @@ func Test_HelmCollector(t *testing.T) { Setup(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { cluster := getClusterFromContext(t, ctx, ClusterName) manager := helm.New(cluster.GetKubeconfig()) - manager.RunInstall(helm.WithName(releaseName), helm.WithNamespace(c.Namespace()), helm.WithChart(filepath.Join(curDir, "testdata/charts/nginx-15.2.0.tgz")), helm.WithWait(), helm.WithTimeout("1m")) + manager.RunInstall(helm.WithName(releaseName), helm.WithNamespace(c.Namespace()), helm.WithChart(filepath.Join(curDir, "testdata/charts/nginx-15.2.0.tgz")), helm.WithArgs("-f "+filepath.Join(curDir, "testdata/helm-values.yaml")), helm.WithWait(), helm.WithTimeout("1m")) //ignore error to allow test to speed up, helm collector will catch the pending or deployed helm release status return ctx }). @@ -66,6 +66,7 @@ func Test_HelmCollector(t *testing.T) { assert.Equal(t, 1, len(results)) assert.Equal(t, releaseName, results[0].ReleaseName) assert.Equal(t, "nginx", results[0].Chart) + assert.Equal(t, []interface{}([]interface{}{map[string]interface{}{"name": "TEST_ENV_VAR", "value": "test-value"}}), results[0].VersionInfo[0].Values["extraEnvVars"]) return ctx }). Teardown(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { diff --git a/test/e2e/support-bundle/spec/helm.yaml b/test/e2e/support-bundle/spec/helm.yaml index 3f22529a3..c272fd4e5 100644 --- a/test/e2e/support-bundle/spec/helm.yaml +++ b/test/e2e/support-bundle/spec/helm.yaml @@ -1,7 +1,9 @@ +--- apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: default spec: collectors: - - helm: {} + - helm: + collectValues: true diff --git a/test/e2e/support-bundle/testdata/helm-values.yaml b/test/e2e/support-bundle/testdata/helm-values.yaml new file mode 100644 index 000000000..c5c62d0d1 --- /dev/null +++ b/test/e2e/support-bundle/testdata/helm-values.yaml @@ -0,0 +1,4 @@ +--- +extraEnvVars: + - name: TEST_ENV_VAR + value: "test-value"