container_inspect_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "golang.org/x/net/context"
  12. )
  13. func TestContainerInspectError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. _, err := client.ContainerInspect(context.Background(), "nothing")
  18. if err == nil || err.Error() != "Error response from daemon: Server error" {
  19. t.Fatalf("expected a Server Error, got %v", err)
  20. }
  21. }
  22. func TestContainerInspectContainerNotFound(t *testing.T) {
  23. client := &Client{
  24. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  25. }
  26. _, err := client.ContainerInspect(context.Background(), "unknown")
  27. if err == nil || !IsErrContainerNotFound(err) {
  28. t.Fatalf("expected a containerNotFound error, got %v", err)
  29. }
  30. }
  31. func TestContainerInspect(t *testing.T) {
  32. expectedURL := "/containers/container_id/json"
  33. client := &Client{
  34. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  35. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  36. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  37. }
  38. content, err := json.Marshal(types.ContainerJSON{
  39. ContainerJSONBase: &types.ContainerJSONBase{
  40. ID: "container_id",
  41. Image: "image",
  42. Name: "name",
  43. },
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &http.Response{
  49. StatusCode: http.StatusOK,
  50. Body: ioutil.NopCloser(bytes.NewReader(content)),
  51. }, nil
  52. }),
  53. }
  54. r, err := client.ContainerInspect(context.Background(), "container_id")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if r.ID != "container_id" {
  59. t.Fatalf("expected `container_id`, got %s", r.ID)
  60. }
  61. if r.Image != "image" {
  62. t.Fatalf("expected `image`, got %s", r.Image)
  63. }
  64. if r.Name != "name" {
  65. t.Fatalf("expected `name`, got %s", r.Name)
  66. }
  67. }
  68. func TestContainerInspectNode(t *testing.T) {
  69. client := &Client{
  70. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  71. content, err := json.Marshal(types.ContainerJSON{
  72. ContainerJSONBase: &types.ContainerJSONBase{
  73. ID: "container_id",
  74. Image: "image",
  75. Name: "name",
  76. Node: &types.ContainerNode{
  77. ID: "container_node_id",
  78. Addr: "container_node",
  79. Labels: map[string]string{"foo": "bar"},
  80. },
  81. },
  82. })
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &http.Response{
  87. StatusCode: http.StatusOK,
  88. Body: ioutil.NopCloser(bytes.NewReader(content)),
  89. }, nil
  90. }),
  91. }
  92. r, err := client.ContainerInspect(context.Background(), "container_id")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. if r.ID != "container_id" {
  97. t.Fatalf("expected `container_id`, got %s", r.ID)
  98. }
  99. if r.Image != "image" {
  100. t.Fatalf("expected `image`, got %s", r.Image)
  101. }
  102. if r.Name != "name" {
  103. t.Fatalf("expected `name`, got %s", r.Name)
  104. }
  105. if r.Node.ID != "container_node_id" {
  106. t.Fatalf("expected `container_node_id`, got %s", r.Node.ID)
  107. }
  108. if r.Node.Addr != "container_node" {
  109. t.Fatalf("expected `container_node`, got %s", r.Node.Addr)
  110. }
  111. foo, ok := r.Node.Labels["foo"]
  112. if foo != "bar" || !ok {
  113. t.Fatalf("expected `bar` for label `foo`")
  114. }
  115. }