Browse code

Port environment test Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/18 11:53:08
Showing 2 changed files
... ...
@@ -2,8 +2,10 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"os"
5 6
 	"os/exec"
6 7
 	"regexp"
8
+	"sort"
7 9
 	"strings"
8 10
 	"sync"
9 11
 	"testing"
... ...
@@ -515,3 +517,46 @@ func TestRunTwoConcurrentContainers(t *testing.T) {
515 515
 
516 516
 	logDone("run - two concurrent containers")
517 517
 }
518
+
519
+func TestEnvironment(t *testing.T) {
520
+	cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "busybox", "env")
521
+	cmd.Env = append(os.Environ(),
522
+		"TRUE=false",
523
+		"TRICKY=tri\ncky\n",
524
+	)
525
+
526
+	out, _, err := runCommandWithOutput(cmd)
527
+	if err != nil {
528
+		t.Fatal(err, out)
529
+	}
530
+
531
+	actualEnv := strings.Split(out, "\n")
532
+	if actualEnv[len(actualEnv)-1] == "" {
533
+		actualEnv = actualEnv[:len(actualEnv)-1]
534
+	}
535
+	sort.Strings(actualEnv)
536
+
537
+	goodEnv := []string{
538
+		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
539
+		"HOME=/",
540
+		"HOSTNAME=testing",
541
+		"FALSE=true",
542
+		"TRUE=false",
543
+		"TRICKY=tri",
544
+		"cky",
545
+		"",
546
+	}
547
+	sort.Strings(goodEnv)
548
+	if len(goodEnv) != len(actualEnv) {
549
+		t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
550
+	}
551
+	for i := range goodEnv {
552
+		if actualEnv[i] != goodEnv[i] {
553
+			t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
554
+		}
555
+	}
556
+
557
+	deleteAllContainers()
558
+
559
+	logDone("run - verify environment")
560
+}
... ...
@@ -306,60 +306,6 @@ func TestTty(t *testing.T) {
306 306
 	}
307 307
 }
308 308
 
309
-func TestEnv(t *testing.T) {
310
-	os.Setenv("TRUE", "false")
311
-	os.Setenv("TRICKY", "tri\ncky\n")
312
-	daemon := mkDaemon(t)
313
-	defer nuke(daemon)
314
-	config, _, _, err := runconfig.Parse([]string{"-e=FALSE=true", "-e=TRUE", "-e=TRICKY", GetTestImage(daemon).ID, "env"}, nil)
315
-	if err != nil {
316
-		t.Fatal(err)
317
-	}
318
-	container, _, err := daemon.Create(config, "")
319
-	if err != nil {
320
-		t.Fatal(err)
321
-	}
322
-	defer daemon.Destroy(container)
323
-
324
-	stdout, err := container.StdoutPipe()
325
-	if err != nil {
326
-		t.Fatal(err)
327
-	}
328
-	defer stdout.Close()
329
-	if err := container.Start(); err != nil {
330
-		t.Fatal(err)
331
-	}
332
-	container.Wait()
333
-	output, err := ioutil.ReadAll(stdout)
334
-	if err != nil {
335
-		t.Fatal(err)
336
-	}
337
-	actualEnv := strings.Split(string(output), "\n")
338
-	if actualEnv[len(actualEnv)-1] == "" {
339
-		actualEnv = actualEnv[:len(actualEnv)-1]
340
-	}
341
-	sort.Strings(actualEnv)
342
-	goodEnv := []string{
343
-		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
344
-		"HOME=/",
345
-		"HOSTNAME=" + utils.TruncateID(container.ID),
346
-		"FALSE=true",
347
-		"TRUE=false",
348
-		"TRICKY=tri",
349
-		"cky",
350
-		"",
351
-	}
352
-	sort.Strings(goodEnv)
353
-	if len(goodEnv) != len(actualEnv) {
354
-		t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
355
-	}
356
-	for i := range goodEnv {
357
-		if actualEnv[i] != goodEnv[i] {
358
-			t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
359
-		}
360
-	}
361
-}
362
-
363 309
 func TestEntrypoint(t *testing.T) {
364 310
 	daemon := mkDaemon(t)
365 311
 	defer nuke(daemon)