Skip to content

Commit

Permalink
fix #5 and write unit tests for #4 except for CopyToContainer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedikinci committed May 7, 2022
1 parent e69168b commit b27e0a3
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 39 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ go test ./...
- Create small pieces with extracting codes from runner struct and write unit test:
- Image Manager ✅[Issue#3](https://github.com/muhammedikinci/pin/issues/3)
- Container Manager Not Completed Yet [Issue#4](https://github.com/muhammedikinci/pin/issues/4)
- Shell Commander Not Completed Yet [Issue#5](https://github.com/muhammedikinci/pin/issues/5)
- Shell Commander [Issue#5](https://github.com/muhammedikinci/pin/issues/5)
- Parser
- Runner
- Add port expose support

# Contact

Expand Down
77 changes: 40 additions & 37 deletions pkg/container_manager/container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import (
"github.com/muhammedikinci/pin/pkg/interfaces"
)

type ContainerManager struct {
type containerManager struct {
ctx context.Context
cli interfaces.Client
log interfaces.Log
}

func NewContainerManager(ctx context.Context, cli interfaces.Client, log interfaces.Log) ContainerManager {
return ContainerManager{
func NewContainerManager(ctx context.Context, cli interfaces.Client, log interfaces.Log) containerManager {
return containerManager{
ctx: ctx,
cli: cli,
log: log,
}
}

func (cm ContainerManager) StartContainer(jobName string, image string) (container.ContainerCreateCreatedBody, error) {
func (cm containerManager) StartContainer(jobName string, image string) (container.ContainerCreateCreatedBody, error) {
color.Set(color.FgGreen)
cm.log.Println("Start creating container")
color.Unset()
Expand All @@ -50,7 +50,7 @@ func (cm ContainerManager) StartContainer(jobName string, image string) (contain
return resp, nil
}

func (cm ContainerManager) StopContainer(containerID string) error {
func (cm containerManager) StopContainer(containerID string) error {
color.Set(color.FgBlue)
cm.log.Println("Container stopping")

Expand All @@ -64,7 +64,7 @@ func (cm ContainerManager) StopContainer(containerID string) error {
return nil
}

func (cm ContainerManager) RemoveContainer(containerID string) error {
func (cm containerManager) RemoveContainer(containerID string) error {
color.Set(color.FgBlue)
cm.log.Println("Container removing")

Expand All @@ -78,61 +78,64 @@ func (cm ContainerManager) RemoveContainer(containerID string) error {
return nil
}

func (cm ContainerManager) CopyToContainer(containerID, workDir string) error {
func (cm containerManager) CopyToContainer(containerID, workDir string) error {
var buf bytes.Buffer

tw := tar.NewWriter(&buf)
defer tw.Close()

currentPath, _ := os.Getwd()

// TODO: add dirs, directories does not extract from docker api
err := filepath.Walk(currentPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
return cm.appender(path, info, err, currentPath, tw)
})

if !info.Mode().IsRegular() {
return nil
}
if err != nil {
return err
}

header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
err = cm.cli.CopyToContainer(cm.ctx, containerID, workDir, &buf, types.CopyToContainerOptions{})

header.Name = strings.TrimPrefix(strings.Replace(path, currentPath, "", -1), string(filepath.Separator))
header.Name = strings.ReplaceAll(header.Name, "\\", "/")
if err != nil {
return err
}

if header.Name[0] == '.' {
return nil
}
return nil
}

if err := tw.WriteHeader(header); err != nil {
return err
}
func (cm containerManager) appender(path string, info os.FileInfo, err error, currentPath string, tw *tar.Writer) error {
if err != nil {
return err
}

f, err := os.Open(path)
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}

defer f.Close()
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}

if _, err := io.Copy(tw, f); err != nil {
return err
}
header.Name = strings.TrimPrefix(strings.Replace(path, currentPath, "", -1), string(filepath.Separator))
header.Name = strings.ReplaceAll(header.Name, "\\", "/")

if header.Name[0] == '.' {
return nil
})
}

if err := tw.WriteHeader(header); err != nil {
return err
}

f, err := os.Open(path)
if err != nil {
return err
}

err = cm.cli.CopyToContainer(cm.ctx, containerID, workDir, &buf, types.CopyToContainerOptions{})
defer f.Close()

if err != nil {
if _, err := io.Copy(tw, f); err != nil {
return err
}

Expand Down
199 changes: 199 additions & 0 deletions pkg/container_manager/container_manager_test.go
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)
}
Loading

0 comments on commit b27e0a3

Please sign in to comment.