node_inspect_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/swarm"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/pkg/errors"
  14. )
  15. func TestNodeInspectError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. _, _, err := client.NodeInspectWithRaw(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 TestNodeInspectNodeNotFound(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  27. }
  28. _, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
  29. if err == nil || !IsErrNotFound(err) {
  30. t.Fatalf("expected a nodeNotFoundError error, got %v", err)
  31. }
  32. }
  33. func TestNodeInspectWithEmptyID(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.NodeInspectWithRaw(context.Background(), "")
  40. if !IsErrNotFound(err) {
  41. t.Fatalf("Expected NotFoundError, got %v", err)
  42. }
  43. }
  44. func TestNodeInspect(t *testing.T) {
  45. expectedURL := "/nodes/node_id"
  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(swarm.Node{
  52. ID: "node_id",
  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. nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if nodeInspect.ID != "node_id" {
  68. t.Fatalf("expected `node_id`, got %s", nodeInspect.ID)
  69. }
  70. }