Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 3, 2022
1 parent 0b47b61 commit 3687d4a
Show file tree
Hide file tree
Showing 30 changed files with 221 additions and 218 deletions.
2 changes: 1 addition & 1 deletion cluster/calcium/calcium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewTestCluster() *Calcium {
}

func TestNewCluster(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
config := types.Config{WALFile: "/tmp/a", HAKeepaliveInterval: 16 * time.Second}
_, err := New(ctx, config, nil)
assert.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

Expand Down
10 changes: 5 additions & 5 deletions cluster/calcium/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDoLock(t *testing.T) {
lock := &lockmocks.DistributedLock{}
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
// lock failed
lock.On("Lock", mock.Anything).Return(context.TODO(), types.ErrMockError).Once()
lock.On("Lock", mock.Anything).Return(context.Background(), types.ErrMockError).Once()
lock.On("Unlock", mock.Anything).Return(nil).Once()
_, _, err = c.doLock(ctx, "somename", 1)
assert.Error(t, err)
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestWithWorkloadsLocked(t *testing.T) {
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(context.TODO(), types.ErrMockError).Once()
lock.On("Lock", mock.Anything).Return(context.Background(), types.ErrMockError).Once()
store.On("GetWorkloads", mock.Anything, mock.Anything).Return([]*types.Workload{{}}, nil).Once()
err := c.withWorkloadsLocked(ctx, []string{"c1", "c2"}, func(ctx context.Context, workloads map[string]*types.Workload) error { return nil })
assert.Error(t, err)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestWithWorkloadLocked(t *testing.T) {
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(context.TODO(), types.ErrMockError).Once()
lock.On("Lock", mock.Anything).Return(context.Background(), types.ErrMockError).Once()
store.On("GetWorkloads", mock.Anything, mock.Anything).Return([]*types.Workload{{}}, nil).Once()
err := c.withWorkloadLocked(ctx, "c1", func(ctx context.Context, workload *types.Workload) error { return nil })
assert.Error(t, err)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestWithNodesPodLocked(t *testing.T) {
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(context.TODO(), types.ErrMockError).Once()
lock.On("Lock", mock.Anything).Return(context.Background(), types.ErrMockError).Once()
err = c.withNodesPodLocked(ctx, &types.NodeFilter{Podname: "test", Includes: []string{"test"}, All: false}, func(ctx context.Context, nodes map[string]*types.Node) error { return nil })
assert.Error(t, err)
lock.On("Lock", mock.Anything).Return(ctx, nil)
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestWithNodesOperationLocked(t *testing.T) {
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(context.TODO(), types.ErrMockError).Once()
lock.On("Lock", mock.Anything).Return(context.Background(), types.ErrMockError).Once()
err = c.withNodesOperationLocked(ctx, &types.NodeFilter{Podname: "test", Includes: []string{"test"}, All: false}, func(ctx context.Context, nodes map[string]*types.Node) error { return nil })
assert.Error(t, err)
lock.On("Lock", mock.Anything).Return(ctx, nil)
Expand Down
9 changes: 5 additions & 4 deletions cluster/calcium/realloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/cockroachdb/errors"
enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestRealloc(t *testing.T) {
}
}

store.On("GetWorkload", mock.Anything, "c1").Return(newC1(context.TODO(), nil)[0], nil)
store.On("GetWorkload", mock.Anything, "c1").Return(newC1(context.Background(), nil)[0], nil)
opts := &types.ReallocOptions{
ID: "c1",
ResourceOpts: types.WorkloadResourceOpts{},
Expand All @@ -61,14 +62,14 @@ func TestRealloc(t *testing.T) {
// failed by GetNode
store.On("GetNode", mock.Anything, "node1").Return(nil, types.ErrMockError).Once()
err := c.ReallocResource(ctx, opts)
assert.EqualError(t, err, "ETCD must be set")
assert.True(t, errors.Is(err, types.ErrMockError))
store.AssertExpectations(t)
store.On("GetNode", mock.Anything, "node1").Return(node1, nil)

// failed by lock
store.On("CreateLock", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
err = c.ReallocResource(ctx, opts)
assert.EqualError(t, err, "ETCD must be set")
assert.True(t, errors.Is(err, types.ErrMockError))
store.AssertExpectations(t)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
store.On("GetWorkloads", mock.Anything, []string{"c1"}).Return(newC1, nil)
Expand All @@ -90,7 +91,7 @@ func TestRealloc(t *testing.T) {
// failed by UpdateWorkload
store.On("UpdateWorkload", mock.Anything, mock.Anything).Return(types.ErrMockError).Once()
err = c.ReallocResource(ctx, opts)
assert.EqualError(t, err, "ETCD must be set")
assert.True(t, errors.Is(err, types.ErrMockError))
store.AssertExpectations(t)
store.On("UpdateWorkload", mock.Anything, mock.Anything).Return(nil)

Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/remap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestRemapResource(t *testing.T) {
lock.On("Lock", mock.Anything).Return(context.Background(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
c.doRemapResourceAndLog(context.TODO(), log.WithField("test", "zc"), node)
c.doRemapResourceAndLog(context.Background(), log.WithField("test", "zc"), node)
}
2 changes: 1 addition & 1 deletion cluster/calcium/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down
22 changes: 11 additions & 11 deletions cluster/calcium/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestHandleCreateWorkloadNoHandle(t *testing.T) {
store.On("GetWorkload", mock.Anything, wrkid).Return(wrk, nil).Once()
store.On("GetWorkloads", mock.Anything, mock.Anything).Return(nil, nil)

c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
store.AssertExpectations(t)

// Recovers nothing.
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
}

func TestHandleCreateWorkloadError(t *testing.T) {
Expand Down Expand Up @@ -83,26 +83,26 @@ func TestHandleCreateWorkloadError(t *testing.T) {
err = errors.Wrapf(types.ErrInvaildCount, "keys: [%s]", wrkid)
store.On("GetWorkload", mock.Anything, mock.Anything).Return(wrk, err).Once()
store.On("GetNode", mock.Anything, mock.Anything).Return(nil, err).Once()
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
store.AssertExpectations(t)
engine.AssertExpectations(t)

store.On("GetWorkload", mock.Anything, mock.Anything).Return(wrk, err).Once()
store.On("GetNode", mock.Anything, wrk.Nodename).Return(node, nil).Once()
engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(err).Once()
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
store.AssertExpectations(t)
engine.AssertExpectations(t)

store.On("GetWorkload", mock.Anything, wrkid).Return(wrk, fmt.Errorf("err")).Once()
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil).Once()
engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(types.ErrWorkloadNotExists).Once()
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
store.AssertExpectations(t)
engine.AssertExpectations(t)

// Nothing recovered.
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
}

func TestHandleCreateWorkloadHandled(t *testing.T) {
Expand Down Expand Up @@ -145,12 +145,12 @@ func TestHandleCreateWorkloadHandled(t *testing.T) {
Return(nil).
Once()

c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
store.AssertExpectations(t)
eng.AssertExpectations(t)

// Recovers nothing.
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
}

func TestHandleCreateLambda(t *testing.T) {
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestHandleCreateLambda(t *testing.T) {

store := c.store.(*storemocks.Store)
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
time.Sleep(500 * time.Millisecond)
store.AssertExpectations(t)

Expand Down Expand Up @@ -219,9 +219,9 @@ func TestHandleCreateLambda(t *testing.T) {
lock.On("Unlock", mock.Anything).Return(nil)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)

c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
// Recovered nothing.
c.wal.Recover(context.TODO())
c.wal.Recover(context.Background())
time.Sleep(500 * time.Millisecond)
store.AssertExpectations(t)
eng.AssertExpectations(t)
Expand Down
2 changes: 1 addition & 1 deletion engine/docker/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestWithDumpFiles(t *testing.T) {
}
fp := []string{}
for target, content := range data {
withTarfileDump(context.TODO(), target, content, 0, 0, int64(0), func(target, tarfile string) error {
withTarfileDump(context.Background(), target, content, 0, 0, int64(0), func(target, tarfile string) error {
assert.True(t, strings.HasPrefix(target, "/tmp/test"))
fp = append(fp, tarfile)
_, err := os.Stat(tarfile)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/opencontainers/image-spec v1.0.2
github.com/panjf2000/ants/v2 v2.6.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/projecteru2/libyavirt v0.0.0-20220621042712-95cdc6363b1c
github.com/prometheus/client_golang v1.11.0
github.com/rs/zerolog v1.28.0
Expand Down Expand Up @@ -99,6 +98,7 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
Expand Down
24 changes: 12 additions & 12 deletions lock/etcdlock/mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func TestMutex(t *testing.T) {

m2, err := New(cli, "test", time.Second)
assert.NoError(t, err)
_, err = m2.Lock(context.TODO())
_, err = m2.Lock(context.Background())
m3, err := New(cli, "test", 100*time.Millisecond)
assert.NoError(t, err)
_, err = m3.Lock(context.TODO())
_, err = m3.Lock(context.Background())
assert.EqualError(t, err, "context deadline exceeded")
m2.Unlock(context.TODO())
m3.Unlock(context.TODO())
m2.Unlock(context.Background())
m3.Unlock(context.Background())

// round 3: ctx canceled after lock secured
m4, err := New(cli, "test", time.Second)
Expand All @@ -47,7 +47,7 @@ func TestMutex(t *testing.T) {
rCtx, err := m4.Lock(ctx)
<-rCtx.Done()
assert.EqualError(t, rCtx.Err(), "context deadline exceeded")
m4.Unlock(context.TODO())
m4.Unlock(context.Background())

// round 4: passive release

Expand Down Expand Up @@ -75,8 +75,8 @@ func TestTryLock(t *testing.T) {
assert.Nil(t, ctx2)
assert.Error(t, err)

assert.NoError(t, m1.Unlock(context.TODO()))
assert.NoError(t, m2.Unlock(context.TODO()))
assert.NoError(t, m1.Unlock(context.Background()))
assert.NoError(t, m2.Unlock(context.Background()))

// round 2: lock conflict

Expand All @@ -85,12 +85,12 @@ func TestTryLock(t *testing.T) {
m4, err := New(cli, "test", time.Second)
assert.NoError(t, err)

rCtx, err := m3.TryLock(context.TODO())
rCtx, err := m3.TryLock(context.Background())
assert.NoError(t, err)
_, err = m4.TryLock(context.TODO())
_, err = m4.TryLock(context.Background())
assert.EqualError(t, err, "mutex: Locked by another session")
m4.Unlock(context.TODO())
m3.Unlock(context.TODO())
m4.Unlock(context.Background())
m3.Unlock(context.Background())
assert.NoError(t, rCtx.Err())

// round 3: ctx canceled after lock secured
Expand All @@ -101,7 +101,7 @@ func TestTryLock(t *testing.T) {
rCtx, err = m5.TryLock(ctx)
<-rCtx.Done()
assert.EqualError(t, rCtx.Err(), "context deadline exceeded")
m5.Unlock(context.TODO())
m5.Unlock(context.Background())

// round 4: passive release

Expand Down
5 changes: 3 additions & 2 deletions resources/volume/models/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/resources/volume/schedule"
"github.com/projecteru2/core/resources/volume/types"
coretypes "github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func (v *Volume) doAlloc(resourceInfo *types.NodeResourceInfo, deployCount int,
if opts.StorageRequest > 0 {
storageCapacity := int((resourceInfo.Capacity.Storage - resourceInfo.Usage.Storage) / opts.StorageRequest)
if storageCapacity < deployCount {
return nil, nil, errors.Wrapf(types.ErrInsufficientResource, "not enough storage, request: %+v, available: %+v", opts.StorageRequest, storageCapacity)
return nil, nil, errors.Wrapf(coretypes.ErrInsufficientResource, "not enough storage, request: %+v, available: %+v", opts.StorageRequest, storageCapacity)
}
}

Expand All @@ -111,7 +112,7 @@ func (v *Volume) doAlloc(resourceInfo *types.NodeResourceInfo, deployCount int,
} else {
volumePlans, diskPlans = schedule.GetVolumePlans(resourceInfo, opts.VolumesRequest, v.Config.Scheduler.MaxDeployCount)
if len(volumePlans) < deployCount {
return nil, nil, errors.Wrapf(types.ErrInsufficientResource, "not enough volume plan, need %+v, available %+v", deployCount, len(volumePlans))
return nil, nil, errors.Wrapf(coretypes.ErrInsufficientResource, "not enough volume plan, need %+v, available %+v", deployCount, len(volumePlans))
}
volumePlans = volumePlans[:deployCount]
diskPlans = diskPlans[:deployCount]
Expand Down
4 changes: 2 additions & 2 deletions resources/volume/models/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAlloc(t *testing.T) {
}),
}
_, _, err = volume.GetDeployArgs(ctx, node, 2, resourceOpts)
assert.ErrorIs(t, err, types.ErrInsufficientResource)
assert.ErrorIs(t, err, coretypes.ErrInsufficientResource)

// normal case
resourceOpts = &types.WorkloadResourceOpts{
Expand All @@ -61,7 +61,7 @@ func TestAlloc(t *testing.T) {
}),
}
_, _, err = volume.GetDeployArgs(ctx, node, 3, resourceOpts)
assert.ErrorIs(t, err, types.ErrInsufficientResource)
assert.ErrorIs(t, err, coretypes.ErrInsufficientResource)

// normal case
resourceOpts = &types.WorkloadResourceOpts{
Expand Down
2 changes: 1 addition & 1 deletion resources/volume/models/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"testing"

"github.com/cockroachdb/errors"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/projecteru2/core/resources/volume/types"
Expand Down
5 changes: 3 additions & 2 deletions resources/volume/models/realloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/resources/volume/schedule"
"github.com/projecteru2/core/resources/volume/types"
coretypes "github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
)

Expand Down Expand Up @@ -45,15 +46,15 @@ func (v *Volume) GetReallocArgs(ctx context.Context, node string, originResource
}

if finalWorkloadResourceArgs.StorageRequest-originResourceArgs.StorageRequest > resourceInfo.Capacity.Storage-resourceInfo.Usage.Storage {
return nil, nil, nil, types.ErrInsufficientResource
return nil, nil, nil, coretypes.ErrInsufficientResource
}

var volumePlan types.VolumePlan
var diskPlan types.Disks
if needVolumeReschedule {
volumePlan, diskPlan, err = schedule.GetAffinityPlan(resourceInfo, resourceOpts.VolumesRequest, originResourceArgs.VolumePlanRequest, originResourceArgs.VolumesRequest)
if err != nil {
return nil, nil, nil, types.ErrInsufficientResource
return nil, nil, nil, coretypes.ErrInsufficientResource
}
} else {
volumePlan = originResourceArgs.VolumePlanRequest
Expand Down
4 changes: 2 additions & 2 deletions resources/volume/models/realloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestRealloc(t *testing.T) {
StorageLimit: 4 * units.TiB,
}
_, _, _, err = volume.GetReallocArgs(ctx, node, originResourceArgs, opts)
assert.ErrorIs(t, err, types.ErrInsufficientResource)
assert.ErrorIs(t, err, coretypes.ErrInsufficientResource)

// insufficient volume
bindings = generateVolumeBindings(t, []string{
Expand All @@ -97,7 +97,7 @@ func TestRealloc(t *testing.T) {
StorageLimit: 0,
}
_, _, _, err = volume.GetReallocArgs(ctx, node, originResourceArgs, opts)
assert.ErrorIs(t, err, types.ErrInsufficientResource)
assert.ErrorIs(t, err, coretypes.ErrInsufficientResource)

// normal case
bindings = generateVolumeBindings(t, []string{
Expand Down
Loading

0 comments on commit 3687d4a

Please sign in to comment.