node_inspect_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestNodeInspectError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  20. }
  21. _, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
  22. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  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. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  30. }
  31. func TestNodeInspectWithEmptyID(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.NodeInspectWithRaw(context.Background(), "")
  38. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  39. }
  40. func TestNodeInspect(t *testing.T) {
  41. expectedURL := "/nodes/node_id"
  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(swarm.Node{
  48. ID: "node_id",
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &http.Response{
  54. StatusCode: http.StatusOK,
  55. Body: io.NopCloser(bytes.NewReader(content)),
  56. }, nil
  57. }),
  58. }
  59. nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if nodeInspect.ID != "node_id" {
  64. t.Fatalf("expected `node_id`, got %s", nodeInspect.ID)
  65. }
  66. }