From 9a8d89e47e2cc2f591f07f0998c079097be9a61f Mon Sep 17 00:00:00 2001 From: zc Date: Fri, 7 Jan 2022 12:50:50 +0800 Subject: [PATCH] fix test and lint --- cluster/calcium/lambda.go | 1 - cluster/calcium/lambda_test.go | 12 ----------- cluster/calcium/wal.go | 19 +---------------- cluster/calcium/wal_test.go | 39 +++++++++++++++++----------------- log/log.go | 2 +- 5 files changed, 21 insertions(+), 52 deletions(-) diff --git a/cluster/calcium/lambda.go b/cluster/calcium/lambda.go index bc62964e4..6af2ed9b3 100644 --- a/cluster/calcium/lambda.go +++ b/cluster/calcium/lambda.go @@ -20,7 +20,6 @@ import ( const ( exitDataPrefix = "[exitcode] " - labelLambdaID = "LambdaID" ) // RunAndWait implement lambda diff --git a/cluster/calcium/lambda_test.go b/cluster/calcium/lambda_test.go index de36d4267..322240899 100644 --- a/cluster/calcium/lambda_test.go +++ b/cluster/calcium/lambda_test.go @@ -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" @@ -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", @@ -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) @@ -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) } diff --git a/cluster/calcium/wal.go b/cluster/calcium/wal.go index 29ebdda90..84fb8f2cf 100644 --- a/cluster/calcium/wal.go +++ b/cluster/calcium/wal.go @@ -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 } @@ -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) } diff --git a/cluster/calcium/wal_test.go b/cluster/calcium/wal_test.go index dca2a14f2..bda4244e0 100644 --- a/cluster/calcium/wal_test.go +++ b/cluster/calcium/wal_test.go @@ -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" @@ -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{ @@ -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) @@ -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) } diff --git a/log/log.go b/log/log.go index ad7f69f4a..0c2d55643 100644 --- a/log/log.go +++ b/log/log.go @@ -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...)