Browse Source

use of checkers on Integration test

Part of #16756

Use c.Assert instead of condition judgement in
integration-cli/docker_cli_start_test.go

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
Zhang Wei 9 years ago
parent
commit
08944cdef7
1 changed files with 44 additions and 52 deletions
  1. 44 52
      integration-cli/docker_cli_start_test.go

+ 44 - 52
integration-cli/docker_cli_start_test.go

@@ -5,6 +5,7 @@ import (
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
+	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/go-check/check"
 	"github.com/go-check/check"
 )
 )
 
 
@@ -15,9 +16,9 @@ func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
 	dockerCmd(c, "wait", "test")
 	dockerCmd(c, "wait", "test")
 
 
 	// Expect this to fail because the above container is stopped, this is what we want
 	// Expect this to fail because the above container is stopped, this is what we want
-	if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil {
-		c.Fatal("Expected error but got none")
-	}
+	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox")
+	// err shouldn't be nil because container test2 try to link to stopped container
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
 
 
 	ch := make(chan error)
 	ch := make(chan error)
 	go func() {
 	go func() {
@@ -47,12 +48,10 @@ func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
 	dockerCmd(c, "wait", out)
 	dockerCmd(c, "wait", out)
 
 
 	startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
 	startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
-	if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
-		c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
-	}
-	if exitCode != 1 {
-		c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
-	}
+	// start command should fail
+	c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
+	// start -a did not respond with proper exit code
+	c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
 
 
 }
 }
 
 
@@ -65,9 +64,8 @@ func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
 	dockerCmd(c, "wait", name)
 	dockerCmd(c, "wait", name)
 
 
 	startOut, _ := dockerCmd(c, "start", "-a", name)
 	startOut, _ := dockerCmd(c, "start", "-a", name)
-	if expected := "test\n"; startOut != expected {
-		c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
-	}
+	// start -a produced unexpected output
+	c.Assert(startOut, checker.Equals, "test\n")
 }
 }
 
 
 func (s *DockerSuite) TestStartRecordError(c *check.C) {
 func (s *DockerSuite) TestStartRecordError(c *check.C) {
@@ -75,32 +73,26 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
 	// when container runs successfully, we should not have state.Error
 	// when container runs successfully, we should not have state.Error
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
 	stateErr, err := inspectField("test", "State.Error")
 	stateErr, err := inspectField("test", "State.Error")
-	c.Assert(err, check.IsNil)
-	if stateErr != "" {
-		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
-	}
+	c.Assert(err, checker.IsNil, check.Commentf("stateErr: %s", stateErr))
+	// Expected to not have state error
+	c.Assert(stateErr, checker.Equals, "")
 
 
 	// Expect this to fail and records error because of ports conflict
 	// Expect this to fail and records error because of ports conflict
 	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
 	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
-	if err == nil {
-		c.Fatalf("Expected error but got none, output %q", out)
-	}
+	// err shouldn't be nil because docker run will fail
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
 
 
 	stateErr, err = inspectField("test2", "State.Error")
 	stateErr, err = inspectField("test2", "State.Error")
-	c.Assert(err, check.IsNil)
-	expected := "port is already allocated"
-	if stateErr == "" || !strings.Contains(stateErr, expected) {
-		c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
-	}
+	c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
+	c.Assert(stateErr, checker.Contains, "port is already allocated")
 
 
 	// Expect the conflict to be resolved when we stop the initial container
 	// Expect the conflict to be resolved when we stop the initial container
 	dockerCmd(c, "stop", "test")
 	dockerCmd(c, "stop", "test")
 	dockerCmd(c, "start", "test2")
 	dockerCmd(c, "start", "test2")
 	stateErr, err = inspectField("test2", "State.Error")
 	stateErr, err = inspectField("test2", "State.Error")
-	c.Assert(err, check.IsNil)
-	if stateErr != "" {
-		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
-	}
+	c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
+	// Expected to not have state error but got one
+	c.Assert(stateErr, checker.Equals, "")
 }
 }
 
 
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
@@ -111,9 +103,11 @@ func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
 
 
 	dockerCmd(c, "pause", "testing")
 	dockerCmd(c, "pause", "testing")
 
 
-	if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
-		c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
-	}
+	out, _, err := dockerCmdWithError("start", "testing")
+	// an error should have been shown that you cannot start paused container
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
+	// an error should have been shown that you cannot start paused container
+	c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
 }
 }
 
 
 func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
 func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
@@ -129,25 +123,24 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
 	dockerCmd(c, "stop", "parent")
 	dockerCmd(c, "stop", "parent")
 
 
 	out, err := inspectField("parent", "State.Running")
 	out, err := inspectField("parent", "State.Running")
-	c.Assert(err, check.IsNil)
-	if out != "false" {
-		c.Fatal("Container should be stopped")
-	}
+	c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
+	// Container should be stopped
+	c.Assert(out, checker.Equals, "false")
 
 
 	// start all the three containers, container `child_first` start first which should be failed
 	// start all the three containers, container `child_first` start first which should be failed
 	// container 'parent' start second and then start container 'child_second'
 	// container 'parent' start second and then start container 'child_second'
 	out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
 	out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
-	if !strings.Contains(out, "Cannot start container child_first") || err == nil {
-		c.Fatal("Expected error but got none")
-	}
+	// err shouldn't be nil because start will fail
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
+	// output does not correspond to what was expected
+	c.Assert(out, checker.Contains, "Cannot start container child_first")
 
 
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
 		out, err := inspectField(container, "State.Running")
 		out, err := inspectField(container, "State.Running")
-		c.Assert(err, check.IsNil)
-		if out != expected {
-			c.Fatal("Container running state wrong")
-		}
-
+		// Container running state wrong
+		c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
+		// Container running state wrong
+		c.Assert(out, checker.Equals, expected)
 	}
 	}
 }
 }
 
 
@@ -166,19 +159,18 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
 	// test start and attach multiple containers at once, expected error
 	// test start and attach multiple containers at once, expected error
 	for _, option := range []string{"-a", "-i", "-ai"} {
 	for _, option := range []string{"-a", "-i", "-ai"} {
 		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
 		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
-		if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
-			c.Fatal("Expected error but got none")
-		}
+		// err shouldn't be nil because start will fail
+		c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
+		// output does not correspond to what was expected
+		c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
 	}
 	}
 
 
 	// confirm the state of all the containers be stopped
 	// confirm the state of all the containers be stopped
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
 		out, err := inspectField(container, "State.Running")
 		out, err := inspectField(container, "State.Running")
-		if err != nil {
-			c.Fatal(out, err)
-		}
-		if out != expected {
-			c.Fatal("Container running state wrong")
-		}
+		// Container running state wrong
+		c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
+		// Container running state wrong
+		c.Assert(out, checker.Equals, expected)
 	}
 	}
 }
 }