Skip to content

Commit

Permalink
fix test and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 7, 2022
1 parent 69cc755 commit 9a8d89e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 52 deletions.
1 change: 0 additions & 1 deletion cluster/calcium/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

const (
exitDataPrefix = "[exitcode] "
labelLambdaID = "LambdaID"
)

// RunAndWait implement lambda
Expand Down
12 changes: 0 additions & 12 deletions cluster/calcium/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/strategy"
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/wal"
walmocks "github.com/projecteru2/core/wal/mocks"

"github.com/stretchr/testify/assert"
Expand All @@ -31,12 +30,6 @@ func TestRunAndWaitFailedThenWALCommitted(t *testing.T) {

mwal := c.wal.WAL.(*walmocks.WAL)
defer mwal.AssertExpectations(t)
var walCommitted bool
commit := wal.Commit(func() error {
walCommitted = true
return nil
})
mwal.On("Log", eventCreateLambda, mock.Anything).Return(commit, nil).Once()

opts := &types.DeployOptions{
Name: "zc:name",
Expand All @@ -56,7 +49,6 @@ func TestRunAndWaitFailedThenWALCommitted(t *testing.T) {
_, ch, err := c.RunAndWait(context.Background(), opts, make(chan []byte))
assert.NoError(err)
assert.NotNil(ch)
assert.False(walCommitted)
ms := []*types.AttachWorkloadMessage{}
for m := range ch {
ms = append(ms, m)
Expand All @@ -65,10 +57,6 @@ func TestRunAndWaitFailedThenWALCommitted(t *testing.T) {
assert.Equal(m.WorkloadID, "")
assert.True(strings.HasPrefix(string(m.Data), "Create workload failed"))

lambdaID, exists := opts.Labels[labelLambdaID]
assert.True(exists)
assert.True(len(lambdaID) > 1)
assert.True(walCommitted)
assert.Equal(m.StdStreamType, types.EruError)
}

Expand Down
19 changes: 1 addition & 18 deletions cluster/calcium/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (h *CreateLambdaHandler) Handle(ctx context.Context, raw interface{}) error
logger.Infof(ctx, "recovery start")
workload, err := h.calcium.GetWorkload(ctx, workloadID)
if err != nil {
logger.Errorf(nil, "Get workload failed: %v", err)
logger.Errorf(ctx, "Get workload failed: %v", err)
return
}

Expand All @@ -221,23 +221,6 @@ func (h *CreateLambdaHandler) Handle(ctx context.Context, raw interface{}) error
return nil
}

func (h *CreateLambdaHandler) getWorkloadIDs(ctx context.Context, opts *types.ListWorkloadsOptions) ([]string, error) {
ctx, cancel := getReplayContext(ctx)
defer cancel()

workloads, err := h.calcium.ListWorkloads(ctx, opts)
if err != nil {
return nil, err
}

workloadIDs := make([]string, len(workloads))
for i, wrk := range workloads {
workloadIDs[i] = wrk.ID
}

return workloadIDs, nil
}

func getReplayContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(ctx, time.Second*32)
}
39 changes: 19 additions & 20 deletions cluster/calcium/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"testing"
"time"

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 Down Expand Up @@ -131,12 +133,7 @@ func TestHandleCreateLambda(t *testing.T) {
require.NoError(t, err)
c.wal = wal

deployOpts := &types.DeployOptions{
Name: "appname",
Entrypoint: &types.Entrypoint{Name: "entry"},
Labels: map[string]string{labelLambdaID: "lambda"},
}
_, err = c.wal.logCreateLambda(deployOpts)
_, err = c.wal.logCreateLambda(&types.CreateWorkloadMessage{WorkloadID: "workloadid"})
require.NoError(t, err)

node := &types.Node{
Expand All @@ -150,35 +147,34 @@ func TestHandleCreateLambda(t *testing.T) {
}

store := c.store.(*storemocks.Store)
defer store.AssertExpectations(t)
store.On("ListWorkloads", mock.Anything, deployOpts.Name, deployOpts.Entrypoint.Name, "", int64(0), deployOpts.Labels).
Return(nil, fmt.Errorf("err")).
Once()
store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD)
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
c.wal.Recover(context.TODO())
time.Sleep(500 * time.Millisecond)
store.AssertExpectations(t)

store.On("ListWorkloads", mock.Anything, deployOpts.Name, deployOpts.Entrypoint.Name, "", int64(0), deployOpts.Labels).
Return([]*types.Workload{wrk}, nil).
_, err = c.wal.logCreateLambda(&types.CreateWorkloadMessage{WorkloadID: "workloadid"})
require.NoError(t, err)
store.On("GetWorkload", mock.Anything, mock.Anything).
Return(wrk, nil).
Once()
store.On("GetWorkloads", mock.Anything, []string{wrk.ID}).
Return([]*types.Workload{wrk}, nil).
Twice()
store.On("GetNode", mock.Anything, wrk.Nodename).
Return(node, nil)

eng := wrk.Engine.(*enginemocks.API)
defer eng.AssertExpectations(t)
eng.On("VirtualizationWait", mock.Anything, wrk.ID, "").Return(&enginetypes.VirtualizationWaitResult{Code: 0}, nil).Once()
eng.On("VirtualizationRemove", mock.Anything, wrk.ID, true, true).
Return(nil).
Once()

eng.On("VirtualizationResourceRemap", mock.Anything, mock.Anything).Return(nil, nil).Once()
store.On("GetWorkloads", mock.Anything, []string{wrk.ID}).
Return([]*types.Workload{wrk}, nil).
Twice()
store.On("RemoveWorkload", mock.Anything, wrk).
Return(nil).
Once()
store.On("UpdateNodeResource", mock.Anything, node, mock.Anything, mock.Anything).
Return(nil).
Once()

store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil).Once()
lock := &lockmocks.DistributedLock{}
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
Expand All @@ -187,4 +183,7 @@ func TestHandleCreateLambda(t *testing.T) {
c.wal.Recover(context.TODO())
// Recovered nothing.
c.wal.Recover(context.TODO())
time.Sleep(500 * time.Millisecond)
store.AssertExpectations(t)
eng.AssertExpectations(t)
}
2 changes: 1 addition & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (f Fields) Err(ctx context.Context, err error) error {
return err
}

// Infof
// Infof .
func (f Fields) Infof(ctx context.Context, format string, args ...interface{}) {
format = getTracingInfo(ctx) + format
f.e.Infof(format, args...)
Expand Down

0 comments on commit 9a8d89e

Please sign in to comment.