Browse code

Protect images in the integration/ suite

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2017/07/13 06:51:46
Showing 7 changed files
... ...
@@ -1,22 +1,19 @@
1 1
 #!/usr/bin/env bash
2 2
 set -e
3 3
 
4
-if ! docker inspect -t image emptyfs &> /dev/null; then
5
-	# let's build a "docker save" tarball for "emptyfs"
4
+if ! docker image inspect emptyfs > /dev/null; then
5
+	# build a "docker save" tarball for "emptyfs"
6 6
 	# see https://github.com/docker/docker/pull/5262
7 7
 	# and also https://github.com/docker/docker/issues/4242
8 8
 	dir="$DEST/emptyfs"
9
-	mkdir -p "$dir"
9
+	uuid=511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
10
+	mkdir -p "$dir/$uuid"
10 11
 	(
11
-		cd "$dir"
12
-		echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > repositories
13
-		mkdir -p 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
14
-		(
15
-			cd 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
16
-			echo '{"id":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158","comment":"Imported from -","created":"2013-06-13T14:03:50.821769-07:00","container_config":{"Hostname":"","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"OnBuild":null},"docker_version":"0.4.0","architecture":"x86_64","Size":0}' > json
17
-			echo '1.0' > VERSION
18
-			tar -cf layer.tar --files-from /dev/null
19
-		)
12
+		echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > "$dir/repositories"
13
+		cd "$dir/$uuid"
14
+		echo '{"id":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158","comment":"Imported from -","created":"2013-06-13T14:03:50.821769-07:00","container_config":{"Hostname":"","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"OnBuild":null},"docker_version":"0.4.0","architecture":"x86_64","Size":0}' > json
15
+		echo '1.0' > VERSION
16
+		tar -cf layer.tar --files-from /dev/null
20 17
 	)
21 18
 	(
22 19
 		[ -n "$TESTDEBUG" ] && set -x
... ...
@@ -5,3 +5,5 @@ bundle .integration-daemon-setup
5 5
 
6 6
 export ABS_DEST
7 7
 bash +e
8
+
9
+bundle .integration-daemon-stop
... ...
@@ -4,10 +4,8 @@ import (
4 4
 	"fmt"
5 5
 	"net/http/httptest"
6 6
 	"os"
7
-	"os/exec"
8 7
 	"path"
9 8
 	"path/filepath"
10
-	"strings"
11 9
 	"sync"
12 10
 	"syscall"
13 11
 	"testing"
... ...
@@ -72,17 +70,7 @@ func TestMain(m *testing.M) {
72 72
 func Test(t *testing.T) {
73 73
 	cli.EnsureTestEnvIsLoaded(t)
74 74
 	fakestorage.EnsureTestEnvIsLoaded(t)
75
-	cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
76
-	cmd.Env = appendBaseEnv(true)
77
-	out, err := cmd.CombinedOutput()
78
-	if err != nil {
79
-		panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
80
-	}
81
-	images := strings.Split(strings.TrimSpace(string(out)), "\n")
82
-	testEnv.ProtectImage(t, images...)
83
-	if testEnv.DaemonPlatform() == "linux" {
84
-		ensureFrozenImagesLinux(t)
85
-	}
75
+	environment.ProtectImages(t, testEnv)
86 76
 	check.TestingT(t)
87 77
 }
88 78
 
... ...
@@ -1,5 +1,16 @@
1 1
 package environment
2 2
 
3
+import (
4
+	"strings"
5
+
6
+	"github.com/docker/docker/integration-cli/fixtures/load"
7
+	icmd "github.com/docker/docker/pkg/testutil/cmd"
8
+)
9
+
10
+type protectedElements struct {
11
+	images map[string]struct{}
12
+}
13
+
3 14
 // ProtectImage adds the specified image(s) to be protected in case of clean
4 15
 func (e *Execution) ProtectImage(t testingT, images ...string) {
5 16
 	for _, image := range images {
... ...
@@ -7,6 +18,31 @@ func (e *Execution) ProtectImage(t testingT, images ...string) {
7 7
 	}
8 8
 }
9 9
 
10
-type protectedElements struct {
11
-	images map[string]struct{}
10
+// ProtectImages protects existing images and on linux frozen images from being
11
+// cleaned up at the end of test runs
12
+func ProtectImages(t testingT, testEnv *Execution) {
13
+	images := getExistingImages(t, testEnv)
14
+
15
+	if testEnv.DaemonPlatform() == "linux" {
16
+		images = append(images, ensureFrozenImagesLinux(t, testEnv)...)
17
+	}
18
+	testEnv.ProtectImage(t, images...)
19
+}
20
+
21
+func getExistingImages(t testingT, testEnv *Execution) []string {
22
+	// TODO: use API instead of cli
23
+	result := icmd.RunCommand(testEnv.dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
24
+	result.Assert(t, icmd.Success)
25
+	return strings.Split(strings.TrimSpace(result.Stdout()), "\n")
26
+}
27
+
28
+func ensureFrozenImagesLinux(t testingT, testEnv *Execution) []string {
29
+	images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
30
+	err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...)
31
+	if err != nil {
32
+		result := icmd.RunCommand(testEnv.DockerBinary(), "image", "ls")
33
+		t.Logf(result.String())
34
+		t.Fatalf("%+v", err)
35
+	}
36
+	return images
12 37
 }
... ...
@@ -24,16 +24,6 @@ type logT interface {
24 24
 	Logf(string, ...interface{})
25 25
 }
26 26
 
27
-func ensureFrozenImagesLinux(t testingT) {
28
-	images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
29
-	err := load.FrozenImagesLinux(dockerBinary, images...)
30
-	if err != nil {
31
-		t.Logf(dockerCmdWithError("images"))
32
-		t.Fatalf("%+v", err)
33
-	}
34
-	defer testEnv.ProtectImage(t, images...)
35
-}
36
-
37 27
 var ensureSyscallTestOnce sync.Once
38 28
 
39 29
 func ensureSyscallTest(c *check.C) {
... ...
@@ -2,43 +2,14 @@ package container
2 2
 
3 3
 import (
4 4
 	"context"
5
-	"fmt"
6
-	"os"
7
-	"testing"
8
-
9 5
 	"strconv"
6
+	"testing"
10 7
 
11 8
 	"github.com/docker/docker/api/types/container"
12 9
 	"github.com/docker/docker/api/types/network"
13
-	"github.com/docker/docker/client"
14
-	"github.com/docker/docker/integration-cli/environment"
15
-	"github.com/docker/docker/integration-cli/fixtures/load"
16 10
 	"github.com/stretchr/testify/require"
17 11
 )
18 12
 
19
-var (
20
-	testEnv *environment.Execution
21
-)
22
-
23
-func TestMain(m *testing.M) {
24
-	var err error
25
-	testEnv, err = environment.New()
26
-	if err != nil {
27
-		fmt.Println(err)
28
-		os.Exit(1)
29
-	}
30
-
31
-	if testEnv.LocalDaemon() {
32
-		fmt.Println("INFO: Testing against a local daemon")
33
-	} else {
34
-		fmt.Println("INFO: Testing against a remote daemon")
35
-	}
36
-
37
-	// TODO: ensure and protect images
38
-	res := m.Run()
39
-	os.Exit(res)
40
-}
41
-
42 13
 func TestAPICreateWithNotExistImage(t *testing.T) {
43 14
 	defer setupTest(t)()
44 15
 	clt := createClient(t)
... ...
@@ -119,23 +90,3 @@ func TestAPICreateEmptyEnv(t *testing.T) {
119 119
 		})
120 120
 	}
121 121
 }
122
-
123
-func createClient(t *testing.T) client.APIClient {
124
-	clt, err := client.NewEnvClient()
125
-	require.NoError(t, err)
126
-	return clt
127
-}
128
-
129
-func setupTest(t *testing.T) func() {
130
-	if testEnv.DaemonPlatform() == "linux" {
131
-		images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
132
-		err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...)
133
-		if err != nil {
134
-			t.Fatalf("%+v", err)
135
-		}
136
-		defer testEnv.ProtectImage(t, images...)
137
-	}
138
-	return func() {
139
-		testEnv.Clean(t, testEnv.DockerBinary())
140
-	}
141
-}
142 122
new file mode 100644
... ...
@@ -0,0 +1,45 @@
0
+package container
1
+
2
+import (
3
+	"fmt"
4
+	"os"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/client"
8
+	"github.com/docker/docker/integration-cli/environment"
9
+	"github.com/stretchr/testify/require"
10
+)
11
+
12
+var (
13
+	testEnv *environment.Execution
14
+)
15
+
16
+func TestMain(m *testing.M) {
17
+	var err error
18
+	testEnv, err = environment.New()
19
+	if err != nil {
20
+		fmt.Println(err)
21
+		os.Exit(1)
22
+	}
23
+
24
+	// TODO: replace this with `testEnv.Print()` to print the full env
25
+	if testEnv.LocalDaemon() {
26
+		fmt.Println("INFO: Testing against a local daemon")
27
+	} else {
28
+		fmt.Println("INFO: Testing against a remote daemon")
29
+	}
30
+
31
+	res := m.Run()
32
+	os.Exit(res)
33
+}
34
+
35
+func createClient(t *testing.T) client.APIClient {
36
+	clt, err := client.NewEnvClient()
37
+	require.NoError(t, err)
38
+	return clt
39
+}
40
+
41
+func setupTest(t *testing.T) func() {
42
+	environment.ProtectImages(t, testEnv)
43
+	return func() { testEnv.Clean(t, testEnv.DockerBinary()) }
44
+}