diff --git a/test/e2e/annotations_test.go b/test/e2e/annotations_test.go index 6952a371c..077340f5c 100644 --- a/test/e2e/annotations_test.go +++ b/test/e2e/annotations_test.go @@ -467,8 +467,9 @@ data: require.NoErrorf(t, err, "Expected to successfully unmarshal") _, versionedAnnExists := respKubectl.Annotations["kapp.k14s.io/versioned"] - isConditionsMet := respKubectl.Kind == "ConfigMap" || respKubectl.Name == "config" || versionedAnnExists - require.True(t, isConditionsMet, "Expected to have versioned ConfigMap resource") + require.Condition(t, func() bool { + return respKubectl.Kind == "ConfigMap" && respKubectl.Name == "config" && versionedAnnExists + }, "Expected to have versioned ConfigMap resource") kappOut, _ := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name, "--json"}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml)}) diff --git a/test/e2e/cluster_resource.go b/test/e2e/cluster_resource.go index aaad82773..2e4ffacb0 100644 --- a/test/e2e/cluster_resource.go +++ b/test/e2e/cluster_resource.go @@ -42,9 +42,9 @@ func NewPresentClusterResource(kind, name, ns string, kubectl Kubectl) ClusterRe func NewMissingClusterResource(t *testing.T, kind, name, ns string, kubectl Kubectl) { _, err := kubectl.RunWithOpts([]string{"get", kind, name, "-n", ns, "-o", "yaml"}, RunOpts{AllowError: true}) - isConditionsMet := err != nil || strings.Contains(err.Error(), "Error from server (NotFound)") - - require.True(t, isConditionsMet, "Expected resource to not exist") + require.Condition(t, func() bool { + return err != nil && strings.Contains(err.Error(), "Error from server (NotFound)") + }, "Expected resource to not exist") } func (r ClusterResource) UID() string { diff --git a/test/e2e/common.go b/test/e2e/common.go index 980f17848..c0702d546 100644 --- a/test/e2e/common.go +++ b/test/e2e/common.go @@ -19,8 +19,8 @@ func validateChanges(t *testing.T, respTable []ui.JSONUITableResp, expected []ma delete(row, "age") } - require.Exactlyf(t, expected, respTable[0].Rows, "Expected to see correct changes, but did not") - require.Equalf(t, notesOp, respTable[0].Notes[0], "Expected to see correct summary, but did not") - require.Equalf(t, notesWaitTo, respTable[0].Notes[1], "Expected to see correct summary, but did not") + require.Exactlyf(t, expected, respTable[0].Rows, "Expected to see correct changes, but did not: '%s'", output) + require.Equalf(t, notesOp, respTable[0].Notes[0], "Expected to see correct summary, but did not: '%s'", output) + require.Equalf(t, notesWaitTo, respTable[0].Notes[1], "Expected to see correct summary, but did not: '%s'", output) } diff --git a/test/e2e/config_test.go b/test/e2e/config_test.go index fe82b7916..30b52b64c 100644 --- a/test/e2e/config_test.go +++ b/test/e2e/config_test.go @@ -193,7 +193,7 @@ secrets: secrets := NewPresentClusterResource("serviceaccount", "test-sa-with-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{}) require.Len(t, secrets, 2, "Expected one set and one generated secret") - require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret") + require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret at idx0: %#v", secrets[0]) generatedSecretName = secrets[1].(map[string]interface{})["name"].(string) require.True(t, strings.HasPrefix(generatedSecretName, "test-sa-with-secrets-token-"), "Expected generated secret at idx1: %#v", secrets[1]) @@ -247,9 +247,9 @@ secrets: secrets = NewPresentClusterResource("serviceaccount", "test-sa-without-secrets", env.Namespace, kubectl).RawPath(ctlres.NewPathFromStrings([]string{"secrets"})).([]interface{}) - require.True(t, strings.HasPrefix(secrets[1].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-"), "Expected generated secret at idx1: %#v", secrets[1]) require.Len(t, secrets, 2, "Expected one set and one generated secret") require.Exactlyf(t, map[string]interface{}{"name": "some-secret"}, secrets[0], "Expected provided secret at idx0") + require.True(t, strings.HasPrefix(secrets[1].(map[string]interface{})["name"].(string), "test-sa-without-secrets-token-"), "Expected generated secret at idx1: %#v", secrets[1]) }) diff --git a/test/e2e/diff_test.go b/test/e2e/diff_test.go index d542ee399..ea6b499f3 100644 --- a/test/e2e/diff_test.go +++ b/test/e2e/diff_test.go @@ -240,6 +240,7 @@ func TestDiffExitStatus(t *testing.T) { RunOpts{IntoNs: true, AllowError: true, StdinReader: strings.NewReader("---\n")}) require.Errorf(t, err, "Expected to receive error") + require.Containsf(t, err.Error(), "Exiting after diffing with no pending changes (exit status 2)", "Expected to find stderr output") require.Containsf(t, err.Error(), "exit code: '2'", "Expected to find exit code") @@ -255,6 +256,7 @@ metadata: RunOpts{IntoNs: true, AllowError: true, StdinReader: strings.NewReader(yaml1)}) require.Errorf(t, err, "Expected to receive error") + require.Containsf(t, err.Error(), "Exiting after diffing with pending changes (exit status 3)", "Expected to find stderr output") require.Containsf(t, err.Error(), "exit code: '3'", "Expected to find exit code") } diff --git a/test/e2e/env.go b/test/e2e/env.go index 3514576ae..20c84cddb 100644 --- a/test/e2e/env.go +++ b/test/e2e/env.go @@ -4,10 +4,11 @@ package e2e import ( - "github.com/stretchr/testify/require" "os" "strings" "testing" + + "github.com/stretchr/testify/require" ) type Env struct { diff --git a/test/e2e/filter_test.go b/test/e2e/filter_test.go index c165d44ba..1e9219aaf 100644 --- a/test/e2e/filter_test.go +++ b/test/e2e/filter_test.go @@ -4,9 +4,10 @@ package e2e import ( - "github.com/stretchr/testify/require" "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestFilter(t *testing.T) { diff --git a/test/e2e/formatted_error_test.go b/test/e2e/formatted_error_test.go index 593d2a676..d19d24c7c 100644 --- a/test/e2e/formatted_error_test.go +++ b/test/e2e/formatted_error_test.go @@ -4,10 +4,11 @@ package e2e import ( - "github.com/stretchr/testify/require" "regexp" "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestFormattedError(t *testing.T) { @@ -71,11 +72,6 @@ kapp: Error: Applying create job/successful-job (batch/v1) namespace: default: replaceUIDs := regexp.MustCompile(`"controller-uid":"[^"]+"`) out = replaceUIDs.ReplaceAllString(out, "-replaced-") - require.Containsf(t, out, expectedErr, "Expected to see expected err in output, but did not: %d chars >>>%s<<< vs %d chars >>>%s<<<", - len(out), replaceSpace(out), len(expectedErr), replaceSpace(expectedErr)) + require.Containsf(t, out, expectedErr, "Expected to see expected err in output, but did not") }) } - -func replaceSpace(in string) string { - return strings.ReplaceAll(in, " ", "#") -} diff --git a/test/e2e/help_test.go b/test/e2e/help_test.go index f6d0a64f9..ccaf5382e 100644 --- a/test/e2e/help_test.go +++ b/test/e2e/help_test.go @@ -13,8 +13,7 @@ func TestHelpCommandGroup(t *testing.T) { kapp := Kapp{t, env.Namespace, env.KappBinaryPath, Logger{}} _, err := kapp.RunWithOpts([]string{"app-group"}, RunOpts{NoNamespace: true, AllowError: true}) - require.Errorf(t, err, "Expected to receive error") - require.Contains(t, err.Error(), "Error: Use one of available subcommands: delete, deploy", - "Expected helpful error message") + + require.Contains(t, err.Error(), "Error: Use one of available subcommands: delete, deploy", "Expected helpful error message") } diff --git a/test/e2e/ignore_failing_api_services_flag_test.go b/test/e2e/ignore_failing_api_services_flag_test.go index 2beec80cf..2fbbcd5ef 100644 --- a/test/e2e/ignore_failing_api_services_flag_test.go +++ b/test/e2e/ignore_failing_api_services_flag_test.go @@ -128,8 +128,8 @@ metadata: logger.Section("deploy app that uses failing api service", func() { _, err := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name3}, RunOpts{ AllowError: true, IntoNs: true, StdinReader: strings.NewReader(yaml3)}) - require.Errorf(t, err, "Expected error when deploying with failing api service") + require.Contains(t, err.Error(), "unable to retrieve the complete list of server APIs: dummykapptest.com/v1: the server is currently unable to handle the request", "Expected api retrieval error") }) @@ -139,10 +139,9 @@ metadata: _, err := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name3, ignoreFlag}, RunOpts{ AllowError: true, IntoNs: true, StdinReader: strings.NewReader(yaml3)}) - require.Errorf(t, err, "Expected error when deploying with failing api service") - require.Contains(t, err.Error(), "Expected to find kind 'dummykapptest.com/v1/Foo', but did not", - "Expected CRD retrieval error") + + require.Contains(t, err.Error(), "Expected to find kind 'dummykapptest.com/v1/Foo', but did not", "Expected CRD retrieval error") }) logger.Section("delete app that does not use api service", func() { @@ -279,10 +278,9 @@ spec: {} logger.Section("deploy app that uses failing group version", func() { _, err := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name3, "--apply-timeout=5s"}, RunOpts{ AllowError: true, IntoNs: true, StdinReader: strings.NewReader(yaml3)}) - require.Errorf(t, err, "Expected error when deploying with failing group version") - require.Contains(t, err.Error(), `service "failing-group-version-webhook" not found`, - "Expected api retrieval error") + + require.Contains(t, err.Error(), `service "failing-group-version-webhook" not found`, "Expected api retrieval error") }) logger.Section("delete app that uses failing group version", func() { diff --git a/test/e2e/kapp.go b/test/e2e/kapp.go index cb6ce7a28..35097ad0a 100644 --- a/test/e2e/kapp.go +++ b/test/e2e/kapp.go @@ -6,12 +6,13 @@ package e2e import ( "bytes" "fmt" - "github.com/stretchr/testify/require" "io" "os" "os/exec" "strings" "testing" + + "github.com/stretchr/testify/require" ) type Kapp struct { diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 048dd24ab..9bb992596 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -6,11 +6,12 @@ package e2e import ( "bytes" "fmt" - "github.com/stretchr/testify/require" "os" "os/exec" "strings" "testing" + + "github.com/stretchr/testify/require" ) type Kubectl struct { diff --git a/test/e2e/order_test.go b/test/e2e/order_test.go index 4d0bf267e..661c08481 100644 --- a/test/e2e/order_test.go +++ b/test/e2e/order_test.go @@ -4,11 +4,12 @@ package e2e import ( - "github.com/stretchr/testify/require" "regexp" "sort" "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestOrder(t *testing.T) { diff --git a/test/e2e/update_replace_test.go b/test/e2e/update_replace_test.go index 248d4927b..ceabc3f57 100644 --- a/test/e2e/update_replace_test.go +++ b/test/e2e/update_replace_test.go @@ -4,9 +4,10 @@ package e2e import ( - "github.com/stretchr/testify/require" "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestUpdateFallbackOnReplace(t *testing.T) { diff --git a/test/e2e/update_retry_on_conflict_test.go b/test/e2e/update_retry_on_conflict_test.go index 9c79939b0..af16b5d48 100644 --- a/test/e2e/update_retry_on_conflict_test.go +++ b/test/e2e/update_retry_on_conflict_test.go @@ -183,7 +183,6 @@ spec: _, err := kapp.RunWithOpts([]string{"deploy", "--tty", "-f", tmpFile.Name(), "-a", name}, RunOpts{IntoNs: true, StdinReader: promptOutput.YesReader(), StdoutWriter: promptOutput.OutputWriter(), Interactive: true, AllowError: true}) - require.Errorf(t, err, "Expected error, but err was nil") require.Contains(t, err.Error(), "Failed to update due to resource conflict (approved diff no longer matches)", @@ -312,7 +311,8 @@ func (p promptOutput) WaitPresented() { break } } - require.NoError(p.t, scanner.Err()) + err := scanner.Err() + require.NoError(p.t, err) } func newTmpFile(content string, t *testing.T) *os.File {