From f1632b35b3a241818697aa338d54b61eb3550f39 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sat, 19 Oct 2024 00:00:52 +0200 Subject: [PATCH] chore: use Contains or ErrorContains checks (#2839) Simplify test logic by using require.Contains and require.ErrorContains when possible. Signed-off-by: Matthieu MOREL --- container_test.go | 3 +-- docker_test.go | 2 +- generic_test.go | 5 ++-- modules/redpanda/redpanda_test.go | 7 ++--- modules/registry/registry_test.go | 3 +-- options_test.go | 3 +-- wait/host_port_test.go | 45 +++++++------------------------ 7 files changed, 17 insertions(+), 51 deletions(-) diff --git a/container_test.go b/container_test.go index 562eb038a4..16411283c6 100644 --- a/container_test.go +++ b/container_test.go @@ -376,8 +376,7 @@ func Test_GetLogsFromFailedContainer(t *testing.T) { Started: true, }) testcontainers.CleanupContainer(t, c) - require.Error(t, err) - require.Contains(t, err.Error(), "container exited with code 0") + require.ErrorContains(t, err, "container exited with code 0") logs, logErr := c.Logs(ctx) require.NoError(t, logErr) diff --git a/docker_test.go b/docker_test.go index 0c636d2296..d01f21f01f 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1478,7 +1478,7 @@ func TestDockerCreateContainerWithFiles(t *testing.T) { CleanupContainer(t, nginxC) if err != nil { - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } else { for _, f := range tc.files { require.NoError(t, err) diff --git a/generic_test.go b/generic_test.go index 2e9bc44d6a..250a9aaab2 100644 --- a/generic_test.go +++ b/generic_test.go @@ -5,7 +5,6 @@ import ( "errors" "os" "os/exec" - "strings" "sync" "testing" "time" @@ -138,8 +137,8 @@ func TestGenericReusableContainerInSubprocess(t *testing.T) { t.Log(output) // check is reuse container with WaitingFor work correctly. - require.True(t, strings.Contains(output, "⏳ Waiting for container id")) - require.True(t, strings.Contains(output, "🔔 Container is ready")) + require.Contains(t, output, "⏳ Waiting for container id") + require.Contains(t, output, "🔔 Container is ready") }() } diff --git a/modules/redpanda/redpanda_test.go b/modules/redpanda/redpanda_test.go index 3983b7f8ba..9697b30525 100644 --- a/modules/redpanda/redpanda_test.go +++ b/modules/redpanda/redpanda_test.go @@ -507,8 +507,7 @@ func TestRedpandaListener_InvalidPort(t *testing.T) { network.WithNetwork([]string{"redpanda-host"}, RPNetwork), ) testcontainers.CleanupContainer(t, ctr) - require.Error(t, err) - require.Contains(t, err.Error(), "invalid port on listener redpanda:99092") + require.ErrorContains(t, err, "invalid port on listener redpanda:99092") } func TestRedpandaListener_NoNetwork(t *testing.T) { @@ -520,9 +519,7 @@ func TestRedpandaListener_NoNetwork(t *testing.T) { redpanda.WithListener("redpanda:99092"), ) testcontainers.CleanupContainer(t, ctr) - require.Error(t, err) - - require.Contains(t, err.Error(), "container must be attached to at least one network") + require.ErrorContains(t, err, "container must be attached to at least one network") } func TestRedpandaBootstrapConfig(t *testing.T) { diff --git a/modules/registry/registry_test.go b/modules/registry/registry_test.go index 6b90349ff3..2b647c2c86 100644 --- a/modules/registry/registry_test.go +++ b/modules/registry/registry_test.go @@ -207,8 +207,7 @@ func TestRunContainer_wrongData(t *testing.T) { Started: true, }) testcontainers.CleanupContainer(t, redisC) - require.Error(t, err) - require.Contains(t, err.Error(), "manifest unknown") + require.ErrorContains(t, err, "manifest unknown") } // setAuthConfig sets the DOCKER_AUTH_CONFIG environment variable with diff --git a/options_test.go b/options_test.go index dbbb2964d6..7ba1b9ef94 100644 --- a/options_test.go +++ b/options_test.go @@ -96,8 +96,7 @@ func TestWithLogConsumers(t *testing.T) { testcontainers.CleanupContainer(t, c) // we expect an error because the MySQL environment variables are not set // but this is expected because we just want to test the log consumer - require.Error(t, err) - require.Contains(t, err.Error(), "container exited with code 1") + require.ErrorContains(t, err, "container exited with code 1") require.NotEmpty(t, lc.msgs) } diff --git a/wait/host_port_test.go b/wait/host_port_test.go index 18a15aed82..1623ef95c0 100644 --- a/wait/host_port_test.go +++ b/wait/host_port_test.go @@ -155,10 +155,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToOOMKilledContainer(t *testing { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container crashed with out-of-memory (OOMKilled)" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)") } } @@ -189,10 +186,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToExitedContainer(t *testing.T) { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container exited with code 1" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container exited with code 1") } } @@ -222,10 +216,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToUnexpectedContainerStatus(t * { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "unexpected container status \"dead\"" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "unexpected container status \"dead\"") } } @@ -250,10 +241,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToOOMKilledContainer(t *te { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container crashed with out-of-memory (OOMKilled)" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)") } } @@ -279,10 +267,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToExitedContainer(t *testi { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container exited with code 1" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container exited with code 1") } } @@ -307,10 +292,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToUnexpectedContainerStatu { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "unexpected container status \"dead\"" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "unexpected container status \"dead\"") } } @@ -354,10 +336,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToOOMKilledContainer(t *te { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container crashed with out-of-memory (OOMKilled)" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)") } } @@ -402,10 +381,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToExitedContainer(t *testi { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "container exited with code 1" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "container exited with code 1") } } @@ -449,10 +425,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToUnexpectedContainerStatu { err := wg.WaitUntilReady(context.Background(), target) - require.Error(t, err) - - expected := "unexpected container status \"dead\"" - require.Contains(t, err.Error(), expected) + require.ErrorContains(t, err, "unexpected container status \"dead\"") } }