Skip to content

Commit 56bbdea

Browse files
committed
ci: fix test logs
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent 0b388bb commit 56bbdea

1 file changed

Lines changed: 116 additions & 73 deletions

File tree

cmd/nerdctl/container/container_logs_test.go

Lines changed: 116 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -37,97 +37,140 @@ import (
3737
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3838
)
3939

40-
func TestLogs(t *testing.T) {
41-
const expected = `foo
40+
const expected = `foo
4241
bar
4342
`
43+
const ExitSuccess = 0
4444

45+
func newLogTestCase(name string) *test.Case {
4546
testCase := nerdtest.Setup()
46-
47-
testCase.Require = nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/4782")
48-
if runtime.GOOS == "windows" {
49-
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
50-
}
51-
47+
testCase.NoParallel = true
5248
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
53-
helpers.Anyhow("rm", "-f", data.Identifier())
49+
helpers.Anyhow("rm", "-f", name)
5450
}
55-
5651
testCase.Setup = func(data test.Data, helpers test.Helpers) {
57-
helpers.Ensure("run", "--quiet", "--name", data.Identifier(), testutil.CommonImage, "sh", "-euxc", "echo foo; echo bar;")
58-
data.Labels().Set("cID", data.Identifier())
52+
helpers.Ensure("run", "--quiet", "--name", name, testutil.CommonImage,
53+
"sh", "-euxc", "echo foo; echo bar;")
5954
}
55+
return testCase
56+
}
6057

61-
testCase.SubTests = []*test.Case{
62-
{
63-
Description: "since 1s",
64-
Setup: func(data test.Data, helpers test.Helpers) {
65-
// Ensure at least 2 seconds have elapsed since the container ran,
66-
// so that --since 1s does not include the container's output.
67-
time.Sleep(2 * time.Second)
68-
},
69-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
70-
return helpers.Command("logs", "--since", "1s", data.Labels().Get("cID"))
71-
},
72-
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
73-
},
74-
{
75-
Description: "since 60s",
76-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
77-
return helpers.Command("logs", "--since", "60s", data.Labels().Get("cID"))
78-
},
79-
Expected: test.Expects(0, nil, expect.Equals(expected)),
58+
func TestLogs_Since1s(t *testing.T) {
59+
testCase := newLogTestCase(t.Name())
60+
if runtime.GOOS == "windows" {
61+
// Logging seems broken on windows.
62+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
63+
}
64+
testCase.SubTests = []*test.Case{{
65+
Description: "since 1s",
66+
Setup: func(data test.Data, helpers test.Helpers) {
67+
// Ensure at least 2 seconds have elapsed since the container ran,
68+
// so that --since 1s does not include the container's output.
69+
time.Sleep(2 * time.Second)
8070
},
81-
{
82-
Description: "until 60s",
83-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
84-
return helpers.Command("logs", "--until", "60s", data.Labels().Get("cID"))
85-
},
86-
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
71+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
72+
return helpers.Command("logs", "--since", "1s", t.Name())
8773
},
88-
{
89-
Description: "until 1s",
90-
Setup: func(data test.Data, helpers test.Helpers) {
91-
// Ensure at least 2 seconds have elapsed since the container ran,
92-
// so that --until 1s includes the container's output.
93-
time.Sleep(2 * time.Second)
94-
},
95-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
96-
return helpers.Command("logs", "--until", "1s", data.Labels().Get("cID"))
97-
},
98-
Expected: test.Expects(0, nil, expect.Equals(expected)),
74+
Expected: test.Expects(ExitSuccess, nil, expect.DoesNotContain(expected)),
75+
}}
76+
testCase.Run(t)
77+
}
78+
79+
func TestLogs_Since60s(t *testing.T) {
80+
testCase := newLogTestCase(t.Name())
81+
if runtime.GOOS == "windows" {
82+
// Logging seems broken on windows.
83+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
84+
}
85+
testCase.SubTests = []*test.Case{{
86+
Description: "since 60s",
87+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
88+
return helpers.Command("logs", "--since", "60s", t.Name())
9989
},
100-
{
101-
Description: "follow",
102-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
103-
return helpers.Command("logs", "-f", data.Labels().Get("cID"))
104-
},
105-
Expected: test.Expects(0, nil, expect.Equals(expected)),
90+
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
91+
}}
92+
testCase.Run(t)
93+
}
94+
95+
func TestLogs_Until60s(t *testing.T) {
96+
testCase := newLogTestCase(t.Name())
97+
if runtime.GOOS == "windows" {
98+
// Logging seems broken on windows.
99+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
100+
}
101+
testCase.SubTests = []*test.Case{{
102+
Description: "until 60s",
103+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
104+
return helpers.Command("logs", "--until", "60s", t.Name())
106105
},
107-
{
108-
Description: "timestamp",
109-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
110-
return helpers.Command("logs", "-t", data.Labels().Get("cID"))
111-
},
112-
Expected: test.Expects(0, nil, expect.Contains(time.Now().UTC().Format("2006-01-02"))),
106+
Expected: test.Expects(ExitSuccess, nil, expect.DoesNotContain(expected)),
107+
}}
108+
testCase.Run(t)
109+
}
110+
111+
func TestLogs_Until1s(t *testing.T) {
112+
testCase := newLogTestCase(t.Name())
113+
if runtime.GOOS == "windows" {
114+
// Logging seems broken on windows.
115+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
116+
}
117+
testCase.SubTests = []*test.Case{{
118+
Description: "until 1s",
119+
// Ensure at least 2 seconds have elapsed since the container ran,
120+
// so that --until 1s includes the container's output.
121+
Setup: func(data test.Data, helpers test.Helpers) {
122+
time.Sleep(2 * time.Second)
113123
},
114-
{
115-
Description: "tail flag",
116-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
117-
return helpers.Command("logs", "-n", "all", data.Labels().Get("cID"))
118-
},
119-
Expected: test.Expects(0, nil, expect.Equals(expected)),
124+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
125+
return helpers.Command("logs", "--until", "1s", t.Name())
120126
},
121-
{
122-
Description: "tail flag",
123-
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
124-
return helpers.Command("logs", "-n", "1", data.Labels().Get("cID"))
125-
},
126-
// FIXME: why?
127-
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile("^(?:bar\n|)$"))),
127+
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
128+
}}
129+
testCase.Run(t)
130+
}
131+
132+
func TestLogs_Follow(t *testing.T) {
133+
testCase := newLogTestCase(t.Name())
134+
if runtime.GOOS == "windows" {
135+
// Logging seems broken on windows.
136+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
137+
}
138+
testCase.SubTests = []*test.Case{{
139+
Description: "follow",
140+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
141+
return helpers.Command("logs", "-f", t.Name())
128142
},
143+
Expected: test.Expects(ExitSuccess, nil, expect.Equals(expected)),
144+
}}
145+
testCase.Run(t)
146+
}
147+
148+
func TestLogs_Timestamp(t *testing.T) {
149+
testCase := newLogTestCase(t.Name())
150+
if runtime.GOOS == "windows" {
151+
// Logging seems broken on windows.
152+
testCase.Require = nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/4237")
129153
}
154+
testCase.SubTests = []*test.Case{{
155+
Description: "timestamp",
156+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
157+
return helpers.Command("logs", "-t", t.Name())
158+
},
159+
Expected: test.Expects(ExitSuccess, nil, expect.Contains(time.Now().UTC().Format("2006-01-02"))),
160+
}}
161+
testCase.Run(t)
162+
}
130163

164+
func TestLogs_Tail1(t *testing.T) {
165+
testCase := newLogTestCase(t.Name())
166+
testCase.SubTests = []*test.Case{{
167+
Description: "tail flag",
168+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
169+
return helpers.Command("logs", "-n", "1", t.Name())
170+
},
171+
// FIXME: why?
172+
Expected: test.Expects(ExitSuccess, nil, expect.Match(regexp.MustCompile("^(?:bar\n|)$"))),
173+
}}
131174
testCase.Run(t)
132175
}
133176

0 commit comments

Comments
 (0)