Allow user-configurable DOCKER_HOST
parent
00f2875abf
commit
4ba21639a0
@ -0,0 +1,81 @@
|
||||
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)
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package mockclient
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/container"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockClient) ListContainers(cf container.ContainerFilter) ([]container.Container, error) {
|
||||
args := m.Called(cf)
|
||||
return args.Get(0).([]container.Container), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockClient) RefreshImage(c *container.Container) error {
|
||||
args := m.Called(c)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockClient) Stop(c container.Container, timeout time.Duration) error {
|
||||
args := m.Called(c, timeout)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockClient) Start(c container.Container) error {
|
||||
args := m.Called(c)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockClient) Rename(c container.Container, name string) error {
|
||||
args := m.Called(c, name)
|
||||
return args.Error(0)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package mockclient
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/container"
|
||||
)
|
||||
|
||||
func TestMockInterface(t *testing.T) {
|
||||
iface := reflect.TypeOf((*container.Client)(nil)).Elem()
|
||||
mock := &MockClient{}
|
||||
|
||||
if !reflect.TypeOf(mock).Implements(iface) {
|
||||
t.Fatalf("Mock does not implement the Client interface")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue