-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e69168b
commit b27e0a3
Showing
6 changed files
with
345 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,200 @@ | ||
package container_manager | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/golang/mock/gomock" | ||
"github.com/muhammedikinci/pin/pkg/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWhenContainerCreateReturnErrorStartContainerMustReturnSameError(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
merror := errors.New("test") | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Start creating container") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(container.ContainerCreateCreatedBody{}, merror) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
resp, err := cm.StartContainer("", "") | ||
|
||
assert.Equal(t, resp, container.ContainerCreateCreatedBody{}) | ||
assert.Equal(t, err, merror) | ||
} | ||
|
||
func TestWhenContainerCreateReturnResponseStartContainerMustSameResponseWithNilError(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
mres := container.ContainerCreateCreatedBody{ | ||
ID: "test", | ||
} | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Start creating container") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(mres, nil) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
resp, err := cm.StartContainer("", "") | ||
|
||
assert.Equal(t, resp.ID, mres.ID) | ||
assert.Equal(t, err, nil) | ||
} | ||
|
||
func TestWhenContainerStopReturnErrorStopContainerMustReturnSameError(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
merror := errors.New("test") | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container stopping") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerStop(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(merror) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
err := cm.StopContainer("") | ||
|
||
assert.Equal(t, err, merror) | ||
} | ||
|
||
func TestWhenContainerStopReturnNilStopContainerMustReturnNil(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container stopping") | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container stopped") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerStop(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(nil) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
err := cm.StopContainer("") | ||
|
||
assert.Equal(t, err, nil) | ||
} | ||
|
||
func TestWhenRemoveContainerReturnErrorStopContainerMustReturnSameError(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
merror := errors.New("test") | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container removing") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerRemove(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(merror) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
err := cm.RemoveContainer("") | ||
|
||
assert.Equal(t, err, merror) | ||
} | ||
|
||
func TestWhenContainerRemoveReturnNilRemoveContainerMustReturnNil(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
defer ctrl.Finish() | ||
|
||
mockCli := mocks.NewMockClient(ctrl) | ||
mockLog := mocks.NewMockLog(ctrl) | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container removing") | ||
|
||
mockLog. | ||
EXPECT(). | ||
Println("Container removed") | ||
|
||
mockCli. | ||
EXPECT(). | ||
ContainerRemove(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(nil) | ||
|
||
cm := containerManager{ | ||
ctx: context.Background(), | ||
cli: mockCli, | ||
log: mockLog, | ||
} | ||
|
||
err := cm.RemoveContainer("") | ||
|
||
assert.Equal(t, err, nil) | ||
} |
Oops, something went wrong.