swarm_join_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/swarm"
  11. "github.com/docker/docker/errdefs"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. )
  15. func TestSwarmJoinError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. err := client.SwarmJoin(context.Background(), swarm.JoinRequest{})
  20. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  21. }
  22. func TestSwarmJoin(t *testing.T) {
  23. expectedURL := "/swarm/join"
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  27. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  28. }
  29. if req.Method != http.MethodPost {
  30. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  31. }
  32. return &http.Response{
  33. StatusCode: http.StatusOK,
  34. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  35. }, nil
  36. }),
  37. }
  38. err := client.SwarmJoin(context.Background(), swarm.JoinRequest{
  39. ListenAddr: "0.0.0.0:2377",
  40. })
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. }