github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/swarm_join_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types/swarm" 13 ) 14 15 func TestSwarmJoinError(t *testing.T) { 16 client := &Client{ 17 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 18 } 19 20 err := client.SwarmJoin(context.Background(), swarm.JoinRequest{}) 21 if err == nil || err.Error() != "Error response from daemon: Server error" { 22 t.Fatalf("expected a Server Error, got %v", err) 23 } 24 } 25 26 func TestSwarmJoin(t *testing.T) { 27 expectedURL := "/swarm/join" 28 29 client := &Client{ 30 client: newMockClient(func(req *http.Request) (*http.Response, error) { 31 if !strings.HasPrefix(req.URL.Path, expectedURL) { 32 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 33 } 34 if req.Method != "POST" { 35 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 36 } 37 return &http.Response{ 38 StatusCode: http.StatusOK, 39 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 40 }, nil 41 }), 42 } 43 44 err := client.SwarmJoin(context.Background(), swarm.JoinRequest{ 45 ListenAddr: "0.0.0.0:2377", 46 }) 47 if err != nil { 48 t.Fatal(err) 49 } 50 }