github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/workload_test.go (about) 1 package calcium 2 3 import ( 4 "context" 5 "testing" 6 7 storemocks "github.com/projecteru2/core/store/mocks" 8 "github.com/projecteru2/core/types" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/mock" 12 ) 13 14 func TestGetWorkload(t *testing.T) { 15 c := NewTestCluster() 16 ctx := context.Background() 17 18 ID := "testID" 19 workload := &types.Workload{ID: ID} 20 store := c.store.(*storemocks.Store) 21 store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil) 22 _, err := c.GetWorkload(ctx, "") 23 assert.Error(t, err) 24 25 _, err = c.GetWorkload(ctx, ID) 26 assert.NoError(t, err) 27 28 } 29 30 func TestGetWorkloads(t *testing.T) { 31 c := NewTestCluster() 32 ctx := context.Background() 33 ID := "testID" 34 workload := &types.Workload{ID: ID} 35 workloads := []*types.Workload{workload} 36 37 store := c.store.(*storemocks.Store) 38 store.On("GetWorkloads", mock.Anything, mock.Anything).Return(workloads, nil) 39 40 cs, err := c.GetWorkloads(ctx, []string{}) 41 assert.NoError(t, err) 42 assert.Equal(t, len(cs), 1) 43 assert.Equal(t, cs[0].ID, ID) 44 } 45 46 func TestListWorkloads(t *testing.T) { 47 c := NewTestCluster() 48 ctx := context.Background() 49 ID := "testID" 50 workloads := []*types.Workload{ 51 {ID: ID}, 52 } 53 54 // failed by ListWorkloads 55 store := c.store.(*storemocks.Store) 56 store.On("ListWorkloads", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once() 57 _, err := c.ListWorkloads(ctx, &types.ListWorkloadsOptions{Appname: "", Entrypoint: "", Nodename: ""}) 58 assert.Error(t, err) 59 60 // success 61 store.On("ListWorkloads", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) 62 cs, err := c.ListWorkloads(ctx, &types.ListWorkloadsOptions{Appname: "", Entrypoint: "", Nodename: ""}) 63 assert.NoError(t, err) 64 assert.Equal(t, len(cs), 1) 65 assert.Equal(t, cs[0].ID, ID) 66 } 67 68 func TestListNodeWorkloads(t *testing.T) { 69 c := NewTestCluster() 70 ctx := context.Background() 71 ID := "testID" 72 workloads := []*types.Workload{ 73 {ID: ID}, 74 } 75 76 _, err := c.ListNodeWorkloads(ctx, "", nil) 77 assert.Error(t, err) 78 79 store := c.store.(*storemocks.Store) 80 store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) 81 cs, err := c.ListNodeWorkloads(ctx, "nodename", nil) 82 assert.NoError(t, err) 83 assert.Equal(t, len(cs), 1) 84 assert.Equal(t, cs[0].ID, ID) 85 }