github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/client/swarm_init_test.go (about) 1 package client // import "github.com/Prakhar-Agarwal-byte/moby/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/Prakhar-Agarwal-byte/moby/api/types/swarm" 13 "github.com/Prakhar-Agarwal-byte/moby/errdefs" 14 "gotest.tools/v3/assert" 15 is "gotest.tools/v3/assert/cmp" 16 ) 17 18 func TestSwarmInitError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 23 _, err := client.SwarmInit(context.Background(), swarm.InitRequest{}) 24 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 25 } 26 27 func TestSwarmInit(t *testing.T) { 28 expectedURL := "/swarm/init" 29 30 client := &Client{ 31 client: newMockClient(func(req *http.Request) (*http.Response, error) { 32 if !strings.HasPrefix(req.URL.Path, expectedURL) { 33 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 34 } 35 if req.Method != http.MethodPost { 36 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 37 } 38 return &http.Response{ 39 StatusCode: http.StatusOK, 40 Body: io.NopCloser(bytes.NewReader([]byte(`"body"`))), 41 }, nil 42 }), 43 } 44 45 resp, err := client.SwarmInit(context.Background(), swarm.InitRequest{ 46 ListenAddr: "0.0.0.0:2377", 47 }) 48 if err != nil { 49 t.Fatal(err) 50 } 51 if resp != "body" { 52 t.Fatalf("Expected 'body', got %s", resp) 53 } 54 }