add unittest for context.go
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
		
							parent
							
								
									85671c4dc1
								
							
						
					
					
						commit
						e2cb7fe176
					
				|  | @ -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) | ||||
| 
 | ||||
| } | ||||
		Loading…
	
		Reference in New Issue