github.com/rish1988/moby@v25.0.2+incompatible/client/swarm_inspect_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types/swarm" 14 "github.com/docker/docker/errdefs" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestSwarmInspectError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 24 _, err := client.SwarmInspect(context.Background()) 25 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 26 } 27 28 func TestSwarmInspect(t *testing.T) { 29 expectedURL := "/swarm" 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 content, err := json.Marshal(swarm.Swarm{ 36 ClusterInfo: swarm.ClusterInfo{ 37 ID: "swarm_id", 38 }, 39 }) 40 if err != nil { 41 return nil, err 42 } 43 return &http.Response{ 44 StatusCode: http.StatusOK, 45 Body: io.NopCloser(bytes.NewReader(content)), 46 }, nil 47 }), 48 } 49 50 swarmInspect, err := client.SwarmInspect(context.Background()) 51 if err != nil { 52 t.Fatal(err) 53 } 54 if swarmInspect.ID != "swarm_id" { 55 t.Fatalf("expected `swarm_id`, got %s", swarmInspect.ID) 56 } 57 }