mirror of
https://github.com/containrrr/watchtower.git
synced 2026-03-16 01:06:31 +01:00
add additional test and further rework client mocks
This commit is contained in:
parent
abbdaa834c
commit
e97671f293
6 changed files with 384 additions and 67 deletions
|
|
@ -3,9 +3,10 @@ package mocks
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"github.com/onsi/ginkgo"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
|
|
@ -19,10 +20,9 @@ import (
|
|||
|
||||
func getMockJSONFile(relPath string) ([]byte, error) {
|
||||
absPath, _ := filepath.Abs(relPath)
|
||||
buf, err := ioutil.ReadFile(absPath)
|
||||
buf, err := os.ReadFile(absPath)
|
||||
if err != nil {
|
||||
// logrus.WithError(err).WithField("file", absPath).Error(err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("mock JSON file %q not found: %e", absPath, err)
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
|
@ -43,19 +43,22 @@ func respondWithJSONFile(relPath string, statusCode int, optionalHeader ...http.
|
|||
}
|
||||
|
||||
// GetContainerHandlers returns the handlers serving lookups for the supplied container mock files
|
||||
func GetContainerHandlers(containerFiles ...string) []http.HandlerFunc {
|
||||
handlers := make([]http.HandlerFunc, 0, len(containerFiles)*2)
|
||||
for _, file := range containerFiles {
|
||||
handlers = append(handlers, getContainerFileHandler(file))
|
||||
func GetContainerHandlers(containerRefs ...*ContainerRef) []http.HandlerFunc {
|
||||
handlers := make([]http.HandlerFunc, 0, len(containerRefs)*3)
|
||||
for _, containerRef := range containerRefs {
|
||||
handlers = append(handlers, getContainerFileHandler(containerRef))
|
||||
|
||||
if file == "net_consumer" {
|
||||
// Also append the net_producer container, since it's used to reconfigure networking
|
||||
handlers = append(handlers, getContainerFileHandler("net_producer"))
|
||||
// Also append any containers that the container references, if any
|
||||
for _, ref := range containerRef.references {
|
||||
handlers = append(handlers, getContainerFileHandler(ref))
|
||||
}
|
||||
|
||||
// Also append the image request since that will be called for every container
|
||||
handlers = append(handlers, getImageFileHandler(file))
|
||||
handlers = append(handlers, getImageHandler(containerRef.image.id,
|
||||
RespondWithJSONFile(containerRef.image.getFileName(), http.StatusOK),
|
||||
))
|
||||
}
|
||||
|
||||
return handlers
|
||||
}
|
||||
|
||||
|
|
@ -67,32 +70,90 @@ func createFilterArgs(statuses []string) filters.Args {
|
|||
return args
|
||||
}
|
||||
|
||||
const NetConsumerID = "1f6b79d2aff23244382026c76f4995851322bed5f9c50631620162f6f9aafbd6"
|
||||
const NetProducerID = "25e75393800b5c450a6841212a3b92ed28fa35414a586dec9f2c8a520d4910c2"
|
||||
const NetProducerContainerName = "/wt-contnet-producer-1"
|
||||
|
||||
var containerFileIds = map[string]string{
|
||||
"stopped": "ae8964ba86c7cd7522cf84e09781343d88e0e3543281c747d88b27e246578b65",
|
||||
"watchtower": "3d88e0e3543281c747d88b27e246578b65ae8964ba86c7cd7522cf84e0978134",
|
||||
"running": "b978af0b858aa8855cce46b628817d4ed58e58f2c4f66c9b9c5449134ed4c008",
|
||||
"restarting": "ae8964ba86c7cd7522cf84e09781343d88e0e3543281c747d88b27e246578b67",
|
||||
"net_consumer": NetConsumerID,
|
||||
"net_producer": NetProducerID,
|
||||
var defaultImage = imageRef{
|
||||
// watchtower
|
||||
id: t.ImageID("sha256:4dbc5f9c07028a985e14d1393e849ea07f68804c4293050d5a641b138db72daa"),
|
||||
file: "default",
|
||||
}
|
||||
|
||||
var imageIds = map[string]t.ImageID{
|
||||
"default": t.ImageID("sha256:4dbc5f9c07028a985e14d1393e849ea07f68804c4293050d5a641b138db72daa"), // watchtower
|
||||
"running": t.ImageID("sha256:19d07168491a3f9e2798a9bed96544e34d57ddc4757a4ac5bb199dea896c87fd"), // portainer
|
||||
"net_consumer": t.ImageID("sha256:904b8cb13b932e23230836850610fa45dce9eb0650d5618c2b1487c2a4f577b8"), // nginx
|
||||
"net_producer": t.ImageID("sha256:c22b543d33bfdcb9992cbef23961677133cdf09da71d782468ae2517138bad51"), // gluetun
|
||||
var Watchtower = ContainerRef{
|
||||
name: "watchtower",
|
||||
id: "3d88e0e3543281c747d88b27e246578b65ae8964ba86c7cd7522cf84e0978134",
|
||||
image: &defaultImage,
|
||||
}
|
||||
var Stopped = ContainerRef{
|
||||
name: "stopped",
|
||||
id: "ae8964ba86c7cd7522cf84e09781343d88e0e3543281c747d88b27e246578b65",
|
||||
image: &defaultImage,
|
||||
}
|
||||
var Running = ContainerRef{
|
||||
name: "running",
|
||||
id: "b978af0b858aa8855cce46b628817d4ed58e58f2c4f66c9b9c5449134ed4c008",
|
||||
image: &imageRef{
|
||||
// portainer
|
||||
id: t.ImageID("sha256:19d07168491a3f9e2798a9bed96544e34d57ddc4757a4ac5bb199dea896c87fd"),
|
||||
file: "running",
|
||||
},
|
||||
}
|
||||
var Restarting = ContainerRef{
|
||||
name: "restarting",
|
||||
id: "ae8964ba86c7cd7522cf84e09781343d88e0e3543281c747d88b27e246578b67",
|
||||
image: &defaultImage,
|
||||
}
|
||||
|
||||
func getContainerFileHandler(file string) http.HandlerFunc {
|
||||
id, ok := containerFileIds[file]
|
||||
failTestUnless(ok)
|
||||
var netSupplierOK = ContainerRef{
|
||||
id: "25e75393800b5c450a6841212a3b92ed28fa35414a586dec9f2c8a520d4910c2",
|
||||
name: "net_supplier",
|
||||
image: &imageRef{
|
||||
// gluetun
|
||||
id: t.ImageID("sha256:c22b543d33bfdcb9992cbef23961677133cdf09da71d782468ae2517138bad51"),
|
||||
file: "net_producer",
|
||||
},
|
||||
}
|
||||
var netSupplierNotFound = ContainerRef{
|
||||
id: NetSupplierNotFoundID,
|
||||
name: netSupplierOK.name,
|
||||
isMissing: true,
|
||||
}
|
||||
|
||||
// NetConsumerOK is used for testing `container` networking mode
|
||||
// returns a container that consumes an existing supplier container
|
||||
var NetConsumerOK = ContainerRef{
|
||||
id: "1f6b79d2aff23244382026c76f4995851322bed5f9c50631620162f6f9aafbd6",
|
||||
name: "net_consumer",
|
||||
image: &imageRef{
|
||||
id: t.ImageID("sha256:904b8cb13b932e23230836850610fa45dce9eb0650d5618c2b1487c2a4f577b8"), // nginx
|
||||
file: "net_consumer",
|
||||
},
|
||||
references: []*ContainerRef{&netSupplierOK},
|
||||
}
|
||||
|
||||
// NetConsumerInvalidSupplier is used for testing `container` networking mode
|
||||
// returns a container that references a supplying container that does not exist
|
||||
var NetConsumerInvalidSupplier = ContainerRef{
|
||||
id: NetConsumerOK.id,
|
||||
name: "net_consumer-missing_supplier",
|
||||
image: NetConsumerOK.image,
|
||||
references: []*ContainerRef{&netSupplierNotFound},
|
||||
}
|
||||
|
||||
const NetSupplierNotFoundID = "badc1dbadc1dbadc1dbadc1dbadc1dbadc1dbadc1dbadc1dbadc1dbadc1dbadc"
|
||||
const NetSupplierContainerName = "/wt-contnet-producer-1"
|
||||
|
||||
func getContainerFileHandler(cr *ContainerRef) http.HandlerFunc {
|
||||
|
||||
if cr.isMissing {
|
||||
return containerNotFoundResponse(string(cr.id))
|
||||
}
|
||||
|
||||
containerFile, err := cr.getContainerFile()
|
||||
if err != nil {
|
||||
ginkgo.Fail(fmt.Sprintf("Failed to get container mock file: %v", err))
|
||||
}
|
||||
|
||||
return getContainerHandler(
|
||||
id,
|
||||
RespondWithJSONFile(fmt.Sprintf("./mocks/data/container_%v.json", file), http.StatusOK),
|
||||
string(cr.id),
|
||||
RespondWithJSONFile(containerFile, http.StatusOK),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -155,20 +216,6 @@ func getImageHandler(imageId t.ImageID, responseHandler http.HandlerFunc) http.H
|
|||
)
|
||||
}
|
||||
|
||||
func getImageFileHandler(key string) http.HandlerFunc {
|
||||
if _, found := imageIds[key]; !found {
|
||||
// The default image (watchtower) is used for most of the containers
|
||||
key = `default`
|
||||
}
|
||||
return getImageHandler(imageIds[key],
|
||||
RespondWithJSONFile(fmt.Sprintf("./mocks/data/image_%v.json", key), http.StatusOK),
|
||||
)
|
||||
}
|
||||
|
||||
func failTestUnless(ok bool) {
|
||||
O.ExpectWithOffset(2, ok).To(O.BeTrue(), "test setup failed")
|
||||
}
|
||||
|
||||
// KillContainerHandler mocks the POST containers/{id}/kill endpoint
|
||||
func KillContainerHandler(containerID string, found FoundStatus) http.HandlerFunc {
|
||||
responseHandler := noContentStatusResponse
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue