github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/pod_test.go (about)

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	lockmocks "github.com/projecteru2/core/lock/mocks"
     8  	storemocks "github.com/projecteru2/core/store/mocks"
     9  	"github.com/projecteru2/core/types"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  )
    14  
    15  func TestAddPod(t *testing.T) {
    16  	c := NewTestCluster()
    17  	ctx := context.Background()
    18  
    19  	_, err := c.AddPod(ctx, "", "")
    20  	assert.Error(t, err)
    21  
    22  	name := "test"
    23  	pod := &types.Pod{
    24  		Name: name,
    25  	}
    26  
    27  	store := c.store.(*storemocks.Store)
    28  	store.On("AddPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(pod, nil)
    29  
    30  	p, err := c.AddPod(ctx, name, "")
    31  	assert.NoError(t, err)
    32  	assert.Equal(t, p.Name, name)
    33  }
    34  
    35  func TestRemovePod(t *testing.T) {
    36  	c := NewTestCluster()
    37  	ctx := context.Background()
    38  
    39  	// failed by validating
    40  	assert.Error(t, c.RemovePod(ctx, ""))
    41  
    42  	store := c.store.(*storemocks.Store)
    43  	lock := &lockmocks.DistributedLock{}
    44  	lock.On("Lock", mock.Anything).Return(ctx, nil)
    45  	lock.On("Unlock", mock.Anything).Return(nil)
    46  	store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
    47  	store.On("RemovePod", mock.Anything, mock.Anything).Return(nil)
    48  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(
    49  		[]*types.Node{{NodeMeta: types.NodeMeta{Name: "test"}}}, nil)
    50  
    51  	assert.NoError(t, c.RemovePod(ctx, "podname"))
    52  	store.AssertExpectations(t)
    53  }
    54  
    55  func TestGetPod(t *testing.T) {
    56  	c := NewTestCluster()
    57  	ctx := context.Background()
    58  
    59  	_, err := c.GetPod(ctx, "")
    60  	assert.Error(t, err)
    61  
    62  	name := "test"
    63  	pod := &types.Pod{Name: name}
    64  	store := c.store.(*storemocks.Store)
    65  	store.On("GetPod", mock.Anything, mock.Anything).Return(pod, nil)
    66  
    67  	p, err := c.GetPod(ctx, name)
    68  	assert.NoError(t, err)
    69  	assert.Equal(t, p.Name, name)
    70  }
    71  
    72  func TestListPods(t *testing.T) {
    73  	c := NewTestCluster()
    74  	ctx := context.Background()
    75  
    76  	name := "test"
    77  	pods := []*types.Pod{
    78  		{Name: name},
    79  	}
    80  
    81  	store := c.store.(*storemocks.Store)
    82  	store.On("GetAllPods", mock.Anything).Return(pods, nil)
    83  
    84  	ps, err := c.ListPods(ctx)
    85  	assert.NoError(t, err)
    86  	assert.Equal(t, len(ps), 1)
    87  	assert.Equal(t, ps[0].Name, name)
    88  }