Skip to content

Commit

Permalink
chore: use Contains or ErrorContains checks (#2839)
Browse files Browse the repository at this point in the history
Simplify test logic by using require.Contains and require.ErrorContains
when possible.

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Oct 18, 2024
1 parent b74984d commit f1632b3
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 51 deletions.
3 changes: 1 addition & 2 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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")
}()
}

Expand Down
7 changes: 2 additions & 5 deletions modules/redpanda/redpanda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions modules/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
45 changes: 9 additions & 36 deletions wait/host_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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\"")
}
}

Expand All @@ -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)")
}
}

Expand All @@ -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")
}
}

Expand All @@ -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\"")
}
}

Expand Down Expand Up @@ -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)")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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\"")
}
}

Expand Down

0 comments on commit f1632b3

Please sign in to comment.