func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) { c.Assert(s.d.StartWithBusybox(), checker.IsNil) out, _, err := s.d.buildImageWithOut("test", `FROM busybox LABEL foo=bar`, true, "-q") c.Assert(err, checker.IsNil) id := strings.TrimSpace(out) out, err = s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id) out, err = s.d.Cmd("image", "prune", "--force") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id) out, err = s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id) out, err = s.d.Cmd("image", "prune", "--force", "--all") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id) out, err = s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id) }
// regression test for non-empty fields from #13901 func (s *DockerSuite) TestContainerPsOmitFields(c *check.C) { testRequires(c, DaemonIsLinux) name := "pstest" port := 80 dockerCmd(c, "run", "-d", "--name", name, "--expose", strconv.Itoa(port), "busybox", "top") status, body, err := sockRequest("GET", "/containers/json?all=1", nil) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) var resp []containerPs err = json.Unmarshal(body, &resp) c.Assert(err, checker.IsNil) var foundContainer *containerPs for _, container := range resp { for _, testName := range container.Names { if "/"+name == testName { foundContainer = &container break } } } c.Assert(foundContainer.Ports, checker.HasLen, 1) c.Assert(foundContainer.Ports[0]["PrivatePort"], checker.Equals, float64(port)) _, ok := foundContainer.Ports[0]["PublicPort"] c.Assert(ok, checker.Not(checker.Equals), true) _, ok = foundContainer.Ports[0]["IP"] c.Assert(ok, checker.Not(checker.Equals), true) }
func (s *DockerSwarmSuite) TestSecretCreateWithFile(c *check.C) { d := s.AddDaemon(c, true, true) testFile, err := ioutil.TempFile("", "secretCreateTest") c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file")) defer os.Remove(testFile.Name()) testData := "TESTINGDATA" _, err = testFile.Write([]byte(testData)) c.Assert(err, checker.IsNil, check.Commentf("failed to write to temporary file")) testName := "test_secret" out, err := d.Cmd("secret", "create", "--file", testFile.Name(), testName) c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "", check.Commentf(out)) id := strings.TrimSpace(out) secret := d.GetSecret(c, id) c.Assert(secret.Spec.Name, checker.Equals, testName) testName = "test_secret_2" out, err = d.Cmd("secret", "create", testName, "-f", testFile.Name()) c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "", check.Commentf(out)) id = strings.TrimSpace(out) secret = d.GetSecret(c, id) c.Assert(secret.Spec.Name, checker.Equals, testName) }
func (s *DockerSuite) TestSaveLoadNoTag(c *check.C) { testRequires(c, DaemonIsLinux) name := "saveloadnotag" _, err := buildImage(name, "FROM busybox\nENV foo=bar", true) c.Assert(err, checker.IsNil, check.Commentf("%v", err)) id := inspectField(c, name, "Id") // Test to make sure that save w/o name just shows imageID during load out, _, err := runCommandPipelineWithOutput( exec.Command(dockerBinary, "save", id), exec.Command(dockerBinary, "load")) c.Assert(err, checker.IsNil, check.Commentf("failed to save and load repo: %s, %v", out, err)) // Should not show 'name' but should show the image ID during the load c.Assert(out, checker.Not(checker.Contains), "Loaded image: ") c.Assert(out, checker.Contains, "Loaded image ID:") c.Assert(out, checker.Contains, id) // Test to make sure that save by name shows that name during load out, _, err = runCommandPipelineWithOutput( exec.Command(dockerBinary, "save", name), exec.Command(dockerBinary, "load")) c.Assert(err, checker.IsNil, check.Commentf("failed to save and load repo: %s, %v", out, err)) c.Assert(out, checker.Contains, "Loaded image: "+name+":latest") c.Assert(out, checker.Not(checker.Contains), "Loaded image ID:") }
func (s *DockerSuite) TestPsByOrder(c *check.C) { name1 := "xyz-abc" out, err := runSleepingContainer(c, "--name", name1) c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") container1 := strings.TrimSpace(out) name2 := "xyz-123" out, err = runSleepingContainer(c, "--name", name2) c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") container2 := strings.TrimSpace(out) name3 := "789-abc" out, err = runSleepingContainer(c, "--name", name3) c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") name4 := "789-123" out, err = runSleepingContainer(c, "--name", name4) c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") // Run multiple time should have the same result out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz") c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1)) // Run multiple time should have the same result out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz") c.Assert(err, checker.NotNil) c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1)) }
func (s *DockerSwarmSuite) TestServiceCreateWithSecretSourceTarget(c *check.C) { d := s.AddDaemon(c, true, true) serviceName := "test-service-secret" testName := "test_secret" id := d.createSecret(c, swarm.SecretSpec{ swarm.Annotations{ Name: testName, }, []byte("TESTINGDATA"), }) c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id)) testTarget := "testing" out, err := d.Cmd("service", "create", "--name", serviceName, "--secret", fmt.Sprintf("source=%s,target=%s", testName, testTarget), "busybox", "top") c.Assert(err, checker.IsNil, check.Commentf(out)) out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName) c.Assert(err, checker.IsNil) var refs []swarm.SecretReference c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil) c.Assert(refs, checker.HasLen, 1) c.Assert(refs[0].SecretName, checker.Equals, testName) c.Assert(refs[0].File, checker.Not(checker.IsNil)) c.Assert(refs[0].File.Name, checker.Equals, testTarget) }
// regression test for non-empty fields from #13901 func (s *DockerSuite) TestContainerApiPsOmitFields(c *check.C) { // Problematic for Windows porting due to networking not yet being passed back testRequires(c, DaemonIsLinux) name := "pstest" port := 80 runSleepingContainer(c, "--name", name, "--expose", strconv.Itoa(port)) status, body, err := sockRequest("GET", "/containers/json?all=1", nil) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) var resp []containerPs err = json.Unmarshal(body, &resp) c.Assert(err, checker.IsNil) var foundContainer *containerPs for _, container := range resp { for _, testName := range container.Names { if "/"+name == testName { foundContainer = &container break } } } c.Assert(foundContainer.Ports, checker.HasLen, 1) c.Assert(foundContainer.Ports[0]["PrivatePort"], checker.Equals, float64(port)) _, ok := foundContainer.Ports[0]["PublicPort"] c.Assert(ok, checker.Not(checker.Equals), true) _, ok = foundContainer.Ports[0]["IP"] c.Assert(ok, checker.Not(checker.Equals), true) }
func (s *DockerSwarmSuite) TestSwarmNodeTaskListFilter(c *check.C) { d := s.AddDaemon(c, true, true) name := "redis-cluster-md5" out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") // make sure task has been deployed. waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) filter := "name=redis-cluster" out, err = d.Cmd("node", "tasks", "--filter", filter, "self") c.Assert(err, checker.IsNil) c.Assert(out, checker.Contains, name+".1") c.Assert(out, checker.Contains, name+".2") c.Assert(out, checker.Contains, name+".3") out, err = d.Cmd("node", "tasks", "--filter", "name=none", "self") c.Assert(err, checker.IsNil) c.Assert(out, checker.Not(checker.Contains), name+".1") c.Assert(out, checker.Not(checker.Contains), name+".2") c.Assert(out, checker.Not(checker.Contains), name+".3") }
func (s *DockerSuite) TestSaveImageId(c *check.C) { testRequires(c, DaemonIsLinux) repoName := "foobar-save-image-id-test" dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName)) out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName) cleanedLongImageID := strings.TrimSpace(out) out, _ = dockerCmd(c, "images", "-q", repoName) cleanedShortImageID := strings.TrimSpace(out) // Make sure IDs are not empty c.Assert(cleanedLongImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty.")) c.Assert(cleanedShortImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty.")) saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID) tarCmd := exec.Command("tar", "t") var err error tarCmd.Stdin, err = saveCmd.StdoutPipe() c.Assert(err, checker.IsNil, check.Commentf("cannot set stdout pipe for tar: %v", err)) grepCmd := exec.Command("grep", cleanedLongImageID) grepCmd.Stdin, err = tarCmd.StdoutPipe() c.Assert(err, checker.IsNil, check.Commentf("cannot set stdout pipe for grep: %v", err)) c.Assert(tarCmd.Start(), checker.IsNil, check.Commentf("tar failed with error: %v", err)) c.Assert(saveCmd.Start(), checker.IsNil, check.Commentf("docker save failed with error: %v", err)) defer saveCmd.Wait() defer tarCmd.Wait() out, _, err = runCommandWithOutput(grepCmd) c.Assert(err, checker.IsNil, check.Commentf("failed to save repo with image ID: %s, %v", out, err)) }
// test case for #23055 func (s *DockerSuite) TestSearchWithLimit(c *check.C) { testRequires(c, Network, DaemonIsLinux) limit := 10 out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker") c.Assert(err, checker.IsNil) outSlice := strings.Split(out, "\n") c.Assert(outSlice, checker.HasLen, limit+2) // 1 header, 1 carriage return limit = 50 out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker") c.Assert(err, checker.IsNil) outSlice = strings.Split(out, "\n") c.Assert(outSlice, checker.HasLen, limit+2) // 1 header, 1 carriage return limit = 100 out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker") c.Assert(err, checker.IsNil) outSlice = strings.Split(out, "\n") c.Assert(outSlice, checker.HasLen, limit+2) // 1 header, 1 carriage return limit = 0 out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker") c.Assert(err, checker.Not(checker.IsNil)) limit = 200 out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker") c.Assert(err, checker.Not(checker.IsNil)) }
func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) { // TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now. testRequires(c, DaemonIsLinux) name := "testlogssincefuturefollow" out, _ := dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`) // Extract one timestamp from the log file to give us a starting point for // our `--since` argument. Because the log producer runs in the background, // we need to check repeatedly for some output to be produced. var timestamp string for i := 0; i != 100 && timestamp == ""; i++ { if out, _ = dockerCmd(c, "logs", "-t", name); out == "" { time.Sleep(time.Millisecond * 100) // Retry } else { timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0] } } c.Assert(timestamp, checker.Not(checker.Equals), "") t, err := time.Parse(time.RFC3339Nano, timestamp) c.Assert(err, check.IsNil) since := t.Unix() + 2 out, _ = dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name) c.Assert(out, checker.Not(checker.HasLen), 0, check.Commentf("cannot read from empty log")) lines := strings.Split(strings.TrimSpace(out), "\n") for _, v := range lines { ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0]) c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'", v)) c.Assert(ts.Unix() >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts)) } }
func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *check.C) { d := s.AddDaemon(c, true, true) out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "testnet") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") networkID := strings.TrimSpace(out) out, err = d.Cmd("run", "-d", "--net", networkID, "busybox", "top") c.Assert(err, checker.IsNil) cID := strings.TrimSpace(out) d.waitRun(cID) _, err = d.Cmd("rm", "-f", cID) c.Assert(err, checker.IsNil) out, err = d.Cmd("network", "rm", "testnet") c.Assert(err, checker.IsNil) checkNetwork := func(*check.C) (interface{}, check.CommentInterface) { out, err := d.Cmd("network", "ls") c.Assert(err, checker.IsNil) return out, nil } waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains), "testnet") }
func (s *DockerSuite) TestEventsFilterImageName(c *check.C) { since := daemonUnixTime(c) out, _ := dockerCmd(c, "run", "--name", "container_1", "-d", "busybox:latest", "true") container1 := strings.TrimSpace(out) out, _ = dockerCmd(c, "run", "--name", "container_2", "-d", "busybox", "true") container2 := strings.TrimSpace(out) name := "busybox" out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("image=%s", name)) events := strings.Split(out, "\n") events = events[:len(events)-1] c.Assert(events, checker.Not(checker.HasLen), 0) //Expected events but found none for the image busybox:latest count1 := 0 count2 := 0 for _, e := range events { if strings.Contains(e, container1) { count1++ } else if strings.Contains(e, container2) { count2++ } } c.Assert(count1, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count1, container1)) c.Assert(count2, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count2, container2)) }
func (s *DockerSwarmSuite) TestPsListContainersFilterIsTask(c *check.C) { d := s.AddDaemon(c, true, true) // Create a bare container out, err := d.Cmd("run", "-d", "--name=bare-container", "busybox", "top") c.Assert(err, checker.IsNil) bareID := strings.TrimSpace(out)[:12] // Create a service name := "busybox-top" out, err = d.Cmd("service", "create", "--name", name, "busybox", "top") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") // make sure task has been deployed. waitAndAssert(c, defaultReconciliationTimeout, d.checkServiceRunningTasks(c, name), checker.Equals, 1) // Filter non-tasks out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=false") c.Assert(err, checker.IsNil) psOut := strings.TrimSpace(out) c.Assert(psOut, checker.Equals, bareID, check.Commentf("Expected id %s, got %s for is-task label, output %q", bareID, psOut, out)) // Filter tasks out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=true") c.Assert(err, checker.IsNil) lines := strings.Split(strings.Trim(out, "\n "), "\n") c.Assert(lines, checker.HasLen, 1) c.Assert(lines[0], checker.Not(checker.Equals), bareID, check.Commentf("Expected not %s, but got it for is-task label, output %q", bareID, out)) }
func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) { dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") dockerCmd(c, "run", "--name=not-shown", "-d", "busybox", "top") out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown") c.Assert(err, checker.Not(check.IsNil)) c.Assert(out, checker.Contains, "busybox") c.Assert(out, checker.Not(checker.Contains), "not-shown") c.Assert(out, checker.Contains, "Error: No such container: missing") }
func (s *DockerSwarmSuite) TestSwarmInitLocked(c *check.C) { d := s.AddDaemon(c, false, false) outs, err := d.Cmd("swarm", "init", "--autolock") c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs)) c.Assert(outs, checker.Contains, "docker swarm unlock") var unlockKey string for _, line := range strings.Split(outs, "\n") { if strings.Contains(line, "SWMKEY") { unlockKey = strings.TrimSpace(line) break } } c.Assert(unlockKey, checker.Not(checker.Equals), "") outs, err = d.Cmd("swarm", "unlock-key", "-q") c.Assert(outs, checker.Equals, unlockKey+"\n") c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive) // It starts off locked c.Assert(d.Restart(), checker.IsNil) c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked) cmd := d.command("swarm", "unlock") cmd.Stdin = bytes.NewBufferString("wrong-secret-key") out, err := cmd.CombinedOutput() c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(out))) c.Assert(string(out), checker.Contains, "invalid key") c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked) cmd = d.command("swarm", "unlock") cmd.Stdin = bytes.NewBufferString(unlockKey) out, err = cmd.CombinedOutput() c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out))) c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive) outs, err = d.Cmd("node", "ls") c.Assert(err, checker.IsNil) c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked") outs, err = d.Cmd("swarm", "update", "--autolock=false") c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs)) checkSwarmLockedToUnlocked(c, d, unlockKey) outs, err = d.Cmd("node", "ls") c.Assert(err, checker.IsNil) c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked") }
func (s *DockerDaemonSuite) TestDaemonEventsWithFilters(c *check.C) { testRequires(c, SameHostDaemon, DaemonIsLinux) // daemon config file configFilePath := "test.json" configFile, err := os.Create(configFilePath) c.Assert(err, checker.IsNil) defer os.Remove(configFilePath) daemonConfig := `{"labels":["foo=bar"]}` fmt.Fprintf(configFile, "%s", daemonConfig) configFile.Close() c.Assert(s.d.Start(fmt.Sprintf("--config-file=%s", configFilePath)), check.IsNil) // Get daemon ID out, err := s.d.Cmd("info") c.Assert(err, checker.IsNil) daemonID := "" daemonName := "" for _, line := range strings.Split(out, "\n") { if strings.HasPrefix(line, "ID: ") { daemonID = strings.TrimPrefix(line, "ID: ") } else if strings.HasPrefix(line, "Name: ") { daemonName = strings.TrimPrefix(line, "Name: ") } } c.Assert(daemonID, checker.Not(checker.Equals), "") syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP) time.Sleep(3 * time.Second) out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonID)) c.Assert(err, checker.IsNil) c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonName)) c.Assert(err, checker.IsNil) c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "daemon=foo") c.Assert(err, checker.IsNil) c.Assert(out, checker.Not(checker.Contains), fmt.Sprintf("daemon reload %s", daemonID)) out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=daemon") c.Assert(err, checker.IsNil) c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=container") c.Assert(err, checker.IsNil) c.Assert(out, checker.Not(checker.Contains), fmt.Sprintf("daemon reload %s", daemonID)) }
func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) { testRequires(c, DaemonIsLinux) var out string out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top") idA := strings.TrimSpace(out) c.Assert(idA, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out)) out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top") idB := strings.TrimSpace(out) c.Assert(idB, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out)) dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1") dockerCmd(c, "rename", "container1", "container_new") dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1") }
// TestInfoDiscoveryInvalidAdvertise verifies that a daemon run with // an invalid `--cluster-advertise` configuration func (s *DockerSuite) TestInfoDiscoveryInvalidAdvertise(c *check.C) { testRequires(c, SameHostDaemon, DaemonIsLinux) d := NewDaemon(c) discoveryBackend := "consul://consuladdr:consulport/some/path" // --cluster-advertise with an invalid string is an error err := d.Start(fmt.Sprintf("--cluster-store=%s", discoveryBackend), "--cluster-advertise=invalid") c.Assert(err, checker.Not(checker.IsNil)) // --cluster-advertise without --cluster-store is also an error err = d.Start("--cluster-advertise=1.1.1.1:2375") c.Assert(err, checker.Not(checker.IsNil)) }
func (s *DockerSuite) TestInspectApiBridgeNetworkSettings121(c *check.C) { out, _ := dockerCmd(c, "run", "-d", "busybox", "true") cleanedContainerID := strings.TrimSpace(out) body := getInspectBody(c, "v1.21", cleanedContainerID) var inspectJSON types.ContainerJSON err := json.Unmarshal(body, &inspectJSON) c.Assert(err, checker.IsNil) settings := inspectJSON.NetworkSettings c.Assert(settings.IPAddress, checker.Not(checker.HasLen), 0) c.Assert(settings.Networks["bridge"], checker.Not(checker.IsNil)) c.Assert(settings.IPAddress, checker.Equals, settings.Networks["bridge"].IPAddress) }
func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) { testRequires(c, DaemonIsLinux) out, _ := dockerCmd(c, "run", "-d", "busybox", "true") out = strings.TrimSpace(out) name, err := inspectField(out, "GraphDriver.Name") c.Assert(err, checker.IsNil) checkValidGraphDriver(c, name) if name != "devicemapper" { return } imageDeviceID, err := inspectField("busybox", "GraphDriver.Data.DeviceId") c.Assert(err, checker.IsNil) deviceID, err := inspectField(out, "GraphDriver.Data.DeviceId") c.Assert(err, checker.IsNil) c.Assert(imageDeviceID, checker.Not(checker.Equals), deviceID) _, err = strconv.Atoi(deviceID) c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err)) deviceSize, err := inspectField(out, "GraphDriver.Data.DeviceSize") c.Assert(err, checker.IsNil) _, err = strconv.ParseUint(deviceSize, 10, 64) c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)) }
func (s *DockerSuite) TestEventsFilterLabels(c *check.C) { since := daemonUnixTime(c) label := "io.docker.testing=foo" out, _ := dockerCmd(c, "run", "-d", "-l", label, "busybox:latest", "true") container1 := strings.TrimSpace(out) out, _ = dockerCmd(c, "run", "-d", "busybox", "true") container2 := strings.TrimSpace(out) out, _ = dockerCmd( c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("label=%s", label)) events := strings.Split(strings.TrimSpace(out), "\n") c.Assert(len(events), checker.Equals, 3) for _, e := range events { c.Assert(e, checker.Contains, container1) c.Assert(e, checker.Not(checker.Contains), container2) } }
func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { testRequires(c, DaemonIsLinux) out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") containerID := strings.TrimSpace(out) dockerCmd(c, "commit", containerID, "busybox-test") imagesBefore, _ := dockerCmd(c, "images", "-a") dockerCmd(c, "tag", "busybox-test", "utest:tag1") dockerCmd(c, "tag", "busybox-test", "utest:tag2") dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3") dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4") { imagesAfter, _ := dockerCmd(c, "images", "-a") c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+4, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) } imgID, err := inspectField("busybox-test", "Id") c.Assert(err, checker.IsNil) // first checkout without force it fails out, _, err = dockerCmdWithError("rmi", imgID) // rmi tagged in multiple repos should have failed without force c.Assert(err, checker.NotNil) // rmi tagged in multiple repos should have failed without force c.Assert(out, checker.Contains, "(must be forced) - image is referenced in one or more repositories", check.Commentf("out: %s; err: %v;", out, err)) dockerCmd(c, "rmi", "-f", imgID) { imagesAfter, _ := dockerCmd(c, "images", "-a") // rmi failed, image still exists c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12]) } }
func (s *DockerSwarmSuite) TestSwarmNodeTaskListAll(c *check.C) { d := s.AddDaemon(c, true, true) name := "node-task-list" out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") // make sure task has been deployed. waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) out, err = d.Cmd("service", "ps", name) c.Assert(err, checker.IsNil) c.Assert(out, checker.Contains, name+".1") c.Assert(out, checker.Contains, name+".2") c.Assert(out, checker.Contains, name+".3") // Get the last container id so we can restart it to cause a task error in the history containerID, err := d.Cmd("ps", "-q", "-l") c.Assert(err, checker.IsNil) _, err = d.Cmd("stop", strings.TrimSpace(containerID)) c.Assert(err, checker.IsNil) waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) out, err = d.Cmd("node", "ps", "self") c.Assert(err, checker.IsNil) c.Assert(out, checker.Count, name, 3) out, err = d.Cmd("node", "ps", "self", "-a") c.Assert(err, checker.IsNil) c.Assert(out, checker.Count, name, 4) }
func (s *DockerSuite) TestLogsSince(c *check.C) { name := "testlogssince" dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done") out, _ := dockerCmd(c, "logs", "-t", name) log2Line := strings.Split(strings.Split(out, "\n")[1], " ") t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written c.Assert(err, checker.IsNil) since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name) // Skip 2 seconds unexpected := []string{"log1", "log2"} for _, v := range unexpected { c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since)) } // Test to make sure a bad since format is caught by the client out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name) c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server")) // Test with default value specified and parameter omitted expected := []string{"log1", "log2", "log3"} for _, cmd := range []*exec.Cmd{ exec.Command(dockerBinary, "logs", "-t", name), exec.Command(dockerBinary, "logs", "-t", "--since=0", name), } { out, _, err = runCommandWithOutput(cmd) c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out)) for _, v := range expected { c.Assert(out, checker.Contains, v) } } }
func (s *DockerSwarmSuite) TestSwarmNetworkPlugin(c *check.C) { d := s.AddDaemon(c, true, true) out, err := d.Cmd("network", "create", "-d", globalNetworkPlugin, "foo") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") name := "top" out, err = d.Cmd("service", "create", "--name", name, "--network", "foo", "busybox", "top") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") out, err = d.Cmd("service", "inspect", "--format", "{{range .Spec.Networks}}{{.Target}}{{end}}", name) c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Equals, "foo") }
// TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected. func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) { testRequires(c, DaemonIsLinux) out, err := s.CmdWithError("pull", "scratch") c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail")) c.Assert(out, checker.Contains, "'scratch' is a reserved name") c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch") }
func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) { testRequires(c, DaemonIsLinux) // create a container out, _ := dockerCmd(c, "create", "busybox") cID := strings.TrimSpace(out) shortCID := cID[:12] // Make sure it DOESN'T show up w/o a '-a' for normal 'ps' out, _ = dockerCmd(c, "ps", "-q") c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out)) // Make sure it DOES show up as 'Created' for 'ps -a' out, _ = dockerCmd(c, "ps", "-a") hits := 0 for _, line := range strings.Split(out, "\n") { if !strings.Contains(line, shortCID) { continue } hits++ c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line)) } c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)) // filter containers by 'create' - note, no -a needed out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created") containerOut := strings.TrimSpace(out) c.Assert(cID, checker.HasPrefix, containerOut) }
func (s *DockerSuite) TestExecWithPrivileged(c *check.C) { // Not applicable on Windows testRequires(c, DaemonIsLinux, NotUserNamespace) // Start main loop which attempts mknod repeatedly dockerCmd(c, "run", "-d", "--name", "parent", "--cap-drop=ALL", "busybox", "sh", "-c", `while (true); do if [ -e /exec_priv ]; then cat /exec_priv && mknod /tmp/sda b 8 0 && echo "Success"; else echo "Privileged exec has not run yet"; fi; usleep 10000; done`) // Check exec mknod doesn't work cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16") out, _, err := runCommandWithOutput(cmd) c.Assert(err, checker.NotNil, check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail")) c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail")) // Check exec mknod does work with --privileged cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", `echo "Running exec --privileged" > /exec_priv && mknod /tmp/sdb b 8 16 && usleep 50000 && echo "Finished exec --privileged" > /exec_priv && echo ok`) out, _, err = runCommandWithOutput(cmd) c.Assert(err, checker.IsNil) actual := strings.TrimSpace(out) c.Assert(actual, checker.Equals, "ok", check.Commentf("exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", out)) // Check subsequent unprivileged exec cannot mknod cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32") out, _, err = runCommandWithOutput(cmd) c.Assert(err, checker.NotNil, check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail")) c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail")) // Confirm at no point was mknod allowed logCmd := exec.Command(dockerBinary, "logs", "parent") out, _, err = runCommandWithOutput(logCmd) c.Assert(err, checker.IsNil) c.Assert(out, checker.Not(checker.Contains), "Success") }
func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) { testRequires(c, DaemonIsLinux) // create container 1 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") containerID1 := strings.TrimSpace(out) // tag as foobox out, _ = dockerCmd(c, "commit", containerID1, "foobox") imageID := stringid.TruncateID(strings.TrimSpace(out)) // overwrite the tag, making the previous image dangling dockerCmd(c, "tag", "-f", "busybox", "foobox") out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true") // Expect one dangling image c.Assert(strings.Count(out, imageID), checker.Equals, 1) out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=false") //dangling=false would not include dangling images c.Assert(out, checker.Not(checker.Contains), imageID) out, _ = dockerCmd(c, "images") //docker images still include dangling images c.Assert(out, checker.Contains, imageID) }