Browse code

Test for updating hosts files via links.

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)

Erik Hollensbe authored on 2014/10/25 06:39:12
Showing 2 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"os/exec"
7 7
 	"strings"
8 8
 	"testing"
9
+	"time"
9 10
 
10 11
 	"github.com/docker/docker/pkg/iptables"
11 12
 )
... ...
@@ -177,3 +178,39 @@ func TestLinksNotStartedParentNotFail(t *testing.T) {
177 177
 	}
178 178
 	logDone("link - container start not failing on updating stopped parent links")
179 179
 }
180
+
181
+func TestLinksHostsFilesInject(t *testing.T) {
182
+	defer deleteAllContainers()
183
+
184
+	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
185
+	if err != nil {
186
+		t.Fatal(err, out)
187
+	}
188
+
189
+	idOne := strings.TrimSpace(out)
190
+
191
+	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
192
+	if err != nil {
193
+		t.Fatal(err, out)
194
+	}
195
+
196
+	idTwo := strings.TrimSpace(out)
197
+
198
+	time.Sleep(1 * time.Second)
199
+
200
+	contentOne, err := readContainerFile(idOne, "hosts")
201
+	if err != nil {
202
+		t.Fatal(err, string(contentOne))
203
+	}
204
+
205
+	contentTwo, err := readContainerFile(idTwo, "hosts")
206
+	if err != nil {
207
+		t.Fatal(err, string(contentTwo))
208
+	}
209
+
210
+	if !strings.Contains(string(contentTwo), "onetwo") {
211
+		t.Fatal("Host is not present in updated hosts file", string(contentTwo))
212
+	}
213
+
214
+	logDone("link - ensure containers hosts files are updated with the link alias.")
215
+}
... ...
@@ -736,10 +736,11 @@ func containerStorageFile(containerId, basename string) string {
736 736
 	return filepath.Join("/var/lib/docker/containers", containerId, basename)
737 737
 }
738 738
 
739
+// docker commands that use this function must be run with the '-d' switch.
739 740
 func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
740 741
 	out, _, err := runCommandWithOutput(cmd)
741 742
 	if err != nil {
742
-		return nil, err
743
+		return nil, fmt.Errorf("%v: %q", err, out)
743 744
 	}
744 745
 
745 746
 	time.Sleep(1 * time.Second)