github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/node_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 "github.com/pkg/errors" 16 ) 17 18 func TestNodeInspectError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 23 _, _, err := client.NodeInspectWithRaw(context.Background(), "nothing") 24 if !errdefs.IsSystem(err) { 25 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 26 } 27 } 28 29 func TestNodeInspectNodeNotFound(t *testing.T) { 30 client := &Client{ 31 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 32 } 33 34 _, _, err := client.NodeInspectWithRaw(context.Background(), "unknown") 35 if err == nil || !IsErrNotFound(err) { 36 t.Fatalf("expected a nodeNotFoundError error, got %v", err) 37 } 38 } 39 40 func TestNodeInspectWithEmptyID(t *testing.T) { 41 client := &Client{ 42 client: newMockClient(func(req *http.Request) (*http.Response, error) { 43 return nil, errors.New("should not make request") 44 }), 45 } 46 _, _, err := client.NodeInspectWithRaw(context.Background(), "") 47 if !IsErrNotFound(err) { 48 t.Fatalf("Expected NotFoundError, got %v", err) 49 } 50 } 51 52 func TestNodeInspect(t *testing.T) { 53 expectedURL := "/nodes/node_id" 54 client := &Client{ 55 client: newMockClient(func(req *http.Request) (*http.Response, error) { 56 if !strings.HasPrefix(req.URL.Path, expectedURL) { 57 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 58 } 59 content, err := json.Marshal(swarm.Node{ 60 ID: "node_id", 61 }) 62 if err != nil { 63 return nil, err 64 } 65 return &http.Response{ 66 StatusCode: http.StatusOK, 67 Body: io.NopCloser(bytes.NewReader(content)), 68 }, nil 69 }), 70 } 71 72 nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id") 73 if err != nil { 74 t.Fatal(err) 75 } 76 if nodeInspect.ID != "node_id" { 77 t.Fatalf("expected `node_id`, got %s", nodeInspect.ID) 78 } 79 }