From b887c1a277c5060af7234cf4404bd626cfd88c3a Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 13 Jun 2018 17:45:27 +0100 Subject: [PATCH 1/3] Fix ExecContainer executing commands twice --- test/docker/docker_api_test.go | 8 ++++--- test/docker/docker_api_test_util.go | 36 +++++++++++++++-------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/test/docker/docker_api_test.go b/test/docker/docker_api_test.go index e93bfec..7f82800 100644 --- a/test/docker/docker_api_test.go +++ b/test/docker/docker_api_test.go @@ -329,9 +329,9 @@ func TestVolumeUnmount(t *testing.T) { defer cleanContainer(t, cli, ctr.ID) waitForReady(t, cli, ctr.ID) // Unmount the volume as root - rc, _ := execContainer(t, cli, ctr.ID, "root", []string{"umount", "-l", "-f", "/mnt/mqm"}) + rc, out := execContainer(t, cli, ctr.ID, "root", []string{"umount", "-l", "-f", "/mnt/mqm"}) if rc != 0 { - t.Fatalf("Expected umount to work with rc=0, got %v", rc) + t.Fatalf("Expected umount to work with rc=0, got %v. Output was: %s", rc, out) } time.Sleep(3 * time.Second) rc, _ = execContainer(t, cli, ctr.ID, "mqm", []string{"chkmqhealthy"}) @@ -366,7 +366,9 @@ func TestZombies(t *testing.T) { // will be adopted by PID 1, and should then be reaped when they die. _, out := execContainer(t, cli, id, "mqm", []string{"pkill", "--signal", "kill", "-c", "amqzxma0"}) if out == "0" { - t.Fatalf("Expected pkill to kill a process, got %v", out) + t.Log("Failed to kill process 'amqzxma0'") + _, out := execContainer(t, cli, id, "root", []string{"ps", "-lA"}) + t.Fatalf("Here is a list of currently running processes:\n%s", out) } time.Sleep(3 * time.Second) _, out = execContainer(t, cli, id, "mqm", []string{"bash", "-c", "ps -lA | grep '^. Z'"}) diff --git a/test/docker/docker_api_test_util.go b/test/docker/docker_api_test_util.go index dc3a989..5d71212 100644 --- a/test/docker/docker_api_test_util.go +++ b/test/docker/docker_api_test_util.go @@ -328,7 +328,6 @@ func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64 // execContainer runs a command in a running container, and returns the exit code and output func execContainer(t *testing.T, cli *client.Client, ID string, user string, cmd []string) (int, string) { -rerun: config := types.ExecConfig{ User: user, Privileged: false, @@ -357,30 +356,33 @@ rerun: } // Wait for the command to finish var exitcode int + var outputStr string for { inspect, err := cli.ContainerExecInspect(context.Background(), resp.ID) if err != nil { t.Fatal(err) } - if !inspect.Running { - exitcode = inspect.ExitCode - break + if inspect.Running { + continue } - } - buf := new(bytes.Buffer) - // Each output line has a header, which needs to be removed - _, err = stdcopy.StdCopy(buf, buf, hijack.Reader) - if err != nil { - t.Fatal(err) - } - outputStr := strings.TrimSpace(buf.String()) + exitcode = inspect.ExitCode + buf := new(bytes.Buffer) + // Each output line has a header, which needs to be removed + _, err = stdcopy.StdCopy(buf, buf, hijack.Reader) + if err != nil { + t.Fatal(err) + } - // Before we go let's just double check it did actually run because sometimes we get a "Exec command already running error" - alreadyRunningErr := regexp.MustCompile("Error: Exec command .* is already running") - if alreadyRunningErr.MatchString(outputStr) { - time.Sleep(1 * time.Second) - goto rerun + outputStr = strings.TrimSpace(buf.String()) + + // Before we go let's just double check it did actually finish running + // because sometimes we get a "Exec command already running error" + alreadyRunningErr := regexp.MustCompile("Error: Exec command .* is already running") + if alreadyRunningErr.MatchString(outputStr) { + continue + } + break } return exitcode, outputStr From 16e244427bf5ece26add1d1685c6dcaa6f23726d Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Thu, 14 Jun 2018 11:54:35 +0100 Subject: [PATCH 2/3] Fix timing in execContainer --- test/docker/docker_api_test_util.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/docker/docker_api_test_util.go b/test/docker/docker_api_test_util.go index a2ad493..fd87991 100644 --- a/test/docker/docker_api_test_util.go +++ b/test/docker/docker_api_test_util.go @@ -27,7 +27,6 @@ import ( "os" "os/exec" "path/filepath" - "regexp" "runtime" "strconv" "strings" @@ -347,7 +346,9 @@ func execContainer(t *testing.T, cli *client.Client, ID string, user string, cmd if err != nil { t.Fatal(err) } - cli.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{ + defer hijack.Close() + time.Sleep(time.Millisecond * 10) + err = cli.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{ Detach: false, Tty: false, }) @@ -376,12 +377,17 @@ func execContainer(t *testing.T, cli *client.Client, ID string, user string, cmd outputStr = strings.TrimSpace(buf.String()) - // Before we go let's just double check it did actually finish running - // because sometimes we get a "Exec command already running error" - alreadyRunningErr := regexp.MustCompile("Error: Exec command .* is already running") - if alreadyRunningErr.MatchString(outputStr) { - continue - } + /* Commented out on 14/06/2018 as it might not be needed after adding + * pause between ContainerExecAttach and ContainerExecStart. + * TODO If intermittent failures do not occur, remove and refactor. + * + * // Before we go let's just double check it did actually finish running + * // because sometimes we get a "Exec command already running error" + * alreadyRunningErr := regexp.MustCompile("Error: Exec command .* is already running") + * if alreadyRunningErr.MatchString(outputStr) { + * continue + * } + */ break } From d4d5e09a02d8fa593737464d4034e5d588e8d44a Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Thu, 14 Jun 2018 12:41:06 +0100 Subject: [PATCH 3/3] Add waitForReady to TestCorrectLicense --- test/docker/docker_api_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/docker/docker_api_test.go b/test/docker/docker_api_test.go index 7f82800..0299556 100644 --- a/test/docker/docker_api_test.go +++ b/test/docker/docker_api_test.go @@ -637,6 +637,7 @@ func TestCorrectLicense(t *testing.T) { } id := runContainer(t, cli, &containerConfig) defer cleanContainer(t, cli, id) + waitForReady(t, cli, id) rc, license := execContainer(t, cli, id, "mqm", []string{"dspmqver", "-f", "8192", "-b"}) if rc != 0 {