Skip to content

Commit

Permalink
chore: use testify instead of testing.Fatal or testing.Error in cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 11, 2024
1 parent b39f3b1 commit 0dacc2e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 170 deletions.
9 changes: 3 additions & 6 deletions cmd/argocd/commands/app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"text/tabwriter"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)
Expand Down Expand Up @@ -37,9 +38,7 @@ func TestPrintTreeViewAppResources(t *testing.T) {
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)

printTreeViewAppResourcesNotOrphaned(nodeMapping, mapParentToChild, parentNode, w)
if err := w.Flush(); err != nil {
t.Fatal(err)
}
require.NoError(t, w.Flush())
output := buf.String()

assert.Contains(t, output, "Rollout")
Expand Down Expand Up @@ -78,9 +77,7 @@ func TestPrintTreeViewDetailedAppResources(t *testing.T) {
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)

printDetailedTreeViewAppResourcesNotOrphaned(nodeMapping, mapParentToChild, parentNode, w)
if err := w.Flush(); err != nil {
t.Fatal(err)
}
require.NoError(t, w.Flush())
output := buf.String()

assert.Contains(t, output, "Rollout")
Expand Down
151 changes: 36 additions & 115 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,8 @@ func TestFindRevisionHistoryWithoutPassedId(t *testing.T) {
}

history, err := findRevisionHistory(&application, -1)
if err != nil {
t.Fatal("Find revision history should fail without errors")
}

if history == nil {
t.Fatal("History should be found")
}
require.NoError(t, err, "Find revision history should fail without errors")
require.NotNil(t, history, "History should be found")
}

func TestPrintTreeViewAppGet(t *testing.T) {
Expand Down Expand Up @@ -249,13 +244,8 @@ func TestFindRevisionHistoryWithoutPassedIdWithMultipleSources(t *testing.T) {
}

history, err := findRevisionHistory(&application, -1)
if err != nil {
t.Fatal("Find revision history should fail without errors")
}

if history == nil {
t.Fatal("History should be found")
}
require.NoError(t, err, "Find revision history should fail without errors")
require.NotNil(t, history, "History should be found")
}

func TestDefaultWaitOptions(t *testing.T) {
Expand Down Expand Up @@ -308,17 +298,9 @@ func TestFindRevisionHistoryWithoutPassedIdAndEmptyHistoryList(t *testing.T) {

history, err := findRevisionHistory(&application, -1)

if err == nil {
t.Fatal("Find revision history should fail with errors")
}

if history != nil {
t.Fatal("History should be empty")
}

if err.Error() != "Application '' should have at least two successful deployments" {
t.Fatal("Find revision history should fail with correct error message")
}
require.Error(t, err, "Find revision history should fail with errors")
require.Nil(t, history, "History should be empty")
require.EqualError(t, err, "Application '' should have at least two successful deployments", "Find revision history should fail with correct error message")
}

func TestFindRevisionHistoryWithPassedId(t *testing.T) {
Expand Down Expand Up @@ -346,17 +328,9 @@ func TestFindRevisionHistoryWithPassedId(t *testing.T) {
}

history, err := findRevisionHistory(&application, 3)
if err != nil {
t.Fatal("Find revision history should fail without errors")
}

if history == nil {
t.Fatal("History should be found")
}

if history.Revision != "123" {
t.Fatal("Failed to find correct history with correct revision")
}
require.NoError(t, err, "Find revision history should fail without errors")
require.NotNil(t, history, "History should be found")
require.Equal(t, "123", history.Revision, "Failed to find correct history with correct revision")
}

func TestFindRevisionHistoryWithPassedIdThatNotExist(t *testing.T) {
Expand Down Expand Up @@ -385,17 +359,9 @@ func TestFindRevisionHistoryWithPassedIdThatNotExist(t *testing.T) {

history, err := findRevisionHistory(&application, 4)

if err == nil {
t.Fatal("Find revision history should fail with errors")
}

if history != nil {
t.Fatal("History should be not found")
}

if err.Error() != "Application '' does not have deployment id '4' in history\n" {
t.Fatal("Find revision history should fail with correct error message")
}
require.Error(t, err, "Find revision history should fail with errors")
require.Nil(t, history, "History should be not found")
require.EqualError(t, err, "Application '' does not have deployment id '4' in history\n", "Find revision history should fail with correct error message")
}

func Test_groupObjsByKey(t *testing.T) {
Expand Down Expand Up @@ -457,9 +423,7 @@ func TestFormatSyncPolicy(t *testing.T) {

policy := formatSyncPolicy(app)

if policy != "Manual" {
t.Fatalf("Incorrect policy %q, should be Manual", policy)
}
require.Equalf(t, "Manual", policy, "Incorrect policy %q, should be Manual", policy)
})

t.Run("Auto policy", func(t *testing.T) {
Expand All @@ -473,9 +437,7 @@ func TestFormatSyncPolicy(t *testing.T) {

policy := formatSyncPolicy(app)

if policy != "Auto" {
t.Fatalf("Incorrect policy %q, should be Auto", policy)
}
require.Equalf(t, "Auto", policy, "Incorrect policy %q, should be Auto", policy)
})

t.Run("Auto policy with prune", func(t *testing.T) {
Expand All @@ -491,9 +453,7 @@ func TestFormatSyncPolicy(t *testing.T) {

policy := formatSyncPolicy(app)

if policy != "Auto-Prune" {
t.Fatalf("Incorrect policy %q, should be Auto-Prune", policy)
}
require.Equalf(t, "Auto-Prune", policy, "Incorrect policy %q, should be Auto-Prune", policy)
})
}

Expand All @@ -510,9 +470,7 @@ func TestFormatConditionSummary(t *testing.T) {
}

summary := formatConditionsSummary(app)
if summary != "<none>" {
t.Fatalf("Incorrect summary %q, should be <none>", summary)
}
require.Equalf(t, "<none>", summary, "Incorrect summary %q, should be <none>", summary)
})

t.Run("Few conditions are defined", func(t *testing.T) {
Expand All @@ -533,9 +491,7 @@ func TestFormatConditionSummary(t *testing.T) {
}

summary := formatConditionsSummary(app)
if summary != "type1(2),type2" && summary != "type2,type1(2)" {
t.Fatalf("Incorrect summary %q, should be type1(2),type2", summary)
}
require.Equalf(t, "type1(2),type2", summary, "Incorrect summary %q, should be type1(2),type2", summary)
})
}

Expand All @@ -546,9 +502,7 @@ func TestPrintOperationResult(t *testing.T) {
return nil
})

if output != "" {
t.Fatalf("Incorrect print operation output %q, should be ''", output)
}
require.Emptyf(t, output, "Incorrect print operation output %q, should be ''", output)
})

t.Run("Operation state sync result is not empty", func(t *testing.T) {
Expand All @@ -562,9 +516,7 @@ func TestPrintOperationResult(t *testing.T) {
})

expectation := "Operation: Sync\nSync Revision: revision\nPhase: \nStart: 0001-01-01 00:00:00 +0000 UTC\nFinished: 2020-11-10 23:00:00 +0000 UTC\nDuration: 2333448h16m18.871345152s\n"
if output != expectation {
t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print operation output %q, should be %q", output, expectation)
})

t.Run("Operation state sync result with message is not empty", func(t *testing.T) {
Expand All @@ -579,9 +531,7 @@ func TestPrintOperationResult(t *testing.T) {
})

expectation := "Operation: Sync\nSync Revision: revision\nPhase: \nStart: 0001-01-01 00:00:00 +0000 UTC\nFinished: 2020-11-10 23:00:00 +0000 UTC\nDuration: 2333448h16m18.871345152s\nMessage: test\n"
if output != expectation {
t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print operation output %q, should be %q", output, expectation)
})
}

Expand Down Expand Up @@ -617,9 +567,7 @@ func TestPrintApplicationHistoryTable(t *testing.T) {

expectation := "SOURCE test\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1\n2 0001-01-01 00:00:00 +0000 UTC 2\n3 0001-01-01 00:00:00 +0000 UTC 3\n"

if output != expectation {
t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print operation output %q, should be %q", output, expectation)
}

func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) {
Expand Down Expand Up @@ -696,9 +644,7 @@ func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) {

expectation := "SOURCE test\nID DATE REVISION\n0 0001-01-01 00:00:00 +0000 UTC 0\n\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n"

if output != expectation {
t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print operation output %q, should be %q", output, expectation)
}

func TestPrintAppSummaryTable(t *testing.T) {
Expand Down Expand Up @@ -912,9 +858,7 @@ func TestPrintAppConditions(t *testing.T) {
return nil
})
expectation := "CONDITION\tMESSAGE\tLAST TRANSITION\nDeletionError\ttest\t<nil>\nExcludedResourceWarning\ttest2\t<nil>\nRepeatedResourceWarning\ttest3\t<nil>\n"
if output != expectation {
t.Fatalf("Incorrect print app conditions output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print app conditions output %q, should be %q", output, expectation)
}

func TestPrintParams(t *testing.T) {
Expand Down Expand Up @@ -991,9 +935,7 @@ func TestPrintParams(t *testing.T) {
return nil
})

if output != tc.expectedOutput {
t.Fatalf("Incorrect print params output %q, should be %q\n", output, tc.expectedOutput)
}
require.Equalf(t, tc.expectedOutput, output, "Incorrect print params output %q, should be %q\n", output, tc.expectedOutput)
})
}
}
Expand All @@ -1005,28 +947,22 @@ func TestAppUrlDefault(t *testing.T) {
PlainText: true,
}), "test")
expectation := "http://localhost:80/applications/test"
if result != expectation {
t.Fatalf("Incorrect url %q, should be %q", result, expectation)
}
require.Equalf(t, result, expectation, "Incorrect url %q, should be %q", result, expectation)
})
t.Run("https", func(t *testing.T) {
result := appURLDefault(argocdclient.NewClientOrDie(&argocdclient.ClientOptions{
ServerAddr: "localhost:443",
PlainText: false,
}), "test")
expectation := "https://localhost/applications/test"
if result != expectation {
t.Fatalf("Incorrect url %q, should be %q", result, expectation)
}
require.Equalf(t, result, expectation, "Incorrect url %q, should be %q", result, expectation)
})
}

func TestTruncateString(t *testing.T) {
result := truncateString("argocdtool", 2)
expectation := "ar..."
if result != expectation {
t.Fatalf("Incorrect truncate string %q, should be %q", result, expectation)
}
require.Equalf(t, result, expectation, "Incorrect truncate string %q, should be %q", result, expectation)
}

func TestGetService(t *testing.T) {
Expand All @@ -1040,9 +976,7 @@ func TestGetService(t *testing.T) {
}
result := getServer(app)
expectation := "test-server"
if result != expectation {
t.Fatalf("Incorrect server %q, should be %q", result, expectation)
}
require.Equal(t, result, expectation, "Incorrect server %q, should be %q", result, expectation)
})
t.Run("Name", func(t *testing.T) {
app := &v1alpha1.Application{
Expand All @@ -1054,9 +988,7 @@ func TestGetService(t *testing.T) {
}
result := getServer(app)
expectation := "test-name"
if result != expectation {
t.Fatalf("Incorrect server name %q, should be %q", result, expectation)
}
require.Equal(t, result, expectation, "Incorrect server name %q, should be %q", result, expectation)
})
}

Expand All @@ -1070,17 +1002,9 @@ func TestTargetObjects(t *testing.T) {
},
}
objects, err := targetObjects(resources)
if err != nil {
t.Fatal("operation should finish without error")
}

if len(objects) != 2 {
t.Fatalf("incorrect number of objects %v, should be 2", len(objects))
}

if objects[0].GetName() != "test-helm-guestbook" {
t.Fatalf("incorrect name %q, should be %q", objects[0].GetName(), "test-helm-guestbook")
}
require.NoError(t, err, "operation should finish without error")
require.Lenf(t, objects, 2, "incorrect number of objects %v, should be 2", len(objects))
require.Equalf(t, "test-helm-guestbook", objects[0].GetName(), "incorrect name %q, should be %q", objects[0].GetName(), "test-helm-guestbook")
}

func TestTargetObjects_invalid(t *testing.T) {
Expand All @@ -1107,9 +1031,7 @@ func TestPrintApplicationNames(t *testing.T) {
return nil
})
expectation := "test\ntest\n"
if output != expectation {
t.Fatalf("Incorrect print params output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print params output %q, should be %q", output, expectation)
}

func Test_unset(t *testing.T) {
Expand Down Expand Up @@ -1899,9 +1821,8 @@ func Test_hasAppChanged(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasAppChanged(tt.args.appReq, tt.args.appRes, tt.args.upsert); got != tt.want {
t.Errorf("hasAppChanged() = %v, want %v", got, tt.want)
}
got := hasAppChanged(tt.args.appReq, tt.args.appRes, tt.args.upsert)
assert.Equalf(t, tt.want, got, "hasAppChanged() = %v, want %v", got, tt.want)
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/argocd/commands/applicationset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func TestPrintApplicationSetNames(t *testing.T) {
return nil
})
expectation := "test\nteam-one/test\n"
if output != expectation {
t.Fatalf("Incorrect print params output %q, should be %q", output, expectation)
}
require.Equalf(t, output, expectation, "Incorrect print params output %q, should be %q", output, expectation)
}

func TestPrintApplicationSetTable(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/argocd/commands/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ func Test_getRestConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := getRestConfig(tt.args.pathOpts, tt.args.ctxName); err == nil {
require.Equal(t, tt.expected, got)
} else if tt.wantErr {
require.Equal(t, tt.expectedErr, err.Error())
got, err := getRestConfig(tt.args.pathOpts, tt.args.ctxName)
if tt.wantErr {
require.EqualError(t, err, tt.expectedErr)
} else {
t.Errorf("An unexpected error occurred during test %s:\n%s", tt.name, err.Error())
require.Errorf(t, err, "An unexpected error occurred during test %s:\n%s", tt.name, err.Error())
require.Equal(t, tt.expected, got)
}
})
}
Expand Down
Loading

0 comments on commit 0dacc2e

Please sign in to comment.