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. defer deleteAllContainers()
  9. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  10. out, _, err := runCommandWithOutput(runCmd)
  11. if err != nil {
  12. t.Fatalf("failed to create a container: %s, %v", out, err)
  13. }
  14. cleanedContainerID := stripTrailingCharacters(out)
  15. // test on json marshal version
  16. // and latest version
  17. testVersions := []string{"v1.11", "latest"}
  18. for _, testVersion := range testVersions {
  19. endpoint := "/containers/" + cleanedContainerID + "/json"
  20. if testVersion != "latest" {
  21. endpoint = "/" + testVersion + endpoint
  22. }
  23. body, err := sockRequest("GET", endpoint, nil)
  24. if err != nil {
  25. t.Fatalf("sockRequest failed for %s version: %v", testVersion, err)
  26. }
  27. var inspectJSON map[string]interface{}
  28. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  29. t.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. t.Fatalf("%s does not exist in reponse 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. t.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
  45. }
  46. }
  47. logDone("container json - check keys in container json response")
  48. }