Skip to content

Commit 254231a

Browse files
committed
chore: stabilize TestContainerLogWithErrClosed and re-enable it for rootful CI
1 parent ea854ec commit 254231a

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

logconsumer_test.go

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,47 @@ func Test_MultipleLogConsumers(t *testing.T) {
219219
require.Equal(t, expected, second.Msgs())
220220
}
221221

222+
// waitLogsQuiet waits until the consumer has received no new log messages for
223+
// quiet, returning the count observed. It fails the test if the stream is
224+
// still producing new messages after timeout.
225+
func waitLogsQuiet(t *testing.T, c *TestLogConsumer, quiet, timeout time.Duration) int {
226+
t.Helper()
227+
228+
count := len(c.Msgs())
229+
lastChange := time.Now()
230+
deadline := time.Now().Add(timeout)
231+
for time.Now().Before(deadline) {
232+
if n := len(c.Msgs()); n != count {
233+
count = n
234+
lastChange = time.Now()
235+
} else if time.Since(lastChange) >= quiet {
236+
return count
237+
}
238+
time.Sleep(50 * time.Millisecond)
239+
}
240+
t.Fatalf("log stream still producing new messages after %v", timeout)
241+
return 0
242+
}
243+
244+
// requireNewMsgs waits until at least want new log messages (beyond existing)
245+
// have been consumed, lets the stream settle so unexpected extra lines still
246+
// surface, and then asserts the exact count. It returns the new total count.
247+
func requireNewMsgs(t *testing.T, c *TestLogConsumer, existing, want int, hint string) int {
248+
t.Helper()
249+
250+
require.Eventuallyf(t, func() bool {
251+
return len(c.Msgs())-existing >= want
252+
}, time.Minute, 50*time.Millisecond, "%s: expected %d new log message(s)", hint, want)
253+
254+
count := waitLogsQuiet(t, c, time.Second, 10*time.Second)
255+
msgs := c.Msgs()
256+
require.Equalf(t, want, count-existing, "%s: expected exactly %d new log message(s), instead has:\n%v", hint, want, msgs[existing:])
257+
return count
258+
}
259+
222260
func TestContainerLogWithErrClosed(t *testing.T) {
223-
if os.Getenv("GITHUB_RUN_ID") != "" {
224-
t.Skip("Skipping as flaky on GitHub Actions, Please see https://github.com/testcontainers/testcontainers-go/issues/1924")
261+
if os.Getenv("XDG_RUNTIME_DIR") != "" {
262+
t.Skip("Docker-in-Docker does not work with rootless Docker")
225263
}
226264

227265
t.Cleanup(func() {
@@ -304,9 +342,9 @@ func TestContainerLogWithErrClosed(t *testing.T) {
304342
port, err := nginx.MappedPort(ctx, "80/tcp")
305343
require.NoError(t, err)
306344

307-
// Gather the initial container logs
308-
time.Sleep(time.Second * 1)
309-
existingLogs := len(consumer.Msgs())
345+
// Wait for the initial nginx startup logs to drain so the baseline count
346+
// is stable before asserting on new messages.
347+
existingLogs := waitLogsQuiet(t, &consumer, time.Second, 30*time.Second)
310348

311349
hitNginx := func() {
312350
i, _, err := dind.Exec(ctx, []string{"wget", "--spider", net.JoinHostPort("localhost", port.Port())})
@@ -315,10 +353,7 @@ func TestContainerLogWithErrClosed(t *testing.T) {
315353
}
316354

317355
hitNginx()
318-
time.Sleep(time.Second * 1)
319-
msgs := consumer.Msgs()
320-
require.Equalf(t, 1, len(msgs)-existingLogs, "logConsumer should have 1 new log message, instead has: %v", msgs[existingLogs:])
321-
existingLogs = len(consumer.Msgs())
356+
existingLogs = requireNewMsgs(t, &consumer, existingLogs, 1, "logConsumer should have 1 new log message")
322357

323358
iptableArgs := []string{
324359
"INPUT", "-p", "tcp", "--dport", "2375",
@@ -331,16 +366,14 @@ func TestContainerLogWithErrClosed(t *testing.T) {
331366
i, _, err = dind.Exec(ctx, append([]string{"iptables", "-D"}, iptableArgs...))
332367
require.NoErrorf(t, err, "Failed to re-open connection to dind daemon: i(%d), err %v", i, err)
333368
require.Zerof(t, i, "Failed to re-open connection to dind daemon: i(%d), err %v", i, err)
369+
// Deliberate fixed wait (not an assertion race): give the log producer
370+
// time to observe the TCP reset and restart its log stream.
334371
time.Sleep(time.Second * 3)
335372

336373
hitNginx()
337374
hitNginx()
338-
time.Sleep(time.Second * 1)
339-
msgs = consumer.Msgs()
340-
require.Equalf(t, 2, len(msgs)-existingLogs,
341-
"LogConsumer should have 2 new log messages after detecting closed connection and"+
342-
" re-requesting logs. Instead has:\n%s", msgs[existingLogs:],
343-
)
375+
requireNewMsgs(t, &consumer, existingLogs, 2,
376+
"LogConsumer should have 2 new log messages after detecting closed connection and re-requesting logs")
344377
}
345378

346379
func TestContainerLogsShouldBeWithoutStreamHeader(t *testing.T) {

0 commit comments

Comments
 (0)