github.com/kinvolk/docker@v1.13.1/client/swarm_leave_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 11 "golang.org/x/net/context" 12 ) 13 14 func TestSwarmLeaveError(t *testing.T) { 15 client := &Client{ 16 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 17 } 18 19 err := client.SwarmLeave(context.Background(), false) 20 if err == nil || err.Error() != "Error response from daemon: Server error" { 21 t.Fatalf("expected a Server Error, got %v", err) 22 } 23 } 24 25 func TestSwarmLeave(t *testing.T) { 26 expectedURL := "/swarm/leave" 27 28 leaveCases := []struct { 29 force bool 30 expectedForce string 31 }{ 32 { 33 expectedForce: "", 34 }, 35 { 36 force: true, 37 expectedForce: "1", 38 }, 39 } 40 41 for _, leaveCase := range leaveCases { 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 if req.Method != "POST" { 48 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 49 } 50 force := req.URL.Query().Get("force") 51 if force != leaveCase.expectedForce { 52 return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", leaveCase.expectedForce, force) 53 } 54 return &http.Response{ 55 StatusCode: http.StatusOK, 56 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 57 }, nil 58 }), 59 } 60 61 err := client.SwarmLeave(context.Background(), leaveCase.force) 62 if err != nil { 63 t.Fatal(err) 64 } 65 } 66 }