@@ -3,14 +3,15 @@ package mocks
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "github.com/docker/docker/api/types"
7
- "github.com/docker/docker/api/types/filters"
8
- O "github.com/onsi/gomega"
9
- "github.com/onsi/gomega/ghttp"
10
6
"io/ioutil"
11
7
"net/http"
12
8
"net/url"
13
9
"path/filepath"
10
+
11
+ "github.com/docker/docker/api/types"
12
+ "github.com/docker/docker/api/types/filters"
13
+ O "github.com/onsi/gomega"
14
+ "github.com/onsi/gomega/ghttp"
14
15
)
15
16
16
17
func getMockJSONFile (relPath string ) ([]byte , error ) {
@@ -42,14 +43,14 @@ func respondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.
42
43
func GetContainerHandlers (containerFiles ... string ) []http.HandlerFunc {
43
44
handlers := make ([]http.HandlerFunc , 0 , len (containerFiles )* 2 )
44
45
for _ , file := range containerFiles {
45
- handlers = append (handlers , getContainerHandler (file ))
46
+ handlers = append (handlers , getContainerFileHandler (file ))
46
47
47
48
// Also append the image request since that will be called for every container
48
49
if file == "running" {
49
50
// The "running" container is the only one using image02
50
- handlers = append (handlers , getImageHandler (1 ))
51
+ handlers = append (handlers , getImageFileHandler (1 ))
51
52
} else {
52
- handlers = append (handlers , getImageHandler (0 ))
53
+ handlers = append (handlers , getImageFileHandler (0 ))
53
54
}
54
55
}
55
56
return handlers
@@ -75,15 +76,36 @@ var imageIds = []string{
75
76
"sha256:19d07168491a3f9e2798a9bed96544e34d57ddc4757a4ac5bb199dea896c87fd" ,
76
77
}
77
78
78
- func getContainerHandler (file string ) http.HandlerFunc {
79
+ func getContainerFileHandler (file string ) http.HandlerFunc {
79
80
id , ok := containerFileIds [file ]
80
81
failTestUnless (ok )
81
- return ghttp . CombineHandlers (
82
- ghttp . VerifyRequest ( "GET" , O . HaveSuffix ( "/containers/%v/json" , id )) ,
82
+ return getContainerHandler (
83
+ id ,
83
84
RespondWithJSONFile (fmt .Sprintf ("./mocks/data/container_%v.json" , file ), http .StatusOK ),
84
85
)
85
86
}
86
87
88
+ func getContainerHandler (containerId string , responseHandler http.HandlerFunc ) http.HandlerFunc {
89
+ return ghttp .CombineHandlers (
90
+ ghttp .VerifyRequest ("GET" , O .HaveSuffix ("/containers/%v/json" , containerId )),
91
+ responseHandler ,
92
+ )
93
+ }
94
+
95
+ // GetContainerHandler mocks the GET containers/{id}/json endpoint
96
+ func GetContainerHandler (containerID string , containerInfo * types.ContainerJSON ) http.HandlerFunc {
97
+ responseHandler := containerNotFoundResponse (containerID )
98
+ if containerInfo != nil {
99
+ responseHandler = ghttp .RespondWithJSONEncoded (http .StatusOK , containerInfo )
100
+ }
101
+ return getContainerHandler (containerID , responseHandler )
102
+ }
103
+
104
+ // GetImageHandler mocks the GET images/{id}/json endpoint
105
+ func GetImageHandler (imageInfo * types.ImageInspect ) http.HandlerFunc {
106
+ return getImageHandler (imageInfo .ID , ghttp .RespondWithJSONEncoded (http .StatusOK , imageInfo ))
107
+ }
108
+
87
109
// ListContainersHandler mocks the GET containers/json endpoint, filtering the returned containers based on statuses
88
110
func ListContainersHandler (statuses ... string ) http.HandlerFunc {
89
111
filterArgs := createFilterArgs (statuses )
@@ -116,13 +138,56 @@ func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
116
138
return ghttp .RespondWithJSONEncoded (http .StatusOK , filteredContainers )
117
139
}
118
140
119
- func getImageHandler (index int ) http.HandlerFunc {
141
+ func getImageHandler (imageId string , responseHandler http. HandlerFunc ) http.HandlerFunc {
120
142
return ghttp .CombineHandlers (
121
- ghttp .VerifyRequest ("GET" , O .HaveSuffix ("/images/%v/json" , imageIds [index ])),
143
+ ghttp .VerifyRequest ("GET" , O .HaveSuffix ("/images/%s/json" , imageId )),
144
+ responseHandler ,
145
+ )
146
+ }
147
+
148
+ func getImageFileHandler (index int ) http.HandlerFunc {
149
+ return getImageHandler (imageIds [index ],
122
150
RespondWithJSONFile (fmt .Sprintf ("./mocks/data/image%02d.json" , index + 1 ), http .StatusOK ),
123
151
)
124
152
}
125
153
126
154
func failTestUnless (ok bool ) {
127
155
O .ExpectWithOffset (2 , ok ).To (O .BeTrue (), "test setup failed" )
128
156
}
157
+
158
+ // KillContainerHandler mocks the POST containers/{id}/kill endpoint
159
+ func KillContainerHandler (containerID string , found FoundStatus ) http.HandlerFunc {
160
+ responseHandler := noContentStatusResponse
161
+ if ! found {
162
+ responseHandler = containerNotFoundResponse (containerID )
163
+ }
164
+ return ghttp .CombineHandlers (
165
+ ghttp .VerifyRequest ("POST" , O .HaveSuffix ("containers/%s/kill" , containerID )),
166
+ responseHandler ,
167
+ )
168
+ }
169
+
170
+ // RemoveContainerHandler mocks the DELETE containers/{id} endpoint
171
+ func RemoveContainerHandler (containerID string , found FoundStatus ) http.HandlerFunc {
172
+ responseHandler := noContentStatusResponse
173
+ if ! found {
174
+ responseHandler = containerNotFoundResponse (containerID )
175
+ }
176
+ return ghttp .CombineHandlers (
177
+ ghttp .VerifyRequest ("DELETE" , O .HaveSuffix ("containers/%s" , containerID )),
178
+ responseHandler ,
179
+ )
180
+ }
181
+
182
+ func containerNotFoundResponse (containerID string ) http.HandlerFunc {
183
+ return ghttp .RespondWithJSONEncoded (http .StatusNotFound , struct { message string }{message : "No such container: " + containerID })
184
+ }
185
+
186
+ var noContentStatusResponse = ghttp .RespondWith (http .StatusNoContent , nil )
187
+
188
+ type FoundStatus bool
189
+
190
+ const (
191
+ Found FoundStatus = true
192
+ Missing FoundStatus = false
193
+ )
0 commit comments