Browse code

Allowing resize tty to only work when container is started Addresses #8728

Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>

Srini Brahmaroutu authored on 2014/10/24 05:30:18
Showing 3 changed files
... ...
@@ -691,6 +691,9 @@ func (container *Container) Restart(seconds int) error {
691 691
 }
692 692
 
693 693
 func (container *Container) Resize(h, w int) error {
694
+	if !container.IsRunning() {
695
+		return fmt.Errorf("Cannot resize container %s, container is not running", container.ID)
696
+	}
694 697
 	return container.command.ProcessConfig.Terminal.Resize(h, w)
695 698
 }
696 699
 
697 700
new file mode 100644
... ...
@@ -0,0 +1,53 @@
0
+package main
1
+
2
+import (
3
+	"os/exec"
4
+	"strings"
5
+	"testing"
6
+)
7
+
8
+func TestResizeApiResponse(t *testing.T) {
9
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
10
+	out, _, err := runCommandWithOutput(runCmd)
11
+	if err != nil {
12
+		t.Fatalf(out, err)
13
+	}
14
+	defer deleteAllContainers()
15
+	cleanedContainerID := stripTrailingCharacters(out)
16
+
17
+	endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
18
+	_, err = sockRequest("POST", endpoint)
19
+	if err != nil {
20
+		t.Fatalf("resize Request failed %v", err)
21
+	}
22
+
23
+	logDone("container resize - when started")
24
+}
25
+
26
+func TestResizeApiResponseWhenContainerNotStarted(t *testing.T) {
27
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
28
+	out, _, err := runCommandWithOutput(runCmd)
29
+	if err != nil {
30
+		t.Fatalf(out, err)
31
+	}
32
+	defer deleteAllContainers()
33
+	cleanedContainerID := stripTrailingCharacters(out)
34
+
35
+	// make sure the exited cintainer is not running
36
+	runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
37
+	out, _, err = runCommandWithOutput(runCmd)
38
+	if err != nil {
39
+		t.Fatalf(out, err)
40
+	}
41
+
42
+	endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
43
+	body, err := sockRequest("POST", endpoint)
44
+	if err == nil {
45
+		t.Fatalf("resize should fail when container is not started")
46
+	}
47
+	if !strings.Contains(string(body), "Cannot resize container") && !strings.Contains(string(body), cleanedContainerID) {
48
+		t.Fatalf("resize should fail with message 'Cannot resize container' but instead received %s", string(body))
49
+	}
50
+
51
+	logDone("container resize - when not started should not resize")
52
+}
... ...
@@ -254,7 +254,8 @@ func sockRequest(method, endpoint string) ([]byte, error) {
254 254
 	}
255 255
 	defer resp.Body.Close()
256 256
 	if resp.StatusCode != http.StatusOK {
257
-		return nil, fmt.Errorf("received status != 200 OK: %s", resp.Status)
257
+		body, _ := ioutil.ReadAll(resp.Body)
258
+		return body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
258 259
 	}
259 260
 
260 261
 	return ioutil.ReadAll(resp.Body)