mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-05 21:21:42 +00: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:
@@ -5,42 +5,42 @@ import (
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func TestPortList(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
func (s *DockerSuite) TestPortList(c *check.C) {
|
||||
|
||||
// one port
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
firstID := strings.TrimSpace(out)
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
|
||||
t.Error("Port list is not correct")
|
||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", firstID)
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
|
||||
t.Error("Port list is not correct")
|
||||
if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
// three port
|
||||
@@ -51,36 +51,36 @@ func TestPortList(t *testing.T) {
|
||||
"busybox", "top")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
ID := strings.TrimSpace(out)
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
|
||||
t.Error("Port list is not correct")
|
||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", ID)
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{
|
||||
if !assertPortList(c, out, []string{
|
||||
"80/tcp -> 0.0.0.0:9876",
|
||||
"81/tcp -> 0.0.0.0:9877",
|
||||
"82/tcp -> 0.0.0.0:9878"}) {
|
||||
t.Error("Port list is not correct")
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
// more and one port mapped to the same container port
|
||||
@@ -92,46 +92,45 @@ func TestPortList(t *testing.T) {
|
||||
"busybox", "top")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
ID = strings.TrimSpace(out)
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
|
||||
t.Error("Port list is not correct")
|
||||
if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", ID)
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{
|
||||
if !assertPortList(c, out, []string{
|
||||
"80/tcp -> 0.0.0.0:9876",
|
||||
"80/tcp -> 0.0.0.0:9999",
|
||||
"81/tcp -> 0.0.0.0:9877",
|
||||
"82/tcp -> 0.0.0.0:9878"}) {
|
||||
t.Error("Port list is not correct\n", out)
|
||||
c.Error("Port list is not correct\n", out)
|
||||
}
|
||||
runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
logDone("port - test port list")
|
||||
}
|
||||
|
||||
func assertPortList(t *testing.T, out string, expected []string) bool {
|
||||
func assertPortList(c *check.C, out string, expected []string) bool {
|
||||
//lines := strings.Split(out, "\n")
|
||||
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
||||
if len(lines) != len(expected) {
|
||||
t.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
||||
c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
||||
return false
|
||||
}
|
||||
sort.Strings(lines)
|
||||
@@ -139,7 +138,7 @@ func assertPortList(t *testing.T, out string, expected []string) bool {
|
||||
|
||||
for i := 0; i < len(expected); i++ {
|
||||
if lines[i] != expected[i] {
|
||||
t.Error("|" + lines[i] + "!=" + expected[i] + "|")
|
||||
c.Error("|" + lines[i] + "!=" + expected[i] + "|")
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -147,84 +146,78 @@ func assertPortList(t *testing.T, out string, expected []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func TestPortHostBinding(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
|
||||
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
|
||||
"nc", "-l", "-p", "80")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
firstID := strings.TrimSpace(out)
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
|
||||
t.Error("Port list is not correct")
|
||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
||||
c.Error("Port list is not correct")
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
||||
"nc", "localhost", "9876")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
||||
"nc", "localhost", "9876")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
||||
t.Error("Port is still bound after the Container is removed")
|
||||
c.Error("Port is still bound after the Container is removed")
|
||||
}
|
||||
logDone("port - test host binding done")
|
||||
}
|
||||
|
||||
func TestPortExposeHostBinding(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
|
||||
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
|
||||
"nc", "-l", "-p", "80")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
firstID := strings.TrimSpace(out)
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
||||
out, _, err = runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
_, exposedPort, err := net.SplitHostPort(out)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
||||
"nc", "localhost", strings.TrimSpace(exposedPort))
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
c.Fatal(out, err)
|
||||
}
|
||||
|
||||
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
||||
"nc", "localhost", strings.TrimSpace(exposedPort))
|
||||
if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
||||
t.Error("Port is still bound after the Container is removed")
|
||||
c.Error("Port is still bound after the Container is removed")
|
||||
}
|
||||
logDone("port - test port expose done")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user