Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 92 additions & 73 deletions cmd/nerdctl/container/container_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,97 +37,116 @@ import (
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

func TestLogs(t *testing.T) {
const expected = `foo
const expected = `foo
bar
`
const ExitSuccess = 0

func newLogTestCase(name string) *test.Case {
testCase := nerdtest.Setup()

testCase.Require = nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/4782")
if runtime.GOOS == "windows" {
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
}

testCase.NoParallel = true
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rm", "-f", name)
}

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "--quiet", "--name", data.Identifier(), testutil.CommonImage, "sh", "-euxc", "echo foo; echo bar;")
data.Labels().Set("cID", data.Identifier())
helpers.Ensure("run", "--quiet", "--name", name, testutil.CommonImage,
"sh", "-euxc", "echo foo; echo bar;")
}
return testCase
}

testCase.SubTests = []*test.Case{
{
Description: "since 1s",
Setup: func(data test.Data, helpers test.Helpers) {
// Ensure at least 2 seconds have elapsed since the container ran,
// so that --since 1s does not include the container's output.
time.Sleep(2 * time.Second)
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--since", "1s", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
func TestLogs_Since1s(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "since 1s",
Setup: func(data test.Data, helpers test.Helpers) {
// Ensure at least 2 seconds have elapsed since the container ran,
// so that --since 1s does not include the container's output.
time.Sleep(2 * time.Second)
},
{
Description: "since 60s",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--since", "60s", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.Equals(expected)),
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--since", "1s", t.Name())
},
{
Description: "until 60s",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--until", "60s", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
Expected: test.Expects(ExitSuccess, nil, expect.DoesNotContain(expected)),
}}
testCase.Run(t)
}

func TestLogs_Since60s(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "since 60s",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--since", "60s", t.Name())
},
{
Description: "until 1s",
Setup: func(data test.Data, helpers test.Helpers) {
// Ensure at least 2 seconds have elapsed since the container ran,
// so that --until 1s includes the container's output.
time.Sleep(2 * time.Second)
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--until", "1s", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.Equals(expected)),
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
}}
testCase.Run(t)
}

func TestLogs_Until60s(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "until 60s",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--until", "60s", t.Name())
},
{
Description: "follow",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-f", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.Equals(expected)),
Expected: test.Expects(ExitSuccess, nil, expect.DoesNotContain(expected)),
}}
testCase.Run(t)
}

func TestLogs_Until1s(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "until 1s",
// Ensure at least 2 seconds have elapsed since the container ran,
// so that --until 1s includes the container's output.
Setup: func(data test.Data, helpers test.Helpers) {
time.Sleep(2 * time.Second)
},
{
Description: "timestamp",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-t", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.Contains(time.Now().UTC().Format("2006-01-02"))),
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--until", "1s", t.Name())
},
{
Description: "tail flag",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-n", "all", data.Labels().Get("cID"))
},
Expected: test.Expects(0, nil, expect.Equals(expected)),
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
}}
testCase.Run(t)
}

func TestLogs_Follow(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "follow",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-f", t.Name())
},
{
Description: "tail flag",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-n", "1", data.Labels().Get("cID"))
},
// FIXME: why?
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile("^(?:bar\n|)$"))),
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
}}
testCase.Run(t)
}

func TestLogs_Timestamp(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "timestamp",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-t", t.Name())
},
}
Expected: test.Expects(ExitSuccess, nil, expect.Contains(time.Now().UTC().Format("2006-01-02"))),
}}
testCase.Run(t)
}

func TestLogs_Tail1(t *testing.T) {
testCase := newLogTestCase(t.Name())
testCase.SubTests = []*test.Case{{
Description: "tail flag",
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "-n", "1", t.Name())
},
// FIXME: why?
Expected: test.Expects(ExitSuccess, nil, expect.Match(regexp.MustCompile("^(?:bar\n|)$"))),
}}
testCase.Run(t)
}

Expand Down
Loading