Browse code

fixes #7802, when api version 1.11 is `json.Marshal`ing the container struct

Signed-off-by: Jessica Frazelle <jfrazelle@users.noreply.github.com>

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jfrazelle@users.noreply.github.com> (github: )

Jessica Frazelle authored on 2014/09/06 06:35:55
Showing 3 changed files
... ...
@@ -50,9 +50,9 @@ type StreamConfig struct {
50 50
 }
51 51
 
52 52
 type Container struct {
53
-	*State
54
-	root   string // Path to the "home" of the container, including metadata.
55
-	basefs string // Path to the graphdriver mountpoint
53
+	*State `json:"State"` // Needed for remote api version <= 1.11
54
+	root   string         // Path to the "home" of the container, including metadata.
55
+	basefs string         // Path to the graphdriver mountpoint
56 56
 
57 57
 	ID string
58 58
 
59 59
new file mode 100644
... ...
@@ -0,0 +1,54 @@
0
+package main
1
+
2
+import (
3
+	"encoding/json"
4
+	"fmt"
5
+	"os/exec"
6
+	"testing"
7
+)
8
+
9
+func TestInspectContainerResponse(t *testing.T) {
10
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
11
+	out, _, err := runCommandWithOutput(runCmd)
12
+	errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
13
+
14
+	cleanedContainerID := stripTrailingCharacters(out)
15
+
16
+	// test on json marshal version
17
+	// and latest version
18
+	testVersions := []string{"v1.11", "latest"}
19
+
20
+	for _, testVersion := range testVersions {
21
+		endpoint := "/containers/" + cleanedContainerID + "/json"
22
+		if testVersion != "latest" {
23
+			endpoint = "/" + testVersion + endpoint
24
+		}
25
+		body, err := sockRequest("GET", endpoint)
26
+		if err != nil {
27
+			t.Fatal("sockRequest failed for %s version: %v", testVersion, err)
28
+		}
29
+
30
+		var inspect_json map[string]interface{}
31
+		if err = json.Unmarshal(body, &inspect_json); err != nil {
32
+			t.Fatalf("unable to unmarshal body for %s version: %v", testVersion, err)
33
+		}
34
+
35
+		keys := []string{"State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", "ResolvConfPath", "HostnamePath", "HostsPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "Volumes", "VolumesRW"}
36
+
37
+		if testVersion == "v1.11" {
38
+			keys = append(keys, "ID")
39
+		} else {
40
+			keys = append(keys, "Id")
41
+		}
42
+
43
+		for _, key := range keys {
44
+			if _, ok := inspect_json[key]; !ok {
45
+				t.Fatalf("%s does not exist in reponse for %s version", key, testVersion)
46
+			}
47
+		}
48
+	}
49
+
50
+	deleteAllContainers()
51
+
52
+	logDone("container json - check keys in container json response")
53
+}
... ...
@@ -231,6 +231,35 @@ func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
231 231
 	return string(b), err
232 232
 }
233 233
 
234
+func sockRequest(method, endpoint string) ([]byte, error) {
235
+	// FIX: the path to sock should not be hardcoded
236
+	sock := filepath.Join("/", "var", "run", "docker.sock")
237
+	c, err := net.DialTimeout("unix", sock, time.Duration(10*time.Second))
238
+	if err != nil {
239
+		return nil, fmt.Errorf("could not dial docker sock at %s: %v", sock, err)
240
+	}
241
+
242
+	client := httputil.NewClientConn(c, nil)
243
+	defer client.Close()
244
+
245
+	req, err := http.NewRequest(method, endpoint, nil)
246
+	req.Header.Set("Content-Type", "application/json")
247
+	if err != nil {
248
+		return nil, fmt.Errorf("could not create new request: %v", err)
249
+	}
250
+
251
+	resp, err := client.Do(req)
252
+	if err != nil {
253
+		return nil, fmt.Errorf("could not perform request: %v", err)
254
+	}
255
+	defer resp.Body.Close()
256
+	if resp.StatusCode != http.StatusOK {
257
+		return nil, fmt.Errorf("received status != 200 OK: %s", resp.Status)
258
+	}
259
+
260
+	return ioutil.ReadAll(resp.Body)
261
+}
262
+
234 263
 func deleteContainer(container string) error {
235 264
 	container = strings.Replace(container, "\n", " ", -1)
236 265
 	container = strings.Trim(container, " ")