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

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	enginemocks "github.com/projecteru2/core/engine/mocks"
     8  	enginetypes "github.com/projecteru2/core/engine/types"
     9  	resourcemocks "github.com/projecteru2/core/resource/mocks"
    10  	storemocks "github.com/projecteru2/core/store/mocks"
    11  	"github.com/projecteru2/core/types"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/mock"
    15  )
    16  
    17  func TestListNetworks(t *testing.T) {
    18  	c := NewTestCluster()
    19  	ctx := context.Background()
    20  
    21  	store := c.store.(*storemocks.Store)
    22  
    23  	// failed by GetNodesByPod
    24  	store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    25  	_, err := c.ListNetworks(ctx, "", "")
    26  	assert.Error(t, err)
    27  
    28  	// No nodes
    29  	store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
    30  	_, err = c.ListNetworks(ctx, "", "")
    31  	assert.Error(t, err)
    32  
    33  	// vaild
    34  	name := "test"
    35  	engine := &enginemocks.API{}
    36  	engine.On("NetworkList", mock.Anything, mock.Anything).Return([]*enginetypes.Network{{Name: name}}, nil)
    37  	node := &types.Node{
    38  		NodeMeta: types.NodeMeta{
    39  			Name: name,
    40  		},
    41  		Available: true,
    42  		Engine:    engine,
    43  	}
    44  	rmgr := c.rmgr.(*resourcemocks.Manager)
    45  	rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil)
    46  	store.On("GetNodesByPod", mock.AnythingOfType("*context.emptyCtx"), mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{node}, nil)
    47  	ns, err := c.ListNetworks(ctx, "", "xx")
    48  	assert.NoError(t, err)
    49  	assert.Equal(t, len(ns), 1)
    50  	assert.Equal(t, ns[0].Name, name)
    51  	store.AssertExpectations(t)
    52  }
    53  
    54  func TestConnectNetwork(t *testing.T) {
    55  	c := NewTestCluster()
    56  	ctx := context.Background()
    57  
    58  	store := c.store.(*storemocks.Store)
    59  
    60  	// failed by GetWorkload
    61  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    62  	_, err := c.ConnectNetwork(ctx, "network", "123", "", "")
    63  	assert.Error(t, err)
    64  
    65  	// success
    66  	engine := &enginemocks.API{}
    67  	engine.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil)
    68  	workload := &types.Workload{Engine: engine}
    69  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
    70  	_, err = c.ConnectNetwork(ctx, "network", "123", "", "")
    71  	assert.NoError(t, err)
    72  	store.AssertExpectations(t)
    73  }
    74  
    75  func TestDisConnectNetwork(t *testing.T) {
    76  	c := NewTestCluster()
    77  	ctx := context.Background()
    78  
    79  	store := c.store.(*storemocks.Store)
    80  
    81  	// failed by GetWorkload
    82  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    83  	assert.Error(t, c.DisconnectNetwork(ctx, "network", "123", true))
    84  
    85  	// success
    86  	engine := &enginemocks.API{}
    87  	engine.On("NetworkDisconnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
    88  	workload := &types.Workload{Engine: engine}
    89  	store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
    90  	assert.NoError(t, c.DisconnectNetwork(ctx, "network", "123", true))
    91  	store.AssertExpectations(t)
    92  }