Skip to content

Commit f1632b3

Browse files
authored
chore: use Contains or ErrorContains checks (#2839)
Simplify test logic by using require.Contains and require.ErrorContains when possible. Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent b74984d commit f1632b3

File tree

7 files changed

+17
-51
lines changed

7 files changed

+17
-51
lines changed

container_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ func Test_GetLogsFromFailedContainer(t *testing.T) {
376376
Started: true,
377377
})
378378
testcontainers.CleanupContainer(t, c)
379-
require.Error(t, err)
380-
require.Contains(t, err.Error(), "container exited with code 0")
379+
require.ErrorContains(t, err, "container exited with code 0")
381380

382381
logs, logErr := c.Logs(ctx)
383382
require.NoError(t, logErr)

docker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ func TestDockerCreateContainerWithFiles(t *testing.T) {
14781478
CleanupContainer(t, nginxC)
14791479

14801480
if err != nil {
1481-
require.Contains(t, err.Error(), tc.errMsg)
1481+
require.ErrorContains(t, err, tc.errMsg)
14821482
} else {
14831483
for _, f := range tc.files {
14841484
require.NoError(t, err)

generic_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"os"
77
"os/exec"
8-
"strings"
98
"sync"
109
"testing"
1110
"time"
@@ -138,8 +137,8 @@ func TestGenericReusableContainerInSubprocess(t *testing.T) {
138137

139138
t.Log(output)
140139
// check is reuse container with WaitingFor work correctly.
141-
require.True(t, strings.Contains(output, "⏳ Waiting for container id"))
142-
require.True(t, strings.Contains(output, "🔔 Container is ready"))
140+
require.Contains(t, output, "⏳ Waiting for container id")
141+
require.Contains(t, output, "🔔 Container is ready")
143142
}()
144143
}
145144

modules/redpanda/redpanda_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ func TestRedpandaListener_InvalidPort(t *testing.T) {
507507
network.WithNetwork([]string{"redpanda-host"}, RPNetwork),
508508
)
509509
testcontainers.CleanupContainer(t, ctr)
510-
require.Error(t, err)
511-
require.Contains(t, err.Error(), "invalid port on listener redpanda:99092")
510+
require.ErrorContains(t, err, "invalid port on listener redpanda:99092")
512511
}
513512

514513
func TestRedpandaListener_NoNetwork(t *testing.T) {
@@ -520,9 +519,7 @@ func TestRedpandaListener_NoNetwork(t *testing.T) {
520519
redpanda.WithListener("redpanda:99092"),
521520
)
522521
testcontainers.CleanupContainer(t, ctr)
523-
require.Error(t, err)
524-
525-
require.Contains(t, err.Error(), "container must be attached to at least one network")
522+
require.ErrorContains(t, err, "container must be attached to at least one network")
526523
}
527524

528525
func TestRedpandaBootstrapConfig(t *testing.T) {

modules/registry/registry_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ func TestRunContainer_wrongData(t *testing.T) {
207207
Started: true,
208208
})
209209
testcontainers.CleanupContainer(t, redisC)
210-
require.Error(t, err)
211-
require.Contains(t, err.Error(), "manifest unknown")
210+
require.ErrorContains(t, err, "manifest unknown")
212211
}
213212

214213
// setAuthConfig sets the DOCKER_AUTH_CONFIG environment variable with

options_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ func TestWithLogConsumers(t *testing.T) {
9696
testcontainers.CleanupContainer(t, c)
9797
// we expect an error because the MySQL environment variables are not set
9898
// but this is expected because we just want to test the log consumer
99-
require.Error(t, err)
100-
require.Contains(t, err.Error(), "container exited with code 1")
99+
require.ErrorContains(t, err, "container exited with code 1")
101100
require.NotEmpty(t, lc.msgs)
102101
}
103102

wait/host_port_test.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToOOMKilledContainer(t *testing
155155

156156
{
157157
err := wg.WaitUntilReady(context.Background(), target)
158-
require.Error(t, err)
159-
160-
expected := "container crashed with out-of-memory (OOMKilled)"
161-
require.Contains(t, err.Error(), expected)
158+
require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)")
162159
}
163160
}
164161

@@ -189,10 +186,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToExitedContainer(t *testing.T)
189186

190187
{
191188
err := wg.WaitUntilReady(context.Background(), target)
192-
require.Error(t, err)
193-
194-
expected := "container exited with code 1"
195-
require.Contains(t, err.Error(), expected)
189+
require.ErrorContains(t, err, "container exited with code 1")
196190
}
197191
}
198192

@@ -222,10 +216,7 @@ func TestHostPortStrategyFailsWhileGettingPortDueToUnexpectedContainerStatus(t *
222216

223217
{
224218
err := wg.WaitUntilReady(context.Background(), target)
225-
require.Error(t, err)
226-
227-
expected := "unexpected container status \"dead\""
228-
require.Contains(t, err.Error(), expected)
219+
require.ErrorContains(t, err, "unexpected container status \"dead\"")
229220
}
230221
}
231222

@@ -250,10 +241,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToOOMKilledContainer(t *te
250241

251242
{
252243
err := wg.WaitUntilReady(context.Background(), target)
253-
require.Error(t, err)
254-
255-
expected := "container crashed with out-of-memory (OOMKilled)"
256-
require.Contains(t, err.Error(), expected)
244+
require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)")
257245
}
258246
}
259247

@@ -279,10 +267,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToExitedContainer(t *testi
279267

280268
{
281269
err := wg.WaitUntilReady(context.Background(), target)
282-
require.Error(t, err)
283-
284-
expected := "container exited with code 1"
285-
require.Contains(t, err.Error(), expected)
270+
require.ErrorContains(t, err, "container exited with code 1")
286271
}
287272
}
288273

@@ -307,10 +292,7 @@ func TestHostPortStrategyFailsWhileExternalCheckingDueToUnexpectedContainerStatu
307292

308293
{
309294
err := wg.WaitUntilReady(context.Background(), target)
310-
require.Error(t, err)
311-
312-
expected := "unexpected container status \"dead\""
313-
require.Contains(t, err.Error(), expected)
295+
require.ErrorContains(t, err, "unexpected container status \"dead\"")
314296
}
315297
}
316298

@@ -354,10 +336,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToOOMKilledContainer(t *te
354336

355337
{
356338
err := wg.WaitUntilReady(context.Background(), target)
357-
require.Error(t, err)
358-
359-
expected := "container crashed with out-of-memory (OOMKilled)"
360-
require.Contains(t, err.Error(), expected)
339+
require.ErrorContains(t, err, "container crashed with out-of-memory (OOMKilled)")
361340
}
362341
}
363342

@@ -402,10 +381,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToExitedContainer(t *testi
402381

403382
{
404383
err := wg.WaitUntilReady(context.Background(), target)
405-
require.Error(t, err)
406-
407-
expected := "container exited with code 1"
408-
require.Contains(t, err.Error(), expected)
384+
require.ErrorContains(t, err, "container exited with code 1")
409385
}
410386
}
411387

@@ -449,10 +425,7 @@ func TestHostPortStrategyFailsWhileInternalCheckingDueToUnexpectedContainerStatu
449425

450426
{
451427
err := wg.WaitUntilReady(context.Background(), target)
452-
require.Error(t, err)
453-
454-
expected := "unexpected container status \"dead\""
455-
require.Contains(t, err.Error(), expected)
428+
require.ErrorContains(t, err, "unexpected container status \"dead\"")
456429
}
457430
}
458431

0 commit comments

Comments
 (0)