From b6b72952e4448d4fa471a9a35d7cb94f0c58a937 Mon Sep 17 00:00:00 2001 From: Arthur Barr Date: Mon, 18 Dec 2017 12:00:19 +0000 Subject: [PATCH] Add TestMQSC --- test/docker/docker_api_test.go | 40 ++++++++++++++++++---- test/docker/docker_api_test_util.go | 52 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/test/docker/docker_api_test.go b/test/docker/docker_api_test.go index a85022d..15aad29 100644 --- a/test/docker/docker_api_test.go +++ b/test/docker/docker_api_test.go @@ -17,7 +17,7 @@ package main import ( "context" - "strconv" + "fmt" "strings" "testing" "time" @@ -323,13 +323,41 @@ func TestZombies(t *testing.T) { t.Fatalf("Expected pkill to kill a process, got %v", out) } time.Sleep(3 * time.Second) - // Create a zombie process for up to ten seconds - out = execContainerWithOutput(t, cli, id, "mqm", []string{"bash", "-c", "ps -lA | grep '^. Z' | wc -l"}) - count, err := strconv.Atoi(out) + out = execContainerWithOutput(t, cli, id, "mqm", []string{"bash", "-c", "ps -lA | grep '^. Z'"}) + if out != "" { + count := strings.Count(out, "\n") + 1 + t.Errorf("Expected zombies=0, got %v", count) + t.Error(out) + t.Fail() + } +} + +// TestMQSC creates a new image with an MQSC file in, starts a container based +// on that image, and checks that the MQSC has been applied correctly. +func TestMQSC(t *testing.T) { + t.Parallel() + cli, err := client.NewEnvClient() if err != nil { t.Fatal(err) } - if count != 0 { - t.Fatalf("Expected zombies=0, got %v", count) + var files = []struct { + Name, Body string + }{ + {"Dockerfile", fmt.Sprintf("FROM %v\nADD test.mqsc /etc/mqm/", imageName())}, + {"test.mqsc", "DEFINE QLOCAL(test)"}, + } + tag := createImage(t, cli, files) + defer deleteImage(t, cli, tag) + + containerConfig := container.Config{ + Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"}, + Image: tag, + } + id := runContainer(t, cli, &containerConfig) + defer cleanContainer(t, cli, id) + waitForReady(t, cli, id) + rc := execContainerWithExitCode(t, cli, id, "mqm", []string{"bash", "-c", "echo 'DISPLAY QLOCAL(test)' | runmqsc"}) + if rc != 0 { + t.Fatalf("Expected runmqsc to exit with rc=0, got %v", rc) } } diff --git a/test/docker/docker_api_test_util.go b/test/docker/docker_api_test_util.go index cd0ec71..e1ad2a7 100644 --- a/test/docker/docker_api_test_util.go +++ b/test/docker/docker_api_test_util.go @@ -16,6 +16,7 @@ limitations under the License. package main import ( + "archive/tar" "bytes" "context" "io/ioutil" @@ -338,3 +339,54 @@ func inspectLogs(t *testing.T, cli *client.Client, ID string) string { } return buf.String() } + +// generateTAR creates a TAR-formatted []byte, with the specified files included. +func generateTAR(t *testing.T, files []struct{ Name, Body string }) []byte { + buf := new(bytes.Buffer) + tw := tar.NewWriter(buf) + for _, file := range files { + hdr := &tar.Header{ + Name: file.Name, + Mode: 0600, + Size: int64(len(file.Body)), + } + err := tw.WriteHeader(hdr) + if err != nil { + t.Fatal(err) + } + _, err = tw.Write([]byte(file.Body)) + if err != nil { + t.Fatal(err) + } + } + err := tw.Close() + if err != nil { + t.Fatal(err) + } + return buf.Bytes() +} + +// createImage creates a new Docker image with the specified files included. +func createImage(t *testing.T, cli *client.Client, files []struct{ Name, Body string }) string { + r := bytes.NewReader(generateTAR(t, files)) + tag := strings.ToLower(t.Name()) + buildOptions := types.ImageBuildOptions{ + Context: r, + Tags: []string{tag}, + } + resp, err := cli.ImageBuild(context.Background(), r, buildOptions) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + // Sleep for two seconds, to try and prevent "No such image" errors + time.Sleep(2 * time.Second) + return tag +} + +// deleteImage deletes a Docker image +func deleteImage(t *testing.T, cli *client.Client, id string) { + cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{ + Force: true, + }) +}