Skip to content

Commit

Permalink
add Copy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 27, 2018
1 parent d6b8be9 commit 6ad8b1c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cluster/calcium/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
log "github.com/sirupsen/logrus"
)

//Copy uses docker cp to copy specified things and send to remote
// Copy uses docker cp to copy specified things and send to remote
func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *types.CopyMessage, error) {
ch := make(chan *types.CopyMessage)
go func() {
Expand All @@ -23,7 +23,7 @@ func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *type
defer wg.Done()
container, err := c.GetContainer(ctx, cid)
if err != nil {
log.Errorf("[Copy] Error when get container %s", cid)
log.Errorf("[Copy] Error when get container %s, err %v", cid, err)
ch <- makeCopyMessage(cid, cluster.CopyFailed, "", "", err, nil)
return
}
Expand All @@ -32,12 +32,12 @@ func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *type
go func(path string) {
defer wg.Done()
resp, stat, err := container.Engine.CopyFromContainer(ctx, container.ID, path)
log.Debugf("[Copy] Docker cp stat: %v", stat)
if err != nil {
log.Errorf("[Copy] Error during CopyFromContainer: %v", err)
ch <- makeCopyMessage(cid, cluster.CopyFailed, "", path, err, nil)
return
}
log.Debugf("[Copy] Docker cp stat: %v", stat)
ch <- makeCopyMessage(cid, cluster.CopyOK, stat.Name, path, nil, resp)
}(path)
}
Expand Down
54 changes: 54 additions & 0 deletions cluster/calcium/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package calcium

import (
"context"
"testing"

enginetypes "github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"

enginemocks "github.com/projecteru2/core/3rdmocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
"github.com/stretchr/testify/mock"
)

func TestCopy(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
opts := &types.CopyOptions{
Targets: map[string][]string{
"cid": []string{
"path1",
"path2",
},
},
}
store := &storemocks.Store{}
c.store = store
// failed by GetContainer
store.On("GetContainer", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
ch, err := c.Copy(ctx, opts)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
container := &types.Container{ID: "cid"}
engine := &enginemocks.APIClient{}
container.Engine = engine
store.On("GetContainer", mock.Anything, mock.Anything).Return(container, nil)
// failed by CopyFromContainer
engine.On("CopyFromContainer", mock.Anything, mock.Anything, mock.Anything).Return(nil, enginetypes.ContainerPathStat{}, types.ErrNilEngine).Twice()
ch, err = c.Copy(ctx, opts)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
engine.On("CopyFromContainer", mock.Anything, mock.Anything, mock.Anything).Return(nil, enginetypes.ContainerPathStat{Name: "omg"}, nil)
// success
ch, err = c.Copy(ctx, opts)
assert.NoError(t, err)
for r := range ch {
assert.NoError(t, r.Error)
}
}

0 comments on commit 6ad8b1c

Please sign in to comment.