Exemplo n.º 1
0
func TestGet(t *testing.T) {
	daemon := mkDaemon(t)
	defer nuke(daemon)

	container1, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
	defer daemon.Rm(container1)

	container2, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
	defer daemon.Rm(container2)

	container3, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
	defer daemon.Rm(container3)

	if c, _ := daemon.Get(container1.ID); c != container1 {
		t.Errorf("Get(test1) returned %v while expecting %v", c, container1)
	}

	if c, _ := daemon.Get(container2.ID); c != container2 {
		t.Errorf("Get(test2) returned %v while expecting %v", c, container2)
	}

	if c, _ := daemon.Get(container3.ID); c != container3 {
		t.Errorf("Get(test3) returned %v while expecting %v", c, container3)
	}

}
Exemplo n.º 2
0
func TestDestroy(t *testing.T) {
	daemon := mkDaemon(t)
	defer nuke(daemon)

	container, _, err := daemon.Create(&runconfig.Config{
		Image: GetTestImage(daemon).ID,
		Cmd:   runconfig.NewCommand("ls", "-al"),
	},
		&runconfig.HostConfig{},
		"")
	if err != nil {
		t.Fatal(err)
	}
	// Destroy
	if err := daemon.Rm(container); err != nil {
		t.Error(err)
	}

	// Make sure daemon.Exists() behaves correctly
	if daemon.Exists("test_destroy") {
		t.Errorf("Exists() returned true")
	}

	// Make sure daemon.List() doesn't list the destroyed container
	if len(daemon.List()) != 0 {
		t.Errorf("Expected 0 container, %v found", len(daemon.List()))
	}

	// Make sure daemon.Get() refuses to return the unexisting container
	if c, _ := daemon.Get(container.ID); c != nil {
		t.Errorf("Got a container that should not exist")
	}

	// Test double destroy
	if err := daemon.Rm(container); err == nil {
		// It should have failed
		t.Errorf("Double destroy did not fail")
	}
}
Exemplo n.º 3
0
// FIXME: cleanup and nuke are redundant.
func cleanup(eng *engine.Engine, t *testing.T) error {
	daemon := mkDaemonFromEngine(eng, t)
	for _, container := range daemon.List() {
		container.Kill()
		daemon.Rm(container)
	}
	images, err := daemon.Repositories().Images(&graph.ImagesConfig{})
	if err != nil {
		t.Fatal(err)
	}
	for _, image := range images {
		if image.ID != unitTestImageID {
			eng.Job("image_delete", image.ID).Run()
		}
	}
	return nil
}
Exemplo n.º 4
0
func TestDestroyWithInitLayer(t *testing.T) {
	daemon := mkDaemon(t)
	defer nuke(daemon)

	container, _, err := daemon.Create(&runconfig.Config{
		Image: GetTestImage(daemon).ID,
		Cmd:   runconfig.NewCommand("ls", "-al"),
	},
		&runconfig.HostConfig{},
		"")

	if err != nil {
		t.Fatal(err)
	}
	// Destroy
	if err := daemon.Rm(container); err != nil {
		t.Fatal(err)
	}

	// Make sure daemon.Exists() behaves correctly
	if daemon.Exists("test_destroy") {
		t.Fatalf("Exists() returned true")
	}

	// Make sure daemon.List() doesn't list the destroyed container
	if len(daemon.List()) != 0 {
		t.Fatalf("Expected 0 container, %v found", len(daemon.List()))
	}

	driver := daemon.Graph().Driver()

	// Make sure that the container does not exist in the driver
	if _, err := driver.Get(container.ID, ""); err == nil {
		t.Fatal("Conttainer should not exist in the driver")
	}

	// Make sure that the init layer is removed from the driver
	if _, err := driver.Get(fmt.Sprintf("%s-init", container.ID), ""); err == nil {
		t.Fatal("Container's init layer should not exist in the driver")
	}
}
Exemplo n.º 5
0
// FIXME: cleanup and nuke are redundant.
func cleanup(eng *engine.Engine, t *testing.T) error {
	daemon := mkDaemonFromEngine(eng, t)
	for _, container := range daemon.List() {
		container.Kill()
		daemon.Rm(container)
	}
	job := eng.Job("images")
	images, err := job.Stdout.AddTable()
	if err != nil {
		t.Fatal(err)
	}
	if err := job.Run(); err != nil {
		t.Fatal(err)
	}
	for _, image := range images.Data {
		if image.Get("Id") != unitTestImageID {
			eng.Job("image_delete", image.Get("Id")).Run()
		}
	}
	return nil
}
Exemplo n.º 6
0
func TestDaemonCreate(t *testing.T) {
	daemon := mkDaemon(t)
	defer nuke(daemon)

	// Make sure we start we 0 containers
	if len(daemon.List()) != 0 {
		t.Errorf("Expected 0 containers, %v found", len(daemon.List()))
	}

	container, _, err := daemon.Create(&runconfig.Config{
		Image: GetTestImage(daemon).ID,
		Cmd:   runconfig.NewCommand("ls", "-al"),
	},
		&runconfig.HostConfig{},
		"",
	)
	if err != nil {
		t.Fatal(err)
	}

	defer func() {
		if err := daemon.Rm(container); err != nil {
			t.Error(err)
		}
	}()

	// Make sure we can find the newly created container with List()
	if len(daemon.List()) != 1 {
		t.Errorf("Expected 1 container, %v found", len(daemon.List()))
	}

	// Make sure the container List() returns is the right one
	if daemon.List()[0].ID != container.ID {
		t.Errorf("Unexpected container %v returned by List", daemon.List()[0])
	}

	// Make sure we can get the container with Get()
	if _, err := daemon.Get(container.ID); err != nil {
		t.Errorf("Unable to get newly created container")
	}

	// Make sure it is the right container
	if c, _ := daemon.Get(container.ID); c != container {
		t.Errorf("Get() returned the wrong container")
	}

	// Make sure Exists returns it as existing
	if !daemon.Exists(container.ID) {
		t.Errorf("Exists() returned false for a newly created container")
	}

	// Test that conflict error displays correct details
	cmd := runconfig.NewCommand("ls", "-al")
	testContainer, _, _ := daemon.Create(
		&runconfig.Config{
			Image: GetTestImage(daemon).ID,
			Cmd:   cmd,
		},
		&runconfig.HostConfig{},
		"conflictname",
	)
	if _, _, err := daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID, Cmd: cmd}, &runconfig.HostConfig{}, testContainer.Name); err == nil || !strings.Contains(err.Error(), stringid.TruncateID(testContainer.ID)) {
		t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %v", err)
	}

	// Make sure create with bad parameters returns an error
	if _, _, err = daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID}, &runconfig.HostConfig{}, ""); err == nil {
		t.Fatal("Builder.Create should throw an error when Cmd is missing")
	}

	if _, _, err := daemon.Create(
		&runconfig.Config{
			Image: GetTestImage(daemon).ID,
			Cmd:   runconfig.NewCommand(),
		},
		&runconfig.HostConfig{},
		"",
	); err == nil {
		t.Fatal("Builder.Create should throw an error when Cmd is empty")
	}

	config := &runconfig.Config{
		Image:     GetTestImage(daemon).ID,
		Cmd:       runconfig.NewCommand("/bin/ls"),
		PortSpecs: []string{"80"},
	}
	container, _, err = daemon.Create(config, &runconfig.HostConfig{}, "")

	_, err = daemon.Commit(container, "testrepo", "testtag", "", "", true, config)
	if err != nil {
		t.Error(err)
	}

	// test expose 80:8000
	container, warnings, err := daemon.Create(&runconfig.Config{
		Image:     GetTestImage(daemon).ID,
		Cmd:       runconfig.NewCommand("ls", "-al"),
		PortSpecs: []string{"80:8000"},
	},
		&runconfig.HostConfig{},
		"",
	)
	if err != nil {
		t.Fatal(err)
	}
	if warnings == nil || len(warnings) != 1 {
		t.Error("Expected a warning, got none")
	}
}