container_inspect_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/pkg/errors"
  14. )
  15. func TestContainerInspectError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. _, err := client.ContainerInspect(context.Background(), "nothing")
  20. if !errdefs.IsSystem(err) {
  21. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  22. }
  23. }
  24. func TestContainerInspectContainerNotFound(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  27. }
  28. _, err := client.ContainerInspect(context.Background(), "unknown")
  29. if err == nil || !IsErrNotFound(err) {
  30. t.Fatalf("expected a containerNotFound error, got %v", err)
  31. }
  32. }
  33. func TestContainerInspectWithEmptyID(t *testing.T) {
  34. client := &Client{
  35. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  36. return nil, errors.New("should not make request")
  37. }),
  38. }
  39. _, _, err := client.ContainerInspectWithRaw(context.Background(), "", true)
  40. if !IsErrNotFound(err) {
  41. t.Fatalf("Expected NotFoundError, got %v", err)
  42. }
  43. }
  44. func TestContainerInspect(t *testing.T) {
  45. expectedURL := "/containers/container_id/json"
  46. client := &Client{
  47. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  48. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  49. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  50. }
  51. content, err := json.Marshal(types.ContainerJSON{
  52. ContainerJSONBase: &types.ContainerJSONBase{
  53. ID: "container_id",
  54. Image: "image",
  55. Name: "name",
  56. },
  57. })
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &http.Response{
  62. StatusCode: http.StatusOK,
  63. Body: io.NopCloser(bytes.NewReader(content)),
  64. }, nil
  65. }),
  66. }
  67. r, err := client.ContainerInspect(context.Background(), "container_id")
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if r.ID != "container_id" {
  72. t.Fatalf("expected `container_id`, got %s", r.ID)
  73. }
  74. if r.Image != "image" {
  75. t.Fatalf("expected `image`, got %s", r.Image)
  76. }
  77. if r.Name != "name" {
  78. t.Fatalf("expected `name`, got %s", r.Name)
  79. }
  80. }
  81. // TestContainerInspectNode tests that the "Node" field is included in the "inspect"
  82. // output. This information is only present when connected to a Swarm standalone API.
  83. func TestContainerInspectNode(t *testing.T) {
  84. client := &Client{
  85. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  86. content, err := json.Marshal(types.ContainerJSON{
  87. ContainerJSONBase: &types.ContainerJSONBase{
  88. ID: "container_id",
  89. Image: "image",
  90. Name: "name",
  91. Node: &types.ContainerNode{
  92. ID: "container_node_id",
  93. Addr: "container_node",
  94. Labels: map[string]string{"foo": "bar"},
  95. },
  96. },
  97. })
  98. if err != nil {
  99. return nil, err
  100. }
  101. return &http.Response{
  102. StatusCode: http.StatusOK,
  103. Body: io.NopCloser(bytes.NewReader(content)),
  104. }, nil
  105. }),
  106. }
  107. r, err := client.ContainerInspect(context.Background(), "container_id")
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if r.ID != "container_id" {
  112. t.Fatalf("expected `container_id`, got %s", r.ID)
  113. }
  114. if r.Image != "image" {
  115. t.Fatalf("expected `image`, got %s", r.Image)
  116. }
  117. if r.Name != "name" {
  118. t.Fatalf("expected `name`, got %s", r.Name)
  119. }
  120. if r.Node.ID != "container_node_id" {
  121. t.Fatalf("expected `container_node_id`, got %s", r.Node.ID)
  122. }
  123. if r.Node.Addr != "container_node" {
  124. t.Fatalf("expected `container_node`, got %s", r.Node.Addr)
  125. }
  126. foo, ok := r.Node.Labels["foo"]
  127. if foo != "bar" || !ok {
  128. t.Fatalf("expected `bar` for label `foo`")
  129. }
  130. }