Skip to content

Commit

Permalink
Adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 committed Dec 3, 2018
1 parent 957a510 commit b92de8a
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docker/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package docker

import (
"context"
"errors"
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"testing"
)

type mockedProxy struct {
mock.Mock
dockerProxy
}

func (m *mockedProxy) ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error) {
args := m.Called()
containers, ok := args.Get(0).([]types.Container)
if !ok && args.Get(0) != nil {
panic("containers is not of type []types.Container")
}
return containers, args.Error(1)

}
func Test_dockerClient_ListContainers_null(t *testing.T) {
proxy := mockedProxy{}
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, nil)
client := &dockerClient{&proxy}

list, err := client.ListContainers()
assert.Empty(t, list, "list should be empty")
require.NoError(t, err, "error should not return an error.")

proxy.AssertExpectations(t)
}

func Test_dockerClient_ListContainers_error(t *testing.T) {
proxy := mockedProxy{}
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, errors.New("test"))
client := &dockerClient{&proxy}

list, err := client.ListContainers()
assert.Nil(t, list, "list should be nil")
require.Error(t, err, "test.")

proxy.AssertExpectations(t)
}

func Test_dockerClient_ListContainers_happy(t *testing.T) {
containers := []types.Container{
{
ID: "abcdefghijklmnopqrst",
Names: []string{"/z_test_container"},
},
{
ID: "1234567890_abcxyzdef",
Names: []string{"/a_test_container"},
},
}

proxy := mockedProxy{}
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(containers, nil)
client := &dockerClient{&proxy}

list, err := client.ListContainers()
require.NoError(t, err, "error should not return an error.")

assert.Equal(t, list, []Container{
{
ID: "1234567890_a",
Name: "a_test_container",
Names: []string{"/a_test_container"},
},
{
ID: "abcdefghijkl",
Name: "z_test_container",
Names: []string{"/z_test_container"},
},
})

proxy.AssertExpectations(t)
}

0 comments on commit b92de8a

Please sign in to comment.