#387 fix: switch to image id map and add additional tests
parent
4e000fa89c
commit
7b8b8e8ad9
@ -0,0 +1,72 @@
|
|||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/containrrr/watchtower/pkg/container"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
t "github.com/containrrr/watchtower/pkg/types"
|
||||||
|
cli "github.com/docker/docker/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MockClient struct {
|
||||||
|
TestData *TestData
|
||||||
|
api cli.CommonAPIClient
|
||||||
|
pullImages bool
|
||||||
|
removeVolumes bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestData struct {
|
||||||
|
TriedToRemoveImageCount int
|
||||||
|
NameOfContainerToKeep string
|
||||||
|
Containers []container.Container
|
||||||
|
}
|
||||||
|
|
||||||
|
func (testdata *TestData) TriedToRemoveImage() bool {
|
||||||
|
return testdata.TriedToRemoveImageCount > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateMockClient(data *TestData, api cli.CommonAPIClient, pullImages bool, removeVolumes bool) MockClient {
|
||||||
|
return MockClient {
|
||||||
|
data,
|
||||||
|
api,
|
||||||
|
pullImages,
|
||||||
|
removeVolumes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) ListContainers(f t.Filter) ([]container.Container, error) {
|
||||||
|
return client.TestData.Containers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) StopContainer(c container.Container, d time.Duration) error {
|
||||||
|
if c.Name() == client.TestData.NameOfContainerToKeep {
|
||||||
|
return errors.New("tried to stop the instance we want to keep")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (client MockClient) StartContainer(c container.Container) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) RenameContainer(c container.Container, s string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) RemoveImageByID(id string) error {
|
||||||
|
client.TestData.TriedToRemoveImageCount++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) GetContainer(containerID string) (container.Container, error) {
|
||||||
|
return container.Container{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) ExecuteCommand(containerID string, command string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client MockClient) IsContainerStale(c container.Container) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/containrrr/watchtower/pkg/container"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
container2 "github.com/docker/docker/api/types/container"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateMockContainer(id string, name string, image string, created time.Time) container.Container {
|
||||||
|
content := types.ContainerJSON{
|
||||||
|
ContainerJSONBase: &types.ContainerJSONBase{
|
||||||
|
ID: id,
|
||||||
|
Image: image,
|
||||||
|
Name: name,
|
||||||
|
Created: created.String(),
|
||||||
|
},
|
||||||
|
Config: &container2.Config{
|
||||||
|
Labels: make(map[string]string),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return *container.NewContainer(
|
||||||
|
&content,
|
||||||
|
&types.ImageInspect{
|
||||||
|
ID: image,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package actions_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/containrrr/watchtower/internal/actions"
|
||||||
|
"github.com/containrrr/watchtower/pkg/container"
|
||||||
|
"github.com/containrrr/watchtower/pkg/container/mocks"
|
||||||
|
cli "github.com/docker/docker/client"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
. "github.com/containrrr/watchtower/internal/actions/mocks"
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var _ = Describe("the update action", func() {
|
||||||
|
var dockerClient cli.CommonAPIClient
|
||||||
|
var client MockClient
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
server := mocks.NewMockAPIServer()
|
||||||
|
dockerClient, _ = cli.NewClientWithOpts(
|
||||||
|
cli.WithHost(server.URL),
|
||||||
|
cli.WithHTTPClient(server.Client()))
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
When("watchtower has been instructed to clean up", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
pullImages := false
|
||||||
|
removeVolumes := false
|
||||||
|
client = CreateMockClient(
|
||||||
|
&TestData{
|
||||||
|
NameOfContainerToKeep: "test-container-02",
|
||||||
|
Containers: []container.Container{
|
||||||
|
CreateMockContainer(
|
||||||
|
"test-container-01",
|
||||||
|
"test-container-01",
|
||||||
|
"fake-image:latest",
|
||||||
|
time.Now().AddDate(0, 0, -1)),
|
||||||
|
CreateMockContainer(
|
||||||
|
"test-container-02",
|
||||||
|
"test-container-02",
|
||||||
|
"fake-image:latest",
|
||||||
|
time.Now()),
|
||||||
|
CreateMockContainer(
|
||||||
|
"test-container-02",
|
||||||
|
"test-container-02",
|
||||||
|
"fake-image:latest",
|
||||||
|
time.Now()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dockerClient,
|
||||||
|
pullImages,
|
||||||
|
removeVolumes,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
When("there are multiple containers using the same image", func() {
|
||||||
|
It("should only try to remove the image once", func() {
|
||||||
|
|
||||||
|
err := actions.Update(client, actions.UpdateParams{ Cleanup: true })
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
When("there are multiple containers using different images", func() {
|
||||||
|
It("should try to remove each of them", func() {
|
||||||
|
client.TestData.Containers = append(
|
||||||
|
client.TestData.Containers,
|
||||||
|
CreateMockContainer(
|
||||||
|
"unique-test-container",
|
||||||
|
"unique-test-container",
|
||||||
|
"unique-fake-image:latest",
|
||||||
|
time.Now(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
err := actions.Update(client, actions.UpdateParams{ Cleanup: true })
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(2))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue