mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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>
This commit is contained in:
parent
648ae4979e
commit
9722214c8a
7 changed files with 45 additions and 37 deletions
|
@ -70,18 +70,6 @@ func (d *Daemon) GetIDByName(name string) (string, error) {
|
|||
return d.inspectFieldWithError(name, "Id")
|
||||
}
|
||||
|
||||
// ActiveContainers returns the list of ids of the currently running containers
|
||||
func (d *Daemon) ActiveContainers() (ids []string) {
|
||||
// FIXME(vdemeester) shouldn't ignore the error
|
||||
out, _ := d.Cmd("ps", "-q")
|
||||
for _, id := range strings.Split(out, "\n") {
|
||||
if id = strings.TrimSpace(id); id != "" {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InspectField returns the field filter by 'filter'
|
||||
func (d *Daemon) InspectField(name, filter string) (string, error) {
|
||||
return d.inspectFilter(name, filter)
|
||||
|
@ -100,15 +88,6 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
|
|||
return d.inspectFilter(name, fmt.Sprintf(".%s", field))
|
||||
}
|
||||
|
||||
// FindContainerIP returns the ip of the specified container
|
||||
func (d *Daemon) FindContainerIP(id string) (string, error) {
|
||||
out, err := d.Cmd("inspect", "--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'", id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.Trim(out, " \r\n'"), nil
|
||||
}
|
||||
|
||||
// BuildImageWithOut builds an image with the specified dockerfile and options and returns the output
|
||||
func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) {
|
||||
buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...)
|
||||
|
|
|
@ -111,7 +111,7 @@ func (s *DockerSwarmSuite) TestAPISwarmNodeDrainPause(c *check.C) {
|
|||
waitAndAssert(c, defaultReconciliationTimeout, d1.CheckActiveContainerCount, checker.GreaterThan, 0)
|
||||
waitAndAssert(c, defaultReconciliationTimeout*2, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals, instances)
|
||||
|
||||
d2ContainerCount := len(d2.ActiveContainers())
|
||||
d2ContainerCount := len(d2.ActiveContainers(c))
|
||||
|
||||
// set d2 to paused, scale service up, only d1 gets new tasks
|
||||
d1.UpdateNode(c, d2.NodeID(), func(n *swarm.Node) {
|
||||
|
|
|
@ -556,7 +556,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesStateReporting(c *check.C) {
|
|||
getContainers := func() map[string]*daemon.Daemon {
|
||||
m := make(map[string]*daemon.Daemon)
|
||||
for _, d := range []*daemon.Daemon{d1, d2, d3} {
|
||||
for _, id := range d.ActiveContainers() {
|
||||
for _, id := range d.ActiveContainers(c) {
|
||||
m[id] = d
|
||||
}
|
||||
}
|
||||
|
|
|
@ -507,11 +507,11 @@ func (s *DockerSwarmSuite) TestAPISwarmScaleNoRollingUpdate(c *check.C) {
|
|||
id := d.CreateService(c, simpleTestService, setInstances(instances))
|
||||
|
||||
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
|
||||
containers := d.ActiveContainers()
|
||||
containers := d.ActiveContainers(c)
|
||||
instances = 4
|
||||
d.UpdateService(c, d.GetService(c, id), setInstances(instances))
|
||||
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
|
||||
containers2 := d.ActiveContainers()
|
||||
containers2 := d.ActiveContainers(c)
|
||||
|
||||
loop0:
|
||||
for _, c1 := range containers {
|
||||
|
@ -943,7 +943,7 @@ func (s *DockerSwarmSuite) TestAPISwarmHealthcheckNone(c *check.C) {
|
|||
|
||||
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
|
||||
|
||||
containers := d.ActiveContainers()
|
||||
containers := d.ActiveContainers(c)
|
||||
|
||||
out, err = d.Cmd("exec", containers[0], "ping", "-c1", "-W3", "top")
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
|
|
@ -604,8 +604,7 @@ func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
|
|||
_, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
containerIP, err := d.FindContainerIP("ExtContainer")
|
||||
c.Assert(err, checker.IsNil)
|
||||
containerIP := d.FindContainerIP(c, "ExtContainer")
|
||||
ip := net.ParseIP(containerIP)
|
||||
c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
|
||||
check.Commentf("Container IP-Address must be in the same subnet range : %s",
|
||||
|
@ -676,8 +675,7 @@ func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
|
|||
_, err := d.Cmd("run", "-d", "--name", "test", "busybox", "top")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
containerIP, err := d.FindContainerIP("test")
|
||||
c.Assert(err, checker.IsNil)
|
||||
containerIP := d.FindContainerIP(c, "test")
|
||||
ip = net.ParseIP(containerIP)
|
||||
c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
|
||||
check.Commentf("Container IP-Address must be in the same subnet range : %s",
|
||||
|
@ -947,10 +945,8 @@ func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *che
|
|||
_, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
childIP, err := s.d.FindContainerIP("child")
|
||||
c.Assert(err, checker.IsNil)
|
||||
parentIP, err := s.d.FindContainerIP("parent")
|
||||
c.Assert(err, checker.IsNil)
|
||||
childIP := s.d.FindContainerIP(c, "child")
|
||||
parentIP := s.d.FindContainerIP(c, "parent")
|
||||
|
||||
sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
|
||||
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) {
|
|||
// make sure task has been deployed.
|
||||
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
|
||||
|
||||
containers := d.ActiveContainers()
|
||||
containers := d.ActiveContainers(c)
|
||||
out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.Hostname}}", containers[0])
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
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) {
|
|||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, "true")
|
||||
|
||||
containers := d.ActiveContainers()
|
||||
containers := d.ActiveContainers(c)
|
||||
out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.HostConfig.ReadonlyRootfs}}", containers[0])
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, "true")
|
||||
|
@ -1762,7 +1762,7 @@ func (s *DockerSwarmSuite) TestSwarmStopSignal(c *check.C) {
|
|||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, "SIGHUP")
|
||||
|
||||
containers := d.ActiveContainers()
|
||||
containers := d.ActiveContainers(c)
|
||||
out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.StopSignal}}", containers[0])
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, "SIGHUP")
|
||||
|
|
33
internal/test/daemon/container.go
Normal file
33
internal/test/daemon/container.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
)
|
||||
|
||||
// ActiveContainers returns the list of ids of the currently running containers
|
||||
func (d *Daemon) ActiveContainers(t testingT) []string {
|
||||
cli := d.NewClientT(t)
|
||||
defer cli.Close()
|
||||
|
||||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
||||
assert.NilError(t, err)
|
||||
|
||||
ids := make([]string, len(containers))
|
||||
for i, c := range containers {
|
||||
ids[i] = c.ID
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// FindContainerIP returns the ip of the specified container
|
||||
func (d *Daemon) FindContainerIP(t testingT, id string) string {
|
||||
cli := d.NewClientT(t)
|
||||
defer cli.Close()
|
||||
|
||||
i, err := cli.ContainerInspect(context.Background(), id)
|
||||
assert.NilError(t, err)
|
||||
return i.NetworkSettings.IPAddress
|
||||
}
|
Loading…
Add table
Reference in a new issue