docker_api_inspect_test.go 1.7 KB

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