Skip to content

Commit

Permalink
fix: logs updated on starting of a stopped container
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Feb 18, 2025
1 parent 87cc074 commit ccb179d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
97 changes: 97 additions & 0 deletions cmd/nerdctl/container/container_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -28,6 +29,8 @@ import (
"gotest.tools/v3/icmd"

"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
)

func TestLogs(t *testing.T) {
Expand Down Expand Up @@ -255,3 +258,97 @@ func TestTailFollowRotateLogs(t *testing.T) {
}
assert.Equal(t, true, len(tailLogs) > linesPerFile, logRun.Stderr())
}

func TestLogsWithStartContainer(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("dual logging test is not supported on Windows")
}

testCase := nerdtest.Setup()
testCase.SubTests = []*test.Case{
{
Description: "Test logs are directed correctly for container start of a interactive container",
Require: test.Require(
test.Not(nerdtest.Docker),
test.Not(test.Windows),
),
Setup: func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
cmd.WithWrapper("unbuffer", "-p")
cmd.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("echo foo\nexit\n")))
cmd.Run(&test.Expected{
ExitCode: 0,
})

},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
cmd := helpers.Command("start", "-a", data.Identifier())
cmd.WithWrapper("unbuffer", "-p")
cmd.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("echo bar\nexit\n")))
cmd.Run(&test.Expected{
ExitCode: 0,
})

cmd = helpers.Command("logs", data.Identifier())

return cmd
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Output: test.All(
func(stdout string, info string, t *testing.T) {
assert.Assert(t, strings.Contains(stdout, "foo"))
assert.Assert(t, strings.Contains(stdout, "bar"))
},
),
}
},
},
{
Description: "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs",
Require: test.Require(
test.Not(nerdtest.Docker),
test.Not(test.Windows),
),
Setup: func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
cmd.Run(&test.Expected{
ExitCode: 0,
})
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {

helpers.Anyhow("stop", data.Identifier())
initialLogs := helpers.Capture("logs", data.Identifier())
initialFooCount := strings.Count(initialLogs, "foo")
data.Set("initialFooCount", strconv.Itoa(initialFooCount))
helpers.Anyhow("start", data.Identifier())
time.Sleep(5 * time.Second)
cmd := helpers.Command("logs", data.Identifier())
return cmd
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Output: test.All(
func(stdout string, info string, t *testing.T) {
finalLogsCount := strings.Count(stdout, "foo")
initialFooCount, _ := strconv.Atoi(data.Get("initialFooCount"))
assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart")
},
),
}
},
},
}
testCase.Run(t)
}
4 changes: 2 additions & 2 deletions pkg/taskutil/taskutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
if err != nil {
return nil, err
}
ioCreator = cio.NewCreator(cio.WithStreams(in, con, nil), cio.WithTerminal)
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, con, nil)
} else {
streams := processAttachStreamsOpt(attachStreamOpt)
ioCreator = cio.NewCreator(cio.WithStreams(streams.stdIn, streams.stdOut, streams.stdErr))
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
}

} else if flagT && flagD {
Expand Down
10 changes: 9 additions & 1 deletion pkg/testutil/testutil_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package testutil

import "fmt"
import (
"fmt"
"io"
)

const (
CommonImage = "docker.io/knast/freebsd:13-STABLE"
Expand All @@ -40,3 +43,8 @@ func mirrorOf(s string) string {
// plain mirror, NOT stargz-converted images
return fmt.Sprintf("ghcr.io/stargz-containers/%s-org", s)
}

func NewDelayOnceReader(wrapped io.Reader) io.Reader {
// not implemented
return nil
}
6 changes: 6 additions & 0 deletions pkg/testutil/testutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package testutil

import (
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -110,3 +111,8 @@ func HyperVContainer(inspect dockercompat.Container) (bool, error) {

return hypervContainer, nil
}

func NewDelayOnceReader(wrapped io.Reader) io.Reader {
// not implemented
return nil
}

0 comments on commit ccb179d

Please sign in to comment.