docker_api_inspect_test.go 3.4 KB

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