Browse code

Test for check /etc/resolv.conf on every docker run for 127.* content.

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)

Jessica Frazelle authored on 2014/10/08 07:35:32
Showing 1 changed files
... ...
@@ -751,7 +751,7 @@ func TestRunEnvironment(t *testing.T) {
751 751
 	}
752 752
 	sort.Strings(goodEnv)
753 753
 	if len(goodEnv) != len(actualEnv) {
754
-		t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
754
+		t.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
755 755
 	}
756 756
 	for i := range goodEnv {
757 757
 		if actualEnv[i] != goodEnv[i] {
... ...
@@ -1168,7 +1168,7 @@ func TestRunModeHostname(t *testing.T) {
1168 1168
 	}
1169 1169
 
1170 1170
 	if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
1171
-		t.Fatalf("expected 'testhostname', but says: '%s'", actual)
1171
+		t.Fatalf("expected 'testhostname', but says: %q", actual)
1172 1172
 	}
1173 1173
 
1174 1174
 	cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
... ...
@@ -1182,7 +1182,7 @@ func TestRunModeHostname(t *testing.T) {
1182 1182
 		t.Fatal(err)
1183 1183
 	}
1184 1184
 	if actual := strings.Trim(out, "\r\n"); actual != hostname {
1185
-		t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
1185
+		t.Fatalf("expected %q, but says: '%s'", hostname, actual)
1186 1186
 	}
1187 1187
 
1188 1188
 	deleteAllContainers()
... ...
@@ -1196,7 +1196,7 @@ func TestRunRootWorkdir(t *testing.T) {
1196 1196
 		t.Fatal(s, err)
1197 1197
 	}
1198 1198
 	if s != "/\n" {
1199
-		t.Fatalf("pwd returned '%s' (expected /\\n)", s)
1199
+		t.Fatalf("pwd returned %q (expected /\\n)", s)
1200 1200
 	}
1201 1201
 
1202 1202
 	deleteAllContainers()
... ...
@@ -1297,7 +1297,7 @@ func TestRunDnsOptions(t *testing.T) {
1297 1297
 
1298 1298
 	actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
1299 1299
 	if actual != "nameserver 127.0.0.1 search mydomain" {
1300
-		t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
1300
+		t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: %q", actual)
1301 1301
 	}
1302 1302
 
1303 1303
 	cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
... ...
@@ -1309,61 +1309,101 @@ func TestRunDnsOptions(t *testing.T) {
1309 1309
 
1310 1310
 	actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
1311 1311
 	if actual != "nameserver 127.0.0.1" {
1312
-		t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
1312
+		t.Fatalf("expected 'nameserver 127.0.0.1', but says: %q", actual)
1313 1313
 	}
1314 1314
 
1315 1315
 	logDone("run - dns options")
1316 1316
 }
1317 1317
 
1318 1318
 func TestRunDnsOptionsBasedOnHostResolvConf(t *testing.T) {
1319
-	resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
1319
+	var out string
1320
+
1321
+	origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
1320 1322
 	if os.IsNotExist(err) {
1321 1323
 		t.Fatalf("/etc/resolv.conf does not exist")
1322 1324
 	}
1323 1325
 
1324
-	hostNamservers := resolvconf.GetNameservers(resolvConf)
1325
-	hostSearch := resolvconf.GetSearchDomains(resolvConf)
1326
+	hostNamservers := resolvconf.GetNameservers(origResolvConf)
1327
+	hostSearch := resolvconf.GetSearchDomains(origResolvConf)
1326 1328
 
1327 1329
 	cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
1328 1330
 
1329
-	out, _, err := runCommandWithOutput(cmd)
1330
-	if err != nil {
1331
+	if out, _, err = runCommandWithOutput(cmd); err != nil {
1331 1332
 		t.Fatal(err, out)
1332 1333
 	}
1333 1334
 
1334 1335
 	if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
1335
-		t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
1336
+		t.Fatalf("expected '127.0.0.1', but says: %q", string(actualNameservers[0]))
1336 1337
 	}
1337 1338
 
1338 1339
 	actualSearch := resolvconf.GetSearchDomains([]byte(out))
1339 1340
 	if len(actualSearch) != len(hostSearch) {
1340
-		t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
1341
+		t.Fatalf("expected %q search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
1341 1342
 	}
1342 1343
 	for i := range actualSearch {
1343 1344
 		if actualSearch[i] != hostSearch[i] {
1344
-			t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
1345
+			t.Fatalf("expected %q domain, but says: '%s'", actualSearch[i], hostSearch[i])
1345 1346
 		}
1346 1347
 	}
1347 1348
 
1348 1349
 	cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
1349 1350
 
1350
-	out, _, err = runCommandWithOutput(cmd)
1351
-	if err != nil {
1351
+	if out, _, err = runCommandWithOutput(cmd); err != nil {
1352 1352
 		t.Fatal(err, out)
1353 1353
 	}
1354 1354
 
1355 1355
 	actualNameservers := resolvconf.GetNameservers([]byte(out))
1356 1356
 	if len(actualNameservers) != len(hostNamservers) {
1357
-		t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
1357
+		t.Fatalf("expected %q nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
1358 1358
 	}
1359 1359
 	for i := range actualNameservers {
1360 1360
 		if actualNameservers[i] != hostNamservers[i] {
1361
-			t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
1361
+			t.Fatalf("expected %q nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
1362 1362
 		}
1363 1363
 	}
1364 1364
 
1365 1365
 	if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
1366
-		t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
1366
+		t.Fatalf("expected 'mydomain', but says: %q", string(actualSearch[0]))
1367
+	}
1368
+
1369
+	// test with file
1370
+	tmpResolvConf := []byte("search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1")
1371
+	if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
1372
+		t.Fatal(err)
1373
+	}
1374
+	// put the old resolvconf back
1375
+	defer func() {
1376
+		if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
1377
+			t.Fatal(err)
1378
+		}
1379
+	}()
1380
+
1381
+	resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
1382
+	if os.IsNotExist(err) {
1383
+		t.Fatalf("/etc/resolv.conf does not exist")
1384
+	}
1385
+
1386
+	hostNamservers = resolvconf.GetNameservers(resolvConf)
1387
+	hostSearch = resolvconf.GetSearchDomains(resolvConf)
1388
+
1389
+	cmd = exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
1390
+
1391
+	if out, _, err = runCommandWithOutput(cmd); err != nil {
1392
+		t.Fatal(err, out)
1393
+	}
1394
+
1395
+	if actualNameservers = resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "12.34.56.78" || len(actualNameservers) != 1 {
1396
+		t.Fatalf("expected '12.34.56.78', but has: %v", actualNameservers)
1397
+	}
1398
+
1399
+	actualSearch = resolvconf.GetSearchDomains([]byte(out))
1400
+	if len(actualSearch) != len(hostSearch) {
1401
+		t.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch))
1402
+	}
1403
+	for i := range actualSearch {
1404
+		if actualSearch[i] != hostSearch[i] {
1405
+			t.Fatalf("expected %q domain, but says: '%s'", actualSearch[i], hostSearch[i])
1406
+		}
1367 1407
 	}
1368 1408
 
1369 1409
 	deleteAllContainers()
... ...
@@ -1382,7 +1422,7 @@ func TestRunAddHost(t *testing.T) {
1382 1382
 
1383 1383
 	actual := strings.Trim(out, "\r\n")
1384 1384
 	if actual != "86.75.30.9\textra" {
1385
-		t.Fatalf("expected '86.75.30.9\textra', but says: '%s'", actual)
1385
+		t.Fatalf("expected '86.75.30.9\textra', but says: %q", actual)
1386 1386
 	}
1387 1387
 
1388 1388
 	logDone("run - add-host option")
... ...
@@ -1989,7 +2029,7 @@ func TestRunCidFileCleanupIfEmpty(t *testing.T) {
1989 1989
 	}
1990 1990
 
1991 1991
 	if _, err := os.Stat(tmpCidFile); err == nil {
1992
-		t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile)
1992
+		t.Fatalf("empty CIDFile %q should've been deleted", tmpCidFile)
1993 1993
 	}
1994 1994
 	deleteAllContainers()
1995 1995
 	logDone("run - cleanup empty cidfile on fail")
... ...
@@ -2017,7 +2057,7 @@ func TestRunCidFileCheckIDLength(t *testing.T) {
2017 2017
 	}
2018 2018
 	cid := string(buffer)
2019 2019
 	if len(cid) != 64 {
2020
-		t.Fatalf("--cidfile should be a long id, not '%s'", id)
2020
+		t.Fatalf("--cidfile should be a long id, not %q", id)
2021 2021
 	}
2022 2022
 	if cid != id {
2023 2023
 		t.Fatalf("cid must be equal to %s, got %s", id, cid)