docker_api_inspect_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/pkg/stringutils"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestInspectApiContainerResponse(c *check.C) {
  12. testRequires(c, DaemonIsLinux)
  13. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  14. cleanedContainerID := strings.TrimSpace(out)
  15. keysBase := []string{"Id", "State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings",
  16. "ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "GraphDriver"}
  17. cases := []struct {
  18. version string
  19. keys []string
  20. }{
  21. {"1.20", append(keysBase, "Mounts")},
  22. {"1.19", append(keysBase, "Volumes", "VolumesRW")},
  23. }
  24. for _, cs := range cases {
  25. endpoint := fmt.Sprintf("/v%s/containers/%s/json", cs.version, cleanedContainerID)
  26. status, body, err := sockRequest("GET", endpoint, nil)
  27. c.Assert(status, check.Equals, http.StatusOK)
  28. c.Assert(err, check.IsNil)
  29. var inspectJSON map[string]interface{}
  30. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  31. c.Fatalf("unable to unmarshal body for version %s: %v", cs.version, err)
  32. }
  33. for _, key := range cs.keys {
  34. if _, ok := inspectJSON[key]; !ok {
  35. c.Fatalf("%s does not exist in response for version %s", key, cs.version)
  36. }
  37. }
  38. //Issue #6830: type not properly converted to JSON/back
  39. if _, ok := inspectJSON["Path"].(bool); ok {
  40. c.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
  41. }
  42. }
  43. }
  44. func (s *DockerSuite) TestInspectApiContainerVolumeDriverLegacy(c *check.C) {
  45. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  46. cleanedContainerID := strings.TrimSpace(out)
  47. cases := []string{"1.19", "1.20"}
  48. for _, version := range cases {
  49. endpoint := fmt.Sprintf("/v%s/containers/%s/json", version, cleanedContainerID)
  50. status, body, err := sockRequest("GET", endpoint, nil)
  51. c.Assert(status, check.Equals, http.StatusOK)
  52. c.Assert(err, check.IsNil)
  53. var inspectJSON map[string]interface{}
  54. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  55. c.Fatalf("unable to unmarshal body for version %s: %v", version, err)
  56. }
  57. config, ok := inspectJSON["Config"]
  58. if !ok {
  59. c.Fatal("Unable to find 'Config'")
  60. }
  61. cfg := config.(map[string]interface{})
  62. if _, ok := cfg["VolumeDriver"]; !ok {
  63. c.Fatalf("Api version %s expected to include VolumeDriver in 'Config'", version)
  64. }
  65. }
  66. }
  67. func (s *DockerSuite) TestInspectApiContainerVolumeDriver(c *check.C) {
  68. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  69. cleanedContainerID := strings.TrimSpace(out)
  70. endpoint := fmt.Sprintf("/v1.21/containers/%s/json", cleanedContainerID)
  71. status, body, err := sockRequest("GET", endpoint, nil)
  72. c.Assert(status, check.Equals, http.StatusOK)
  73. c.Assert(err, check.IsNil)
  74. var inspectJSON map[string]interface{}
  75. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  76. c.Fatalf("unable to unmarshal body for version 1.21: %v", err)
  77. }
  78. config, ok := inspectJSON["Config"]
  79. if !ok {
  80. c.Fatal("Unable to find 'Config'")
  81. }
  82. cfg := config.(map[string]interface{})
  83. if _, ok := cfg["VolumeDriver"]; ok {
  84. c.Fatal("Api version 1.21 expected to not include VolumeDriver in 'Config'")
  85. }
  86. config, ok = inspectJSON["HostConfig"]
  87. if !ok {
  88. c.Fatal("Unable to find 'HostConfig'")
  89. }
  90. cfg = config.(map[string]interface{})
  91. if _, ok := cfg["VolumeDriver"]; !ok {
  92. c.Fatal("Api version 1.21 expected to include VolumeDriver in 'HostConfig'")
  93. }
  94. }
  95. func (s *DockerSuite) TestInspectApiImageResponse(c *check.C) {
  96. dockerCmd(c, "tag", "busybox:latest", "busybox:mytag")
  97. endpoint := "/images/busybox/json"
  98. status, body, err := sockRequest("GET", endpoint, nil)
  99. c.Assert(err, check.IsNil)
  100. c.Assert(status, check.Equals, http.StatusOK)
  101. var imageJSON types.ImageInspect
  102. if err = json.Unmarshal(body, &imageJSON); err != nil {
  103. c.Fatalf("unable to unmarshal body for latest version: %v", err)
  104. }
  105. c.Assert(len(imageJSON.Tags), check.Equals, 2)
  106. c.Assert(stringutils.InSlice(imageJSON.Tags, "busybox:latest"), check.Equals, true)
  107. c.Assert(stringutils.InSlice(imageJSON.Tags, "busybox:mytag"), check.Equals, true)
  108. }