Sfoglia il codice sorgente

Autonomous testing: Don't depend on /var/lib/docker/images/ubuntu being
there. Instead, automatically downlaod the test image if not present.

Andrea Luzzardi 12 anni fa
parent
commit
dd84ba3485
3 ha cambiato i file con 55 aggiunte e 29 eliminazioni
  1. 19 19
      container_test.go
  2. 33 7
      docker_test.go
  3. 3 3
      filesystem_test.go

+ 19 - 19
container_test.go

@@ -18,7 +18,7 @@ func TestStart(t *testing.T) {
 		"start_test",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			Ram: 33554432,
 		},
@@ -54,7 +54,7 @@ func TestRun(t *testing.T) {
 		"run_test",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			Ram: 33554432,
 		},
@@ -84,7 +84,7 @@ func TestOutput(t *testing.T) {
 		"output_test",
 		"echo",
 		[]string{"-n", "foobar"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -109,7 +109,7 @@ func TestKill(t *testing.T) {
 		"stop_test",
 		"cat",
 		[]string{"/dev/zero"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -152,7 +152,7 @@ func TestExitCode(t *testing.T) {
 		"exit_test_1",
 		"/bin/true",
 		[]string{""},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -167,7 +167,7 @@ func TestExitCode(t *testing.T) {
 		"exit_test_2",
 		"/bin/false",
 		[]string{""},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -196,7 +196,7 @@ func TestRestart(t *testing.T) {
 		"restart_test",
 		"echo",
 		[]string{"-n", "foobar"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -230,7 +230,7 @@ func TestRestartStdin(t *testing.T) {
 		"restart_stdin_test",
 		"cat",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			OpenStdin: true,
 		},
@@ -281,7 +281,7 @@ func TestUser(t *testing.T) {
 		"user_default",
 		"id",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -301,7 +301,7 @@ func TestUser(t *testing.T) {
 		"user_root",
 		"id",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			User: "root",
 		},
@@ -323,7 +323,7 @@ func TestUser(t *testing.T) {
 		"user_uid0",
 		"id",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			User: "0",
 		},
@@ -345,7 +345,7 @@ func TestUser(t *testing.T) {
 		"user_uid1",
 		"id",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			User: "1",
 		},
@@ -367,7 +367,7 @@ func TestUser(t *testing.T) {
 		"user_daemon",
 		"id",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			User: "daemon",
 		},
@@ -395,7 +395,7 @@ func TestMultipleContainers(t *testing.T) {
 		"container1",
 		"cat",
 		[]string{"/dev/zero"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -407,7 +407,7 @@ func TestMultipleContainers(t *testing.T) {
 		"container2",
 		"cat",
 		[]string{"/dev/zero"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -450,7 +450,7 @@ func TestStdin(t *testing.T) {
 		"stdin_test",
 		"cat",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			OpenStdin: true,
 		},
@@ -485,7 +485,7 @@ func TestTty(t *testing.T) {
 		"tty_test",
 		"cat",
 		[]string{},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{
 			OpenStdin: true,
 		},
@@ -521,7 +521,7 @@ func BenchmarkRunSequencial(b *testing.B) {
 			fmt.Sprintf("bench_%v", i),
 			"echo",
 			[]string{"-n", "foo"},
-			[]string{"/var/lib/docker/images/ubuntu"},
+			[]string{testLayerPath},
 			&Config{},
 		)
 		if err != nil {
@@ -557,7 +557,7 @@ func BenchmarkRunParallel(b *testing.B) {
 				fmt.Sprintf("bench_%v", i),
 				"echo",
 				[]string{"-n", "foo"},
-				[]string{"/var/lib/docker/images/ubuntu"},
+				[]string{testLayerPath},
 				&Config{},
 			)
 			if err != nil {

+ 33 - 7
docker_test.go

@@ -1,16 +1,42 @@
 package docker
 
 import (
+	"fmt"
 	"io/ioutil"
+	"log"
 	"os"
+	"os/exec"
 	"testing"
 )
 
-// Hack to run sys init during unit testing
+const testLayerPath string = "/var/lib/docker/images/docker-ut"
+
 func init() {
+	// Hack to run sys init during unit testing
 	if SelfPath() == "/sbin/init" {
 		SysInit()
 	}
+
+	// Make sure the unit test image is there, download otherwise
+	_, err := os.Stat(testLayerPath)
+	// The image is already there
+	if err == nil {
+		return
+	}
+	// Otherwise, we'll have to fetch it
+	if !os.IsNotExist(err) {
+		panic(err)
+	}
+	log.Printf("Test image not found. Downloading and extracting for the first time.")
+	if err := os.MkdirAll(testLayerPath, 0755); err != nil {
+		panic(err)
+	}
+	cmd := exec.Command("sh", "-c", fmt.Sprintf("wget https://s3.amazonaws.com/docker.io/images/base -O - | tar -jx -C %v/", testLayerPath))
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stdout
+	if err := cmd.Run(); err != nil {
+		panic(err)
+	}
 }
 
 func newTestDocker() (*Docker, error) {
@@ -39,7 +65,7 @@ func TestCreate(t *testing.T) {
 		"test_create",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -87,7 +113,7 @@ func TestDestroy(t *testing.T) {
 		"test_destroy",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -135,7 +161,7 @@ func TestGet(t *testing.T) {
 		"test1",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -147,7 +173,7 @@ func TestGet(t *testing.T) {
 		"test2",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -159,7 +185,7 @@ func TestGet(t *testing.T) {
 		"test3",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {
@@ -196,7 +222,7 @@ func TestRestore(t *testing.T) {
 		"restore_test",
 		"ls",
 		[]string{"-al"},
-		[]string{"/var/lib/docker/images/ubuntu"},
+		[]string{testLayerPath},
 		&Config{},
 	)
 	if err != nil {

+ 3 - 3
filesystem_test.go

@@ -22,7 +22,7 @@ func newTestFilesystem(t *testing.T, layers []string) (rootfs string, fs *Filesy
 }
 
 func TestFilesystem(t *testing.T) {
-	_, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu"})
+	_, filesystem := newTestFilesystem(t, []string{testLayerPath})
 	if err := filesystem.Umount(); err == nil {
 		t.Errorf("Umount succeeded even though the filesystem was not mounted")
 	}
@@ -76,7 +76,7 @@ func TestFilesystemMultiLayer(t *testing.T) {
 	}
 
 	// Create the layered filesystem and add our fake layer on top
-	rootfs, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu", fakeLayer})
+	rootfs, filesystem := newTestFilesystem(t, []string{testLayerPath, fakeLayer})
 
 	// Mount it
 	if err := filesystem.Mount(); err != nil {
@@ -102,7 +102,7 @@ func TestFilesystemMultiLayer(t *testing.T) {
 }
 
 func TestChanges(t *testing.T) {
-	rootfs, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu"})
+	rootfs, filesystem := newTestFilesystem(t, []string{testLayerPath})
 	// Mount it
 	if err := filesystem.Mount(); err != nil {
 		t.Fatal(err)