github.com/slava-ustovytski/docker@v1.8.2-rc1/integration-cli/docker_cli_service_test.go (about)

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