docker_api_inspect_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os/exec"
  6. "testing"
  7. )
  8. func TestInspectContainerResponse(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.Fatal("sockRequest failed for %s version: %v", testVersion, err)
  24. }
  25. var inspect_json map[string]interface{}
  26. if err = json.Unmarshal(body, &inspect_json); 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 := inspect_json[key]; !ok {
  37. t.Fatalf("%s does not exist in reponse for %s version", key, testVersion)
  38. }
  39. }
  40. }
  41. deleteAllContainers()
  42. logDone("container json - check keys in container json response")
  43. }