Browse code

Add api helpers to internal/test/daemon.Daemon

Porting helpers from `integration-cli/daemon.Daemon` to this struct
and use the API instead of the cli.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2018/04/11 22:52:13
Showing 7 changed files
... ...
@@ -70,18 +70,6 @@ func (d *Daemon) GetIDByName(name string) (string, error) {
70 70
 	return d.inspectFieldWithError(name, "Id")
71 71
 }
72 72
 
73
-// ActiveContainers returns the list of ids of the currently running containers
74
-func (d *Daemon) ActiveContainers() (ids []string) {
75
-	// FIXME(vdemeester) shouldn't ignore the error
76
-	out, _ := d.Cmd("ps", "-q")
77
-	for _, id := range strings.Split(out, "\n") {
78
-		if id = strings.TrimSpace(id); id != "" {
79
-			ids = append(ids, id)
80
-		}
81
-	}
82
-	return
83
-}
84
-
85 73
 // InspectField returns the field filter by 'filter'
86 74
 func (d *Daemon) InspectField(name, filter string) (string, error) {
87 75
 	return d.inspectFilter(name, filter)
... ...
@@ -100,15 +88,6 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
100 100
 	return d.inspectFilter(name, fmt.Sprintf(".%s", field))
101 101
 }
102 102
 
103
-// FindContainerIP returns the ip of the specified container
104
-func (d *Daemon) FindContainerIP(id string) (string, error) {
105
-	out, err := d.Cmd("inspect", "--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'", id)
106
-	if err != nil {
107
-		return "", err
108
-	}
109
-	return strings.Trim(out, " \r\n'"), nil
110
-}
111
-
112 103
 // BuildImageWithOut builds an image with the specified dockerfile and options and returns the output
113 104
 func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) {
114 105
 	buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...)
... ...
@@ -111,7 +111,7 @@ func (s *DockerSwarmSuite) TestAPISwarmNodeDrainPause(c *check.C) {
111 111
 	waitAndAssert(c, defaultReconciliationTimeout, d1.CheckActiveContainerCount, checker.GreaterThan, 0)
112 112
 	waitAndAssert(c, defaultReconciliationTimeout*2, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals, instances)
113 113
 
114
-	d2ContainerCount := len(d2.ActiveContainers())
114
+	d2ContainerCount := len(d2.ActiveContainers(c))
115 115
 
116 116
 	// set d2 to paused, scale service up, only d1 gets new tasks
117 117
 	d1.UpdateNode(c, d2.NodeID(), func(n *swarm.Node) {
... ...
@@ -556,7 +556,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesStateReporting(c *check.C) {
556 556
 	getContainers := func() map[string]*daemon.Daemon {
557 557
 		m := make(map[string]*daemon.Daemon)
558 558
 		for _, d := range []*daemon.Daemon{d1, d2, d3} {
559
-			for _, id := range d.ActiveContainers() {
559
+			for _, id := range d.ActiveContainers(c) {
560 560
 				m[id] = d
561 561
 			}
562 562
 		}
... ...
@@ -507,11 +507,11 @@ func (s *DockerSwarmSuite) TestAPISwarmScaleNoRollingUpdate(c *check.C) {
507 507
 	id := d.CreateService(c, simpleTestService, setInstances(instances))
508 508
 
509 509
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
510
-	containers := d.ActiveContainers()
510
+	containers := d.ActiveContainers(c)
511 511
 	instances = 4
512 512
 	d.UpdateService(c, d.GetService(c, id), setInstances(instances))
513 513
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
514
-	containers2 := d.ActiveContainers()
514
+	containers2 := d.ActiveContainers(c)
515 515
 
516 516
 loop0:
517 517
 	for _, c1 := range containers {
... ...
@@ -943,7 +943,7 @@ func (s *DockerSwarmSuite) TestAPISwarmHealthcheckNone(c *check.C) {
943 943
 
944 944
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
945 945
 
946
-	containers := d.ActiveContainers()
946
+	containers := d.ActiveContainers(c)
947 947
 
948 948
 	out, err = d.Cmd("exec", containers[0], "ping", "-c1", "-W3", "top")
949 949
 	c.Assert(err, checker.IsNil, check.Commentf(out))
... ...
@@ -604,8 +604,7 @@ func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
604 604
 	_, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
605 605
 	c.Assert(err, check.IsNil)
606 606
 
607
-	containerIP, err := d.FindContainerIP("ExtContainer")
608
-	c.Assert(err, checker.IsNil)
607
+	containerIP := d.FindContainerIP(c, "ExtContainer")
609 608
 	ip := net.ParseIP(containerIP)
610 609
 	c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
611 610
 		check.Commentf("Container IP-Address must be in the same subnet range : %s",
... ...
@@ -676,8 +675,7 @@ func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
676 676
 	_, err := d.Cmd("run", "-d", "--name", "test", "busybox", "top")
677 677
 	c.Assert(err, check.IsNil)
678 678
 
679
-	containerIP, err := d.FindContainerIP("test")
680
-	c.Assert(err, checker.IsNil)
679
+	containerIP := d.FindContainerIP(c, "test")
681 680
 	ip = net.ParseIP(containerIP)
682 681
 	c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
683 682
 		check.Commentf("Container IP-Address must be in the same subnet range : %s",
... ...
@@ -947,10 +945,8 @@ func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *che
947 947
 	_, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
948 948
 	c.Assert(err, check.IsNil)
949 949
 
950
-	childIP, err := s.d.FindContainerIP("child")
951
-	c.Assert(err, checker.IsNil)
952
-	parentIP, err := s.d.FindContainerIP("parent")
953
-	c.Assert(err, checker.IsNil)
950
+	childIP := s.d.FindContainerIP(c, "child")
951
+	parentIP := s.d.FindContainerIP(c, "parent")
954 952
 
955 953
 	sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
956 954
 	destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
... ...
@@ -177,7 +177,7 @@ func (s *DockerSwarmSuite) TestSwarmServiceTemplatingHostname(c *check.C) {
177 177
 	// make sure task has been deployed.
178 178
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
179 179
 
180
-	containers := d.ActiveContainers()
180
+	containers := d.ActiveContainers(c)
181 181
 	out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.Hostname}}", containers[0])
182 182
 	c.Assert(err, checker.IsNil, check.Commentf(out))
183 183
 	c.Assert(strings.Split(out, "\n")[0], checker.Equals, "test-1-"+strings.Split(hostname, "\n")[0], check.Commentf("hostname with templating invalid"))
... ...
@@ -1675,7 +1675,7 @@ func (s *DockerSwarmSuite) TestSwarmReadonlyRootfs(c *check.C) {
1675 1675
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1676 1676
 	c.Assert(strings.TrimSpace(out), checker.Equals, "true")
1677 1677
 
1678
-	containers := d.ActiveContainers()
1678
+	containers := d.ActiveContainers(c)
1679 1679
 	out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.HostConfig.ReadonlyRootfs}}", containers[0])
1680 1680
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1681 1681
 	c.Assert(strings.TrimSpace(out), checker.Equals, "true")
... ...
@@ -1762,7 +1762,7 @@ func (s *DockerSwarmSuite) TestSwarmStopSignal(c *check.C) {
1762 1762
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1763 1763
 	c.Assert(strings.TrimSpace(out), checker.Equals, "SIGHUP")
1764 1764
 
1765
-	containers := d.ActiveContainers()
1765
+	containers := d.ActiveContainers(c)
1766 1766
 	out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.StopSignal}}", containers[0])
1767 1767
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1768 1768
 	c.Assert(strings.TrimSpace(out), checker.Equals, "SIGHUP")
1769 1769
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+package daemon
1
+
2
+import (
3
+	"context"
4
+
5
+	"github.com/docker/docker/api/types"
6
+	"github.com/gotestyourself/gotestyourself/assert"
7
+)
8
+
9
+// ActiveContainers returns the list of ids of the currently running containers
10
+func (d *Daemon) ActiveContainers(t testingT) []string {
11
+	cli := d.NewClientT(t)
12
+	defer cli.Close()
13
+
14
+	containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
15
+	assert.NilError(t, err)
16
+
17
+	ids := make([]string, len(containers))
18
+	for i, c := range containers {
19
+		ids[i] = c.ID
20
+	}
21
+	return ids
22
+}
23
+
24
+// FindContainerIP returns the ip of the specified container
25
+func (d *Daemon) FindContainerIP(t testingT, id string) string {
26
+	cli := d.NewClientT(t)
27
+	defer cli.Close()
28
+
29
+	i, err := cli.ContainerInspect(context.Background(), id)
30
+	assert.NilError(t, err)
31
+	return i.NetworkSettings.IPAddress
32
+}