mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Protect images in the integration/ suite
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
		
							parent
							
								
									4308e881cc
								
							
						
					
					
						commit
						5fa134b906
					
				
					 7 changed files with 96 additions and 87 deletions
				
			
		| 
						 | 
				
			
			@ -1,22 +1,19 @@
 | 
			
		|||
#!/usr/bin/env bash
 | 
			
		||||
set -e
 | 
			
		||||
 | 
			
		||||
if ! docker inspect -t image emptyfs &> /dev/null; then
 | 
			
		||||
	# let's build a "docker save" tarball for "emptyfs"
 | 
			
		||||
if ! docker image inspect emptyfs > /dev/null; then
 | 
			
		||||
	# build a "docker save" tarball for "emptyfs"
 | 
			
		||||
	# see https://github.com/docker/docker/pull/5262
 | 
			
		||||
	# and also https://github.com/docker/docker/issues/4242
 | 
			
		||||
	dir="$DEST/emptyfs"
 | 
			
		||||
	mkdir -p "$dir"
 | 
			
		||||
	uuid=511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
 | 
			
		||||
	mkdir -p "$dir/$uuid"
 | 
			
		||||
	(
 | 
			
		||||
		cd "$dir"
 | 
			
		||||
		echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > repositories
 | 
			
		||||
		mkdir -p 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
 | 
			
		||||
		(
 | 
			
		||||
			cd 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
 | 
			
		||||
			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
 | 
			
		||||
			echo '1.0' > VERSION
 | 
			
		||||
			tar -cf layer.tar --files-from /dev/null
 | 
			
		||||
		)
 | 
			
		||||
		echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > "$dir/repositories"
 | 
			
		||||
		cd "$dir/$uuid"
 | 
			
		||||
		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
 | 
			
		||||
		echo '1.0' > VERSION
 | 
			
		||||
		tar -cf layer.tar --files-from /dev/null
 | 
			
		||||
	)
 | 
			
		||||
	(
 | 
			
		||||
		[ -n "$TESTDEBUG" ] && set -x
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,3 +5,5 @@ bundle .integration-daemon-setup
 | 
			
		|||
 | 
			
		||||
export ABS_DEST
 | 
			
		||||
bash +e
 | 
			
		||||
 | 
			
		||||
bundle .integration-daemon-stop
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,10 +4,8 @@ import (
 | 
			
		|||
	"fmt"
 | 
			
		||||
	"net/http/httptest"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"path"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"syscall"
 | 
			
		||||
	"testing"
 | 
			
		||||
| 
						 | 
				
			
			@ -72,17 +70,7 @@ func TestMain(m *testing.M) {
 | 
			
		|||
func Test(t *testing.T) {
 | 
			
		||||
	cli.EnsureTestEnvIsLoaded(t)
 | 
			
		||||
	fakestorage.EnsureTestEnvIsLoaded(t)
 | 
			
		||||
	cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
 | 
			
		||||
	cmd.Env = appendBaseEnv(true)
 | 
			
		||||
	out, err := cmd.CombinedOutput()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
 | 
			
		||||
	}
 | 
			
		||||
	images := strings.Split(strings.TrimSpace(string(out)), "\n")
 | 
			
		||||
	testEnv.ProtectImage(t, images...)
 | 
			
		||||
	if testEnv.DaemonPlatform() == "linux" {
 | 
			
		||||
		ensureFrozenImagesLinux(t)
 | 
			
		||||
	}
 | 
			
		||||
	environment.ProtectImages(t, testEnv)
 | 
			
		||||
	check.TestingT(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,16 @@
 | 
			
		|||
package environment
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/docker/integration-cli/fixtures/load"
 | 
			
		||||
	icmd "github.com/docker/docker/pkg/testutil/cmd"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type protectedElements struct {
 | 
			
		||||
	images map[string]struct{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ProtectImage adds the specified image(s) to be protected in case of clean
 | 
			
		||||
func (e *Execution) ProtectImage(t testingT, images ...string) {
 | 
			
		||||
	for _, image := range images {
 | 
			
		||||
| 
						 | 
				
			
			@ -7,6 +18,31 @@ func (e *Execution) ProtectImage(t testingT, images ...string) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type protectedElements struct {
 | 
			
		||||
	images map[string]struct{}
 | 
			
		||||
// ProtectImages protects existing images and on linux frozen images from being
 | 
			
		||||
// cleaned up at the end of test runs
 | 
			
		||||
func ProtectImages(t testingT, testEnv *Execution) {
 | 
			
		||||
	images := getExistingImages(t, testEnv)
 | 
			
		||||
 | 
			
		||||
	if testEnv.DaemonPlatform() == "linux" {
 | 
			
		||||
		images = append(images, ensureFrozenImagesLinux(t, testEnv)...)
 | 
			
		||||
	}
 | 
			
		||||
	testEnv.ProtectImage(t, images...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getExistingImages(t testingT, testEnv *Execution) []string {
 | 
			
		||||
	// TODO: use API instead of cli
 | 
			
		||||
	result := icmd.RunCommand(testEnv.dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
 | 
			
		||||
	result.Assert(t, icmd.Success)
 | 
			
		||||
	return strings.Split(strings.TrimSpace(result.Stdout()), "\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ensureFrozenImagesLinux(t testingT, testEnv *Execution) []string {
 | 
			
		||||
	images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
 | 
			
		||||
	err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		result := icmd.RunCommand(testEnv.DockerBinary(), "image", "ls")
 | 
			
		||||
		t.Logf(result.String())
 | 
			
		||||
		t.Fatalf("%+v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return images
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,16 +24,6 @@ type logT interface {
 | 
			
		|||
	Logf(string, ...interface{})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ensureFrozenImagesLinux(t testingT) {
 | 
			
		||||
	images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
 | 
			
		||||
	err := load.FrozenImagesLinux(dockerBinary, images...)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Logf(dockerCmdWithError("images"))
 | 
			
		||||
		t.Fatalf("%+v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer testEnv.ProtectImage(t, images...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var ensureSyscallTestOnce sync.Once
 | 
			
		||||
 | 
			
		||||
func ensureSyscallTest(c *check.C) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,43 +2,14 @@ package container
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/docker/api/types/container"
 | 
			
		||||
	"github.com/docker/docker/api/types/network"
 | 
			
		||||
	"github.com/docker/docker/client"
 | 
			
		||||
	"github.com/docker/docker/integration-cli/environment"
 | 
			
		||||
	"github.com/docker/docker/integration-cli/fixtures/load"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	testEnv *environment.Execution
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestMain(m *testing.M) {
 | 
			
		||||
	var err error
 | 
			
		||||
	testEnv, err = environment.New()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if testEnv.LocalDaemon() {
 | 
			
		||||
		fmt.Println("INFO: Testing against a local daemon")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("INFO: Testing against a remote daemon")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: ensure and protect images
 | 
			
		||||
	res := m.Run()
 | 
			
		||||
	os.Exit(res)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAPICreateWithNotExistImage(t *testing.T) {
 | 
			
		||||
	defer setupTest(t)()
 | 
			
		||||
	clt := createClient(t)
 | 
			
		||||
| 
						 | 
				
			
			@ -119,23 +90,3 @@ func TestAPICreateEmptyEnv(t *testing.T) {
 | 
			
		|||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func createClient(t *testing.T) client.APIClient {
 | 
			
		||||
	clt, err := client.NewEnvClient()
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
	return clt
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setupTest(t *testing.T) func() {
 | 
			
		||||
	if testEnv.DaemonPlatform() == "linux" {
 | 
			
		||||
		images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
 | 
			
		||||
		err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatalf("%+v", err)
 | 
			
		||||
		}
 | 
			
		||||
		defer testEnv.ProtectImage(t, images...)
 | 
			
		||||
	}
 | 
			
		||||
	return func() {
 | 
			
		||||
		testEnv.Clean(t, testEnv.DockerBinary())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										45
									
								
								integration/container/main_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								integration/container/main_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
package container
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/docker/client"
 | 
			
		||||
	"github.com/docker/docker/integration-cli/environment"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	testEnv *environment.Execution
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestMain(m *testing.M) {
 | 
			
		||||
	var err error
 | 
			
		||||
	testEnv, err = environment.New()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: replace this with `testEnv.Print()` to print the full env
 | 
			
		||||
	if testEnv.LocalDaemon() {
 | 
			
		||||
		fmt.Println("INFO: Testing against a local daemon")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("INFO: Testing against a remote daemon")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res := m.Run()
 | 
			
		||||
	os.Exit(res)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func createClient(t *testing.T) client.APIClient {
 | 
			
		||||
	clt, err := client.NewEnvClient()
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
	return clt
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setupTest(t *testing.T) func() {
 | 
			
		||||
	environment.ProtectImages(t, testEnv)
 | 
			
		||||
	return func() { testEnv.Clean(t, testEnv.DockerBinary()) }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue