docker_api_inspect_test.go 1.7 KB

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