docker_cli_service_test.go 2.0 KB

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