Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cluster/calcium/calcium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/mock"
"golang.org/x/crypto/ssh"

lockmocks "github.com/projecteru2/core/lock/mocks"
"github.com/projecteru2/core/metrics"
resourcemocks "github.com/projecteru2/core/resource/mocks"
sourcemocks "github.com/projecteru2/core/source/mocks"
Expand Down Expand Up @@ -110,3 +111,18 @@ func NewTestCluster() *Calcium {

return c
}

func heldLock(t *testing.T) *lockmocks.DistributedLock {
t.Helper()
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(t.Context(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
return lock
}

func mockLocks(t *testing.T, store *storemocks.Store) *lockmocks.DistributedLock {
t.Helper()
lock := heldLock(t)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
return lock
}
7 changes: 1 addition & 6 deletions cluster/calcium/capacity_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package calcium

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

enginemocks "github.com/projecteru2/core/engine/mocks"
lockmocks "github.com/projecteru2/core/lock/mocks"
resourcemocks "github.com/projecteru2/core/resource/mocks"
plugintypes "github.com/projecteru2/core/resource/plugins/types"
resourcetypes "github.com/projecteru2/core/resource/types"
Expand All @@ -22,10 +20,7 @@ func TestCalculateCapacity(t *testing.T) {
ctx := t.Context()
store := c.store.(*storemocks.Store)

lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)

engine := &enginemocks.API{}
name := "n1"
Expand Down
6 changes: 1 addition & 5 deletions cluster/calcium/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/projecteru2/core/cluster"
enginemocks "github.com/projecteru2/core/engine/mocks"
lockmocks "github.com/projecteru2/core/lock/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
)
Expand Down Expand Up @@ -212,9 +211,6 @@ func newControlTestCluster(t *testing.T) (*Calcium, context.Context, *storemocks
c := NewTestCluster()
ctx := t.Context()
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
return c, ctx, store
}
7 changes: 1 addition & 6 deletions cluster/calcium/copy_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package calcium

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

enginemocks "github.com/projecteru2/core/engine/mocks"
lockmocks "github.com/projecteru2/core/lock/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
)
Expand All @@ -31,10 +29,7 @@ func TestCopy(t *testing.T) {
},
}
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
ch, err := c.Copy(ctx, opts)
assert.NoError(t, err)
Expand Down
9 changes: 3 additions & 6 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,9 @@ func (c *Calcium) doDeployOneWorkload(ctx context.Context, node *types.Node, opt
}
logger.Infof(ctx, "workload %s metadata created", workload.ID)

if len(opts.Files) > 0 {
for _, file := range opts.Files {
if err = c.doSendFileToWorkload(ctx, node.Engine, workload.ID, file); err != nil {
return err
}
for _, file := range opts.Files {
if err = c.doSendFileToWorkload(ctx, node.Engine, workload.ID, file); err != nil {
return err
}
}

Expand Down Expand Up @@ -410,7 +408,6 @@ func (c *Calcium) doMakeWorkloadOptions(ctx context.Context, no int, msg *types.
createOpts := &enginetypes.VirtualizationCreateOptions{}
createOpts.EngineParams = msg.EngineParams
createOpts.RawArgs = opts.RawArgs
createOpts.Lambda = opts.Lambda
createOpts.User = opts.User
createOpts.DNS = opts.DNS
createOpts.Image = opts.Image
Expand Down
11 changes: 2 additions & 9 deletions cluster/calcium/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
resourcemocks "github.com/projecteru2/core/resource/mocks"
plugintypes "github.com/projecteru2/core/resource/plugins/types"
resourcetypes "github.com/projecteru2/core/resource/types"
Expand Down Expand Up @@ -336,10 +335,7 @@ func TestDoDeployWorkloadsOnNodeErrorPerWorkload(t *testing.T) {
store := c.store.(*storemocks.Store)
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(nil, types.ErrMockError)
engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(nil, types.ErrWorkloadNotExists)

Expand Down Expand Up @@ -495,10 +491,7 @@ func newCreateWorkloadCluster(t *testing.T, createProcessingErr, deleteProcessin
store.On("CreateProcessing", mock.Anything, mock.Anything, mock.Anything).Return(createProcessingErr)
store.On("DeleteProcessing", mock.Anything, mock.Anything, mock.Anything).Return(deleteProcessingErr)

lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)

store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil)
store.On("GetNode", mock.Anything, mock.Anything).Return(
Expand Down
6 changes: 1 addition & 5 deletions cluster/calcium/dissociate_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package calcium

import (
"context"
"testing"
"testing/synctest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

lockmocks "github.com/projecteru2/core/lock/mocks"
resourcemocks "github.com/projecteru2/core/resource/mocks"
resourcetypes "github.com/projecteru2/core/resource/types"
storemocks "github.com/projecteru2/core/store/mocks"
Expand All @@ -23,9 +21,7 @@ func TestDissociateWorkload(t *testing.T) {
store := c.store.(*storemocks.Store)
rmgr := c.rmgr.(*resourcemocks.Manager)

lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.Background(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
lock := heldLock(t)

c1 := &types.Workload{
Resources: resourcetypes.Resources{},
Expand Down
3 changes: 1 addition & 2 deletions cluster/calcium/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC
workloadIDs := []string{}

logger := log.WithFunc("calcium.RunAndWait").WithField("app", opts.Name)
opts.Lambda = true
if opts.OpenStdin && (opts.Count != 1 || opts.DeployStrategy != strategy.Auto) {
logger.Errorf(ctx, types.ErrRunAndWaitCountOneWithStdin, "count %d method %s", opts.Count, opts.DeployStrategy)
return workloadIDs, nil, types.ErrRunAndWaitCountOneWithStdin
Expand Down Expand Up @@ -144,7 +143,7 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC
func newEruErrMsg(workloadID, format string, args ...any) *types.AttachWorkloadMessage {
return &types.AttachWorkloadMessage{
WorkloadID: workloadID,
Data: []byte(fmt.Sprintf(format, args...)),
Data: fmt.Appendf(nil, format, args...),
StdStreamType: types.EruError,
}
}
6 changes: 1 addition & 5 deletions cluster/calcium/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
resourcemocks "github.com/projecteru2/core/resource/mocks"
plugintypes "github.com/projecteru2/core/resource/plugins/types"
resourcetypes "github.com/projecteru2/core/resource/types"
Expand Down Expand Up @@ -289,10 +288,7 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) {
store.On("UpdateProcessing", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
store.On("DeleteProcessing", mock.Anything, mock.Anything, mock.Anything).Return(nil)

lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil)
store.On("GetNode",
mock.Anything,
Expand Down
13 changes: 3 additions & 10 deletions cluster/calcium/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ func TestWithNodesPlanLockedTakesPodAndNodeLocksInKeyOrder(t *testing.T) {
{NodeMeta: types.NodeMeta{Name: "c1", Podname: "podb"}, Available: true},
}
store.On("GetNodes", mock.Anything, mock.Anything).Return(nodes, nil)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.Background(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
lock := heldLock(t)
keys := []string{}
store.On("CreateLock", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
keys = append(keys, args.String(0))
Expand All @@ -257,9 +255,7 @@ func TestWithNodesPlanLockedTakesOnlyTheNodeLockForOneCandidate(t *testing.T) {
c := NewTestCluster()
store := c.store.(*storemocks.Store)
store.On("GetNode", mock.Anything, "a1").Return(&types.Node{NodeMeta: types.NodeMeta{Name: "a1", Podname: "poda"}, Available: true}, nil)
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.Background(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
lock := heldLock(t)
keys := []string{}
store.On("CreateLock", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
keys = append(keys, args.String(0))
Expand Down Expand Up @@ -339,10 +335,7 @@ func TestWithNodeOperationLocked(t *testing.T) {
},
Available: true,
}
lock := &lockmocks.DistributedLock{}
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Unlock", mock.Anything).Return(nil)
lock.On("Lock", mock.Anything).Return(context.Background(), nil)
mockLocks(t, store)
store.On("GetNode", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
err := c.withNodeOperationLocked(ctx, "test", func(ctx context.Context, node *types.Node) error { return nil })
assert.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Calcium) ListNetworks(ctx context.Context, podname, driver string) ([]*
drivers = append(drivers, driver)
}

// every node of a pod reports the same networks
// Every node of a pod reports the same networks
node := nodes[0]

networks, err = node.Engine.NetworkList(ctx, drivers)
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *Calcium) RemoveNode(ctx context.Context, nodename string) error {

_, txnErr := utils.Txn(ctx,
func(ctx context.Context) error {
// a down node has no status key, so peers miss the removal unless one is written first
// A down node has no status key, so peers miss the removal unless one is written first
if err = c.store.SetNodeStatus(ctx, node, 90); err != nil {
logger.Warnf(ctx, "failed to set node status: %s", err)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*typ
if updateErr := c.store.UpdateNodes(ctx, node); updateErr != nil {
return updateErr
}
// capacity refresh is best effort; the store write already succeeded
// Capacity refresh is best effort; the store write already succeeded
if len(opts.Resources) != 0 {
_ = c.refreshResourceInfo(ctx, node)
}
Expand Down
11 changes: 2 additions & 9 deletions cluster/calcium/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/projecteru2/core/engine/factory"
enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
resourcemocks "github.com/projecteru2/core/resource/mocks"
resourcetypes "github.com/projecteru2/core/resource/types"
storemocks "github.com/projecteru2/core/store/mocks"
Expand Down Expand Up @@ -129,10 +128,7 @@ func TestRemoveNode(t *testing.T) {
ctx := t.Context()
store := c.store.(*storemocks.Store)

lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
name := "test"
node := &types.Node{NodeMeta: types.NodeMeta{Name: name}}
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
Expand Down Expand Up @@ -290,10 +286,7 @@ func TestSetNode(t *testing.T) {
assert.Error(t, err)

store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
name := "test"
opts.Nodename = name
node := &types.Node{NodeMeta: types.NodeMeta{Name: name}}
Expand Down
7 changes: 1 addition & 6 deletions cluster/calcium/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package calcium

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

lockmocks "github.com/projecteru2/core/lock/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
)
Expand Down Expand Up @@ -39,10 +37,7 @@ func TestRemovePod(t *testing.T) {
assert.Error(t, c.RemovePod(ctx, ""))

store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
store.On("RemovePod", mock.Anything, mock.Anything).Return(nil)
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything).Return(
[]*types.Node{{NodeMeta: types.NodeMeta{Name: "test"}}}, nil,
Expand Down
7 changes: 1 addition & 6 deletions cluster/calcium/raw_engine_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package calcium

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
)
Expand All @@ -18,10 +16,7 @@ func TestRawEngine(t *testing.T) {
c := NewTestCluster()
ctx := t.Context()
store := c.store.(*storemocks.Store)
lock := &lockmocks.DistributedLock{}
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)
mockLocks(t, store)
workload := &types.Workload{
ID: "id1",
Privileged: true,
Expand Down
Loading
Loading