github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/copy_test.go (about)

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	enginemocks "github.com/projecteru2/core/engine/mocks"
     8  	lockmocks "github.com/projecteru2/core/lock/mocks"
     9  	storemocks "github.com/projecteru2/core/store/mocks"
    10  	"github.com/projecteru2/core/types"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/mock"
    14  )
    15  
    16  func TestCopy(t *testing.T) {
    17  	c := NewTestCluster()
    18  	ctx := context.Background()
    19  
    20  	// failed by target
    21  	_, err := c.Copy(ctx, &types.CopyOptions{
    22  		Targets: map[string][]string{},
    23  	})
    24  	assert.Error(t, err)
    25  
    26  	opts := &types.CopyOptions{
    27  		Targets: map[string][]string{
    28  			"cid": {
    29  				"path1",
    30  				"path2",
    31  			},
    32  		},
    33  	}
    34  	store := c.store.(*storemocks.Store)
    35  	lock := &lockmocks.DistributedLock{}
    36  	lock.On("Lock", mock.Anything).Return(ctx, nil)
    37  	lock.On("Unlock", mock.Anything).Return(nil)
    38  	store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
    39  	// failed by GetWorkload
    40  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    41  	ch, err := c.Copy(ctx, opts)
    42  	assert.NoError(t, err)
    43  	for r := range ch {
    44  		assert.Error(t, r.Error)
    45  	}
    46  	workload := &types.Workload{ID: "cid"}
    47  	engine := &enginemocks.API{}
    48  	workload.Engine = engine
    49  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
    50  	// failed by VirtualizationCopyFrom
    51  	engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, 0, int64(0), types.ErrMockError).Twice()
    52  	ch, err = c.Copy(ctx, opts)
    53  	assert.NoError(t, err)
    54  	for r := range ch {
    55  		assert.Error(t, r.Error)
    56  	}
    57  	engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte("omg"), 0, 0, int64(0), nil)
    58  	// success
    59  	ch, err = c.Copy(ctx, opts)
    60  	assert.NoError(t, err)
    61  	for r := range ch {
    62  		assert.NoError(t, r.Error)
    63  	}
    64  }