node_inspect_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/swarm"
  11. "golang.org/x/net/context"
  12. )
  13. func TestNodeInspectError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. _, _, err := client.NodeInspectWithRaw(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 TestNodeInspectNodeNotFound(t *testing.T) {
  23. client := &Client{
  24. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  25. }
  26. _, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
  27. if err == nil || !IsErrNodeNotFound(err) {
  28. t.Fatalf("expected a nodeNotFoundError error, got %v", err)
  29. }
  30. }
  31. func TestNodeInspect(t *testing.T) {
  32. expectedURL := "/nodes/node_id"
  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(swarm.Node{
  39. ID: "node_id",
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &http.Response{
  45. StatusCode: http.StatusOK,
  46. Body: ioutil.NopCloser(bytes.NewReader(content)),
  47. }, nil
  48. }),
  49. }
  50. nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if nodeInspect.ID != "node_id" {
  55. t.Fatalf("expected `node_id`, got %s", nodeInspect.ID)
  56. }
  57. }