You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.9 KiB
Go
82 lines
1.9 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.ContainerFilter")).Return(cs, nil)
|
|
client.On("Stop", c2, time.Duration(60)).Return(nil)
|
|
|
|
err := CheckPrereqs(client)
|
|
|
|
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.ContainerFilter")).Return(cs, nil)
|
|
|
|
err := CheckPrereqs(client)
|
|
|
|
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.ContainerFilter")).Return(cs, errors.New("oops"))
|
|
|
|
err := CheckPrereqs(client)
|
|
|
|
assert.Error(t, err)
|
|
assert.EqualError(t, err, "oops")
|
|
client.AssertExpectations(t)
|
|
}
|