2019-04-20 16:44:41 +02:00
|
|
|
package actions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-07-21 22:22:30 +02:00
|
|
|
"github.com/containrrr/watchtower/internal/actions"
|
2019-04-20 16:44:41 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-21 20:14:28 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/container"
|
|
|
|
"github.com/containrrr/watchtower/pkg/container/mocks"
|
2019-04-20 16:44:41 +02:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
t "github.com/containrrr/watchtower/pkg/types"
|
2019-04-20 16:44:41 +02:00
|
|
|
cli "github.com/docker/docker/client"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestActions(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "Actions Suite")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("the actions package", func() {
|
|
|
|
var dockerClient cli.CommonAPIClient
|
|
|
|
var client mockClient
|
|
|
|
BeforeSuite(func() {
|
|
|
|
server := mocks.NewMockAPIServer()
|
|
|
|
dockerClient, _ = cli.NewClientWithOpts(
|
|
|
|
cli.WithHost(server.URL),
|
|
|
|
cli.WithHTTPClient(server.Client()))
|
|
|
|
})
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = mockClient{
|
2019-07-21 18:00:56 +02:00
|
|
|
api: dockerClient,
|
|
|
|
pullImages: false,
|
|
|
|
removeVolumes: false,
|
|
|
|
TestData: &TestData{},
|
2019-04-20 16:44:41 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("the check prerequisites method", func() {
|
|
|
|
When("given an empty array", func() {
|
|
|
|
It("should not do anything", func() {
|
|
|
|
client.TestData.Containers = []container.Container{}
|
2019-07-21 22:22:30 +02:00
|
|
|
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
2019-04-20 16:44:41 +02:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("given an array of one", func() {
|
|
|
|
It("should not do anything", func() {
|
|
|
|
client.TestData.Containers = []container.Container{
|
|
|
|
createMockContainer(
|
|
|
|
"test-container",
|
|
|
|
"test-container",
|
|
|
|
"watchtower",
|
|
|
|
time.Now()),
|
|
|
|
}
|
2019-07-21 22:22:30 +02:00
|
|
|
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
2019-04-20 16:44:41 +02:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("given multiple containers", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = mockClient{
|
2019-07-21 18:00:56 +02:00
|
|
|
api: dockerClient,
|
|
|
|
pullImages: false,
|
|
|
|
removeVolumes: false,
|
2019-04-20 16:44:41 +02:00
|
|
|
TestData: &TestData{
|
|
|
|
NameOfContainerToKeep: "test-container-02",
|
|
|
|
Containers: []container.Container{
|
|
|
|
createMockContainer(
|
|
|
|
"test-container-01",
|
|
|
|
"test-container-01",
|
|
|
|
"watchtower",
|
|
|
|
time.Now().AddDate(0, 0, -1)),
|
|
|
|
createMockContainer(
|
|
|
|
"test-container-02",
|
|
|
|
"test-container-02",
|
|
|
|
"watchtower",
|
|
|
|
time.Now()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("should stop all but the latest one", func() {
|
2019-07-21 22:22:30 +02:00
|
|
|
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
2019-04-20 16:44:41 +02:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("deciding whether to cleanup images", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = mockClient{
|
2019-07-21 18:00:56 +02:00
|
|
|
api: dockerClient,
|
|
|
|
pullImages: false,
|
|
|
|
removeVolumes: false,
|
2019-04-20 16:44:41 +02:00
|
|
|
TestData: &TestData{
|
|
|
|
Containers: []container.Container{
|
|
|
|
createMockContainer(
|
|
|
|
"test-container-01",
|
|
|
|
"test-container-01",
|
|
|
|
"watchtower",
|
|
|
|
time.Now().AddDate(0, 0, -1)),
|
|
|
|
createMockContainer(
|
|
|
|
"test-container-02",
|
|
|
|
"test-container-02",
|
|
|
|
"watchtower",
|
|
|
|
time.Now()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("should try to delete the image if the cleanup flag is true", func() {
|
2019-07-21 22:22:30 +02:00
|
|
|
err := actions.CheckForMultipleWatchtowerInstances(client, true)
|
2019-04-20 16:44:41 +02:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(client.TestData.TriedToRemoveImage).To(BeTrue())
|
|
|
|
})
|
|
|
|
It("should not try to delete the image if the cleanup flag is false", func() {
|
2019-07-21 22:22:30 +02:00
|
|
|
err := actions.CheckForMultipleWatchtowerInstances(client, false)
|
2019-04-20 16:44:41 +02:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
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 {
|
2019-07-21 18:00:56 +02:00
|
|
|
TestData *TestData
|
|
|
|
api cli.CommonAPIClient
|
|
|
|
pullImages bool
|
|
|
|
removeVolumes bool
|
2019-04-20 16:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestData struct {
|
|
|
|
TriedToRemoveImage bool
|
|
|
|
NameOfContainerToKeep string
|
|
|
|
Containers []container.Container
|
|
|
|
}
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
func (client mockClient) ListContainers(f t.Filter) ([]container.Container, error) {
|
2019-04-20 16:44:41 +02:00
|
|
|
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
|
|
|
|
}
|
2019-07-27 01:37:16 +02:00
|
|
|
func (client mockClient) StartContainer(c container.Container) (string, error) {
|
2019-04-20 16:44:41 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:37:16 +02:00
|
|
|
func (client mockClient) GetContainer(containerID string) (container.Container, error) {
|
|
|
|
return container.Container{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client mockClient) ExecuteCommand(containerID string, command string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
func (client mockClient) IsContainerStale(c container.Container) (bool, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|