Dynamically set test image name

This commit is contained in:
Arthur Barr
2017-09-26 12:36:20 +01:00
parent fbcc1e0e25
commit bf9a9b8cb5
12 changed files with 33 additions and 567 deletions

6
.gitignore vendored
View File

@@ -1,8 +1,6 @@
.DS_Store
test/docker-advancedserver/coverage
test/docker-advancedserver/vendor
test/docker-devserver/coverage
test/docker-devserver/vendor
test/docker/coverage
test/docker/vendor
test/kubernetes/vendor
build
coverage

View File

@@ -13,9 +13,11 @@
# limitations under the License.
BUILD_SERVER_CONTAINER=build-server
export GOARCH=amd64
export GOARCH ?= amd64
# Don't set GOOS globally, so that tests can be run locally
DOCKER_TAG_ARCH=x86_64
DOCKER_TAG_ARCH ?= x86_64
DOCKER_IMAGE_DEVSERVER = mq-devserver:latest-$(DOCKER_TAG_ARCH)
DOCKER_IMAGE_ADVANCEDSERVER = mq-advancedserver:latest-$(DOCKER_TAG_ARCH)
.PHONY: default
default: build-devserver test
@@ -44,8 +46,7 @@ downloads: downloads/mqadv_dev903_ubuntu_x86-64.tar.gz
.PHONY: deps
deps:
glide install --strip-vendor
cd test/docker-advancedserver && dep ensure
cd test/docker-devserver && dep ensure
cd test/docker && dep ensure
cd test/kubernetes && dep ensure
build/runmqserver:
@@ -71,12 +72,12 @@ build-cov:
.PHONY: test-advancedserver
test-advancedserver: build
cd pkg/name && go test
cd test/docker-advancedserver && go test
cd test/docker && TEST_IMAGE=$(DOCKER_IMAGE_ADVANCEDSERVER) go test
.PHONY: test-devserver
test-devserver: build
cd pkg/name && go test
cd test/docker-devserver && go test
cd test/docker && TEST_IMAGE=$(DOCKER_IMAGE_DEVSERVER) go test
define docker-build-mq
# Create a temporary network to use for the build
@@ -108,13 +109,13 @@ endef
.PHONY: build-advancedserver
build-advancedserver: build downloads/CNJR7ML.tar.gz
$(call docker-build-mq,mq-advancedserver:latest-$(DOCKER_TAG_ARCH),Dockerfile-server,CNJR7ML.tar.gz,"4486e8c4cc9146fd9b3ce1f14a2dfc5b","IBM MQ Advanced","9.0.3")
docker tag mq-advancedserver:latest-$(DOCKER_TAG_ARCH) mq-advancedserver:9.0.3-$(DOCKER_TAG_ARCH)
$(call docker-build-mq,$(DOCKER_IMAGE_ADVANCEDSERVER),Dockerfile-server,CNJR7ML.tar.gz,"4486e8c4cc9146fd9b3ce1f14a2dfc5b","IBM MQ Advanced","9.0.3")
docker tag $(DOCKER_IMAGE_ADVANCEDSERVER) mq-advancedserver:9.0.3-$(DOCKER_TAG_ARCH)
.PHONY: build-devserver
build-devserver: build downloads/mqadv_dev903_ubuntu_x86-64.tar.gz
$(call docker-build-mq,mq-devserver:latest-$(DOCKER_TAG_ARCH),Dockerfile-server,mqadv_dev903_ubuntu_x86-64.tar.gz,"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)","9.0.3")
docker tag mq-devserver:latest-$(DOCKER_TAG_ARCH) mq-devserver:9.0.3-$(DOCKER_TAG_ARCH)
$(call docker-build-mq,$(DOCKER_IMAGE_DEVSERVER),Dockerfile-server,mqadv_dev903_ubuntu_x86-64.tar.gz,"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)","9.0.3")
docker tag $(DOCKER_IMAGE_DEVSERVER) mq-devserver:9.0.3-$(DOCKER_TAG_ARCH)
# .PHONY: build-server
# build-server: build downloads/CNJR7ML.tar.gz

View File

@@ -1,178 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"strings"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
const image string = "mq-advancedserver:latest-x86_64"
func TestLicenseNotSet(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
rc := waitForContainer(t, cli, id, 5)
if rc != 1 {
t.Errorf("Expected rc=1, got rc=%v", rc)
}
}
func TestLicenseView(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=view"},
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
rc := waitForContainer(t, cli, id, 5)
if rc != 1 {
t.Errorf("Expected rc=1, got rc=%v", rc)
}
l := inspectLogs(t, cli, id)
const s string = "terms"
if !strings.Contains(l, s) {
t.Errorf("Expected license string to contain \"%v\", got %v", s, l)
}
}
func TestGoldenPath(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
//ExposedPorts: ports,
ExposedPorts: nat.PortSet{
"1414/tcp": struct{}{},
},
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
waitForReady(t, cli, id)
}
func utilTestNoQueueManagerName(t *testing.T, hostName string, expectedName string) {
search := "QMNAME(" + expectedName + ")"
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept"},
Hostname: hostName,
ExposedPorts: nat.PortSet{
"1414/tcp": struct{}{},
},
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
waitForReady(t, cli, id)
_, out := execContainer(t, cli, id, []string{"dspmq"})
if !strings.Contains(out, search) {
t.Errorf("Expected result of running dspmq to contain name=%v, got name=%v", search, out)
}
}
func TestNoQueueManagerName(t *testing.T) {
utilTestNoQueueManagerName(t, "test", "test")
}
func TestNoQueueManagerNameInvalidHostname(t *testing.T) {
utilTestNoQueueManagerName(t, "test-1", "test1")
}
// TestWithVolume runs a container with a Docker volume, then removes that
// container and starts a new one with same volume.
func TestWithVolume(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
vol := createVolume(t, cli)
defer removeVolume(t, cli, vol.Name)
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
}
hostConfig := container.HostConfig{
Binds: []string{
coverageBind(t),
//"coverage:/var/coverage",
vol.Name + ":/mnt/mqm",
},
}
networkingConfig := network.NetworkingConfig{}
ctr, err := cli.ContainerCreate(context.Background(), &containerConfig, &hostConfig, &networkingConfig, t.Name())
if err != nil {
t.Fatal(err)
}
startContainer(t, cli, ctr.ID)
// TODO: If this test gets an error waiting for readiness, the first container might not get cleaned up
waitForReady(t, cli, ctr.ID)
// Delete the first container
cleanContainer(t, cli, ctr.ID)
// Start a new container with the same volume
ctr2, err := cli.ContainerCreate(context.Background(), &containerConfig, &hostConfig, &networkingConfig, t.Name())
if err != nil {
t.Fatal(err)
}
defer cleanContainer(t, cli, ctr2.ID)
startContainer(t, cli, ctr2.ID)
waitForReady(t, cli, ctr2.ID)
}
func TestNoVolumeWithRestart(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
//ExposedPorts: ports,
ExposedPorts: nat.PortSet{
"1414/tcp": struct{}{},
},
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
waitForReady(t, cli, id)
stopContainer(t, cli, id)
startContainer(t, cli, id)
waitForReady(t, cli, id)
}

View File

@@ -1,57 +0,0 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/Microsoft/go-winio"
packages = ["."]
revision = "78439966b38d69bf38227fbf57ac8a6fee70f69a"
version = "v0.4.5"
[[projects]]
name = "github.com/docker/distribution"
packages = ["digest","reference"]
revision = "48294d928ced5dd9b378f7fd7c6f5da3ff3f2c89"
version = "v2.6.2"
[[projects]]
name = "github.com/docker/docker"
packages = ["api/types","api/types/blkiodev","api/types/container","api/types/events","api/types/filters","api/types/mount","api/types/network","api/types/reference","api/types/registry","api/types/strslice","api/types/swarm","api/types/time","api/types/versions","api/types/volume","client","pkg/tlsconfig"]
revision = "092cba3727bb9b4a2f0e922cd6c0f93ea270e363"
version = "v1.13.1"
[[projects]]
name = "github.com/docker/go-connections"
packages = ["nat","sockets","tlsconfig"]
revision = "3ede32e2033de7505e6500d6c868c2b9ed9f169d"
version = "v0.3.0"
[[projects]]
name = "github.com/docker/go-units"
packages = ["."]
revision = "0dadbb0345b35ec7ef35e228dabb8de89a65bf52"
version = "v0.3.2"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context","context/ctxhttp","proxy"]
revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = ["windows"]
revision = "9aade4d3a3b7e6d876cd3823ad20ec45fc035402"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "bf6ada0adb63f691f18ca1b3b95f55be8ec360be22928ca9e63ba47846c5687d"
solver-name = "gps-cdcl"
solver-version = 1

View File

@@ -1,20 +0,0 @@
# © Copyright IBM Corporation 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[[constraint]]
name = "github.com/docker/docker"
version = "^1.12"
[[constraint]]
name = "github.com/docker/go-connections"

View File

@@ -1,265 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"context"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
func coverageBind(t *testing.T) string {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return filepath.Join(dir, "coverage") + ":/var/coverage"
}
func cleanContainer(t *testing.T, cli *client.Client, ID string) {
i, err := cli.ContainerInspect(context.Background(), ID)
if err == nil {
// Log the results and continue
t.Logf("Inspected container %v: %#v", ID, i)
}
t.Logf("Killing container: %v", ID)
// Kill the container. This allows the coverage output to be generated.
err = cli.ContainerKill(context.Background(), ID, "SIGTERM")
if err != nil {
// Just log the error and continue
t.Log(err)
}
//waitForContainer(t, cli, ID, 20, container.WaitConditionNotRunning)
// TODO: This is probably no longer necessary
time.Sleep(20 * time.Second)
// Log the container output for any container we're about to delete
t.Logf("Console log from container %v:\n%v", ID, inspectLogs(t, cli, ID))
t.Logf("Removing container: %s", ID)
opts := types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
err = cli.ContainerRemove(context.Background(), ID, opts)
if err != nil {
t.Error(err)
}
}
func runContainer(t *testing.T, cli *client.Client, containerConfig *container.Config) string {
t.Logf("Running container")
hostConfig := container.HostConfig{
PortBindings: nat.PortMap{
"1414/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "1414",
},
},
},
}
networkingConfig := network.NetworkingConfig{}
ctr, err := cli.ContainerCreate(context.Background(), containerConfig, &hostConfig, &networkingConfig, t.Name())
if err != nil {
t.Fatal(err)
}
startContainer(t, cli, ctr.ID)
return ctr.ID
}
func startContainer(t *testing.T, cli *client.Client, ID string) {
t.Logf("Starting container: %v", ID)
startOptions := types.ContainerStartOptions{}
err := cli.ContainerStart(context.Background(), ID, startOptions)
if err != nil {
t.Fatal(err)
}
}
func stopContainer(t *testing.T, cli *client.Client, ID string) {
t.Logf("Stopping container: %v", ID)
timeout := 10 * time.Second
err := cli.ContainerStop(context.Background(), ID, &timeout) //Duration(20)*time.Second)
if err != nil {
t.Fatal(err)
}
}
// waitForContainer waits until a container has exited
func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64) int64 {
//ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
//defer cancel()
rc, err := cli.ContainerWait(context.Background(), ID)
// err := <-errC
if err != nil {
t.Fatal(err)
}
// wait := <-waitC
return rc
}
// execContainer runs the specified command inside the container, returning the
// exit code and the stdout/stderr string.
func execContainer(t *testing.T, cli *client.Client, ID string, cmd []string) (int, string) {
config := types.ExecConfig{
User: "mqm",
Privileged: false,
Tty: false,
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Detach: false,
Cmd: cmd,
}
resp, err := cli.ContainerExecCreate(context.Background(), ID, config)
if err != nil {
t.Fatal(err)
}
hijack, err := cli.ContainerExecAttach(context.Background(), resp.ID, config)
if err != nil {
t.Fatal(err)
}
cli.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{
Detach: false,
Tty: false,
})
if err != nil {
t.Fatal(err)
}
inspect, err := cli.ContainerExecInspect(context.Background(), resp.ID)
if err != nil {
t.Fatal(err)
}
// TODO: For some reason, each line seems to start with an extra, random character
buf, err := ioutil.ReadAll(hijack.Reader)
if err != nil {
t.Fatal(err)
}
hijack.Close()
return inspect.ExitCode, string(buf)
}
func waitForReady(t *testing.T, cli *client.Client, ID string) {
for {
resp, err := cli.ContainerExecCreate(context.Background(), ID, types.ExecConfig{
User: "mqm",
Privileged: false,
Tty: false,
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Detach: false,
Cmd: []string{"chkmqready"},
})
if err != nil {
t.Fatal(err)
}
cli.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{
Detach: false,
Tty: false,
})
if err != nil {
t.Fatal(err)
}
inspect, err := cli.ContainerExecInspect(context.Background(), resp.ID)
if err != nil {
t.Fatal(err)
}
if inspect.ExitCode == 0 {
t.Log("MQ is ready")
return
}
}
}
func getIPAddress(t *testing.T, cli *client.Client, ID string) string {
ctr, err := cli.ContainerInspect(context.Background(), ID)
if err != nil {
t.Fatal(err)
}
return ctr.NetworkSettings.IPAddress
}
func createNetwork(t *testing.T, cli *client.Client) string {
name := "test"
t.Logf("Creating network: %v", name)
opts := types.NetworkCreate{}
net, err := cli.NetworkCreate(context.Background(), name, opts)
if err != nil {
t.Fatal(err)
}
t.Logf("Created network %v with ID %v", name, net.ID)
return net.ID
}
func removeNetwork(t *testing.T, cli *client.Client, ID string) {
t.Logf("Removing network ID: %v", ID)
err := cli.NetworkRemove(context.Background(), ID)
if err != nil {
t.Fatal(err)
}
}
func createVolume(t *testing.T, cli *client.Client) types.Volume {
v, err := cli.VolumeCreate(context.Background(), volume.VolumesCreateBody{
Driver: "local",
DriverOpts: map[string]string{},
Labels: map[string]string{},
Name: t.Name(),
})
if err != nil {
t.Fatal(err)
}
t.Logf("Created volume %v", t.Name())
return v
}
func removeVolume(t *testing.T, cli *client.Client, name string) {
t.Logf("Removing volume %v", name)
err := cli.VolumeRemove(context.Background(), name, true)
if err != nil {
t.Fatal(err)
}
}
func inspectLogs(t *testing.T, cli *client.Client, ID string) string {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
reader, err := cli.ContainerLogs(ctx, ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
log.Fatal(err)
}
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
return buf.String()
}

View File

@@ -1,19 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
func main() {
}

View File

@@ -26,16 +26,12 @@ import (
"github.com/docker/go-connections/nat"
)
const image string = "mq-devserver:latest-x86_64"
func TestLicenseNotSet(t *testing.T) {
cli, err := client.NewEnvClient()
if err != nil {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
}
containerConfig := container.Config{}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
rc := waitForContainer(t, cli, id, 5)
@@ -50,8 +46,7 @@ func TestLicenseView(t *testing.T) {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=view"},
Env: []string{"LICENSE=view"},
}
id := runContainer(t, cli, &containerConfig)
defer cleanContainer(t, cli, id)
@@ -72,8 +67,7 @@ func TestGoldenPath(t *testing.T) {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
//ExposedPorts: ports,
ExposedPorts: nat.PortSet{
"1414/tcp": struct{}{},
@@ -91,7 +85,6 @@ func utilTestNoQueueManagerName(t *testing.T, hostName string, expectedName stri
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept"},
Hostname: hostName,
ExposedPorts: nat.PortSet{
@@ -124,7 +117,7 @@ func TestWithVolume(t *testing.T) {
vol := createVolume(t, cli)
defer removeVolume(t, cli, vol.Name)
containerConfig := container.Config{
Image: image,
Image: imageName(),
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
}
hostConfig := container.HostConfig{
@@ -162,8 +155,7 @@ func TestNoVolumeWithRestart(t *testing.T) {
t.Fatal(err)
}
containerConfig := container.Config{
Image: image,
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
//ExposedPorts: ports,
ExposedPorts: nat.PortSet{
"1414/tcp": struct{}{},

View File

@@ -33,6 +33,14 @@ import (
"github.com/docker/go-connections/nat"
)
func imageName() string {
image, ok := os.LookupEnv("TEST_IMAGE")
if !ok {
image = "mq-devserver:latest-x86-64"
}
return image
}
func coverageBind(t *testing.T) string {
dir, err := os.Getwd()
if err != nil {
@@ -72,8 +80,13 @@ func cleanContainer(t *testing.T, cli *client.Client, ID string) {
}
}
// runContainer creates and starts a container. If no image is specified in
// the container config, then the image name is retrieved from the TEST_IMAGE
// environment variable.
func runContainer(t *testing.T, cli *client.Client, containerConfig *container.Config) string {
t.Logf("Running container")
if containerConfig.Image == "" {
containerConfig.Image = imageName()
}
hostConfig := container.HostConfig{
PortBindings: nat.PortMap{
"1414/tcp": []nat.PortBinding{
@@ -85,6 +98,7 @@ func runContainer(t *testing.T, cli *client.Client, containerConfig *container.C
},
}
networkingConfig := network.NetworkingConfig{}
t.Logf("Running container (%s)", containerConfig.Image)
ctr, err := cli.ContainerCreate(context.Background(), containerConfig, &hostConfig, &networkingConfig, t.Name())
if err != nil {
t.Fatal(err)