github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/daemon/daemon.go (about) 1 package daemon // import "github.com/docker/docker/integration-cli/daemon" 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/integration-cli/checker" 10 "github.com/docker/docker/internal/test/daemon" 11 "github.com/go-check/check" 12 "github.com/gotestyourself/gotestyourself/assert" 13 "github.com/gotestyourself/gotestyourself/icmd" 14 "github.com/pkg/errors" 15 ) 16 17 type testingT interface { 18 assert.TestingT 19 logT 20 Fatalf(string, ...interface{}) 21 } 22 23 type logT interface { 24 Logf(string, ...interface{}) 25 } 26 27 // Daemon represents a Docker daemon for the testing framework. 28 type Daemon struct { 29 *daemon.Daemon 30 dockerBinary string 31 } 32 33 // New returns a Daemon instance to be used for testing. 34 // This will create a directory such as d123456789 in the folder specified by $DOCKER_INTEGRATION_DAEMON_DEST or $DEST. 35 // The daemon will not automatically start. 36 func New(t testingT, dockerBinary string, dockerdBinary string, ops ...func(*daemon.Daemon)) *Daemon { 37 ops = append(ops, daemon.WithDockerdBinary(dockerdBinary)) 38 d := daemon.New(t, ops...) 39 return &Daemon{ 40 Daemon: d, 41 dockerBinary: dockerBinary, 42 } 43 } 44 45 // Cmd executes a docker CLI command against this daemon. 46 // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version 47 func (d *Daemon) Cmd(args ...string) (string, error) { 48 result := icmd.RunCmd(d.Command(args...)) 49 return result.Combined(), result.Error 50 } 51 52 // Command creates a docker CLI command against this daemon, to be executed later. 53 // Example: d.Command("version") creates a command to run "docker -H unix://path/to/unix.sock version" 54 func (d *Daemon) Command(args ...string) icmd.Cmd { 55 return icmd.Command(d.dockerBinary, d.PrependHostArg(args)...) 56 } 57 58 // PrependHostArg prepend the specified arguments by the daemon host flags 59 func (d *Daemon) PrependHostArg(args []string) []string { 60 for _, arg := range args { 61 if arg == "--host" || arg == "-H" { 62 return args 63 } 64 } 65 return append([]string{"--host", d.Sock()}, args...) 66 } 67 68 // GetIDByName returns the ID of an object (container, volume, …) given its name 69 func (d *Daemon) GetIDByName(name string) (string, error) { 70 return d.inspectFieldWithError(name, "Id") 71 } 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 // InspectField returns the field filter by 'filter' 86 func (d *Daemon) InspectField(name, filter string) (string, error) { 87 return d.inspectFilter(name, filter) 88 } 89 90 func (d *Daemon) inspectFilter(name, filter string) (string, error) { 91 format := fmt.Sprintf("{{%s}}", filter) 92 out, err := d.Cmd("inspect", "-f", format, name) 93 if err != nil { 94 return "", errors.Errorf("failed to inspect %s: %s", name, out) 95 } 96 return strings.TrimSpace(out), nil 97 } 98 99 func (d *Daemon) inspectFieldWithError(name, field string) (string, error) { 100 return d.inspectFilter(name, fmt.Sprintf(".%s", field)) 101 } 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 // BuildImageWithOut builds an image with the specified dockerfile and options and returns the output 113 func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) { 114 buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...) 115 result := icmd.RunCmd(icmd.Cmd{ 116 Command: buildCmd.Args, 117 Env: buildCmd.Env, 118 Dir: buildCmd.Dir, 119 Stdin: buildCmd.Stdin, 120 Stdout: buildCmd.Stdout, 121 }) 122 return result.Combined(), result.ExitCode, result.Error 123 } 124 125 // CheckActiveContainerCount returns the number of active containers 126 // FIXME(vdemeester) should re-use ActivateContainers in some way 127 func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) { 128 out, err := d.Cmd("ps", "-q") 129 c.Assert(err, checker.IsNil) 130 if len(strings.TrimSpace(out)) == 0 { 131 return 0, nil 132 } 133 return len(strings.Split(strings.TrimSpace(out), "\n")), check.Commentf("output: %q", string(out)) 134 } 135 136 // WaitRun waits for a container to be running for 10s 137 func (d *Daemon) WaitRun(contID string) error { 138 args := []string{"--host", d.Sock()} 139 return WaitInspectWithArgs(d.dockerBinary, contID, "{{.State.Running}}", "true", 10*time.Second, args...) 140 } 141 142 // CmdRetryOutOfSequence tries the specified command against the current daemon for 10 times 143 func (d *Daemon) CmdRetryOutOfSequence(args ...string) (string, error) { 144 for i := 0; ; i++ { 145 out, err := d.Cmd(args...) 146 if err != nil { 147 if strings.Contains(out, "update out of sequence") { 148 if i < 10 { 149 continue 150 } 151 } 152 } 153 return out, err 154 } 155 } 156 157 // WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time. 158 // Deprecated: use cli.WaitCmd instead 159 func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error { 160 after := time.After(timeout) 161 162 args := append(arg, "inspect", "-f", expr, name) 163 for { 164 result := icmd.RunCommand(dockerBinary, args...) 165 if result.Error != nil { 166 if !strings.Contains(strings.ToLower(result.Stderr()), "no such") { 167 return errors.Errorf("error executing docker inspect: %v\n%s", 168 result.Stderr(), result.Stdout()) 169 } 170 select { 171 case <-after: 172 return result.Error 173 default: 174 time.Sleep(10 * time.Millisecond) 175 continue 176 } 177 } 178 179 out := strings.TrimSpace(result.Stdout()) 180 if out == expected { 181 break 182 } 183 184 select { 185 case <-after: 186 return errors.Errorf("condition \"%q == %q\" not true in time (%v)", out, expected, timeout) 187 default: 188 } 189 190 time.Sleep(100 * time.Millisecond) 191 } 192 return nil 193 } 194 195 // BuildImageCmdWithHost create a build command with the specified arguments. 196 // Deprecated 197 // FIXME(vdemeester) move this away 198 func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd { 199 args := []string{} 200 if host != "" { 201 args = append(args, "--host", host) 202 } 203 args = append(args, "build", "-t", name) 204 if !useCache { 205 args = append(args, "--no-cache") 206 } 207 args = append(args, buildFlags...) 208 args = append(args, "-") 209 buildCmd := exec.Command(dockerBinary, args...) 210 buildCmd.Stdin = strings.NewReader(dockerfile) 211 return buildCmd 212 }