mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01:00
#387 fix: switch to image id map and add additional tests
This commit is contained in:
parent
4e000fa89c
commit
7b8b8e8ad9
9 changed files with 244 additions and 102 deletions
|
|
@ -1,18 +1,16 @@
|
|||
package actions_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/containrrr/watchtower/internal/actions"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containrrr/watchtower/pkg/container"
|
||||
"github.com/containrrr/watchtower/pkg/container/mocks"
|
||||
"github.com/docker/docker/api/types"
|
||||
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
cli "github.com/docker/docker/client"
|
||||
|
||||
. "github.com/containrrr/watchtower/internal/actions/mocks"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
|
@ -24,7 +22,7 @@ func TestActions(t *testing.T) {
|
|||
|
||||
var _ = Describe("the actions package", func() {
|
||||
var dockerClient cli.CommonAPIClient
|
||||
var client mockClient
|
||||
var client MockClient
|
||||
BeforeSuite(func() {
|
||||
server := mocks.NewMockAPIServer()
|
||||
dockerClient, _ = cli.NewClientWithOpts(
|
||||
|
|
@ -32,12 +30,15 @@ var _ = Describe("the actions package", func() {
|
|||
cli.WithHTTPClient(server.Client()))
|
||||
})
|
||||
BeforeEach(func() {
|
||||
client = mockClient{
|
||||
api: dockerClient,
|
||||
pullImages: false,
|
||||
removeVolumes: false,
|
||||
TestData: &TestData{},
|
||||
}
|
||||
pullImages := false
|
||||
removeVolumes := false
|
||||
|
||||
client = CreateMockClient(
|
||||
&TestData {},
|
||||
dockerClient,
|
||||
pullImages,
|
||||
removeVolumes,
|
||||
)
|
||||
})
|
||||
|
||||
Describe("the check prerequisites method", func() {
|
||||
|
|
@ -51,7 +52,7 @@ var _ = Describe("the actions package", func() {
|
|||
When("given an array of one", func() {
|
||||
It("should not do anything", func() {
|
||||
client.TestData.Containers = []container.Container{
|
||||
createMockContainer(
|
||||
CreateMockContainer(
|
||||
"test-container",
|
||||
"test-container",
|
||||
"watchtower",
|
||||
|
|
@ -63,27 +64,30 @@ var _ = Describe("the actions package", func() {
|
|||
})
|
||||
When("given multiple containers", func() {
|
||||
BeforeEach(func() {
|
||||
client = mockClient{
|
||||
api: dockerClient,
|
||||
pullImages: false,
|
||||
removeVolumes: false,
|
||||
TestData: &TestData{
|
||||
pullImages := false
|
||||
removeVolumes := false
|
||||
client = CreateMockClient(
|
||||
&TestData{
|
||||
NameOfContainerToKeep: "test-container-02",
|
||||
Containers: []container.Container{
|
||||
createMockContainer(
|
||||
CreateMockContainer(
|
||||
"test-container-01",
|
||||
"test-container-01",
|
||||
"watchtower",
|
||||
time.Now().AddDate(0, 0, -1)),
|
||||
createMockContainer(
|
||||
CreateMockContainer(
|
||||
"test-container-02",
|
||||
"test-container-02",
|
||||
"watchtower",
|
||||
time.Now()),
|
||||
},
|
||||
},
|
||||
}
|
||||
dockerClient,
|
||||
pullImages,
|
||||
removeVolumes,
|
||||
)
|
||||
})
|
||||
|
||||
It("should stop all but the latest one", func() {
|
||||
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
|
@ -91,96 +95,40 @@ var _ = Describe("the actions package", func() {
|
|||
})
|
||||
When("deciding whether to cleanup images", func() {
|
||||
BeforeEach(func() {
|
||||
client = mockClient{
|
||||
api: dockerClient,
|
||||
pullImages: false,
|
||||
removeVolumes: false,
|
||||
TestData: &TestData{
|
||||
pullImages := false
|
||||
removeVolumes := false
|
||||
|
||||
client = CreateMockClient(
|
||||
&TestData{
|
||||
Containers: []container.Container{
|
||||
createMockContainer(
|
||||
CreateMockContainer(
|
||||
"test-container-01",
|
||||
"test-container-01",
|
||||
"watchtower",
|
||||
time.Now().AddDate(0, 0, -1)),
|
||||
createMockContainer(
|
||||
CreateMockContainer(
|
||||
"test-container-02",
|
||||
"test-container-02",
|
||||
"watchtower",
|
||||
time.Now()),
|
||||
},
|
||||
},
|
||||
}
|
||||
dockerClient,
|
||||
pullImages,
|
||||
removeVolumes,
|
||||
)
|
||||
})
|
||||
It("should try to delete the image if the cleanup flag is true", func() {
|
||||
err := actions.CheckForMultipleWatchtowerInstances(client, true)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImage).To(BeTrue())
|
||||
Expect(client.TestData.TriedToRemoveImage()).To(BeTrue())
|
||||
})
|
||||
It("should not try to delete the image if the cleanup flag is false", func() {
|
||||
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImage).To(BeFalse())
|
||||
Expect(client.TestData.TriedToRemoveImage()).To(BeFalse())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
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(),
|
||||
},
|
||||
}
|
||||
return *container.NewContainer(&content, nil)
|
||||
}
|
||||
|
||||
type mockClient struct {
|
||||
TestData *TestData
|
||||
api cli.CommonAPIClient
|
||||
pullImages bool
|
||||
removeVolumes bool
|
||||
}
|
||||
|
||||
type TestData struct {
|
||||
TriedToRemoveImage bool
|
||||
NameOfContainerToKeep string
|
||||
Containers []container.Container
|
||||
}
|
||||
|
||||
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) {
|
||||
panic("Not implemented")
|
||||
}
|
||||
|
||||
func (client mockClient) RenameContainer(c container.Container, s string) error {
|
||||
panic("Not implemented")
|
||||
}
|
||||
|
||||
func (client mockClient) RemoveImage(c container.Container) error {
|
||||
client.TestData.TriedToRemoveImage = true
|
||||
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) {
|
||||
panic("Not implemented")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue