integration-cli: DockerCLILogsSuite: replace dockerCmd and waitRun

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-27 18:34:10 +02:00
parent 8e2e1f3d28
commit d63cab5193
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -47,22 +47,20 @@ func (s *DockerCLILogsSuite) TestLogsContainerMuchBiggerThanPage(c *testing.T) {
} }
func testLogsContainerPagination(c *testing.T, testLen int) { func testLogsContainerPagination(c *testing.T, testLen int) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)) id := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)).Stdout()
id := strings.TrimSpace(out) id = strings.TrimSpace(id)
dockerCmd(c, "wait", id) cli.DockerCmd(c, "wait", id)
out, _ = dockerCmd(c, "logs", id) out := cli.DockerCmd(c, "logs", id).Combined()
assert.Equal(c, len(out), testLen+1) assert.Equal(c, len(out), testLen+1)
} }
func (s *DockerCLILogsSuite) TestLogsTimestamps(c *testing.T) { func (s *DockerCLILogsSuite) TestLogsTimestamps(c *testing.T) {
testLen := 100 testLen := 100
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen)) id := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen)).Stdout()
id = strings.TrimSpace(id)
id := strings.TrimSpace(out) cli.DockerCmd(c, "wait", id)
dockerCmd(c, "wait", id)
out, _ = dockerCmd(c, "logs", "-t", id)
out := cli.DockerCmd(c, "logs", "-t", id).Combined()
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
assert.Equal(c, len(lines), testLen+1) assert.Equal(c, len(lines), testLen+1)
@ -138,7 +136,7 @@ func (s *DockerCLILogsSuite) TestLogsTail(c *testing.T) {
} }
func (s *DockerCLILogsSuite) TestLogsFollowStopped(c *testing.T) { func (s *DockerCLILogsSuite) TestLogsFollowStopped(c *testing.T) {
dockerCmd(c, "run", "--name=test", "busybox", "echo", "hello") cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "hello")
id := getIDByName(c, "test") id := getIDByName(c, "test")
logsCmd := exec.Command(dockerBinary, "logs", "-f", id) logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
@ -160,14 +158,14 @@ func (s *DockerCLILogsSuite) TestLogsFollowStopped(c *testing.T) {
func (s *DockerCLILogsSuite) TestLogsSince(c *testing.T) { func (s *DockerCLILogsSuite) TestLogsSince(c *testing.T) {
name := "testlogssince" name := "testlogssince"
dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done") cli.DockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")
out, _ := dockerCmd(c, "logs", "-t", name) out := cli.DockerCmd(c, "logs", "-t", name).Combined()
log2Line := strings.Split(strings.Split(out, "\n")[1], " ") log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
assert.NilError(c, err) assert.NilError(c, err)
since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name) out = cli.DockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name).Combined()
// Skip 2 seconds // Skip 2 seconds
unexpected := []string{"log1", "log2"} unexpected := []string{"log1", "log2"}
@ -197,14 +195,14 @@ func (s *DockerCLILogsSuite) TestLogsSinceFutureFollow(c *testing.T) {
// TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now. // TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now.
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
name := "testlogssincefuturefollow" name := "testlogssincefuturefollow"
dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`) cli.DockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`)
// Extract one timestamp from the log file to give us a starting point for // Extract one timestamp from the log file to give us a starting point for
// our `--since` argument. Because the log producer runs in the background, // our `--since` argument. Because the log producer runs in the background,
// we need to check repeatedly for some output to be produced. // we need to check repeatedly for some output to be produced.
var timestamp string var timestamp string
for i := 0; i != 100 && timestamp == ""; i++ { for i := 0; i != 100 && timestamp == ""; i++ {
if out, _ := dockerCmd(c, "logs", "-t", name); out == "" { if out := cli.DockerCmd(c, "logs", "-t", name).Combined(); out == "" {
time.Sleep(time.Millisecond * 100) // Retry time.Sleep(time.Millisecond * 100) // Retry
} else { } else {
timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0] timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0]
@ -216,7 +214,7 @@ func (s *DockerCLILogsSuite) TestLogsSinceFutureFollow(c *testing.T) {
assert.NilError(c, err) assert.NilError(c, err)
since := t.Unix() + 2 since := t.Unix() + 2
out, _ := dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name) out := cli.DockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name).Combined()
assert.Assert(c, len(out) != 0, "cannot read from empty log") assert.Assert(c, len(out) != 0, "cannot read from empty log")
lines := strings.Split(strings.TrimSpace(out), "\n") lines := strings.Split(strings.TrimSpace(out), "\n")
for _, v := range lines { for _, v := range lines {
@ -231,14 +229,13 @@ func (s *DockerCLILogsSuite) TestLogsFollowSlowStdoutConsumer(c *testing.T) {
// TODO Windows: Fix this test for TP5. // TODO Windows: Fix this test for TP5.
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
expected := 150000 expected := 150000
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", fmt.Sprintf("usleep 600000; yes X | head -c %d", expected)) id := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", fmt.Sprintf("usleep 600000; yes X | head -c %d", expected)).Stdout()
id = strings.TrimSpace(id)
id := strings.TrimSpace(out)
stopSlowRead := make(chan bool) stopSlowRead := make(chan bool)
go func() { go func() {
dockerCmd(c, "wait", id) cli.DockerCmd(c, "wait", id)
stopSlowRead <- true stopSlowRead <- true
}() }()
@ -389,8 +386,8 @@ func (s *DockerCLILogsSuite) TestLogsCLIContainerNotFound(c *testing.T) {
} }
func (s *DockerCLILogsSuite) TestLogsWithDetails(c *testing.T) { func (s *DockerCLILogsSuite) TestLogsWithDetails(c *testing.T) {
dockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello") cli.DockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello")
out, _ := dockerCmd(c, "logs", "--details", "--timestamps", "test") out := cli.DockerCmd(c, "logs", "--details", "--timestamps", "test").Combined()
logFields := strings.Fields(strings.TrimSpace(out)) logFields := strings.Fields(strings.TrimSpace(out))
assert.Equal(c, len(logFields), 3, out) assert.Equal(c, len(logFields), 3, out)