mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01:00
test: container client tests refactor (#1117)
This commit is contained in:
parent
2c8695683d
commit
1b405d4c77
1 changed files with 13 additions and 10 deletions
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
. "github.com/onsi/gomega"
|
O "github.com/onsi/gomega"
|
||||||
"github.com/onsi/gomega/ghttp"
|
"github.com/onsi/gomega/ghttp"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -23,9 +23,10 @@ func getMockJSONFile(relPath string) ([]byte, error) {
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RespondWithJSONFile handles a request by returning the contents of the supplied file
|
||||||
func RespondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.Header) http.HandlerFunc {
|
func RespondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.Header) http.HandlerFunc {
|
||||||
handler, err := respondWithJSONFile(relPath, statusCode, optionalHeader...)
|
handler, err := respondWithJSONFile(relPath, statusCode, optionalHeader...)
|
||||||
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
|
O.ExpectWithOffset(1, err).ShouldNot(O.HaveOccurred())
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,6 +38,7 @@ func respondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.
|
||||||
return ghttp.RespondWith(statusCode, buf, optionalHeader...), nil
|
return ghttp.RespondWith(statusCode, buf, optionalHeader...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetContainerHandlers returns the handlers serving lookups for the supplied container mock files
|
||||||
func GetContainerHandlers(containerFiles ...string) []http.HandlerFunc {
|
func GetContainerHandlers(containerFiles ...string) []http.HandlerFunc {
|
||||||
handlers := make([]http.HandlerFunc, 0, len(containerFiles)*2)
|
handlers := make([]http.HandlerFunc, 0, len(containerFiles)*2)
|
||||||
for _, file := range containerFiles {
|
for _, file := range containerFiles {
|
||||||
|
|
@ -77,31 +79,32 @@ func getContainerHandler(file string) http.HandlerFunc {
|
||||||
id, ok := containerFileIds[file]
|
id, ok := containerFileIds[file]
|
||||||
failTestUnless(ok)
|
failTestUnless(ok)
|
||||||
return ghttp.CombineHandlers(
|
return ghttp.CombineHandlers(
|
||||||
ghttp.VerifyRequest("GET", HaveSuffix("/containers/%v/json", id)),
|
ghttp.VerifyRequest("GET", O.HaveSuffix("/containers/%v/json", id)),
|
||||||
RespondWithJSONFile(fmt.Sprintf("./mocks/data/container_%v.json", file), http.StatusOK),
|
RespondWithJSONFile(fmt.Sprintf("./mocks/data/container_%v.json", file), http.StatusOK),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListContainersHandler mocks the GET containers/json endpoint, filtering the returned containers based on statuses
|
||||||
func ListContainersHandler(statuses ...string) http.HandlerFunc {
|
func ListContainersHandler(statuses ...string) http.HandlerFunc {
|
||||||
filterArgs := createFilterArgs(statuses)
|
filterArgs := createFilterArgs(statuses)
|
||||||
bytes, err := filterArgs.MarshalJSON()
|
bytes, err := filterArgs.MarshalJSON()
|
||||||
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
|
O.ExpectWithOffset(1, err).ShouldNot(O.HaveOccurred())
|
||||||
query := url.Values{
|
query := url.Values{
|
||||||
"limit": []string{"0"},
|
"limit": []string{"0"},
|
||||||
"filters": []string{string(bytes)},
|
"filters": []string{string(bytes)},
|
||||||
}
|
}
|
||||||
return ghttp.CombineHandlers(
|
return ghttp.CombineHandlers(
|
||||||
ghttp.VerifyRequest("GET", HaveSuffix("containers/json"), query.Encode()),
|
ghttp.VerifyRequest("GET", O.HaveSuffix("containers/json"), query.Encode()),
|
||||||
respondWithFilteredContainers(filterArgs),
|
respondWithFilteredContainers(filterArgs),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
|
func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
|
||||||
containersJson, err := getMockJSONFile("./mocks/data/containers.json")
|
containersJSON, err := getMockJSONFile("./mocks/data/containers.json")
|
||||||
ExpectWithOffset(2, err).ShouldNot(HaveOccurred())
|
O.ExpectWithOffset(2, err).ShouldNot(O.HaveOccurred())
|
||||||
var filteredContainers []types.Container
|
var filteredContainers []types.Container
|
||||||
var containers []types.Container
|
var containers []types.Container
|
||||||
ExpectWithOffset(2, json.Unmarshal(containersJson, &containers)).To(Succeed())
|
O.ExpectWithOffset(2, json.Unmarshal(containersJSON, &containers)).To(O.Succeed())
|
||||||
for _, v := range containers {
|
for _, v := range containers {
|
||||||
for _, key := range filters.Get("status") {
|
for _, key := range filters.Get("status") {
|
||||||
if v.State == key {
|
if v.State == key {
|
||||||
|
|
@ -115,11 +118,11 @@ func respondWithFilteredContainers(filters filters.Args) http.HandlerFunc {
|
||||||
|
|
||||||
func getImageHandler(index int) http.HandlerFunc {
|
func getImageHandler(index int) http.HandlerFunc {
|
||||||
return ghttp.CombineHandlers(
|
return ghttp.CombineHandlers(
|
||||||
ghttp.VerifyRequest("GET", HaveSuffix("/images/%v/json", imageIds[index])),
|
ghttp.VerifyRequest("GET", O.HaveSuffix("/images/%v/json", imageIds[index])),
|
||||||
RespondWithJSONFile(fmt.Sprintf("./mocks/data/image%02d.json", index+1), http.StatusOK),
|
RespondWithJSONFile(fmt.Sprintf("./mocks/data/image%02d.json", index+1), http.StatusOK),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func failTestUnless(ok bool) {
|
func failTestUnless(ok bool) {
|
||||||
ExpectWithOffset(2, ok).To(BeTrue(), "test setup failed")
|
O.ExpectWithOffset(2, ok).To(O.BeTrue(), "test setup failed")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue