예제 #1
0
파일: api_test.go 프로젝트: BreezeWu/docker
func TestPostContainersCopyWhenContainerNotFound(t *testing.T) {
	eng := NewTestEngine(t)
	defer mkDaemonFromEngine(eng, t).Nuke()

	r := httptest.NewRecorder()

	var copyData engine.Env
	copyData.Set("Resource", "/test.txt")
	copyData.Set("HostPath", ".")

	jsonData := bytes.NewBuffer(nil)
	if err := copyData.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	req, err := http.NewRequest("POST", "/containers/id_not_found/copy", jsonData)
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Add("Content-Type", "application/json")
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	if r.Code != http.StatusNotFound {
		t.Fatalf("404 expected for id_not_found Container, received %v", r.Code)
	}
}
예제 #2
0
파일: api_test.go 프로젝트: BreezeWu/docker
func TestPostContainersCopy(t *testing.T) {
	eng := NewTestEngine(t)
	defer mkDaemonFromEngine(eng, t).Nuke()

	// Create a container and remove a file
	containerID := createTestContainer(eng,
		&runconfig.Config{
			Image: unitTestImageID,
			Cmd:   []string{"touch", "/test.txt"},
		},
		t,
	)
	containerRun(eng, containerID, t)

	r := httptest.NewRecorder()

	var copyData engine.Env
	copyData.Set("Resource", "/test.txt")
	copyData.Set("HostPath", ".")

	jsonData := bytes.NewBuffer(nil)
	if err := copyData.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	req, err := http.NewRequest("POST", "/containers/"+containerID+"/copy", jsonData)
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Add("Content-Type", "application/json")
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	if r.Code != http.StatusOK {
		t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
	}

	found := false
	for tarReader := tar.NewReader(r.Body); ; {
		h, err := tarReader.Next()
		if err != nil {
			if err == io.EOF {
				break
			}
			t.Fatal(err)
		}
		if h.Name == "test.txt" {
			found = true
			break
		}
	}
	if !found {
		t.Fatalf("The created test file has not been found in the copied output")
	}
}
예제 #3
0
파일: server.go 프로젝트: houbl/docker
func writeJSON(w http.ResponseWriter, code int, v engine.Env) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	return v.Encode(w)
}
예제 #4
0
func TestPostContainersCgroup(t *testing.T) {
	eng := NewTestEngine(t)
	defer mkDaemonFromEngine(eng, t).Nuke()

	memory := int64(4194304)
	containerID := createTestContainer(eng,
		&runconfig.Config{
			Memory:    memory,
			Image:     unitTestImageID,
			Cmd:       []string{"/bin/cat"},
			OpenStdin: true,
		},
		t,
	)

	defer func() {
		// Make sure the process dies before destroying runtime
		containerKill(eng, containerID, t)
		containerWait(eng, containerID, t)
	}()

	startContainer(eng, containerID, t)

	// Give some time to the process to start
	containerWaitTimeout(eng, containerID, t)

	if !containerRunning(eng, containerID, t) {
		t.Errorf("Container should be running")
	}

	var (
		cgroupData     engine.Env
		readSubsystem  []string
		writeSubsystem []struct {
			Key   string
			Value string
		}
	)
	readSubsystem = append(readSubsystem, "memory.limit_in_bytes")
	writeSubsystem = append(writeSubsystem, struct {
		Key   string
		Value string
	}{Key: "cpu.shares", Value: "2048"})
	cgroupData.SetList("ReadSubsystem", readSubsystem)
	cgroupData.SetJson("WriteSubsystem", writeSubsystem)

	jsonData := bytes.NewBuffer(nil)
	if err := cgroupData.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	r := httptest.NewRecorder()
	req, err := http.NewRequest("POST", "/containers/"+containerID+"/cgroup?w=1", jsonData)
	req.Header.Add("Content-Type", "application/json")
	if err != nil {
		t.Fatal(err)
	}
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	if r.Code != http.StatusOK {
		t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
	}
	var cgroupResponses []struct {
		Subsystem string
		Out       string
		Err       string
		Status    int
	}

	if err := json.Unmarshal(r.Body.Bytes(), &cgroupResponses); err != nil {
		t.Fatal(err)
	}

	for _, cgroupResponse := range cgroupResponses {
		if cgroupResponse.Status != 0 {
			t.Fatalf("The status of cgroup response is not zero, subsystem: %s, out: %s, err: %s, status: %s",
				cgroupResponse.Subsystem, cgroupResponse.Out, cgroupResponse.Err, cgroupResponse.Status)
		}
	}
}
예제 #5
0
// Regression test for https://github.com/docker/docker/issues/6231
func TestConstainersStartChunkedEncodingHostConfig(t *testing.T) {
	eng := NewTestEngine(t)
	defer mkDaemonFromEngine(eng, t).Nuke()

	r := httptest.NewRecorder()

	var testData engine.Env
	testData.Set("Image", "docker-test-image")
	testData.SetAuto("Volumes", map[string]struct{}{"/foo": {}})
	testData.Set("Cmd", "true")
	jsonData := bytes.NewBuffer(nil)
	if err := testData.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	req, err := http.NewRequest("POST", "/containers/create?name=chunk_test", jsonData)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Add("Content-Type", "application/json")
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	var testData2 engine.Env
	testData2.SetAuto("Binds", []string{"/tmp:/foo"})
	jsonData = bytes.NewBuffer(nil)
	if err := testData2.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	req, err = http.NewRequest("POST", "/containers/chunk_test/start", jsonData)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Add("Content-Type", "application/json")
	// This is a cheat to make the http request do chunked encoding
	// Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
	// http://golang.org/src/pkg/net/http/request.go?s=11980:12172
	req.ContentLength = -1
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	type config struct {
		HostConfig struct {
			Binds []string
		}
	}

	req, err = http.NewRequest("GET", "/containers/chunk_test/json", nil)
	if err != nil {
		t.Fatal(err)
	}

	r2 := httptest.NewRecorder()
	req.Header.Add("Content-Type", "application/json")
	if err := server.ServeRequest(eng, api.APIVERSION, r2, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	c := config{}

	json.Unmarshal(r2.Body.Bytes(), &c)

	if len(c.HostConfig.Binds) == 0 {
		t.Fatal("Chunked Encoding not handled")
	}

	if c.HostConfig.Binds[0] != "/tmp:/foo" {
		t.Fatal("Chunked encoding not properly handled, execpted binds to be /tmp:/foo, got:", c.HostConfig.Binds[0])
	}
}
예제 #6
0
func TestPostContainersSet(t *testing.T) {
	eng := NewTestEngine(t)
	defer mkDaemonFromEngine(eng, t).Nuke()

	memory := int64(4194304)
	containerID := createTestContainer(eng,
		&runconfig.Config{
			Memory:    memory,
			Image:     unitTestImageID,
			Cmd:       []string{"/bin/cat"},
			OpenStdin: true,
		},
		t,
	)

	defer func() {
		// Make sure the process dies before destroying runtime
		containerKill(eng, containerID, t)
		containerWait(eng, containerID, t)
	}()

	startContainer(eng, containerID, t)

	// Give some time to the process to start
	containerWaitTimeout(eng, containerID, t)

	if !containerRunning(eng, containerID, t) {
		t.Errorf("Container should be running")
	}

	var (
		setData engine.Env
		config  []struct {
			Key   string
			Value string
		}
	)
	config = append(config, struct {
		Key   string
		Value string
	}{Key: "image", Value: "test:v1"})
	setData.SetJson("config", config)

	jsonData := bytes.NewBuffer(nil)
	if err := setData.Encode(jsonData); err != nil {
		t.Fatal(err)
	}

	r := httptest.NewRecorder()
	req, err := http.NewRequest("POST", "/containers/"+containerID+"/set", jsonData)
	req.Header.Add("Content-Type", "application/json")
	if err != nil {
		t.Fatal(err)
	}
	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	assertHttpNotError(r, t)

	if r.Code != http.StatusOK {
		t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
	}
}