docker_api_inspect_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "os/exec"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestInspectApiContainerResponse(c *check.C) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. c.Fatalf("failed to create a container: %s, %v", out, err)
  14. }
  15. cleanedContainerID := strings.TrimSpace(out)
  16. // test on json marshal version
  17. // and latest version
  18. testVersions := []string{"v1.11", "latest"}
  19. for _, testVersion := range testVersions {
  20. endpoint := "/containers/" + cleanedContainerID + "/json"
  21. if testVersion != "latest" {
  22. endpoint = "/" + testVersion + endpoint
  23. }
  24. status, body, err := sockRequest("GET", endpoint, nil)
  25. c.Assert(status, check.Equals, http.StatusOK)
  26. c.Assert(err, check.IsNil)
  27. var inspectJSON map[string]interface{}
  28. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  29. c.Fatalf("unable to unmarshal body for %s version: %v", testVersion, err)
  30. }
  31. keys := []string{"State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", "ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "Volumes", "VolumesRW"}
  32. if testVersion == "v1.11" {
  33. keys = append(keys, "ID")
  34. } else {
  35. keys = append(keys, "Id")
  36. }
  37. for _, key := range keys {
  38. if _, ok := inspectJSON[key]; !ok {
  39. c.Fatalf("%s does not exist in response for %s version", key, testVersion)
  40. }
  41. }
  42. //Issue #6830: type not properly converted to JSON/back
  43. if _, ok := inspectJSON["Path"].(bool); ok {
  44. c.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
  45. }
  46. }
  47. }