docker_api_inspect_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "encoding/json"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestInspectApiContainerResponse(t *testing.T) {
  9. defer deleteAllContainers()
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. t.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. body, err := sockRequest("GET", endpoint, nil)
  25. if err != nil {
  26. t.Fatalf("sockRequest failed for %s version: %v", testVersion, err)
  27. }
  28. var inspectJSON map[string]interface{}
  29. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  30. t.Fatalf("unable to unmarshal body for %s version: %v", testVersion, err)
  31. }
  32. keys := []string{"State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", "ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "Volumes", "VolumesRW"}
  33. if testVersion == "v1.11" {
  34. keys = append(keys, "ID")
  35. } else {
  36. keys = append(keys, "Id")
  37. }
  38. for _, key := range keys {
  39. if _, ok := inspectJSON[key]; !ok {
  40. t.Fatalf("%s does not exist in response for %s version", key, testVersion)
  41. }
  42. }
  43. //Issue #6830: type not properly converted to JSON/back
  44. if _, ok := inspectJSON["Path"].(bool); ok {
  45. t.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
  46. }
  47. }
  48. logDone("container json - check keys in container json response")
  49. }