Exemplo n.º 1
0
//NewLocalImageCreator creates an instance of the LocalImageCreator from the docker environment variables, and returns the instance
func NewLocalImageCreator(repo string) (ImageCreator, error) {

	client, err := newEnvClient()
	if err != nil {
		return nil, err
	}

	imageCreator := LocalImageCreator{
		client:     client,
		remoteRepo: repo,
	}

	//TODO, stop selecting all
	_, err = client.ImageList(context.Background(), types.ImageListOptions{All: false})

	if err != nil {
		return nil, err
	}

	return imageCreator, nil
}
Exemplo n.º 2
0
func TestImagePullFromRegistry(t *testing.T) {
	assert := assert.New(t)
	var endpoint string
	if endpoint = os.Getenv("DOCKER_HOST"); endpoint == "" {
		endpoint = client.DefaultDockerHost
	}
	client, err := client.NewClient(endpoint, "", nil, defaultHeaders)
	assert.NoError(err)

	ctx := context.Background()
	box := Box{
		ctx:    ctx,
		client: client,
		config: &dockerBoxConfig{},
	}

	var profile = isolate.Profile{
		"registry": "docker.io",
	}

	t.Logf("Clean up docker.io/alpine:latest if it exists")
	client.ImageRemove(ctx, "docker.io/alpine:latest", types.ImageRemoveOptions{Force: false, PruneChildren: false})
	t.Logf("Spool via box on 'clean' system")
	err = box.Spool(ctx, "alpine", profile)
	assert.NoError(err)
	imgs, err := client.ImageList(ctx, types.ImageListOptions{})
	found := false
	for _, img := range imgs {
		if strings.Contains(img.RepoTags[0], "alpine") {
			found = true
			break
		}
	}
	assert.NoError(err)
	assert.True(found)
	t.Logf("Spool an already spooled image")
	err = box.Spool(ctx, "alpine", profile)
	assert.NoError(err)
}
Exemplo n.º 3
0
func TestContainer(t *testing.T) {
	if runtime.GOOS != "linux" {
		t.Skipf("Skip under %s", runtime.GOOS)
		return
	}
	if os.Getenv("TRAVIS") == "true" {
		t.Skip("Skip Porto tests under Travis CI")
		return
	}
	require := require.New(t)
	ctx := context.Background()

	var endpoint string
	if endpoint = os.Getenv("DOCKER_HOST"); endpoint == "" {
		endpoint = client.DefaultDockerHost
	}
	client, err := client.NewClient(endpoint, "", nil, nil)
	require.NoError(err)

	imgs, err := client.ImageList(ctx, types.ImageListOptions{MatchName: "alpine"})
	require.NoError(err)
	if len(imgs) == 0 {
		resp, err := client.ImagePull(ctx, "alpine", types.ImagePullOptions{})
		require.NoError(err)
		io.Copy(ioutil.Discard, resp)
		resp.Close()
	}

	dir, err := ioutil.TempDir("", "")
	require.NoError(err)
	defer os.RemoveAll(dir)

	resp, err := client.ImageSave(ctx, []string{"alpine"})
	require.NoError(err)
	defer resp.Close()

	imagetar := filepath.Join(dir, "alpine.tar.gz")
	fi, err := os.Create(imagetar)
	require.NoError(err)
	defer fi.Close()
	_, err = io.Copy(fi, resp)
	require.NoError(err)
	fi.Close()
	resp.Close()

	var profile = docker.Profile{
		RuntimePath: "/var/run",
		NetworkMode: "host",
		Cwd:         "/tmp",
		Resources: docker.Resources{
			Memory: 4 * 1024 * 1024,
		},
		Tmpfs: map[string]string{
			"/tmp/a": "size=100000",
		},
	}

	portoConn, err := portoConnect()
	if err != nil {
		t.Fatal(err)
	}
	defer portoConn.Close()

	err = portoConn.ImportLayer("testalpine", imagetar, false)
	if err != nil {
		require.True(isEqualPortoError(err, portorpc.EError_LayerAlreadyExists))
	}

	ei := execInfo{
		// Profile:    &profile,
		name:       "TestContainer",
		executable: "echo",
		args:       map[string]string{"--endpoint": "/var/run/cocaine.sock"},
		env:        map[string]string{"A": "B"},
	}

	cfg := containerConfig{
		Root:  dir,
		ID:    "LinuxAlpine",
		Layer: "testalpine",
	}

	cnt, err := newContainer(ctx, portoConn, cfg, ei)
	require.NoError(err)
	require.NoError(cnt.start(portoConn, ioutil.Discard))
	defer cnt.Kill()

	env, err := portoConn.GetProperty(cnt.containerID, "env")
	require.NoError(err)
	assert.Equal(t, "A:B", env)

	command, err := portoConn.GetProperty(cnt.containerID, "command")
	require.NoError(err)
	assert.Equal(t, "echo --endpoint /var/run/cocaine.sock", command)

	cwd, err := portoConn.GetProperty(cnt.containerID, "cwd")
	require.NoError(err)
	assert.Equal(t, profile.Cwd, cwd)
}
Exemplo n.º 4
0
func TestContainer(t *testing.T) {
	assert := assert.New(t)
	ctx := context.Background()

	var endpoint string
	if endpoint = os.Getenv("DOCKER_HOST"); endpoint == "" {
		endpoint = client.DefaultDockerHost
	}
	client, err := client.NewClient(endpoint, "", nil, defaultHeaders)
	assert.NoError(err)
	version, err := client.ServerVersion(ctx)
	assert.NoError(err)

	imgs, err := client.ImageList(ctx, types.ImageListOptions{MatchName: "alpine"})
	assert.NoError(err)
	if len(imgs) == 0 {
		resp, err := client.ImagePull(ctx, "alpine", types.ImagePullOptions{})
		assert.NoError(err)
		io.Copy(ioutil.Discard, resp)
		resp.Close()
	}

	var profile = Profile{
		RuntimePath: "/var/run",
		NetworkMode: "host",
		Cwd:         "/tmp",
		Resources: Resources{
			Memory: 4 * 1024 * 1024,
		},
		Tmpfs: map[string]string{
			"/tmp/a": "size=100000",
		},
		Binds: []string{"/tmp:/bind:rw"},
	}

	args := map[string]string{"--endpoint": "/var/run/cocaine.sock"}
	env := map[string]string{"A": "B"}

	container, err := newContainer(ctx, client, &profile, "alpine", "echo", args, env)
	assert.NoError(err)

	inspect, err := client.ContainerInspect(ctx, container.containerID)
	assert.NoError(err)
	assert.Equal([]string{"--endpoint", "/var/run/cocaine.sock"}, inspect.Args, "invalid args")

	ver := strings.SplitN(version.Version, ".", 3)
	v, err := strconv.Atoi(ver[1])
	assert.NoError(err)

	if v >= 10 {
		assert.Equal(profile.Tmpfs, inspect.HostConfig.Tmpfs, "invalid tmpfs value")
	} else {
		t.Logf("%s does not support tmpfs", version.Version)
	}

	assert.Equal("/var/run:/var/run", inspect.HostConfig.Binds[0])
	assert.Equal("/tmp:/bind:rw", inspect.HostConfig.Binds[1])
	assert.Equal(profile.Resources.Memory, inspect.HostConfig.Memory, "invalid memory limit")

	container.Kill()
	_, err = client.ContainerInspect(ctx, container.containerID)
	assert.Error(err)
}