github.com/rish1988/moby@v25.0.2+incompatible/client/container_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" 14 "github.com/docker/docker/errdefs" 15 "github.com/pkg/errors" 16 "gotest.tools/v3/assert" 17 is "gotest.tools/v3/assert/cmp" 18 ) 19 20 func TestContainerInspectError(t *testing.T) { 21 client := &Client{ 22 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 23 } 24 25 _, err := client.ContainerInspect(context.Background(), "nothing") 26 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 27 } 28 29 func TestContainerInspectContainerNotFound(t *testing.T) { 30 client := &Client{ 31 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 32 } 33 34 _, err := client.ContainerInspect(context.Background(), "unknown") 35 assert.Check(t, is.ErrorType(err, errdefs.IsNotFound)) 36 } 37 38 func TestContainerInspectWithEmptyID(t *testing.T) { 39 client := &Client{ 40 client: newMockClient(func(req *http.Request) (*http.Response, error) { 41 return nil, errors.New("should not make request") 42 }), 43 } 44 _, _, err := client.ContainerInspectWithRaw(context.Background(), "", true) 45 assert.Check(t, is.ErrorType(err, errdefs.IsNotFound)) 46 } 47 48 func TestContainerInspect(t *testing.T) { 49 expectedURL := "/containers/container_id/json" 50 client := &Client{ 51 client: newMockClient(func(req *http.Request) (*http.Response, error) { 52 if !strings.HasPrefix(req.URL.Path, expectedURL) { 53 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 54 } 55 content, err := json.Marshal(types.ContainerJSON{ 56 ContainerJSONBase: &types.ContainerJSONBase{ 57 ID: "container_id", 58 Image: "image", 59 Name: "name", 60 }, 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 r, err := client.ContainerInspect(context.Background(), "container_id") 73 if err != nil { 74 t.Fatal(err) 75 } 76 if r.ID != "container_id" { 77 t.Fatalf("expected `container_id`, got %s", r.ID) 78 } 79 if r.Image != "image" { 80 t.Fatalf("expected `image`, got %s", r.Image) 81 } 82 if r.Name != "name" { 83 t.Fatalf("expected `name`, got %s", r.Name) 84 } 85 } 86 87 // TestContainerInspectNode tests that the "Node" field is included in the "inspect" 88 // output. This information is only present when connected to a Swarm standalone API. 89 func TestContainerInspectNode(t *testing.T) { 90 client := &Client{ 91 client: newMockClient(func(req *http.Request) (*http.Response, error) { 92 content, err := json.Marshal(types.ContainerJSON{ 93 ContainerJSONBase: &types.ContainerJSONBase{ 94 ID: "container_id", 95 Image: "image", 96 Name: "name", 97 Node: &types.ContainerNode{ 98 ID: "container_node_id", 99 Addr: "container_node", 100 Labels: map[string]string{"foo": "bar"}, 101 }, 102 }, 103 }) 104 if err != nil { 105 return nil, err 106 } 107 return &http.Response{ 108 StatusCode: http.StatusOK, 109 Body: io.NopCloser(bytes.NewReader(content)), 110 }, nil 111 }), 112 } 113 114 r, err := client.ContainerInspect(context.Background(), "container_id") 115 if err != nil { 116 t.Fatal(err) 117 } 118 if r.ID != "container_id" { 119 t.Fatalf("expected `container_id`, got %s", r.ID) 120 } 121 if r.Image != "image" { 122 t.Fatalf("expected `image`, got %s", r.Image) 123 } 124 if r.Name != "name" { 125 t.Fatalf("expected `name`, got %s", r.Name) 126 } 127 if r.Node.ID != "container_node_id" { 128 t.Fatalf("expected `container_node_id`, got %s", r.Node.ID) 129 } 130 if r.Node.Addr != "container_node" { 131 t.Fatalf("expected `container_node`, got %s", r.Node.Addr) 132 } 133 foo, ok := r.Node.Labels["foo"] 134 if foo != "bar" || !ok { 135 t.Fatalf("expected `bar` for label `foo`") 136 } 137 }