From e2cb7fe17643b2a9f71fc4a7e3898a22122b48d9 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Wed, 27 Apr 2022 09:20:33 +0800 Subject: [PATCH 1/2] add unittest for context.go Signed-off-by: yxxhero --- pkg/helmexec/context_test.go | 98 ++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 pkg/helmexec/context_test.go diff --git a/pkg/helmexec/context_test.go b/pkg/helmexec/context_test.go new file mode 100644 index 00000000..60c5421d --- /dev/null +++ b/pkg/helmexec/context_test.go @@ -0,0 +1,98 @@ +package helmexec + +import ( + "os" + "testing" + + "github.com/Masterminds/semver/v3" + "github.com/stretchr/testify/require" +) + +// TestGetTillerlessArgs tests the GetTillerlessArgs function +func TestGetTillerlessArgs(t *testing.T) { + helmBinary := "helm" + + tests := []struct { + tillerless bool + helmMajorVersion string + tillerNamespace string + expected []string + }{ + { + tillerless: true, + helmMajorVersion: "2.0.0", + expected: []string{"tiller", "run", "--", helmBinary}, + }, + { + tillerless: true, + helmMajorVersion: "2.0.0", + tillerNamespace: "test-namespace", + expected: []string{"tiller", "run", "test-namespace", "--", helmBinary}, + }, + { + tillerless: false, + helmMajorVersion: "2.0.0", + expected: []string{}, + }, + { + tillerless: true, + helmMajorVersion: "3.0.0", + expected: []string{}, + }, + } + for _, test := range tests { + hc := &HelmContext{ + Tillerless: test.tillerless, + TillerNamespace: test.tillerNamespace, + } + sr, _ := semver.NewVersion(test.helmMajorVersion) + he := &execer{ + helmBinary: helmBinary, + version: *sr, + } + require.Equalf(t, test.expected, hc.GetTillerlessArgs(he), "expected result %s, received result %s", test.expected, hc.GetTillerlessArgs(he)) + + } +} + +// TestGetTillerlessEnv tests the getTillerlessEnv function +func TestGetTillerlessEnv(t *testing.T) { + kubeconfigEnv := "KUBECONFIG" + + tests := []struct { + tillerless bool + kubeconfig string + expected map[string]string + }{ + { + tillerless: true, + kubeconfig: "", + expected: map[string]string{"HELM_TILLER_SILENT": "true"}, + }, + { + tillerless: true, + kubeconfig: "abc", + expected: map[string]string{"HELM_TILLER_SILENT": "true", kubeconfigEnv: "/workdir/helmfile/pkg/helmexec/abc"}, + }, + { + tillerless: true, + kubeconfig: "/path/to/kubeconfig", + expected: map[string]string{"HELM_TILLER_SILENT": "true", kubeconfigEnv: "/path/to/kubeconfig"}, + }, + { + tillerless: false, + expected: map[string]string{}, + }, + } + for _, test := range tests { + hc := &HelmContext{ + Tillerless: test.tillerless, + } + os.Setenv(kubeconfigEnv, test.kubeconfig) + result := hc.getTillerlessEnv() + require.Equalf(t, test.expected, result, "expected result %s, received result %s", test.expected, result) + + } + defer os.Unsetenv(kubeconfigEnv) + +} From eeb2eee09959a3bfe0702897aa100794109223d6 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Wed, 27 Apr 2022 10:56:05 +0800 Subject: [PATCH 2/2] fix unittest issue for context.go Signed-off-by: yxxhero --- pkg/helmexec/context_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/helmexec/context_test.go b/pkg/helmexec/context_test.go index 60c5421d..d901def7 100644 --- a/pkg/helmexec/context_test.go +++ b/pkg/helmexec/context_test.go @@ -2,6 +2,7 @@ package helmexec import ( "os" + "path/filepath" "testing" "github.com/Masterminds/semver/v3" @@ -55,6 +56,11 @@ func TestGetTillerlessArgs(t *testing.T) { } } +func pwd() string { + pwd, _ := os.Getwd() + return pwd +} + // TestGetTillerlessEnv tests the getTillerlessEnv function func TestGetTillerlessEnv(t *testing.T) { kubeconfigEnv := "KUBECONFIG" @@ -72,7 +78,7 @@ func TestGetTillerlessEnv(t *testing.T) { { tillerless: true, kubeconfig: "abc", - expected: map[string]string{"HELM_TILLER_SILENT": "true", kubeconfigEnv: "/workdir/helmfile/pkg/helmexec/abc"}, + expected: map[string]string{"HELM_TILLER_SILENT": "true", kubeconfigEnv: filepath.Join(pwd(), "abc")}, }, { tillerless: true,