1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-15 09:14:13 +02:00
watchtower/actions/check_test.go
Brian DeHamer dd80aa4a0d Support for --cleanup flag
The --cleanup flag will cause watchtower to automatically remove the old
image after a container is restart with a new image.
2015-07-31 18:24:27 +00:00

115 lines
2.8 KiB
Go

package actions
import (
"errors"
"testing"
"time"
"github.com/CenturyLinkLabs/watchtower/container"
"github.com/CenturyLinkLabs/watchtower/container/mockclient"
"github.com/samalba/dockerclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCheckPrereqs_Success(t *testing.T) {
cc := &dockerclient.ContainerConfig{
Labels: map[string]string{"com.centurylinklabs.watchtower": "true"},
}
c1 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c1",
Config: cc,
Created: "2015-07-01T12:00:01.000000000Z",
},
nil,
)
c2 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c2",
Config: cc,
Created: "2015-07-01T12:00:00.000000000Z",
},
nil,
)
cs := []container.Container{c1, c2}
client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
client.On("StopContainer", c2, time.Duration(60)).Return(nil)
err := CheckPrereqs(client, false)
assert.NoError(t, err)
client.AssertExpectations(t)
}
func TestCheckPrereqs_WithCleanup(t *testing.T) {
cc := &dockerclient.ContainerConfig{
Labels: map[string]string{"com.centurylinklabs.watchtower": "true"},
}
c1 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c1",
Config: cc,
Created: "2015-07-01T12:00:01.000000000Z",
},
nil,
)
c2 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c2",
Config: cc,
Created: "2015-07-01T12:00:00.000000000Z",
},
nil,
)
cs := []container.Container{c1, c2}
client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
client.On("StopContainer", c2, time.Duration(60)).Return(nil)
client.On("RemoveImage", c2).Return(nil)
err := CheckPrereqs(client, true)
assert.NoError(t, err)
client.AssertExpectations(t)
}
func TestCheckPrereqs_OnlyOneContainer(t *testing.T) {
cc := &dockerclient.ContainerConfig{
Labels: map[string]string{"com.centurylinklabs.watchtower": "true"},
}
c1 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c1",
Config: cc,
Created: "2015-07-01T12:00:01.000000000Z",
},
nil,
)
cs := []container.Container{c1}
client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
err := CheckPrereqs(client, false)
assert.NoError(t, err)
client.AssertExpectations(t)
}
func TestCheckPrereqs_ListError(t *testing.T) {
cs := []container.Container{}
client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, errors.New("oops"))
err := CheckPrereqs(client, false)
assert.Error(t, err)
assert.EqualError(t, err, "oops")
client.AssertExpectations(t)
}