Skip to content

Commit 1b405d4

Browse files
authored
test: container client tests refactor (#1117)
1 parent 2c86956 commit 1b405d4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

pkg/container/mocks/ApiServer.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"github.com/docker/docker/api/types"
77
"github.com/docker/docker/api/types/filters"
8-
. "github.com/onsi/gomega"
8+
O "github.com/onsi/gomega"
99
"github.com/onsi/gomega/ghttp"
1010
"io/ioutil"
1111
"net/http"
@@ -23,9 +23,10 @@ func getMockJSONFile(relPath string) ([]byte, error) {
2323
return buf, nil
2424
}
2525

26+
// RespondWithJSONFile handles a request by returning the contents of the supplied file
2627
func RespondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.Header) http.HandlerFunc {
2728
handler, err := respondWithJSONFile(relPath, statusCode, optionalHeader...)
28-
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
29+
O.ExpectWithOffset(1, err).ShouldNot(O.HaveOccurred())
2930
return handler
3031
}
3132

@@ -37,6 +38,7 @@ func respondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.
3738
return ghttp.RespondWith(statusCode, buf, optionalHeader...), nil
3839
}
3940

41+
// GetContainerHandlers returns the handlers serving lookups for the supplied container mock files
4042
func GetContainerHandlers(containerFiles ...string) []http.HandlerFunc {
4143
handlers := make([]http.HandlerFunc, 0, len(containerFiles)*2)
4244
for _, file := range containerFiles {
@@ -77,31 +79,32 @@ func getContainerHandler(file string) http.HandlerFunc {
7779
id, ok := containerFileIds[file]
7880
failTestUnless(ok)
7981
return ghttp.CombineHandlers(
80-
ghttp.VerifyRequest("GET", HaveSuffix("/containers/%v/json", id)),
82+
ghttp.VerifyRequest("GET", O.HaveSuffix("/containers/%v/json", id)),
8183
RespondWithJSONFile(fmt.Sprintf("./mocks/data/container_%v.json", file), http.StatusOK),
8284
)
8385
}
8486

87+
// ListContainersHandler mocks the GET containers/json endpoint, filtering the returned containers based on statuses
8588
func ListContainersHandler(statuses ...string) http.HandlerFunc {
8689
filterArgs := createFilterArgs(statuses)
8790
bytes, err := filterArgs.MarshalJSON()
88-
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
91+
O.ExpectWithOffset(1, err).ShouldNot(O.HaveOccurred())
8992
query := url.Values{
9093
"limit": []string{"0"},
9194
"filters": []string{string(bytes)},
9295
}
9396
return ghttp.CombineHandlers(
94-
ghttp.VerifyRequest("GET", HaveSuffix("containers/json"), query.Encode()),
97+
ghttp.VerifyRequest("GET", O.HaveSuffix("containers/json"), query.Encode()),
9598
respondWithFilteredContainers(filterArgs),
9699
)
97100
}
98101

99102
func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
100-
containersJson, err := getMockJSONFile("./mocks/data/containers.json")
101-
ExpectWithOffset(2, err).ShouldNot(HaveOccurred())
103+
containersJSON, err := getMockJSONFile("./mocks/data/containers.json")
104+
O.ExpectWithOffset(2, err).ShouldNot(O.HaveOccurred())
102105
var filteredContainers []types.Container
103106
var containers []types.Container
104-
ExpectWithOffset(2, json.Unmarshal(containersJson, &containers)).To(Succeed())
107+
O.ExpectWithOffset(2, json.Unmarshal(containersJSON, &containers)).To(O.Succeed())
105108
for _, v := range containers {
106109
for _, key := range filters.Get("status") {
107110
if v.State == key {
@@ -115,11 +118,11 @@ func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
115118

116119
func getImageHandler(index int) http.HandlerFunc {
117120
return ghttp.CombineHandlers(
118-
ghttp.VerifyRequest("GET", HaveSuffix("/images/%v/json", imageIds[index])),
121+
ghttp.VerifyRequest("GET", O.HaveSuffix("/images/%v/json", imageIds[index])),
119122
RespondWithJSONFile(fmt.Sprintf("./mocks/data/image%02d.json", index+1), http.StatusOK),
120123
)
121124
}
122125

123126
func failTestUnless(ok bool) {
124-
ExpectWithOffset(2, ok).To(BeTrue(), "test setup failed")
127+
O.ExpectWithOffset(2, ok).To(O.BeTrue(), "test setup failed")
125128
}

0 commit comments

Comments
 (0)