Esempio n. 1
0
func TestClientRunContainer(t *testing.T) {
	t.Skip()

	dockerCli, err := dockerclient.New()
	if err != nil {
		t.Fatal(err)
	}

	cli, err := NewClient(&DockerClient{Docker: dockerCli})
	if err != nil {
		t.Fatal(err)
	}

	yml := `
namespace: test
containers:
  main:
    image: "busybox:buildroot-2013.08.1"
    labels:
      foo: bar
      xxx: yyy
`

	config, err := config.ReadConfig("test.yml", strings.NewReader(yml), map[string]interface{}{}, map[string]interface{}{}, false)
	if err != nil {
		t.Fatal(err)
	}

	for _, container := range GetContainersFromConfig(config) {
		if err := cli.RunContainer(container); err != nil {
			t.Fatal(err)
		}
	}
}
Esempio n. 2
0
func TestClientResolveVersions(t *testing.T) {
	t.Skip()

	dockerCli, err := dockerclient.New()
	if err != nil {
		t.Fatal(err)
	}

	client, err := NewClient(&DockerClient{
		Docker: dockerCli,
	})
	if err != nil {
		t.Fatal(err)
	}

	containers := []*Container{
		&Container{
			Name:  config.NewContainerName("test", "test"),
			Image: imagename.NewFromString("golang:1.4.*"),
		},
	}

	if err := client.resolveVersions(true, true, template.Vars{}, containers); err != nil {
		t.Fatal(err)
	}

	pretty.Println(containers)
}
Esempio n. 3
0
func TestClientGetContainers(t *testing.T) {
	// TODO: mock?
	t.Skip()
	log.SetOutput(test.Writer("client: ", t))

	dockerCli, err := dockerclient.New()
	if err != nil {
		t.Fatal(err)
	}

	cli, err := NewClient(&DockerClient{Docker: dockerCli})
	if err != nil {
		t.Fatal(err)
	}

	containers, err := cli.GetContainers(false)
	if err != nil {
		t.Fatal(err)
	}

	assert.IsType(t, []*Container{}, containers)
}
Esempio n. 4
0
func TestClientClean(t *testing.T) {
	// This test involves interaction with docker
	// enable it in case you want to test Clean() functionality
	t.Skip()

	dockerCli, err := dockerclient.New()
	if err != nil {
		t.Fatal(err)
	}

	// Create number of images to test

	createdContainers := []string{}
	createdImages := []string{}

	defer func() {
		for _, id := range createdContainers {
			if err := dockerCli.RemoveContainer(docker.RemoveContainerOptions{ID: id, Force: true}); err != nil {
				t.Error(err)
			}
		}
		for _, id := range createdImages {
			if err := dockerCli.RemoveImageExtended(id, docker.RemoveImageOptions{Force: true}); err != nil {
				if err.Error() == "no such image" {
					continue
				}
				t.Error(err)
			}
		}
	}()

	for i := 1; i <= 5; i++ {
		c, err := dockerCli.CreateContainer(docker.CreateContainerOptions{
			Config: &docker.Config{
				Image: "gliderlabs/alpine:3.1",
				Cmd:   []string{"true"},
			},
		})
		if err != nil {
			t.Fatal(err)
		}

		createdContainers = append(createdContainers, c.ID)

		commitOpts := docker.CommitContainerOptions{
			Container:  c.ID,
			Repository: "rocker-compose-test-image-clean",
			Tag:        fmt.Sprintf("%d", i),
		}

		img, err := dockerCli.CommitContainer(commitOpts)
		if err != nil {
			t.Fatal(err)
		}

		createdImages = append(createdImages, img.ID)

		// Make sure images have different timestamps
		time.Sleep(time.Second)
	}

	////////////////////////

	cli, err := NewClient(&DockerClient{Docker: dockerCli, KeepImages: 2})
	if err != nil {
		t.Fatal(err)
	}

	yml := `
namespace: test
containers:
  main:
    image: rocker-compose-test-image-clean:5
`

	config, err := config.ReadConfig("test.yml", strings.NewReader(yml), map[string]interface{}{}, map[string]interface{}{}, false)
	if err != nil {
		t.Fatal(err)
	}

	if err := cli.Clean(config); err != nil {
		t.Fatal(err)
	}

	// test that images left

	all, err := dockerCli.ListImages(docker.ListImagesOptions{})
	if err != nil {
		t.Fatal(err)
	}

	n := 0
	for _, image := range all {
		for _, repoTag := range image.RepoTags {
			imageName := imagename.New(repoTag)
			if imageName.Name == "rocker-compose-test-image-clean" {
				n++
			}
		}
	}

	assert.Equal(t, 2, n, "Expected images to be cleaned up")

	// test removed images list

	removed := cli.GetRemovedImages()
	assert.Equal(t, 3, len(removed), "Expected to remove a particular number of images")

	assert.EqualValues(t, &imagename.ImageName{"", "rocker-compose-test-image-clean", "3"}, removed[0], "removed wrong image")
	assert.EqualValues(t, &imagename.ImageName{"", "rocker-compose-test-image-clean", "2"}, removed[1], "removed wrong image")
	assert.EqualValues(t, &imagename.ImageName{"", "rocker-compose-test-image-clean", "1"}, removed[2], "removed wrong image")
}
Esempio n. 5
0
func TestEntrypointOverride(t *testing.T) {
	t.Skip()

	cli, err := dockerclient.New()
	if err != nil {
		t.Fatal(err)
	}

	container, err := cli.CreateContainer(docker.CreateContainerOptions{
		Config: &docker.Config{
			Image:        "test-entrypoint-override",
			Entrypoint:   []string{},
			Cmd:          []string{"/bin/ls"},
			AttachStdout: true,
			AttachStderr: true,
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := cli.RemoveContainer(docker.RemoveContainerOptions{ID: container.ID, Force: true}); err != nil {
			t.Fatal(err)
		}
	}()

	success := make(chan struct{})
	var buf bytes.Buffer

	attachOpts := docker.AttachToContainerOptions{
		Container:    container.ID,
		OutputStream: &buf,
		ErrorStream:  &buf,
		Stream:       true,
		Stdout:       true,
		Stderr:       true,
		Success:      success,
	}
	go func() {
		if err := cli.AttachToContainer(attachOpts); err != nil {
			t.Fatal(fmt.Errorf("Attach container error: %s", err))
		}
	}()

	success <- <-success

	if err := cli.StartContainer(container.ID, &docker.HostConfig{}); err != nil {
		t.Fatal(fmt.Errorf("Failed to start container, error: %s", err))
	}

	statusCode, err := cli.WaitContainer(container.ID)
	if err != nil {
		t.Fatal(fmt.Errorf("Wait container error: %s", err))
	}

	println(buf.String())

	if statusCode != 0 {
		t.Fatal(fmt.Errorf("Failed to run container, exit with code %d", statusCode))
	}
}