Skip to content

Commit

Permalink
refactor(helm test): Pass extra args with helm tests
Browse files Browse the repository at this point in the history
Pass these extra arguments in Helm test cases:
1. REST client getter to be used with Helm action config
2. Kube client options for be used with fake printer kube client

Related to helm#12722

Signed-off-by: Bhargav Ravuri <[email protected]>
  • Loading branch information
Bhargav-InfraCloud committed Oct 27, 2024
1 parent 717529a commit 65824c9
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmd/helm/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) {
})

testcmd := fmt.Sprintf("__complete %s ''", cmdName)
_, out, err := executeActionCommandC(storage, testcmd)
_, out, err := executeActionCommandC(storage, testcmd, nil, nil)
if err != nil {
t.Errorf("unexpected error, %s", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/helm/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCreateCmd(t *testing.T) {
defer testChdir(t, dir)()

// Run a create
if _, _, err := executeActionCommand("create " + cname); err != nil {
if _, _, err := executeActionCommand("create "+cname, nil, nil); err != nil {
t.Fatalf("Failed to run create: %s", err)
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func TestCreateStarterCmd(t *testing.T) {
}

// Run a create
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname)); err != nil {
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname), nil, nil); err != nil {
t.Errorf("Failed to run create: %s", err)
return
}
Expand Down Expand Up @@ -149,7 +149,8 @@ func TestCreateStarterAbsoluteCmd(t *testing.T) {
starterChartPath := filepath.Join(starterchart, "starterchart")

// Run a create
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil {
_, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname), nil, nil)
if err != nil {
t.Errorf("Failed to run create: %s", err)
return
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/helm/dependency_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestDependencyBuildCmd(t *testing.T) {
repoFile := filepath.Join(rootDir, "repositories.yaml")

cmd := fmt.Sprintf("dependency build '%s' --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, nil, nil)

// In the first pass, we basically want the same results as an update.
if err != nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestDependencyBuildCmd(t *testing.T) {
t.Fatal(err)
}

_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, nil, nil)
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestDependencyBuildCmd(t *testing.T) {
}

skipRefreshCmd := fmt.Sprintf("dependency build '%s' --skip-refresh --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
_, out, err = executeActionCommand(skipRefreshCmd)
_, out, err = executeActionCommand(skipRefreshCmd, nil, nil)

// In this pass, we check --skip-refresh option becomes effective.
if err != nil {
Expand All @@ -139,7 +139,7 @@ func TestDependencyBuildCmd(t *testing.T) {
dir("repositories.yaml"),
dir(),
dir())
_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, nil, nil)
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand All @@ -154,7 +154,7 @@ func TestDependencyBuildCmdWithHelmV2Hash(t *testing.T) {
chartName := "testdata/testcharts/issue-7233"

cmd := fmt.Sprintf("dependency build '%s'", chartName)
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, nil, nil)

// Want to make sure the build can verify Helm v2 hash
if err != nil {
Expand Down
18 changes: 12 additions & 6 deletions cmd/helm/dependency_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestDependencyUpdateCmd(t *testing.T) {

_, out, err := executeActionCommand(
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
)
nil, nil)
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand Down Expand Up @@ -110,7 +110,9 @@ func TestDependencyUpdateCmd(t *testing.T) {
t.Fatal(err)
}

_, out, err = executeActionCommand(fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, out, err = executeActionCommand(
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
nil, nil)
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand All @@ -136,7 +138,7 @@ func TestDependencyUpdateCmd(t *testing.T) {
dir("repositories.yaml"),
dir(),
dir())
_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, nil, nil)
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand Down Expand Up @@ -169,7 +171,9 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
}
createTestingChart(t, dir(), chartname, srv.URL())

_, output, err := executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, output, err := executeActionCommand(
fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
nil, nil)
if err != nil {
t.Logf("Output: %s", output)
t.Fatal(err)
Expand All @@ -178,7 +182,9 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
// Chart repo is down
srv.Stop()

_, output, err = executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, output, err = executeActionCommand(
fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
nil, nil)
if err == nil {
t.Logf("Output: %s", output)
t.Fatal("Expected error, got nil")
Expand Down Expand Up @@ -233,7 +239,7 @@ func TestDependencyUpdateCmd_WithRepoThatWasNotAdded(t *testing.T) {
_, out, err := executeActionCommand(
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname),
dir("repositories.yaml"), dir()),
)
nil, nil)

if err != nil {
t.Logf("Output: %s", out)
Expand Down
49 changes: 38 additions & 11 deletions cmd/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func runTestCmd(t *testing.T, tests []cmdTestCase) {
}
}
t.Logf("running cmd (attempt %d): %s", i+1, tt.cmd)
_, out, err := executeActionCommandC(storage, tt.cmd)
_, out, err := executeActionCommandC(
storage,
tt.cmd,
tt.restClientGetter,
tt.kubeClientOpts,
)
if tt.wantError && err == nil {
t.Errorf("expected error, got success with the following output:\n%s", out)
}
Expand All @@ -78,11 +83,22 @@ func storageFixture() *storage.Storage {
return storage.Init(driver.NewMemory())
}

func executeActionCommandC(store *storage.Storage, cmd string) (*cobra.Command, string, error) {
return executeActionCommandStdinC(store, nil, cmd)
func executeActionCommandC(
store *storage.Storage,
cmd string,
restClientGetter action.RESTClientGetter,
kubeClientOpts *kubefake.Options,
) (*cobra.Command, string, error) {
return executeActionCommandStdinC(store, nil, cmd, restClientGetter, kubeClientOpts)
}

func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string) (*cobra.Command, string, error) {
func executeActionCommandStdinC(
store *storage.Storage,
in *os.File,
cmd string,
restClientGetter action.RESTClientGetter,
kubeClientOpts *kubefake.Options,
) (*cobra.Command, string, error) {
args, err := shellwords.Parse(cmd)
if err != nil {
return nil, "", err
Expand All @@ -91,10 +107,14 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string)
buf := new(bytes.Buffer)

actionConfig := &action.Configuration{
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard},
Capabilities: chartutil.DefaultCapabilities,
Log: func(_ string, _ ...interface{}) {},
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{
Out: io.Discard,
Options: kubeClientOpts,
},
Capabilities: chartutil.DefaultCapabilities,
Log: func(_ string, _ ...interface{}) {},
RESTClientGetter: restClientGetter,
}

root, err := newRootCmd(actionConfig, buf, args)
Expand Down Expand Up @@ -135,10 +155,18 @@ type cmdTestCase struct {
// Number of repeats (in case a feature was previously flaky and the test checks
// it's now stably producing identical results). 0 means test is run exactly once.
repeat int
// REST client getter to be used with Helm action config
restClientGetter action.RESTClientGetter
// Kube client options for be used with fake printer kube client
kubeClientOpts *kubefake.Options
}

func executeActionCommand(cmd string) (*cobra.Command, string, error) {
return executeActionCommandC(storageFixture(), cmd)
func executeActionCommand(
cmd string,
restClientGetter action.RESTClientGetter,
kubeClintOpts *kubefake.Options,
) (*cobra.Command, string, error) {
return executeActionCommandC(storageFixture(), cmd, restClientGetter, kubeClintOpts)
}

func resetEnv() func() {
Expand Down Expand Up @@ -201,7 +229,6 @@ func TestPluginExitCode(t *testing.T) {
cmd.Stderr = stderr
err := cmd.Run()
exiterr, ok := err.(*exec.ExitError)

if !ok {
t.Fatalf("Unexpected error returned by os.Exit: %T", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestPackage(t *testing.T) {
cmd = append(cmd, fmt.Sprintf("--%s=%s", k, v))
}
}
_, _, err = executeActionCommand(strings.Join(cmd, " "))
_, _, err = executeActionCommand(strings.Join(cmd, " "), nil, nil)
if err != nil {
if tt.err && re.MatchString(err.Error()) {
return
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestSetAppVersion(t *testing.T) {
chartToPackage := "testdata/testcharts/alpine"
dir := t.TempDir()
cmd := fmt.Sprintf("package %s --destination=%s --app-version=%s", chartToPackage, dir, expectedAppVersion)
_, output, err := executeActionCommand(cmd)
_, output, err := executeActionCommand(cmd, nil, nil)
if err != nil {
t.Logf("Output: %s", output)
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestPullCmd(t *testing.T) {
t.Fatal(err)
}
}
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, nil, nil)
if err != nil {
if tt.wantError {
if tt.wantErrorMsg != "" && tt.wantErrorMsg == err.Error() {
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestPullWithCredentialsCmd(t *testing.T) {
t.Fatal(err)
}
}
_, _, err := executeActionCommand(cmd)
_, _, err := executeActionCommand(cmd, nil, nil)
if err != nil {
if tt.wantError {
if tt.wantErrorMsg != "" && tt.wantErrorMsg == err.Error() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/repo_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestRepoAddWithPasswordFromStdin(t *testing.T) {
const username = "username"
cmd := fmt.Sprintf("repo add %s %s --repository-config %s --repository-cache %s --username %s --password-stdin", testName, srv.URL(), repoFile, tmpdir, username)
var result string
_, result, err = executeActionCommandStdinC(store, in, cmd)
_, result, err = executeActionCommandStdinC(store, in, cmd, nil, nil)
if err != nil {
t.Errorf("unexpected error, got '%v'", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestRollbackWithLabels(t *testing.T) {
t.Fatal(err)
}
}
_, _, err := executeActionCommandC(storage, fmt.Sprintf("rollback %s 1", releaseName))
_, _, err := executeActionCommandC(storage, fmt.Sprintf("rollback %s 1", releaseName), nil, nil)
if err != nil {
t.Errorf("unexpected error, got '%v'", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestRootCmd(t *testing.T) {
os.Setenv(k, v)
}

if _, _, err := executeActionCommand(tt.args); err != nil {
if _, _, err := executeActionCommand(tt.args, nil, nil); err != nil {
t.Fatalf("unexpected error: %s", err)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestRootCmd(t *testing.T) {
}

func TestUnknownSubCmd(t *testing.T) {
_, _, err := executeActionCommand("foobar")
_, _, err := executeActionCommand("foobar", nil, nil)

if err == nil || err.Error() != `unknown command "foobar" for "helm"` {
t.Errorf("Expect unknown command error, got %q", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/helm/search_hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestSearchHubCmd(t *testing.T) {

testcmd := "search hub --endpoint " + ts.URL + " maria"
storage := storageFixture()
_, out, err := executeActionCommandC(storage, testcmd)
_, out, err := executeActionCommandC(storage, testcmd, nil, nil)
if err != nil {
t.Errorf("unexpected error, %s", err)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestSearchHubListRepoCmd(t *testing.T) {

testcmd := "search hub --list-repo-url --endpoint " + ts.URL + " maria"
storage := storageFixture()
_, out, err := executeActionCommandC(storage, testcmd)
_, out, err := executeActionCommandC(storage, testcmd, nil, nil)
if err != nil {
t.Errorf("unexpected error, %s", err)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestSearchHubCmd_FailOnNoResponseTests(t *testing.T) {

storage := storageFixture()

_, out, err := executeActionCommandC(storage, tt.cmd)
_, out, err := executeActionCommandC(storage, tt.cmd, nil, nil)
if tt.wantErr {
if err == nil {
t.Errorf("expected error due to no record in response, got nil")
Expand Down
3 changes: 1 addition & 2 deletions cmd/helm/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func TestShowPreReleaseChart(t *testing.T) {
filepath.Join(outdir, "repositories.yaml"),
outdir,
)
//_, out, err := executeActionCommand(cmd)
_, _, err := executeActionCommand(cmd)
_, _, err := executeActionCommand(cmd, nil, nil)
if err != nil {
if tt.fail {
if !strings.Contains(err.Error(), tt.expectedErr) {
Expand Down
Loading

0 comments on commit 65824c9

Please sign in to comment.