@@ -32,6 +32,8 @@ var _ = Describe("Container Inspect API", func() {
3232 req * http.Request
3333 resp types.Container
3434 respJSON []byte
35+ req2 * http.Request
36+ err error
3537 )
3638 BeforeEach (func () {
3739 mockCtrl = gomock .NewController (GinkgoT ())
@@ -42,29 +44,60 @@ var _ = Describe("Container Inspect API", func() {
4244 h = newHandler (service , & c , logger )
4345 rr = httptest .NewRecorder ()
4446 cid = "123"
45- var err error
46- req , err = http .NewRequest (http .MethodGet , fmt .Sprintf ("/containers/%s/json" , cid ), nil )
47- Expect (err ).Should (BeNil ())
48- req = mux .SetURLVars (req , map [string ]string {"id" : "123" })
47+
48+ // Create a helper function to create and set up requests
49+ createRequest := func (sizeParam bool ) * http.Request {
50+ url := fmt .Sprintf ("/containers/%s/json" , cid )
51+ if sizeParam {
52+ url += "?size=true"
53+ }
54+ req , err := http .NewRequest (http .MethodGet , url , nil )
55+ Expect (err ).Should (BeNil ())
56+ return mux .SetURLVars (req , map [string ]string {"id" : cid })
57+ }
58+
59+ // Create both requests using the helper function
60+ req = createRequest (false )
61+ req2 = createRequest (true )
62+
63+ sizeRw := int64 (1000 )
64+ sizeRootFs := int64 (5000 )
4965 resp = types.Container {
50- ID : cid ,
51- Image : "test-image" ,
52- Name : "/test-container" ,
66+ ID : cid ,
67+ Image : "test-image" ,
68+ Name : "/test-container" ,
69+ SizeRw : & sizeRw ,
70+ SizeRootFs : & sizeRootFs ,
5371 }
5472 respJSON , err = json .Marshal (resp )
5573 Expect (err ).Should (BeNil ())
5674 })
5775 Context ("handler" , func () {
5876 It ("should return inspect object and 200 status code upon success" , func () {
59- service .EXPECT ().Inspect (gomock .Any (), cid ).Return (& resp , nil )
77+ service .EXPECT ().Inspect (gomock .Any (), cid , false ).Return (& resp , nil )
6078
6179 // handler should return response object with 200 status code
6280 h .inspect (rr , req )
6381 Expect (rr .Body ).Should (MatchJSON (respJSON ))
6482 Expect (rr ).Should (HaveHTTPStatus (http .StatusOK ))
6583 })
84+ It ("should return inspect object with size information and 200 status code upon success" , func () {
85+ service .EXPECT ().Inspect (gomock .Any (), cid , true ).Return (& resp , nil )
86+
87+ h .inspect (rr , req2 )
88+ Expect (rr .Body ).Should (MatchJSON (respJSON ))
89+ Expect (rr ).Should (HaveHTTPStatus (http .StatusOK ))
90+
91+ var returnedResp types.Container
92+ err := json .Unmarshal (rr .Body .Bytes (), & returnedResp )
93+ Expect (err ).Should (BeNil ())
94+ Expect (returnedResp .SizeRw ).ShouldNot (BeNil ())
95+ Expect (* returnedResp .SizeRw ).Should (Equal (int64 (1000 )))
96+ Expect (returnedResp .SizeRootFs ).ShouldNot (BeNil ())
97+ Expect (* returnedResp .SizeRootFs ).Should (Equal (int64 (5000 )))
98+ })
6699 It ("should return 404 status code if container was not found" , func () {
67- service .EXPECT ().Inspect (gomock .Any (), cid ).Return (nil , errdefs .NewNotFound (fmt .Errorf ("no such container" )))
100+ service .EXPECT ().Inspect (gomock .Any (), cid , false ).Return (nil , errdefs .NewNotFound (fmt .Errorf ("no such container" )))
68101 logger .EXPECT ().Debugf (gomock .Any (), gomock .Any ())
69102
70103 // handler should return error message with 404 status code
@@ -73,7 +106,7 @@ var _ = Describe("Container Inspect API", func() {
73106 Expect (rr ).Should (HaveHTTPStatus (http .StatusNotFound ))
74107 })
75108 It ("should return 500 status code if service returns an error message" , func () {
76- service .EXPECT ().Inspect (gomock .Any (), cid ).Return (nil , fmt .Errorf ("error" ))
109+ service .EXPECT ().Inspect (gomock .Any (), cid , false ).Return (nil , fmt .Errorf ("error" ))
77110 logger .EXPECT ().Debugf (gomock .Any (), gomock .Any ())
78111
79112 // handler should return error message
0 commit comments