Skip to content

Commit

Permalink
chore(test): simplify test assertions (argoproj#21242)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crenshaw <[email protected]>
  • Loading branch information
crenshaw-dev authored Dec 18, 2024
1 parent ab07b0a commit 566bc2e
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1893,6 +1891,8 @@ func TestRequeueGeneratorFails(t *testing.T) {
}

func TestValidateGeneratedApplications(t *testing.T) {
t.Parallel()

scheme := runtime.NewScheme()
err := v1alpha1.AddToScheme(scheme)
require.NoError(t, err)
Expand Down Expand Up @@ -1924,7 +1924,6 @@ func TestValidateGeneratedApplications(t *testing.T) {
for _, cc := range []struct {
name string
apps []v1alpha1.Application
expectedErrors []string
validationErrors map[int]error
}{
{
Expand All @@ -1947,7 +1946,6 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
expectedErrors: []string{},
validationErrors: map[int]error{},
},
{
Expand All @@ -1971,7 +1969,6 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
expectedErrors: []string{"application destination can't have both name and server defined"},
validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: application destination can't have both name and server defined: my-cluster my-server")},
},
{
Expand All @@ -1994,7 +1991,6 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
expectedErrors: []string{"application references project DOES-NOT-EXIST which does not exist"},
validationErrors: map[int]error{0: fmt.Errorf("application references project DOES-NOT-EXIST which does not exist")},
},
{
Expand All @@ -2017,7 +2013,6 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
expectedErrors: []string{},
validationErrors: map[int]error{},
},
{
Expand All @@ -2040,11 +2035,12 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
expectedErrors: []string{"there are no clusters with this name: nonexistent-cluster"},
validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: unable to find destination server: there are no clusters with this name: nonexistent-cluster")},
},
} {
t.Run(cc.name, func(t *testing.T) {
t.Parallel()

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "my-secret",
Expand Down Expand Up @@ -2077,36 +2073,8 @@ func TestValidateGeneratedApplications(t *testing.T) {
}

appSetInfo := v1alpha1.ApplicationSet{}

validationErrors, _ := r.validateGeneratedApplications(context.TODO(), cc.apps, appSetInfo)
var errorMessages []string
for _, v := range validationErrors {
errorMessages = append(errorMessages, v.Error())
}

if len(errorMessages) == 0 {
assert.Empty(t, cc.expectedErrors, "Expected errors but none were seen")
} else {
// An error was returned: it should be expected
matched := false
for _, expectedErr := range cc.expectedErrors {
foundMatch := strings.Contains(strings.Join(errorMessages, ";"), expectedErr)
assert.True(t, foundMatch, "Unble to locate expected error: %s", cc.expectedErrors)
matched = matched || foundMatch
}
assert.True(t, matched, "An unexpected error occurrred: %v", err)
// validation message was returned: it should be expected
matched = false
foundMatch := reflect.DeepEqual(validationErrors, cc.validationErrors)
var message string
for _, v := range validationErrors {
message = v.Error()
break
}
assert.True(t, foundMatch, "Unble to locate validation message: %s", message)
matched = matched || foundMatch
assert.True(t, matched, "An unexpected error occurrred: %v", err)
}
assert.Equal(t, cc.validationErrors, validationErrors)
})
}
}
Expand Down

0 comments on commit 566bc2e

Please sign in to comment.