Skip to content

Commit

Permalink
feat: [ISSUE-1401]: Added test cases and fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Shrivastava <[email protected]>
  • Loading branch information
avaakash committed Jan 11, 2024
1 parent b9e8112 commit cbed2c2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
22 changes: 15 additions & 7 deletions pkg/collect/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand All @@ -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{}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/support-bundle/helm_collector_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}).
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/support-bundle/spec/helm.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
apiVersion: troubleshoot.sh/v1beta2
kind: SupportBundle
metadata:
name: default
spec:
collectors:
- helm: {}
- helm:
collectValues: true
4 changes: 4 additions & 0 deletions test/e2e/support-bundle/testdata/helm-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
extraEnvVars:
- name: TEST_ENV_VAR
value: "test-value"

0 comments on commit cbed2c2

Please sign in to comment.