github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/resource_test.go (about) 1 package calcium 2 3 import ( 4 "context" 5 "testing" 6 7 enginemocks "github.com/projecteru2/core/engine/mocks" 8 lockmocks "github.com/projecteru2/core/lock/mocks" 9 resourcemocks "github.com/projecteru2/core/resource/mocks" 10 resourcetypes "github.com/projecteru2/core/resource/types" 11 storemocks "github.com/projecteru2/core/store/mocks" 12 "github.com/projecteru2/core/types" 13 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/mock" 16 ) 17 18 func TestPodResource(t *testing.T) { 19 c := NewTestCluster() 20 ctx := context.Background() 21 podname := "testpod" 22 nodename := "testnode" 23 store := c.store.(*storemocks.Store) 24 rmgr := c.rmgr.(*resourcemocks.Manager) 25 lock := &lockmocks.DistributedLock{} 26 lock.On("Lock", mock.Anything).Return(ctx, nil) 27 lock.On("Unlock", mock.Anything).Return(nil) 28 29 // failed by GetNodesByPod 30 store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once() 31 ch, err := c.PodResource(ctx, podname) 32 assert.Error(t, err) 33 store.AssertExpectations(t) 34 node := &types.Node{ 35 NodeMeta: types.NodeMeta{ 36 Name: nodename, 37 }, 38 } 39 store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{node}, nil) 40 store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil) 41 store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) 42 43 // failed by ListNodeWorkloads 44 store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once() 45 ch, err = c.PodResource(ctx, podname) 46 assert.NoError(t, err) 47 msg := <-ch 48 assert.Equal(t, msg.Name, nodename) 49 assert.NotEmpty(t, msg.Diffs) 50 store.AssertExpectations(t) 51 workloads := []*types.Workload{ 52 {Resources: resourcetypes.Resources{}}, 53 {Resources: resourcetypes.Resources{}}, 54 } 55 store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) 56 57 // failed by GetNodeResourceInfo 58 rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( 59 nil, nil, nil, types.ErrMockError).Once() 60 ch, err = c.PodResource(ctx, podname) 61 msg = <-ch 62 assert.NoError(t, err) 63 assert.Equal(t, msg.Name, nodename) 64 assert.NotEmpty(t, msg.Diffs) 65 store.AssertExpectations(t) 66 rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( 67 resourcetypes.Resources{"test": {"abc": 123}}, 68 resourcetypes.Resources{"test": {"abc": 123}}, 69 []string{}, 70 nil) 71 72 // success 73 ch, err = c.PodResource(ctx, podname) 74 msg = <-ch 75 assert.NoError(t, err) 76 assert.Equal(t, msg.Name, nodename) 77 assert.Empty(t, msg.Diffs) 78 store.AssertExpectations(t) 79 } 80 81 func TestNodeResource(t *testing.T) { 82 c := NewTestCluster() 83 ctx := context.Background() 84 nodename := "testnode" 85 store := c.store.(*storemocks.Store) 86 rmgr := c.rmgr.(*resourcemocks.Manager) 87 lock := &lockmocks.DistributedLock{} 88 store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) 89 lock.On("Lock", mock.Anything).Return(ctx, nil) 90 lock.On("Unlock", mock.Anything).Return(nil) 91 92 node := &types.Node{ 93 NodeMeta: types.NodeMeta{ 94 Name: nodename, 95 }, 96 } 97 engine := &enginemocks.API{} 98 store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil) 99 store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) 100 101 rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( 102 resourcetypes.Resources{"test": {"abc": 123}}, 103 resourcetypes.Resources{"test": {"abc": 123}}, 104 []string{}, 105 nil) 106 107 workloads := []*types.Workload{ 108 {Resources: resourcetypes.Resources{}, Engine: engine}, 109 {Resources: resourcetypes.Resources{}, Engine: engine}, 110 } 111 store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) 112 engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(nil, types.ErrMockError) 113 114 nr, err := c.NodeResource(ctx, nodename, true) 115 assert.NoError(t, err) 116 assert.Equal(t, nr.Name, nodename) 117 assert.NotEmpty(t, nr.Diffs) 118 store.AssertExpectations(t) 119 }