github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/integration-cli/docker_cli_service_test.go (about)

     1  // +build experimental
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func assertSrvIsAvailable(c *check.C, sname, name string) {
    14  	if !isSrvPresent(c, sname, name) {
    15  		c.Fatalf("Service %s on network %s not found in service ls o/p", sname, name)
    16  	}
    17  }
    18  
    19  func assertSrvNotAvailable(c *check.C, sname, name string) {
    20  	if isSrvPresent(c, sname, name) {
    21  		c.Fatalf("Found service %s on network %s in service ls o/p", sname, name)
    22  	}
    23  }
    24  
    25  func isSrvPresent(c *check.C, sname, name string) bool {
    26  	runCmd := exec.Command(dockerBinary, "service", "ls")
    27  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    28  	c.Assert(err, check.IsNil)
    29  	lines := strings.Split(out, "\n")
    30  	for i := 1; i < len(lines)-1; i++ {
    31  		if strings.Contains(lines[i], sname) && strings.Contains(lines[i], name) {
    32  			return true
    33  		}
    34  	}
    35  	return false
    36  }
    37  
    38  func isCntPresent(c *check.C, cname, sname, name string) bool {
    39  	runCmd := exec.Command(dockerBinary, "service", "ls", "--no-trunc")
    40  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    41  	c.Assert(err, check.IsNil)
    42  	lines := strings.Split(out, "\n")
    43  	for i := 1; i < len(lines)-1; i++ {
    44  		fmt.Println(lines)
    45  		if strings.Contains(lines[i], name) && strings.Contains(lines[i], sname) && strings.Contains(lines[i], cname) {
    46  			return true
    47  		}
    48  	}
    49  	return false
    50  }
    51  
    52  func (s *DockerSuite) TestDockerServiceCreateDelete(c *check.C) {
    53  	runCmd := exec.Command(dockerBinary, "network", "create", "test")
    54  	_, _, _, err := runCommandWithStdoutStderr(runCmd)
    55  	c.Assert(err, check.IsNil)
    56  	assertNwIsAvailable(c, "test")
    57  
    58  	runCmd = exec.Command(dockerBinary, "service", "publish", "s1.test")
    59  	_, _, _, err = runCommandWithStdoutStderr(runCmd)
    60  	c.Assert(err, check.IsNil)
    61  	assertSrvIsAvailable(c, "s1", "test")
    62  
    63  	runCmd = exec.Command(dockerBinary, "service", "unpublish", "s1.test")
    64  	_, _, _, err = runCommandWithStdoutStderr(runCmd)
    65  	c.Assert(err, check.IsNil)
    66  	assertSrvNotAvailable(c, "s1", "test")
    67  
    68  	runCmd = exec.Command(dockerBinary, "network", "rm", "test")
    69  	_, _, _, err = runCommandWithStdoutStderr(runCmd)
    70  	c.Assert(err, check.IsNil)
    71  	assertNwNotAvailable(c, "test")
    72  }
    73  
    74  func (s *DockerSuite) TestDockerPublishServiceFlag(c *check.C) {
    75  	// Run saying the container is the backend for the specified service on the specified network
    76  	runCmd := exec.Command(dockerBinary, "run", "-d", "--expose=23", "--publish-service", "telnet.production", "busybox", "top")
    77  	out, _, err := runCommandWithOutput(runCmd)
    78  	c.Assert(err, check.IsNil)
    79  	cid := strings.TrimSpace(out)
    80  
    81  	// Verify container is attached in service ps o/p
    82  	assertSrvIsAvailable(c, "telnet", "production")
    83  	runCmd = exec.Command(dockerBinary, "rm", "-f", cid)
    84  	out, _, err = runCommandWithOutput(runCmd)
    85  	c.Assert(err, check.IsNil)
    86  }