mirror of
https://github.com/moby/moby.git
synced 2025-07-29 07:21:35 +03:00
Use suite for integration-cli
It prints test name and duration for each test. Also performs deleteAllContainers after each test. Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
@ -6,20 +6,19 @@ import (
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/nat"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
// Make sure we can create a simple container with some args
|
||||
func TestCreateArgs(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateArgs(c *check.C) {
|
||||
|
||||
runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
@ -27,7 +26,7 @@ func TestCreateArgs(t *testing.T) {
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
if err != nil {
|
||||
t.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
}
|
||||
|
||||
containers := []struct {
|
||||
@ -38,40 +37,38 @@ func TestCreateArgs(t *testing.T) {
|
||||
Image string
|
||||
}{}
|
||||
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
||||
t.Fatalf("Error inspecting the container: %s", err)
|
||||
c.Fatalf("Error inspecting the container: %s", err)
|
||||
}
|
||||
if len(containers) != 1 {
|
||||
t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
}
|
||||
|
||||
c := containers[0]
|
||||
if c.Path != "command" {
|
||||
t.Fatalf("Unexpected container path. Expected command, received: %s", c.Path)
|
||||
cont := containers[0]
|
||||
if cont.Path != "command" {
|
||||
c.Fatalf("Unexpected container path. Expected command, received: %s", cont.Path)
|
||||
}
|
||||
|
||||
b := false
|
||||
expected := []string{"arg1", "arg2", "arg with space"}
|
||||
for i, arg := range expected {
|
||||
if arg != c.Args[i] {
|
||||
if arg != cont.Args[i] {
|
||||
b = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(c.Args) != len(expected) || b {
|
||||
t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
|
||||
if len(cont.Args) != len(expected) || b {
|
||||
c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
|
||||
}
|
||||
|
||||
logDone("create - args")
|
||||
}
|
||||
|
||||
// Make sure we can set hostconfig options too
|
||||
func TestCreateHostConfig(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
|
||||
|
||||
runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
@ -79,7 +76,7 @@ func TestCreateHostConfig(t *testing.T) {
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
if err != nil {
|
||||
t.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
}
|
||||
|
||||
containers := []struct {
|
||||
@ -88,31 +85,29 @@ func TestCreateHostConfig(t *testing.T) {
|
||||
}
|
||||
}{}
|
||||
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
||||
t.Fatalf("Error inspecting the container: %s", err)
|
||||
c.Fatalf("Error inspecting the container: %s", err)
|
||||
}
|
||||
if len(containers) != 1 {
|
||||
t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
}
|
||||
|
||||
c := containers[0]
|
||||
if c.HostConfig == nil {
|
||||
t.Fatalf("Expected HostConfig, got none")
|
||||
cont := containers[0]
|
||||
if cont.HostConfig == nil {
|
||||
c.Fatalf("Expected HostConfig, got none")
|
||||
}
|
||||
|
||||
if !c.HostConfig.PublishAllPorts {
|
||||
t.Fatalf("Expected PublishAllPorts, got false")
|
||||
if !cont.HostConfig.PublishAllPorts {
|
||||
c.Fatalf("Expected PublishAllPorts, got false")
|
||||
}
|
||||
|
||||
logDone("create - hostconfig")
|
||||
}
|
||||
|
||||
func TestCreateWithPortRange(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
|
||||
|
||||
runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
@ -120,7 +115,7 @@ func TestCreateWithPortRange(t *testing.T) {
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
if err != nil {
|
||||
t.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
}
|
||||
|
||||
containers := []struct {
|
||||
@ -129,39 +124,37 @@ func TestCreateWithPortRange(t *testing.T) {
|
||||
}
|
||||
}{}
|
||||
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
||||
t.Fatalf("Error inspecting the container: %s", err)
|
||||
c.Fatalf("Error inspecting the container: %s", err)
|
||||
}
|
||||
if len(containers) != 1 {
|
||||
t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
}
|
||||
|
||||
c := containers[0]
|
||||
if c.HostConfig == nil {
|
||||
t.Fatalf("Expected HostConfig, got none")
|
||||
cont := containers[0]
|
||||
if cont.HostConfig == nil {
|
||||
c.Fatalf("Expected HostConfig, got none")
|
||||
}
|
||||
|
||||
if len(c.HostConfig.PortBindings) != 4 {
|
||||
t.Fatalf("Expected 4 ports bindings, got %d", len(c.HostConfig.PortBindings))
|
||||
if len(cont.HostConfig.PortBindings) != 4 {
|
||||
c.Fatalf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings))
|
||||
}
|
||||
for k, v := range c.HostConfig.PortBindings {
|
||||
for k, v := range cont.HostConfig.PortBindings {
|
||||
if len(v) != 1 {
|
||||
t.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
||||
c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
||||
}
|
||||
if k.Port() != v[0].HostPort {
|
||||
t.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
||||
c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
||||
}
|
||||
}
|
||||
|
||||
logDone("create - port range")
|
||||
}
|
||||
|
||||
func TestCreateWithiLargePortRange(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateWithiLargePortRange(c *check.C) {
|
||||
|
||||
runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
@ -169,7 +162,7 @@ func TestCreateWithiLargePortRange(t *testing.T) {
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
if err != nil {
|
||||
t.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
||||
}
|
||||
|
||||
containers := []struct {
|
||||
@ -178,40 +171,38 @@ func TestCreateWithiLargePortRange(t *testing.T) {
|
||||
}
|
||||
}{}
|
||||
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
||||
t.Fatalf("Error inspecting the container: %s", err)
|
||||
c.Fatalf("Error inspecting the container: %s", err)
|
||||
}
|
||||
if len(containers) != 1 {
|
||||
t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
||||
}
|
||||
|
||||
c := containers[0]
|
||||
if c.HostConfig == nil {
|
||||
t.Fatalf("Expected HostConfig, got none")
|
||||
cont := containers[0]
|
||||
if cont.HostConfig == nil {
|
||||
c.Fatalf("Expected HostConfig, got none")
|
||||
}
|
||||
|
||||
if len(c.HostConfig.PortBindings) != 65535 {
|
||||
t.Fatalf("Expected 65535 ports bindings, got %d", len(c.HostConfig.PortBindings))
|
||||
if len(cont.HostConfig.PortBindings) != 65535 {
|
||||
c.Fatalf("Expected 65535 ports bindings, got %d", len(cont.HostConfig.PortBindings))
|
||||
}
|
||||
for k, v := range c.HostConfig.PortBindings {
|
||||
for k, v := range cont.HostConfig.PortBindings {
|
||||
if len(v) != 1 {
|
||||
t.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
||||
c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
||||
}
|
||||
if k.Port() != v[0].HostPort {
|
||||
t.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
||||
c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
||||
}
|
||||
}
|
||||
|
||||
logDone("create - large port range")
|
||||
}
|
||||
|
||||
// "test123" should be printed by docker create + start
|
||||
func TestCreateEchoStdout(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
|
||||
|
||||
runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
@ -219,62 +210,58 @@ func TestCreateEchoStdout(t *testing.T) {
|
||||
runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
|
||||
out, _, _, err = runCommandWithStdoutStderr(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if out != "test123\n" {
|
||||
t.Errorf("container should've printed 'test123', got %q", out)
|
||||
c.Errorf("container should've printed 'test123', got %q", out)
|
||||
}
|
||||
|
||||
logDone("create - echo test123")
|
||||
}
|
||||
|
||||
func TestCreateVolumesCreated(t *testing.T) {
|
||||
testRequires(t, SameHostDaemon)
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
|
||||
testRequires(c, SameHostDaemon)
|
||||
|
||||
name := "test_create_volume"
|
||||
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
dir, err := inspectFieldMap(name, "Volumes", "/foo")
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting volume host path: %q", err)
|
||||
c.Fatalf("Error getting volume host path: %q", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
||||
t.Fatalf("Volume was not created")
|
||||
c.Fatalf("Volume was not created")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Error statting volume host path: %q", err)
|
||||
c.Fatalf("Error statting volume host path: %q", err)
|
||||
}
|
||||
|
||||
logDone("create - volumes are created")
|
||||
}
|
||||
|
||||
func TestCreateLabels(t *testing.T) {
|
||||
func (s *DockerSuite) TestCreateLabels(c *check.C) {
|
||||
name := "test_create_labels"
|
||||
expected := map[string]string{"k1": "v1", "k2": "v2"}
|
||||
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
actual := make(map[string]string)
|
||||
err := inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Fatalf("Expected %s got %s", expected, actual)
|
||||
c.Fatalf("Expected %s got %s", expected, actual)
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
||||
logDone("create - labels")
|
||||
}
|
||||
|
||||
func TestCreateLabelFromImage(t *testing.T) {
|
||||
func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
|
||||
imageName := "testcreatebuildlabel"
|
||||
defer deleteImages(imageName)
|
||||
_, err := buildImage(imageName,
|
||||
@ -282,34 +269,32 @@ func TestCreateLabelFromImage(t *testing.T) {
|
||||
LABEL k1=v1 k2=v2`,
|
||||
true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
name := "test_create_labels_from_image"
|
||||
expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
|
||||
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
actual := make(map[string]string)
|
||||
err = inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Fatalf("Expected %s got %s", expected, actual)
|
||||
c.Fatalf("Expected %s got %s", expected, actual)
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
||||
logDone("create - labels from image")
|
||||
}
|
||||
|
||||
func TestCreateHostnameWithNumber(t *testing.T) {
|
||||
out, _ := dockerCmd(t, "run", "-h", "web.0", "busybox", "hostname")
|
||||
func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-h", "web.0", "busybox", "hostname")
|
||||
if strings.TrimSpace(out) != "web.0" {
|
||||
t.Fatalf("hostname not set, expected `web.0`, got: %s", out)
|
||||
c.Fatalf("hostname not set, expected `web.0`, got: %s", out)
|
||||
}
|
||||
logDone("create - use hostname with number")
|
||||
}
|
||||
|
Reference in New Issue
Block a user