From e2cb7fe17643b2a9f71fc4a7e3898a22122b48d9 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Wed, 27 Apr 2022 09:20:33 +0800 Subject: [PATCH] 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) + +}