Skip to content

Commit

Permalink
[skip ci] fix tests exculde lambda, node, realloc, remove, create, an…
Browse files Browse the repository at this point in the history
…d replace
  • Loading branch information
CMGS committed Jul 7, 2022
1 parent 9961fc5 commit ab413e5
Show file tree
Hide file tree
Showing 28 changed files with 307 additions and 410 deletions.
26 changes: 8 additions & 18 deletions cluster/calcium/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
"github.com/projecteru2/core/resources"
resourcemocks "github.com/projecteru2/core/resources/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
Expand All @@ -26,10 +25,6 @@ const (
func TestBuild(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
plugin := c.resource.GetPlugins()[0].(*resourcemocks.Plugin)
plugin.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything).Return(&resources.GetNodeResourceInfoResponse{
ResourceInfo: &resources.NodeResourceInfo{},
}, nil)
opts := &types.BuildOptions{
Name: "xx",
BuildMethod: types.BuildFromSCM,
Expand Down Expand Up @@ -64,21 +59,16 @@ func TestBuild(t *testing.T) {
assert.Error(t, err)
// failed by buildpod not set
c = NewTestCluster()
plugin = c.resource.GetPlugins()[0].(*resourcemocks.Plugin)
plugin.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything).Return(&resources.GetNodeResourceInfoResponse{
ResourceInfo: &resources.NodeResourceInfo{},
}, nil)
_, err = c.BuildImage(ctx, opts)
assert.Error(t, err)
c.config.Docker.BuildPod = "test"
// failed by ListPodNodes failed
store := &storemocks.Store{}
store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrBadMeta).Once()
c.store = store
store := c.store.(*storemocks.Store)
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrBadMeta).Once()
ch, err := c.BuildImage(ctx, opts)
assert.Error(t, err)
// failed by no nodes
store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
ch, err = c.BuildImage(ctx, opts)
assert.Error(t, err)
engine := &enginemocks.API{}
Expand All @@ -90,14 +80,14 @@ func TestBuild(t *testing.T) {
Available: true,
Engine: engine,
}
store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{node}, nil)

store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{node}, nil)
// failed by plugin error
plugin.On("GetMostIdleNode", mock.Anything, mock.Anything).Return(nil, types.ErrGetMostIdleNodeFailed).Once()
rmgr := c.rmgr.(*resourcemocks.Manager)
rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil)
rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("", types.ErrBadCount).Once()
ch, err = c.BuildImage(ctx, opts)
assert.Error(t, err)

plugin.On("GetMostIdleNode", mock.Anything, mock.Anything).Return(&resources.GetMostIdleNodeResponse{NodeName: node.Name, Priority: 100}, nil)
rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("test", nil)
// create image
c.config.Docker.Hub = "test.com"
c.config.Docker.Namespace = "test"
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/calcium.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func New(ctx context.Context, config types.Config, t *testing.T) (*Calcium, erro
return nil, logger.ErrWithTracing(ctx, errors.WithStack(err))
}

// load cpumem plugin
// load internal plugins
cpumem, err := cpumem.NewPlugin(config)
if err != nil {
log.Errorf(ctx, "[NewPluginManager] new cpumem plugin error: %v", err)
Expand Down
18 changes: 4 additions & 14 deletions cluster/calcium/calcium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/projecteru2/core/resources"
resourcemocks "github.com/projecteru2/core/resources/mocks"
sourcemocks "github.com/projecteru2/core/source/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
Expand Down Expand Up @@ -45,20 +44,12 @@ func NewTestCluster() *Calcium {
}
c.store = &storemocks.Store{}
c.source = &sourcemocks.Source{}
c.wal = &WAL{WAL: &walmocks.WAL{}}
c.wal = &walmocks.WAL{}
c.rmgr = &resourcemocks.Manager{}

mwal := c.wal.WAL.(*walmocks.WAL)
mwal := c.wal.(*walmocks.WAL)
commit := wal.Commit(func() error { return nil })
mwal.On("Log", mock.Anything, mock.Anything).Return(commit, nil)

plugin := &resourcemocks.Plugin{}
plugin.On("Name").Return("mock-plugin")
plugin.On("ResolveNodeResourceInfoToMetrics", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, context.DeadlineExceeded)
if c.resource, err = resources.NewPluginManager(c.config); err != nil {
panic(err)
}
c.resource.AddPlugins(plugin)

return c
}

Expand Down Expand Up @@ -107,8 +98,7 @@ func TestNewCluster(t *testing.T) {

func TestFinalizer(t *testing.T) {
c := NewTestCluster()
store := &storemocks.Store{}
c.store = store
store := c.store.(*storemocks.Store)
store.On("TerminateEmbededStorage").Return(nil)
c.Finalizer()
}
44 changes: 17 additions & 27 deletions cluster/calcium/capacity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func TestCalculateCapacity(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
store := c.store.(*storemocks.Store)
rmgr := c.rmgr.(*resourcemocks.Manager)
rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil)
engine := &enginemocks.API{}
plugin := c.resource.GetPlugins()[0].(*resourcemocks.Plugin)

// pod1 := &types.Pod{Name: "p1"}
node1 := &types.Node{
Expand All @@ -36,9 +37,6 @@ func TestCalculateCapacity(t *testing.T) {
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
plugin.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything).Return(&resources.GetNodeResourceInfoResponse{
ResourceInfo: &resources.NodeResourceInfo{},
}, nil)
// failed by call plugin
opts := &types.DeployOptions{
Entrypoint: &types.Entrypoint{
Expand All @@ -51,47 +49,39 @@ func TestCalculateCapacity(t *testing.T) {
},
Count: 3,
}
plugin.On("GetNodesDeployCapacity", mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("not implemented")).Once()
rmgr.On("GetNodesDeployCapacity", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, errors.New("not implemented")).Times(3)
_, err := c.CalculateCapacity(ctx, opts)
assert.Error(t, err)

// failed by get deploy status
plugin.On("GetNodesDeployCapacity", mock.Anything, mock.Anything, mock.Anything).Return(&resources.GetNodesDeployCapacityResponse{
Nodes: map[string]*resources.NodeCapacityInfo{
"n1": {
NodeName: "n1",
Capacity: 10,
Usage: 0.5,
Rate: 0.5,
Weight: 100,
},
},
Total: 0,
}, nil)
store.On("GetDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err = c.CalculateCapacity(ctx, opts)
assert.Error(t, err)

store.On("GetDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(map[string]int{"n1": 0}, nil)

// failed by get deploy plan
opts.DeployStrategy = "FAKE"
_, err = c.CalculateCapacity(ctx, opts)
assert.Error(t, err)

// strategy: auto
opts.DeployStrategy = strategy.Auto
msg, err := c.CalculateCapacity(ctx, opts)
assert.NoError(t, err)
assert.Equal(t, msg.NodeCapacities["n1"], 3)
assert.Equal(t, msg.Total, 3)

// strategy: dummy
opts.DeployStrategy = strategy.Dummy
msg, err = c.CalculateCapacity(ctx, opts)
rmgr.On("GetNodesDeployCapacity", mock.Anything, mock.Anything, mock.Anything).Return(
map[string]*resources.NodeCapacityInfo{
"n1": {
NodeName: "n1",
Capacity: 10,
Usage: 0.5,
Rate: 0.5,
Weight: 100,
},
},
10, nil,
)
msg, err := c.CalculateCapacity(ctx, opts)
assert.NoError(t, err)
assert.Equal(t, msg.NodeCapacities["n1"], 10)
assert.Equal(t, msg.Total, 10)

store.AssertExpectations(t)
rmgr.AssertExpectations(t)
}
9 changes: 3 additions & 6 deletions cluster/calcium/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import (
func TestControlStart(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
store := &storemocks.Store{}
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
c.store = store
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
// failed by GetWorkloads
store.On("GetWorkloads", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
Expand Down Expand Up @@ -101,11 +100,10 @@ func TestControlStart(t *testing.T) {
func TestControlStop(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
store := &storemocks.Store{}
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
c.store = store
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
workload := &types.Workload{
ID: "id1",
Expand Down Expand Up @@ -146,11 +144,10 @@ func TestControlStop(t *testing.T) {
func TestControlRestart(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
store := &storemocks.Store{}
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
c.store = store
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
engine := &enginemocks.API{}
workload := &types.Workload{
Expand Down
3 changes: 1 addition & 2 deletions cluster/calcium/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ func TestCopy(t *testing.T) {
},
},
}
store := &storemocks.Store{}
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
c.store = store
// failed by GetWorkload
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
ch, err := c.Copy(ctx, opts)
Expand Down
Loading

0 comments on commit ab413e5

Please sign in to comment.