container_inspect_test.go 3.6 KB

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