Skip to content

Commit 95a6f62

Browse files
fix: logs updated on starting of a stopped container
Signed-off-by: Shubharanshu Mahapatra <shubhum@amazon.com>
1 parent 9d0a766 commit 95a6f62

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

cmd/nerdctl/container/container_logs_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os/exec"
2222
"runtime"
23+
"strconv"
2324
"strings"
2425
"testing"
2526
"time"
@@ -345,3 +346,78 @@ func TestNoneLoggerHasNoLogURI(t *testing.T) {
345346
testCase.Expected = test.Expects(1, nil, nil)
346347
testCase.Run(t)
347348
}
349+
350+
func TestLogsWithStartContainer(t *testing.T) {
351+
testCase := nerdtest.Setup()
352+
353+
// For windows we havent added support for dual logging so not adding the test.
354+
testCase.Require = require.All(require.Not(require.Windows))
355+
356+
testCase.SubTests = []*test.Case{
357+
{
358+
Description: "Test logs are directed correctly for container start of a interactive container",
359+
Setup: func(data test.Data, helpers test.Helpers) {
360+
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
361+
cmd.WithPseudoTTY()
362+
cmd.Feed(strings.NewReader("echo foo\nexit\n"))
363+
364+
cmd.Run(&test.Expected{
365+
ExitCode: 0,
366+
})
367+
368+
},
369+
Cleanup: func(data test.Data, helpers test.Helpers) {
370+
helpers.Anyhow("rm", "-f", data.Identifier())
371+
},
372+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
373+
cmd := helpers.Command("start", "-a", data.Identifier())
374+
cmd.WithPseudoTTY()
375+
cmd.Feed(strings.NewReader("echo bar\nexit\n"))
376+
cmd.Run(&test.Expected{
377+
ExitCode: 0,
378+
})
379+
cmd = helpers.Command("logs", data.Identifier())
380+
381+
return cmd
382+
},
383+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
384+
return &test.Expected{
385+
ExitCode: 0,
386+
Output: func(stdout string, info string, t *testing.T) {
387+
assert.Assert(t, strings.Contains(stdout, "foo"))
388+
assert.Assert(t, strings.Contains(stdout, "bar"))
389+
},
390+
}
391+
},
392+
},
393+
{
394+
Description: "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs",
395+
Setup: func(data test.Data, helpers test.Helpers) {
396+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
397+
},
398+
Cleanup: func(data test.Data, helpers test.Helpers) {
399+
helpers.Anyhow("rm", "-f", data.Identifier())
400+
},
401+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
402+
helpers.Ensure("stop", data.Identifier())
403+
initialLogs := helpers.Capture("logs", data.Identifier())
404+
initialFooCount := strings.Count(initialLogs, "foo")
405+
data.Set("initialFooCount", strconv.Itoa(initialFooCount))
406+
helpers.Ensure("start", data.Identifier())
407+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
408+
return helpers.Command("logs", data.Identifier())
409+
},
410+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
411+
return &test.Expected{
412+
ExitCode: 0,
413+
Output: func(stdout string, info string, t *testing.T) {
414+
finalLogsCount := strings.Count(stdout, "foo")
415+
initialFooCount, _ := strconv.Atoi(data.Get("initialFooCount"))
416+
assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart", info)
417+
},
418+
}
419+
},
420+
},
421+
}
422+
testCase.Run(t)
423+
}

pkg/taskutil/taskutil.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
6666
var ioCreator cio.Creator
6767
if len(attachStreamOpt) != 0 {
6868
log.G(ctx).Debug("attaching output instead of using the log-uri")
69+
// when attaching a TTY we use writee for stdio and binary for log persistence
6970
if flagT {
7071
in, err := consoleutil.NewDetachableStdin(con, detachKeys, closer)
7172
if err != nil {
7273
return nil, err
7374
}
74-
ioCreator = cio.NewCreator(cio.WithStreams(in, con, nil), cio.WithTerminal)
75+
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, con, nil)
7576
} else {
7677
streams := processAttachStreamsOpt(attachStreamOpt)
77-
ioCreator = cio.NewCreator(cio.WithStreams(streams.stdIn, streams.stdOut, streams.stdErr))
78+
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
7879
}
7980

8081
} else if flagT && flagD {

0 commit comments

Comments
 (0)