|
@@ -1868,57 +1868,87 @@ func TestRunMutableNetworkFiles(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func TestRunHostsLinkedContainerUpdate(t *testing.T) {
|
|
|
- deleteAllContainers()
|
|
|
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", "while true; do sleep 1; done"))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err, out)
|
|
|
- }
|
|
|
+func TestRunStableIPAndPort(t *testing.T) {
|
|
|
+ const nContainers = 2
|
|
|
+ var ids, ips, ports [nContainers]string
|
|
|
+
|
|
|
+ // Setup: Create a couple of containers and collect their IPs and public ports.
|
|
|
+ for i := 0; i < nContainers; i++ {
|
|
|
+ runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
|
|
|
+ out, _, err := runCommandWithOutput(runCmd)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ ids[i] = strings.TrimSpace(out)
|
|
|
+ ips[i], err = inspectField(ids[i], "NetworkSettings.IPAddress")
|
|
|
+ errorOut(err, t, out)
|
|
|
+ if ips[i] == "" {
|
|
|
+ t.Fatal("IP allocation failed")
|
|
|
+ }
|
|
|
|
|
|
- // TODO fix docker cp and /etc/hosts
|
|
|
- out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--link", "c1:c1", "--name", "c2", "busybox", "sh", "-c", "while true;do sleep 1; done"))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err, out)
|
|
|
+ portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
|
|
|
+ ports[i], _, err = runCommandWithOutput(portCmd)
|
|
|
+ errorOut(err, t, out)
|
|
|
+ if ports[i] == "" {
|
|
|
+ t.Fatal("Port allocation failed")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- contID := strings.TrimSpace(out)
|
|
|
-
|
|
|
- f, err := os.Open(filepath.Join("/var/lib/docker/containers", contID, "hosts"))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
+ // Stop them all.
|
|
|
+ for _, id := range ids {
|
|
|
+ cmd := exec.Command(dockerBinary, "stop", id)
|
|
|
+ out, _, err := runCommandWithOutput(cmd)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err, out)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- originalContent, err := ioutil.ReadAll(f)
|
|
|
- f.Close()
|
|
|
+ // Create a new container and ensure it's not getting the IP or port of some stopped container.
|
|
|
+ {
|
|
|
+ runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
|
|
|
+ out, _, err := runCommandWithOutput(runCmd)
|
|
|
+ errorOut(err, t, out)
|
|
|
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ id := strings.TrimSpace(out)
|
|
|
+ ip, err := inspectField(id, "NetworkSettings.IPAddress")
|
|
|
+ errorOut(err, t, out)
|
|
|
|
|
|
- out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "restart", "-t", "0", "c1"))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err, out)
|
|
|
- }
|
|
|
+ portCmd := exec.Command(dockerBinary, "port", id, "1234")
|
|
|
+ port, _, err := runCommandWithOutput(portCmd)
|
|
|
+ errorOut(err, t, out)
|
|
|
|
|
|
- f, err = os.Open(filepath.Join("/var/lib/docker/containers", contID, "hosts"))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
+ for i := range ids {
|
|
|
+ if ip == ips[i] {
|
|
|
+ t.Fatalf("Conflicting IP: %s", ip)
|
|
|
+ }
|
|
|
+ if port == ports[i] {
|
|
|
+ t.Fatalf("Conflicting port: %s", port)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- newContent, err := ioutil.ReadAll(f)
|
|
|
- f.Close()
|
|
|
+ // Start the containers back, and ensure they are getting the same IPs and ports.
|
|
|
+ for i, id := range ids {
|
|
|
+ runCmd := exec.Command(dockerBinary, "start", id)
|
|
|
+ out, _, err := runCommandWithOutput(runCmd)
|
|
|
+ errorOut(err, t, out)
|
|
|
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ ip, err := inspectField(id, "NetworkSettings.IPAddress")
|
|
|
+ errorOut(err, t, out)
|
|
|
+ portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
|
|
|
+ port, _, err := runCommandWithOutput(portCmd)
|
|
|
+ errorOut(err, t, out)
|
|
|
|
|
|
- if strings.TrimSpace(string(originalContent)) == strings.TrimSpace(string(newContent)) {
|
|
|
- t.Fatalf("expected /etc/hosts to be updated, but wasn't")
|
|
|
+ if ips[i] != ip {
|
|
|
+ t.Fatalf("Container started with a different IP: %s != %s", ip, ips[i])
|
|
|
+ }
|
|
|
+ if ports[i] != port {
|
|
|
+ t.Fatalf("Container started with a different port: %s != %s", port, ports[i])
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
deleteAllContainers()
|
|
|
-
|
|
|
- logDone("run - /etc/hosts updated in parent when restart")
|
|
|
+ logDone("run - ips and ports must not change")
|
|
|
}
|
|
|
|
|
|
// Ensure that CIDFile gets deleted if it's empty
|