Skip to content

Commit

Permalink
add types.Container tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 16, 2018
1 parent 4c9d79c commit 736e71b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
9 changes: 5 additions & 4 deletions types/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type Container struct {

// Inspect a container
func (c *Container) Inspect(ctx context.Context) (enginetypes.ContainerJSON, error) {
inspectCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if c.Engine == nil {
return enginetypes.ContainerJSON{}, ErrNilEngine
}
inspectCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.Engine.ContainerInspect(inspectCtx, c.ID)
}

Expand All @@ -41,8 +41,9 @@ func (c *Container) Start(ctx context.Context) error {
if c.Engine == nil {
return ErrNilEngine
}

return c.Engine.ContainerStart(ctx, c.ID, enginetypes.ContainerStartOptions{})
startCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
return c.Engine.ContainerStart(startCtx, c.ID, enginetypes.ContainerStartOptions{})
}

// Stop a container
Expand Down
52 changes: 52 additions & 0 deletions types/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package types

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

enginetypes "github.com/docker/docker/api/types"
"github.com/projecteru2/core/3rdmocks"
"github.com/stretchr/testify/mock"
)

func TestContainerInspect(t *testing.T) {
mockEngine := &mocks.APIClient{}
r := enginetypes.ContainerJSON{}
r.ContainerJSONBase = &enginetypes.ContainerJSONBase{ID: "12345"}
mockEngine.On("ContainerInspect", mock.AnythingOfType("*context.timerCtx"), mock.Anything).Return(r, nil)

ctx := context.Background()
c := Container{}
_, err := c.Inspect(ctx)
assert.Error(t, err)
c.Engine = mockEngine
r2, err := c.Inspect(ctx)
assert.Equal(t, r.ID, r2.ID)
}

func TestContainerControl(t *testing.T) {
mockEngine := &mocks.APIClient{}
mockEngine.On("ContainerStart", mock.AnythingOfType("*context.timerCtx"), mock.Anything, mock.Anything).Return(nil)
mockEngine.On("ContainerStop", mock.AnythingOfType("*context.timerCtx"), mock.Anything, mock.Anything).Return(nil)
mockEngine.On("ContainerRemove", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything).Return(nil)

ctx := context.Background()
c := Container{}
err := c.Start(ctx)
assert.Error(t, err)
err = c.Stop(ctx, 5*time.Second)
assert.Error(t, err)
err = c.Remove(ctx)
assert.Error(t, err)

c.Engine = mockEngine
err = c.Start(ctx)
assert.NoError(t, err)
err = c.Stop(ctx, 5*time.Second)
assert.NoError(t, err)
err = c.Remove(ctx)
assert.NoError(t, err)
}

0 comments on commit 736e71b

Please sign in to comment.