github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/integration-cli/docker_api_inspect_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/versions/v1p20" 11 "github.com/docker/docker/client" 12 "github.com/docker/docker/integration-cli/checker" 13 "github.com/go-check/check" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func (s *DockerSuite) TestInspectAPIContainerResponse(c *check.C) { 18 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 19 20 cleanedContainerID := strings.TrimSpace(out) 21 keysBase := []string{"Id", "State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", 22 "ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "MountLabel", "ProcessLabel", "GraphDriver"} 23 24 type acase struct { 25 version string 26 keys []string 27 } 28 29 var cases []acase 30 31 if testEnv.DaemonPlatform() == "windows" { 32 cases = []acase{ 33 {"v1.25", append(keysBase, "Mounts")}, 34 } 35 36 } else { 37 cases = []acase{ 38 {"v1.20", append(keysBase, "Mounts")}, 39 {"v1.19", append(keysBase, "Volumes", "VolumesRW")}, 40 } 41 } 42 43 for _, cs := range cases { 44 body := getInspectBody(c, cs.version, cleanedContainerID) 45 46 var inspectJSON map[string]interface{} 47 err := json.Unmarshal(body, &inspectJSON) 48 c.Assert(err, checker.IsNil, check.Commentf("Unable to unmarshal body for version %s", cs.version)) 49 50 for _, key := range cs.keys { 51 _, ok := inspectJSON[key] 52 c.Check(ok, checker.True, check.Commentf("%s does not exist in response for version %s", key, cs.version)) 53 } 54 55 //Issue #6830: type not properly converted to JSON/back 56 _, ok := inspectJSON["Path"].(bool) 57 c.Assert(ok, checker.False, check.Commentf("Path of `true` should not be converted to boolean `true` via JSON marshalling")) 58 } 59 } 60 61 func (s *DockerSuite) TestInspectAPIContainerVolumeDriverLegacy(c *check.C) { 62 // No legacy implications for Windows 63 testRequires(c, DaemonIsLinux) 64 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 65 66 cleanedContainerID := strings.TrimSpace(out) 67 68 cases := []string{"v1.19", "v1.20"} 69 for _, version := range cases { 70 body := getInspectBody(c, version, cleanedContainerID) 71 72 var inspectJSON map[string]interface{} 73 err := json.Unmarshal(body, &inspectJSON) 74 c.Assert(err, checker.IsNil, check.Commentf("Unable to unmarshal body for version %s", version)) 75 76 config, ok := inspectJSON["Config"] 77 c.Assert(ok, checker.True, check.Commentf("Unable to find 'Config'")) 78 cfg := config.(map[string]interface{}) 79 _, ok = cfg["VolumeDriver"] 80 c.Assert(ok, checker.True, check.Commentf("API version %s expected to include VolumeDriver in 'Config'", version)) 81 } 82 } 83 84 func (s *DockerSuite) TestInspectAPIContainerVolumeDriver(c *check.C) { 85 out, _ := dockerCmd(c, "run", "-d", "--volume-driver", "local", "busybox", "true") 86 87 cleanedContainerID := strings.TrimSpace(out) 88 89 body := getInspectBody(c, "v1.25", cleanedContainerID) 90 91 var inspectJSON map[string]interface{} 92 err := json.Unmarshal(body, &inspectJSON) 93 c.Assert(err, checker.IsNil, check.Commentf("Unable to unmarshal body for version 1.25")) 94 95 config, ok := inspectJSON["Config"] 96 c.Assert(ok, checker.True, check.Commentf("Unable to find 'Config'")) 97 cfg := config.(map[string]interface{}) 98 _, ok = cfg["VolumeDriver"] 99 c.Assert(ok, checker.False, check.Commentf("API version 1.25 expected to not include VolumeDriver in 'Config'")) 100 101 config, ok = inspectJSON["HostConfig"] 102 c.Assert(ok, checker.True, check.Commentf("Unable to find 'HostConfig'")) 103 cfg = config.(map[string]interface{}) 104 _, ok = cfg["VolumeDriver"] 105 c.Assert(ok, checker.True, check.Commentf("API version 1.25 expected to include VolumeDriver in 'HostConfig'")) 106 } 107 108 func (s *DockerSuite) TestInspectAPIImageResponse(c *check.C) { 109 dockerCmd(c, "tag", "busybox:latest", "busybox:mytag") 110 cli, err := client.NewEnvClient() 111 c.Assert(err, checker.IsNil) 112 defer cli.Close() 113 114 imageJSON, _, err := cli.ImageInspectWithRaw(context.Background(), "busybox") 115 c.Assert(err, checker.IsNil) 116 117 c.Assert(imageJSON.RepoTags, checker.HasLen, 2) 118 assert.Contains(c, imageJSON.RepoTags, "busybox:latest") 119 assert.Contains(c, imageJSON.RepoTags, "busybox:mytag") 120 } 121 122 // #17131, #17139, #17173 123 func (s *DockerSuite) TestInspectAPIEmptyFieldsInConfigPre121(c *check.C) { 124 // Not relevant on Windows 125 testRequires(c, DaemonIsLinux) 126 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 127 128 cleanedContainerID := strings.TrimSpace(out) 129 130 cases := []string{"v1.19", "v1.20"} 131 for _, version := range cases { 132 body := getInspectBody(c, version, cleanedContainerID) 133 134 var inspectJSON map[string]interface{} 135 err := json.Unmarshal(body, &inspectJSON) 136 c.Assert(err, checker.IsNil, check.Commentf("Unable to unmarshal body for version %s", version)) 137 config, ok := inspectJSON["Config"] 138 c.Assert(ok, checker.True, check.Commentf("Unable to find 'Config'")) 139 cfg := config.(map[string]interface{}) 140 for _, f := range []string{"MacAddress", "NetworkDisabled", "ExposedPorts"} { 141 _, ok := cfg[f] 142 c.Check(ok, checker.True, check.Commentf("API version %s expected to include %s in 'Config'", version, f)) 143 } 144 } 145 } 146 147 func (s *DockerSuite) TestInspectAPIBridgeNetworkSettings120(c *check.C) { 148 // Not relevant on Windows, and besides it doesn't have any bridge network settings 149 testRequires(c, DaemonIsLinux) 150 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 151 containerID := strings.TrimSpace(out) 152 waitRun(containerID) 153 154 body := getInspectBody(c, "v1.20", containerID) 155 156 var inspectJSON v1p20.ContainerJSON 157 err := json.Unmarshal(body, &inspectJSON) 158 c.Assert(err, checker.IsNil) 159 160 settings := inspectJSON.NetworkSettings 161 c.Assert(settings.IPAddress, checker.Not(checker.HasLen), 0) 162 } 163 164 func (s *DockerSuite) TestInspectAPIBridgeNetworkSettings121(c *check.C) { 165 // Windows doesn't have any bridge network settings 166 testRequires(c, DaemonIsLinux) 167 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 168 containerID := strings.TrimSpace(out) 169 waitRun(containerID) 170 171 body := getInspectBody(c, "v1.21", containerID) 172 173 var inspectJSON types.ContainerJSON 174 err := json.Unmarshal(body, &inspectJSON) 175 c.Assert(err, checker.IsNil) 176 177 settings := inspectJSON.NetworkSettings 178 c.Assert(settings.IPAddress, checker.Not(checker.HasLen), 0) 179 c.Assert(settings.Networks["bridge"], checker.Not(checker.IsNil)) 180 c.Assert(settings.IPAddress, checker.Equals, settings.Networks["bridge"].IPAddress) 181 }