container_inspect_test.go 3.7 KB

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