Skip to content

Commit f2f1e6f

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

1 file changed

Lines changed: 91 additions & 73 deletions

File tree

cmd/nerdctl/container/container_logs_test.go

Lines changed: 91 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -37,97 +37,115 @@ 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
`
4443

44+
func newLogTestCase(name string) *test.Case {
4545
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-
46+
testCase.NoParallel = true
5247
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
53-
helpers.Anyhow("rm", "-f", data.Identifier())
48+
helpers.Anyhow("rm", "-f", name)
5449
}
55-
5650
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())
51+
helpers.Ensure("run", "--quiet", "--name", name, testutil.CommonImage,
52+
"sh", "-euxc", "echo foo; echo bar;")
5953
}
54+
return testCase
55+
}
6056

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)),
57+
func TestLogs_Since1s(t *testing.T) {
58+
testCase := newLogTestCase(t.Name())
59+
testCase.SubTests = []*test.Case{{
60+
Description: "since 1s",
61+
Setup: func(data test.Data, helpers test.Helpers) {
62+
// Ensure at least 2 seconds have elapsed since the container ran,
63+
// so that --since 1s does not include the container's output.
64+
time.Sleep(2 * time.Second)
7365
},
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)),
66+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
67+
return helpers.Command("logs", "--since", "1s", t.Name())
8068
},
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)),
69+
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
70+
}}
71+
testCase.Run(t)
72+
}
73+
74+
func TestLogs_Since60s(t *testing.T) {
75+
testCase := newLogTestCase(t.Name())
76+
testCase.SubTests = []*test.Case{{
77+
Description: "since 60s",
78+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
79+
return helpers.Command("logs", "--since", "60s", t.Name())
8780
},
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)),
81+
Expected: test.Expects(0, nil, expect.Equals(expected)),
82+
}}
83+
testCase.Run(t)
84+
}
85+
86+
func TestLogs_Until60s(t *testing.T) {
87+
testCase := newLogTestCase(t.Name())
88+
testCase.SubTests = []*test.Case{{
89+
Description: "until 60s",
90+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
91+
return helpers.Command("logs", "--until", "60s", t.Name())
9992
},
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)),
93+
Expected: test.Expects(0, nil, expect.DoesNotContain(expected)),
94+
}}
95+
testCase.Run(t)
96+
}
97+
98+
func TestLogs_Until1s(t *testing.T) {
99+
testCase := newLogTestCase(t.Name())
100+
testCase.SubTests = []*test.Case{{
101+
Description: "until 1s",
102+
// Ensure at least 2 seconds have elapsed since the container ran,
103+
// so that --until 1s includes the container's output.
104+
Setup: func(data test.Data, helpers test.Helpers) {
105+
time.Sleep(2 * time.Second)
106106
},
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"))),
107+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
108+
return helpers.Command("logs", "--until", "1s", t.Name())
113109
},
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)),
110+
Expected: test.Expects(0, nil, expect.Equals(expected)),
111+
}}
112+
testCase.Run(t)
113+
}
114+
115+
func TestLogs_Follow(t *testing.T) {
116+
testCase := newLogTestCase(t.Name())
117+
testCase.SubTests = []*test.Case{{
118+
Description: "follow",
119+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
120+
return helpers.Command("logs", "-f", t.Name())
120121
},
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|)$"))),
122+
Expected: test.Expects(0, nil, expect.Equals(expected)),
123+
}}
124+
testCase.Run(t)
125+
}
126+
127+
func TestLogs_Timestamp(t *testing.T) {
128+
testCase := newLogTestCase(t.Name())
129+
testCase.SubTests = []*test.Case{{
130+
Description: "timestamp",
131+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
132+
return helpers.Command("logs", "-t", t.Name())
128133
},
129-
}
134+
Expected: test.Expects(0, nil, expect.Contains(time.Now().UTC().Format("2006-01-02"))),
135+
}}
136+
testCase.Run(t)
137+
}
130138

139+
func TestLogs_Tail1(t *testing.T) {
140+
testCase := newLogTestCase(t.Name())
141+
testCase.SubTests = []*test.Case{{
142+
Description: "tail flag",
143+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
144+
return helpers.Command("logs", "-n", "1", t.Name())
145+
},
146+
// FIXME: why?
147+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile("^(?:bar\n|)$"))),
148+
}}
131149
testCase.Run(t)
132150
}
133151

0 commit comments

Comments
 (0)