Skip to content

Commit df70bc7

Browse files
fix: logs updated on starting of a stopped container
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 87cc074 commit df70bc7

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

cmd/nerdctl/container/container_logs_test.go

Lines changed: 77 additions & 0 deletions
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"
@@ -28,6 +29,8 @@ import (
2829
"gotest.tools/v3/icmd"
2930

3031
"github.com/containerd/nerdctl/v2/pkg/testutil"
32+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
33+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
3134
)
3235

3336
func TestLogs(t *testing.T) {
@@ -255,3 +258,77 @@ func TestTailFollowRotateLogs(t *testing.T) {
255258
}
256259
assert.Equal(t, true, len(tailLogs) > linesPerFile, logRun.Stderr())
257260
}
261+
262+
func TestLogsWithStartContainer(t *testing.T) {
263+
testCase := nerdtest.Setup()
264+
testCase.Require = test.Require(test.Not(test.Windows),
265+
test.Not(nerdtest.Docker))
266+
testCase.SubTests = []*test.Case{
267+
{
268+
Description: "Test logs are directed correctly for container start of a interactive container",
269+
Setup: func(data test.Data, helpers test.Helpers) {
270+
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
271+
cmd.WithWrapper("unbuffer", "-p")
272+
cmd.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("echo foo\nexit\n")))
273+
cmd.Run(&test.Expected{
274+
ExitCode: 0,
275+
})
276+
277+
},
278+
Cleanup: func(data test.Data, helpers test.Helpers) {
279+
helpers.Anyhow("rm", "-f", data.Identifier())
280+
},
281+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
282+
cmd := helpers.Command("start", "-a", data.Identifier())
283+
cmd.WithWrapper("unbuffer", "-p")
284+
cmd.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("echo bar\nexit\n")))
285+
cmd.Run(&test.Expected{
286+
ExitCode: 0,
287+
})
288+
289+
cmd = helpers.Command("logs", data.Identifier())
290+
291+
return cmd
292+
},
293+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
294+
return &test.Expected{
295+
ExitCode: 0,
296+
Output: func(stdout string, info string, t *testing.T) {
297+
assert.Assert(t, strings.Contains(stdout, "foo"))
298+
assert.Assert(t, strings.Contains(stdout, "bar"))
299+
},
300+
}
301+
},
302+
},
303+
{
304+
Description: "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs",
305+
Setup: func(data test.Data, helpers test.Helpers) {
306+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
307+
},
308+
Cleanup: func(data test.Data, helpers test.Helpers) {
309+
helpers.Anyhow("rm", "-f", data.Identifier())
310+
},
311+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
312+
313+
helpers.Anyhow("stop", data.Identifier())
314+
initialLogs := helpers.Capture("logs", data.Identifier())
315+
initialFooCount := strings.Count(initialLogs, "foo")
316+
data.Set("initialFooCount", strconv.Itoa(initialFooCount))
317+
helpers.Ensure("start", data.Identifier())
318+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
319+
return helpers.Command("logs", data.Identifier())
320+
},
321+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
322+
return &test.Expected{
323+
ExitCode: 0,
324+
Output: func(stdout string, info string, t *testing.T) {
325+
finalLogsCount := strings.Count(stdout, "foo")
326+
initialFooCount, _ := strconv.Atoi(data.Get("initialFooCount"))
327+
assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart", info)
328+
},
329+
}
330+
},
331+
},
332+
}
333+
testCase.Run(t)
334+
}

pkg/taskutil/taskutil.go

Lines changed: 3 additions & 2 deletions
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 {

pkg/testutil/testutil_freebsd.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package testutil
1818

19-
import "fmt"
19+
import (
20+
"fmt"
21+
"io"
22+
)
2023

2124
const (
2225
CommonImage = "docker.io/knast/freebsd:13-STABLE"
@@ -40,3 +43,8 @@ func mirrorOf(s string) string {
4043
// plain mirror, NOT stargz-converted images
4144
return fmt.Sprintf("ghcr.io/stargz-containers/%s-org", s)
4245
}
46+
47+
func NewDelayOnceReader(wrapped io.Reader) io.Reader {
48+
// not implemented
49+
return nil
50+
}

pkg/testutil/testutil_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package testutil
1818

1919
import (
20+
"io"
2021
"os"
2122
"strconv"
2223
"strings"
@@ -110,3 +111,8 @@ func HyperVContainer(inspect dockercompat.Container) (bool, error) {
110111

111112
return hypervContainer, nil
112113
}
114+
115+
func NewDelayOnceReader(wrapped io.Reader) io.Reader {
116+
// not implemented
117+
return nil
118+
}

0 commit comments

Comments
 (0)