Skip to content

Commit 72193e6

Browse files
fix: logs updated on starting of a stopped container
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 6f608da commit 72193e6

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

cmd/nerdctl/container/container_logs_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package container
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"os/exec"
2223
"runtime"
24+
"strconv"
2325
"strings"
2426
"testing"
2527
"time"
@@ -278,3 +280,80 @@ func TestTailFollowRotateLogs(t *testing.T) {
278280
}
279281
assert.Equal(t, true, len(tailLogs) > linesPerFile, logRun.Stderr())
280282
}
283+
284+
func TestLogsWithStartContainer(t *testing.T) {
285+
testCase := nerdtest.Setup()
286+
testCase.Require = test.Require(test.Not(test.Windows),
287+
test.Not(nerdtest.Docker))
288+
testCase.SubTests = []*test.Case{
289+
{
290+
Description: "Test logs are directed correctly for container start of a interactive container",
291+
Setup: func(data test.Data, helpers test.Helpers) {
292+
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
293+
cmd.WithPseudoTTY(func(f *os.File) error {
294+
_, err := f.WriteString("echo foo\nexit\n")
295+
return err
296+
})
297+
cmd.Run(&test.Expected{
298+
ExitCode: 0,
299+
})
300+
301+
},
302+
Cleanup: func(data test.Data, helpers test.Helpers) {
303+
helpers.Anyhow("rm", "-f", data.Identifier())
304+
},
305+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
306+
cmd := helpers.Command("start", "-a", data.Identifier())
307+
cmd.WithPseudoTTY(func(f *os.File) error {
308+
_, err := f.WriteString("echo bar\nexit\n")
309+
return err
310+
})
311+
cmd.Run(&test.Expected{
312+
ExitCode: 0,
313+
})
314+
cmd = helpers.Command("logs", data.Identifier())
315+
316+
return cmd
317+
},
318+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
319+
return &test.Expected{
320+
ExitCode: 0,
321+
Output: func(stdout string, info string, t *testing.T) {
322+
assert.Assert(t, strings.Contains(stdout, "foo"))
323+
assert.Assert(t, strings.Contains(stdout, "bar"))
324+
},
325+
}
326+
},
327+
},
328+
{
329+
Description: "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs",
330+
Setup: func(data test.Data, helpers test.Helpers) {
331+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
332+
},
333+
Cleanup: func(data test.Data, helpers test.Helpers) {
334+
helpers.Anyhow("rm", "-f", data.Identifier())
335+
},
336+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
337+
338+
helpers.Anyhow("stop", data.Identifier())
339+
initialLogs := helpers.Capture("logs", data.Identifier())
340+
initialFooCount := strings.Count(initialLogs, "foo")
341+
data.Set("initialFooCount", strconv.Itoa(initialFooCount))
342+
helpers.Ensure("start", data.Identifier())
343+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
344+
return helpers.Command("logs", data.Identifier())
345+
},
346+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
347+
return &test.Expected{
348+
ExitCode: 0,
349+
Output: func(stdout string, info string, t *testing.T) {
350+
finalLogsCount := strings.Count(stdout, "foo")
351+
initialFooCount, _ := strconv.Atoi(data.Get("initialFooCount"))
352+
assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart", info)
353+
},
354+
}
355+
},
356+
},
357+
}
358+
testCase.Run(t)
359+
}

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 {

pkg/testutil/testutil_freebsd.go

+9-1
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

+6
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)