Skip to content

Commit

Permalink
fix(test): Use t.Fatal instead of os.Exit in tests (part 1) (argoproj…
Browse files Browse the repository at this point in the history
…#21003) (argoproj#22114)

Signed-off-by: Andrii Korotkov <[email protected]>
  • Loading branch information
andrii-korotkov-verkada authored Mar 3, 2025
1 parent 561cbef commit 416b7d0
Show file tree
Hide file tree
Showing 28 changed files with 356 additions and 399 deletions.
4 changes: 2 additions & 2 deletions test/e2e/app_autosync_ns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestNSAutoSyncSelfHealDisabled(t *testing.T) {
// app should not be auto-synced if k8s change detected
When().
And(func() {
errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), metav1.PatchOptions{}))
}).
Then().
Expand All @@ -64,7 +64,7 @@ func TestNSAutoSyncSelfHealEnabled(t *testing.T) {
When().
// app should be auto-synced once k8s change detected
And(func() {
errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), metav1.PatchOptions{}))
}).
Refresh(RefreshTypeNormal).
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app_autosync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAutoSyncSelfHealDisabled(t *testing.T) {
// app should not be auto-synced if k8s change detected
When().
And(func() {
errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), metav1.PatchOptions{}))
}).
Refresh(RefreshTypeNormal).
Expand All @@ -59,7 +59,7 @@ func TestAutoSyncSelfHealEnabled(t *testing.T) {
When().
// app should be auto-synced once k8s change detected
And(func() {
errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), metav1.PatchOptions{}))
}).
Refresh(RefreshTypeNormal).
Expand Down
75 changes: 37 additions & 38 deletions test/e2e/app_management_ns_test.go

Large diffs are not rendered by default.

97 changes: 48 additions & 49 deletions test/e2e/app_management_test.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions test/e2e/app_namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
. "github.com/argoproj/argo-cd/v3/test/e2e/fixture"
. "github.com/argoproj/argo-cd/v3/test/e2e/fixture/app"
. "github.com/argoproj/argo-cd/v3/util/argo"
. "github.com/argoproj/argo-cd/v3/util/errors"
"github.com/argoproj/argo-cd/v3/util/errors"
)

func TestAppCreationInOtherNamespace(t *testing.T) {
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestAppCreationInOtherNamespace(t *testing.T) {
When().
// ensure that update replaces spec and merge labels and annotations
And(func() {
FailOnErr(AppClientset.ArgoprojV1alpha1().Applications(AppNamespace()).Patch(context.Background(),
errors.NewHandler(t).FailOnErr(AppClientset.ArgoprojV1alpha1().Applications(AppNamespace()).Patch(context.Background(),
ctx.AppName(), types.MergePatchType, []byte(`{"metadata": {"labels": { "test": "label" }, "annotations": { "test": "annotation" }}}`), metav1.PatchOptions{}))
}).
CreateApp("--upsert").
Expand All @@ -77,7 +77,7 @@ func TestForbiddenNamespace(t *testing.T) {
func TestDeletingNamespacedAppStuckInSync(t *testing.T) {
ctx := Given(t)
ctx.And(func() {
CheckError(SetResourceOverrides(map[string]ResourceOverride{
errors.CheckError(SetResourceOverrides(map[string]ResourceOverride{
"ConfigMap": {
HealthLua: `return { status = obj.annotations and obj.annotations['health'] or 'Progressing' }`,
},
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/applicationset_git_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
"github.com/argoproj/argo-cd/v3/util/errors"

"github.com/argoproj/argo-cd/v3/pkg/apis/application"
. "github.com/argoproj/argo-cd/v3/test/e2e/fixture/applicationsets"
Expand Down Expand Up @@ -1457,7 +1458,7 @@ func TestGitGeneratorPrivateRepoWithTemplatedProjectAndProjectScopedRepo(t *test
Addr: "localhost:6379",
})
all := r.FlushAll(context.Background())
utils.CheckError(all.Err())
errors.CheckError(all.Err())

generateExpectedApp := func(name string) v1alpha1.Application {
return v1alpha1.Application{
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
accountFixture "github.com/argoproj/argo-cd/v3/test/e2e/fixture/account"
"github.com/argoproj/argo-cd/v3/test/e2e/fixture/app"
clusterFixture "github.com/argoproj/argo-cd/v3/test/e2e/fixture/cluster"
. "github.com/argoproj/argo-cd/v3/util/errors"
"github.com/argoproj/argo-cd/v3/util/errors"
)

func TestClusterList(t *testing.T) {
Expand All @@ -23,7 +23,7 @@ func TestClusterList(t *testing.T) {

last := ""
expected := fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE PROJECT
https://kubernetes.default.svc in-cluster %v Successful `, fixture.GetVersions().ServerVersion)
https://kubernetes.default.svc in-cluster %v Successful `, fixture.GetVersions(t).ServerVersion)

clusterFixture.
Given(t).
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestClusterAdd(t *testing.T) {
Then().
AndCLIOutput(func(output string, _ error) {
assert.Equal(t, fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE PROJECT
https://kubernetes.default.svc test-cluster-add %v Successful %s`, fixture.GetVersions().ServerVersion, fixture.ProjectName), output)
https://kubernetes.default.svc test-cluster-add %v Successful %s`, fixture.GetVersions(t).ServerVersion, fixture.ProjectName), output)
})
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestClusterAddAllowed(t *testing.T) {
Then().
AndCLIOutput(func(output string, _ error) {
assert.Equal(t, fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE PROJECT
https://kubernetes.default.svc test-cluster-add-allowed %v Successful argo-project`, fixture.GetVersions().ServerVersion), output)
https://kubernetes.default.svc test-cluster-add-allowed %v Successful argo-project`, fixture.GetVersions(t).ServerVersion), output)
})
}

Expand Down Expand Up @@ -178,11 +178,11 @@ func TestClusterGet(t *testing.T) {
fixture.SkipIfAlreadyRun(t)
fixture.EnsureCleanState(t)
defer fixture.RecordTestRun(t)
output := FailOnErr(fixture.RunCli("cluster", "get", "https://kubernetes.default.svc")).(string)
output := errors.NewHandler(t).FailOnErr(fixture.RunCli("cluster", "get", "https://kubernetes.default.svc")).(string)

assert.Contains(t, output, "name: in-cluster")
assert.Contains(t, output, "server: https://kubernetes.default.svc")
assert.Contains(t, output, fmt.Sprintf(`serverVersion: "%v"`, fixture.GetVersions().ServerVersion))
assert.Contains(t, output, fmt.Sprintf(`serverVersion: "%v"`, fixture.GetVersions(t).ServerVersion))
assert.Contains(t, output, `config:
tlsClientConfig:
insecure: false`)
Expand Down
16 changes: 8 additions & 8 deletions test/e2e/custom_tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
. "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
. "github.com/argoproj/argo-cd/v3/test/e2e/fixture/app"
. "github.com/argoproj/argo-cd/v3/util/errors"
"github.com/argoproj/argo-cd/v3/util/errors"
)

// make sure we can echo back the Git creds
Expand Down Expand Up @@ -185,13 +185,13 @@ func TestCustomToolWithEnv(t *testing.T) {
assert.Equal(t, "bar", output)
}).
And(func(_ *Application) {
expectedKubeVersion := fixture.GetVersions().ServerVersion.Format("%s.%s")
expectedKubeVersion := fixture.GetVersions(t).ServerVersion.Format("%s.%s")
output, err := fixture.Run("", "kubectl", "-n", fixture.DeploymentNamespace(), "get", "cm", ctx.AppName(), "-o", "jsonpath={.metadata.annotations.KubeVersion}")
require.NoError(t, err)
assert.Equal(t, expectedKubeVersion, output)
}).
And(func(_ *Application) {
expectedAPIVersion := fixture.GetApiResources()
expectedAPIVersion := fixture.GetApiResources(t)
expectedAPIVersionSlice := strings.Split(expectedAPIVersion, ",")
sort.Strings(expectedAPIVersionSlice)

Expand Down Expand Up @@ -226,10 +226,10 @@ func TestCustomToolSyncAndDiffLocal(t *testing.T) {
Expect(SyncStatusIs(SyncStatusCodeSynced)).
Expect(HealthIs(health.HealthStatusHealthy)).
And(func(_ *Application) {
FailOnErr(fixture.RunCli("app", "sync", ctx.AppName(), "--local", appPath, "--local-repo-root", testdataPath))
errors.NewHandler(t).FailOnErr(fixture.RunCli("app", "sync", ctx.AppName(), "--local", appPath, "--local-repo-root", testdataPath))
}).
And(func(_ *Application) {
FailOnErr(fixture.RunCli("app", "diff", ctx.AppName(), "--local", appPath, "--local-repo-root", testdataPath))
errors.NewHandler(t).FailOnErr(fixture.RunCli("app", "diff", ctx.AppName(), "--local", appPath, "--local-repo-root", testdataPath))
})
}

Expand All @@ -244,7 +244,7 @@ func startCMPServer(t *testing.T, configFile string) {
err := os.Mkdir(pluginSockFilePath, 0o700)
require.NoError(t, err)
}
FailOnErr(fixture.RunWithStdin("", "", "../../dist/argocd", "--config-dir-path", configFile))
errors.NewHandler(t).FailOnErr(fixture.RunWithStdin("", "", "../../dist/argocd", "--config-dir-path", configFile))
}

// Discover by fileName
Expand Down Expand Up @@ -329,13 +329,13 @@ func TestCMPDiscoverWithFindCommandWithEnv(t *testing.T) {
assert.Equal(t, "baz", output)
}).
And(func(_ *Application) {
expectedKubeVersion := fixture.GetVersions().ServerVersion.Format("%s.%s")
expectedKubeVersion := fixture.GetVersions(t).ServerVersion.Format("%s.%s")
output, err := fixture.Run("", "kubectl", "-n", fixture.DeploymentNamespace(), "get", "cm", ctx.AppName(), "-o", "jsonpath={.metadata.annotations.KubeVersion}")
require.NoError(t, err)
assert.Equal(t, expectedKubeVersion, output)
}).
And(func(_ *Application) {
expectedAPIVersion := fixture.GetApiResources()
expectedAPIVersion := fixture.GetApiResources(t)
expectedAPIVersionSlice := strings.Split(expectedAPIVersion, ",")
sort.Strings(expectedAPIVersionSlice)

Expand Down
14 changes: 7 additions & 7 deletions test/e2e/fixture/app/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (a *Actions) DoNotIgnoreErrors() *Actions {

func (a *Actions) PatchFile(file string, jsonPath string) *Actions {
a.context.t.Helper()
fixture.Patch(a.context.path+"/"+file, jsonPath)
fixture.Patch(a.context.t, a.context.path+"/"+file, jsonPath)
return a
}

func (a *Actions) DeleteFile(file string) *Actions {
a.context.t.Helper()
fixture.Delete(a.context.path + "/" + file)
fixture.Delete(a.context.t, a.context.path+"/"+file)
return a
}

Expand All @@ -60,31 +60,31 @@ func (a *Actions) WriteFile(fileName, fileContents string) *Actions {

func (a *Actions) AddFile(fileName, fileContents string) *Actions {
a.context.t.Helper()
fixture.AddFile(a.context.path+"/"+fileName, fileContents)
fixture.AddFile(a.context.t, a.context.path+"/"+fileName, fileContents)
return a
}

func (a *Actions) AddSignedFile(fileName, fileContents string) *Actions {
a.context.t.Helper()
fixture.AddSignedFile(a.context.path+"/"+fileName, fileContents)
fixture.AddSignedFile(a.context.t, a.context.path+"/"+fileName, fileContents)
return a
}

func (a *Actions) AddSignedTag(name string) *Actions {
a.context.t.Helper()
fixture.AddSignedTag(name)
fixture.AddSignedTag(a.context.t, name)
return a
}

func (a *Actions) AddTag(name string) *Actions {
a.context.t.Helper()
fixture.AddTag(name)
fixture.AddTag(a.context.t, name)
return a
}

func (a *Actions) RemoveSubmodule() *Actions {
a.context.t.Helper()
fixture.RemoveSubmodule()
fixture.RemoveSubmodule(a.context.t)
return a
}

Expand Down
44 changes: 22 additions & 22 deletions test/e2e/fixture/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,104 +116,104 @@ func (c *Context) SetAppNamespace(namespace string) *Context {
}

func (c *Context) GPGPublicKeyAdded() *Context {
gpgkeys.AddGPGPublicKey()
gpgkeys.AddGPGPublicKey(c.t)
return c
}

func (c *Context) GPGPublicKeyRemoved() *Context {
gpgkeys.DeleteGPGPublicKey()
gpgkeys.DeleteGPGPublicKey(c.t)
return c
}

func (c *Context) CustomCACertAdded() *Context {
certs.AddCustomCACert()
certs.AddCustomCACert(c.t)
return c
}

func (c *Context) CustomSSHKnownHostsAdded() *Context {
certs.AddCustomSSHKnownHostsKeys()
certs.AddCustomSSHKnownHostsKeys(c.t)
return c
}

func (c *Context) HTTPSRepoURLAdded(withCreds bool) *Context {
repos.AddHTTPSRepo(false, withCreds, "", fixture.RepoURLTypeHTTPS)
repos.AddHTTPSRepo(c.t, false, withCreds, "", fixture.RepoURLTypeHTTPS)
return c
}

func (c *Context) HTTPSInsecureRepoURLAdded(withCreds bool) *Context {
repos.AddHTTPSRepo(true, withCreds, "", fixture.RepoURLTypeHTTPS)
repos.AddHTTPSRepo(c.t, true, withCreds, "", fixture.RepoURLTypeHTTPS)
return c
}

func (c *Context) HTTPSInsecureRepoURLWithClientCertAdded() *Context {
repos.AddHTTPSRepoClientCert(true)
repos.AddHTTPSRepoClientCert(c.t, true)
return c
}

func (c *Context) HTTPSRepoURLWithClientCertAdded() *Context {
repos.AddHTTPSRepoClientCert(false)
repos.AddHTTPSRepoClientCert(c.t, false)
return c
}

func (c *Context) SubmoduleHTTPSRepoURLAdded(withCreds bool) *Context {
fixture.CreateSubmoduleRepos("https")
repos.AddHTTPSRepo(false, withCreds, "", fixture.RepoURLTypeHTTPSSubmoduleParent)
fixture.CreateSubmoduleRepos(c.t, "https")
repos.AddHTTPSRepo(c.t, false, withCreds, "", fixture.RepoURLTypeHTTPSSubmoduleParent)
return c
}

func (c *Context) SSHRepoURLAdded(withCreds bool) *Context {
repos.AddSSHRepo(false, withCreds, fixture.RepoURLTypeSSH)
repos.AddSSHRepo(c.t, false, withCreds, fixture.RepoURLTypeSSH)
return c
}

func (c *Context) SSHInsecureRepoURLAdded(withCreds bool) *Context {
repos.AddSSHRepo(true, withCreds, fixture.RepoURLTypeSSH)
repos.AddSSHRepo(c.t, true, withCreds, fixture.RepoURLTypeSSH)
return c
}

func (c *Context) SubmoduleSSHRepoURLAdded(withCreds bool) *Context {
fixture.CreateSubmoduleRepos("ssh")
repos.AddSSHRepo(false, withCreds, fixture.RepoURLTypeSSHSubmoduleParent)
fixture.CreateSubmoduleRepos(c.t, "ssh")
repos.AddSSHRepo(c.t, false, withCreds, fixture.RepoURLTypeSSHSubmoduleParent)
return c
}

func (c *Context) HelmRepoAdded(name string) *Context {
repos.AddHelmRepo(name)
repos.AddHelmRepo(c.t, name)
return c
}

func (c *Context) HelmOCIRepoAdded(name string) *Context {
repos.AddHelmOCIRepo(name)
repos.AddHelmOCIRepo(c.t, name)
return c
}

func (c *Context) PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) *Context {
repos.PushChartToOCIRegistry(chartPathName, chartName, chartVersion)
repos.PushChartToOCIRegistry(c.t, chartPathName, chartName, chartVersion)
return c
}

func (c *Context) HTTPSCredentialsUserPassAdded() *Context {
repos.AddHTTPSCredentialsUserPass()
repos.AddHTTPSCredentialsUserPass(c.t)
return c
}

func (c *Context) HelmHTTPSCredentialsUserPassAdded() *Context {
repos.AddHelmHTTPSCredentialsTLSClientCert()
repos.AddHelmHTTPSCredentialsTLSClientCert(c.t)
return c
}

func (c *Context) HelmoOCICredentialsWithoutUserPassAdded() *Context {
repos.AddHelmoOCICredentialsWithoutUserPass()
repos.AddHelmoOCICredentialsWithoutUserPass(c.t)
return c
}

func (c *Context) HTTPSCredentialsTLSClientCertAdded() *Context {
repos.AddHTTPSCredentialsTLSClientCert()
repos.AddHTTPSCredentialsTLSClientCert(c.t)
return c
}

func (c *Context) SSHCredentialsAdded() *Context {
repos.AddSSHCredentials()
repos.AddSSHCredentials(c.t)
return c
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/fixture/applicationsets/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,6 @@ func (a *Actions) runCli(args ...string) {

func (a *Actions) AddSignedFile(fileName, fileContents string) *Actions {
a.context.t.Helper()
fixture.AddSignedFile(a.context.path+"/"+fileName, fileContents)
fixture.AddSignedFile(a.context.t, a.context.path+"/"+fileName, fileContents)
return a
}
4 changes: 2 additions & 2 deletions test/e2e/fixture/applicationsets/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (c *Context) Path(path string) *Context {
}

func (c *Context) GPGPublicKeyAdded() *Context {
gpgkeys.AddGPGPublicKey()
gpgkeys.AddGPGPublicKey(c.t)
return c
}

func (c *Context) HTTPSInsecureRepoURLAdded(project string) *Context {
repos.AddHTTPSRepo(true, true, project, fixture.RepoURLTypeHTTPS)
repos.AddHTTPSRepo(c.t, true, true, project, fixture.RepoURLTypeHTTPS)
return c
}
Loading

0 comments on commit 416b7d0

Please sign in to comment.