github.com/skf/moby@v1.13.1/client/container_inspect_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "golang.org/x/net/context" 14 ) 15 16 func TestContainerInspectError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 21 _, err := client.ContainerInspect(context.Background(), "nothing") 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestContainerInspectContainerNotFound(t *testing.T) { 28 client := &Client{ 29 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 30 } 31 32 _, err := client.ContainerInspect(context.Background(), "unknown") 33 if err == nil || !IsErrContainerNotFound(err) { 34 t.Fatalf("expected a containerNotFound error, got %v", err) 35 } 36 } 37 38 func TestContainerInspect(t *testing.T) { 39 expectedURL := "/containers/container_id/json" 40 client := &Client{ 41 client: newMockClient(func(req *http.Request) (*http.Response, error) { 42 if !strings.HasPrefix(req.URL.Path, expectedURL) { 43 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 44 } 45 content, err := json.Marshal(types.ContainerJSON{ 46 ContainerJSONBase: &types.ContainerJSONBase{ 47 ID: "container_id", 48 Image: "image", 49 Name: "name", 50 }, 51 }) 52 if err != nil { 53 return nil, err 54 } 55 return &http.Response{ 56 StatusCode: http.StatusOK, 57 Body: ioutil.NopCloser(bytes.NewReader(content)), 58 }, nil 59 }), 60 } 61 62 r, err := client.ContainerInspect(context.Background(), "container_id") 63 if err != nil { 64 t.Fatal(err) 65 } 66 if r.ID != "container_id" { 67 t.Fatalf("expected `container_id`, got %s", r.ID) 68 } 69 if r.Image != "image" { 70 t.Fatalf("expected `image`, got %s", r.ID) 71 } 72 if r.Name != "name" { 73 t.Fatalf("expected `name`, got %s", r.ID) 74 } 75 } 76 77 func TestContainerInspectNode(t *testing.T) { 78 client := &Client{ 79 client: newMockClient(func(req *http.Request) (*http.Response, error) { 80 content, err := json.Marshal(types.ContainerJSON{ 81 ContainerJSONBase: &types.ContainerJSONBase{ 82 ID: "container_id", 83 Image: "image", 84 Name: "name", 85 Node: &types.ContainerNode{ 86 ID: "container_node_id", 87 Addr: "container_node", 88 Labels: map[string]string{"foo": "bar"}, 89 }, 90 }, 91 }) 92 if err != nil { 93 return nil, err 94 } 95 return &http.Response{ 96 StatusCode: http.StatusOK, 97 Body: ioutil.NopCloser(bytes.NewReader(content)), 98 }, nil 99 }), 100 } 101 102 r, err := client.ContainerInspect(context.Background(), "container_id") 103 if err != nil { 104 t.Fatal(err) 105 } 106 if r.ID != "container_id" { 107 t.Fatalf("expected `container_id`, got %s", r.ID) 108 } 109 if r.Image != "image" { 110 t.Fatalf("expected `image`, got %s", r.ID) 111 } 112 if r.Name != "name" { 113 t.Fatalf("expected `name`, got %s", r.ID) 114 } 115 if r.Node.ID != "container_node_id" { 116 t.Fatalf("expected `container_node_id`, got %s", r.Node.ID) 117 } 118 if r.Node.Addr != "container_node" { 119 t.Fatalf("expected `container_node`, got %s", r.Node.Addr) 120 } 121 foo, ok := r.Node.Labels["foo"] 122 if foo != "bar" || !ok { 123 t.Fatalf("expected `bar` for label `foo`") 124 } 125 }