Browse code

Version exec json check.

Keep backwards compatibility with old versions of the api.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/10/24 01:48:55
Showing 2 changed files
... ...
@@ -60,9 +60,14 @@ func (s *router) postContainerExecStart(ctx context.Context, w http.ResponseWrit
60 60
 	if err := httputils.ParseForm(r); err != nil {
61 61
 		return err
62 62
 	}
63
-	if err := httputils.CheckForJSON(r); err != nil {
64
-		return err
63
+
64
+	version := httputils.VersionFromContext(ctx)
65
+	if version.GreaterThan("1.21") {
66
+		if err := httputils.CheckForJSON(r); err != nil {
67
+			return err
68
+		}
65 69
 	}
70
+
66 71
 	var (
67 72
 		execName                  = vars["name"]
68 73
 		stdin, inStream           io.ReadCloser
... ...
@@ -52,29 +52,20 @@ func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) {
52 52
 func (s *DockerSuite) TestExecAPIStart(c *check.C) {
53 53
 	dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
54 54
 
55
-	createExec := func() string {
56
-		_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", "test"), map[string]interface{}{"Cmd": []string{"true"}})
57
-		c.Assert(err, check.IsNil, check.Commentf(string(b)))
58
-
59
-		createResp := struct {
60
-			ID string `json:"Id"`
61
-		}{}
62
-		c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b)))
63
-		return createResp.ID
64
-	}
65
-
66 55
 	startExec := func(id string, code int) {
67 56
 		resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
68 57
 		c.Assert(err, check.IsNil)
69 58
 
70 59
 		b, err := readBody(body)
71
-		c.Assert(err, check.IsNil, check.Commentf(string(b)))
72
-		c.Assert(resp.StatusCode, check.Equals, code, check.Commentf(string(b)))
60
+		comment := check.Commentf("response body: %s", b)
61
+		c.Assert(err, check.IsNil, comment)
62
+		c.Assert(resp.StatusCode, check.Equals, code, comment)
73 63
 	}
74 64
 
75
-	startExec(createExec(), http.StatusOK)
65
+	id := createExec(c, "test")
66
+	startExec(id, http.StatusOK)
76 67
 
77
-	id := createExec()
68
+	id = createExec(c, "test")
78 69
 	dockerCmd(c, "stop", "test")
79 70
 
80 71
 	startExec(id, http.StatusNotFound)
... ...
@@ -83,9 +74,33 @@ func (s *DockerSuite) TestExecAPIStart(c *check.C) {
83 83
 	startExec(id, http.StatusNotFound)
84 84
 
85 85
 	// make sure exec is created before pausing
86
-	id = createExec()
86
+	id = createExec(c, "test")
87 87
 	dockerCmd(c, "pause", "test")
88 88
 	startExec(id, http.StatusConflict)
89 89
 	dockerCmd(c, "unpause", "test")
90 90
 	startExec(id, http.StatusOK)
91 91
 }
92
+
93
+func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
94
+	dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
95
+	id := createExec(c, "test")
96
+
97
+	resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain")
98
+	c.Assert(err, check.IsNil)
99
+
100
+	b, err := readBody(body)
101
+	comment := check.Commentf("response body: %s", b)
102
+	c.Assert(err, check.IsNil, comment)
103
+	c.Assert(resp.StatusCode, check.Equals, http.StatusOK, comment)
104
+}
105
+
106
+func createExec(c *check.C, name string) string {
107
+	_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
108
+	c.Assert(err, check.IsNil, check.Commentf(string(b)))
109
+
110
+	createResp := struct {
111
+		ID string `json:"Id"`
112
+	}{}
113
+	c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b)))
114
+	return createResp.ID
115
+}