Allow user-configurable DOCKER_HOST

This commit is contained in:
Brian DeHamer 2015-07-21 19:37:18 +00:00
parent 00f2875abf
commit 4ba21639a0
10 changed files with 202 additions and 35 deletions

View file

@ -32,8 +32,8 @@ type Client interface {
Rename(Container, string) error
}
func NewClient() Client {
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
func NewClient(dockerHost string) Client {
docker, err := dockerclient.NewDockerClient(dockerHost, nil)
if err != nil {
log.Fatalf("Error instantiating Docker client: %s\n", err)

View file

@ -7,6 +7,13 @@ import (
"github.com/samalba/dockerclient"
)
func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockerclient.ImageInfo) *Container {
return &Container{
containerInfo: containerInfo,
imageInfo: imageInfo,
}
}
type Container struct {
Stale bool
@ -95,14 +102,3 @@ func (c Container) hostConfig() *dockerclient.HostConfig {
return hostConfig
}
func NewTestContainer(name string, links []string) Container {
return Container{
containerInfo: &dockerclient.ContainerInfo{
Name: name,
HostConfig: &dockerclient.HostConfig{
Links: links,
},
},
}
}

View file

@ -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)
}

View file

@ -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")
}
}

View file

@ -32,12 +32,12 @@ func TestByCreated(t *testing.T) {
}
func TestSortByDependencies_Success(t *testing.T) {
c1 := NewTestContainer("1", []string{})
c2 := NewTestContainer("2", []string{"1:"})
c3 := NewTestContainer("3", []string{"2:"})
c4 := NewTestContainer("4", []string{"3:"})
c5 := NewTestContainer("5", []string{"4:"})
c6 := NewTestContainer("6", []string{"5:", "3:"})
c1 := newTestContainer("1", []string{})
c2 := newTestContainer("2", []string{"1:"})
c3 := newTestContainer("3", []string{"2:"})
c4 := newTestContainer("4", []string{"3:"})
c5 := newTestContainer("5", []string{"4:"})
c6 := newTestContainer("6", []string{"5:", "3:"})
containers := []Container{c6, c2, c4, c1, c3, c5}
result, err := SortByDependencies(containers)
@ -47,9 +47,9 @@ func TestSortByDependencies_Success(t *testing.T) {
}
func TestSortByDependencies_Error(t *testing.T) {
c1 := NewTestContainer("1", []string{"3:"})
c2 := NewTestContainer("2", []string{"1:"})
c3 := NewTestContainer("3", []string{"2:"})
c1 := newTestContainer("1", []string{"3:"})
c2 := newTestContainer("2", []string{"1:"})
c3 := newTestContainer("3", []string{"2:"})
containers := []Container{c1, c2, c3}
_, err := SortByDependencies(containers)
@ -57,3 +57,15 @@ func TestSortByDependencies_Error(t *testing.T) {
assert.Error(t, err)
assert.EqualError(t, err, "Circular reference to 1")
}
func newTestContainer(name string, links []string) Container {
return *NewContainer(
&dockerclient.ContainerInfo{
Name: name,
HostConfig: &dockerclient.HostConfig{
Links: links,
},
},
nil,
)
}