Skip to content

Commit f49c3f2

Browse files
Jessica Frazellejessfraz
authored andcommitted
fixes moby#7802, when api version 1.11 is json.Marshaling the container struct
Signed-off-by: Jessica Frazelle <[email protected]> Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <[email protected]> (github: )
1 parent 3818608 commit f49c3f2

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

daemon/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ type StreamConfig struct {
5050
}
5151

5252
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
5656

5757
ID string
5858

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os/exec"
7+
"testing"
8+
)
9+
10+
func TestInspectContainerResponse(t *testing.T) {
11+
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
12+
out, _, err := runCommandWithOutput(runCmd)
13+
errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
14+
15+
cleanedContainerID := stripTrailingCharacters(out)
16+
17+
// test on json marshal version
18+
// and latest version
19+
testVersions := []string{"v1.11", "latest"}
20+
21+
for _, testVersion := range testVersions {
22+
endpoint := "/containers/" + cleanedContainerID + "/json"
23+
if testVersion != "latest" {
24+
endpoint = "/" + testVersion + endpoint
25+
}
26+
body, err := sockRequest("GET", endpoint)
27+
if err != nil {
28+
t.Fatal("sockRequest failed for %s version: %v", testVersion, err)
29+
}
30+
31+
var inspect_json map[string]interface{}
32+
if err = json.Unmarshal(body, &inspect_json); err != nil {
33+
t.Fatalf("unable to unmarshal body for %s version: %v", testVersion, err)
34+
}
35+
36+
keys := []string{"State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", "ResolvConfPath", "HostnamePath", "HostsPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "Volumes", "VolumesRW"}
37+
38+
if testVersion == "v1.11" {
39+
keys = append(keys, "ID")
40+
} else {
41+
keys = append(keys, "Id")
42+
}
43+
44+
for _, key := range keys {
45+
if _, ok := inspect_json[key]; !ok {
46+
t.Fatalf("%s does not exist in reponse for %s version", key, testVersion)
47+
}
48+
}
49+
}
50+
51+
deleteAllContainers()
52+
53+
logDone("container json - check keys in container json response")
54+
}

integration-cli/docker_utils.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,35 @@ func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
231231
return string(b), err
232232
}
233233

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+
234263
func deleteContainer(container string) error {
235264
container = strings.Replace(container, "\n", " ", -1)
236265
container = strings.Trim(container, " ")

0 commit comments

Comments
 (0)