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

     1  package calcium
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io"
     7  	"testing"
     8  
     9  	enginemocks "github.com/projecteru2/core/engine/mocks"
    10  	enginetypes "github.com/projecteru2/core/engine/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 TestCacheImage(t *testing.T) {
    19  	c := NewTestCluster()
    20  	ctx := context.Background()
    21  	store := c.store.(*storemocks.Store)
    22  	// fail by validating
    23  	_, err := c.CacheImage(ctx, &types.ImageOptions{Podname: ""})
    24  	assert.Error(t, err)
    25  	// fail by get nodes
    26  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    27  	_, err = c.CacheImage(ctx, &types.ImageOptions{Podname: "podname"})
    28  	assert.Error(t, err)
    29  	// fail 0 nodes
    30  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
    31  	_, err = c.CacheImage(ctx, &types.ImageOptions{Podname: "podname"})
    32  	assert.Error(t, err)
    33  	engine := &enginemocks.API{}
    34  	nodes := []*types.Node{
    35  		{
    36  			NodeMeta: types.NodeMeta{
    37  				Name: "test",
    38  			},
    39  			Engine: engine,
    40  		},
    41  	}
    42  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nodes, nil)
    43  	// fail by ImageRemoteDigest
    44  	engine.On("ImageLocalDigests", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    45  	engine.On("ImageRemoteDigest", mock.Anything, mock.Anything).Return("", types.ErrMockError).Once()
    46  	engine.On("ImagePull", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    47  	ch, err := c.CacheImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}})
    48  	for c := range ch {
    49  		assert.False(t, c.Success)
    50  	}
    51  	// succ
    52  	engine.On("ImageRemoteDigest", mock.Anything, mock.Anything).Return("yy", nil)
    53  	engine.On("ImageLocalDigests", mock.Anything, mock.Anything).Return([]string{"xx"}, nil)
    54  	engine.On("ImagePull", mock.Anything, mock.Anything, mock.Anything).Return(io.NopCloser(bytes.NewReader([]byte{})), nil)
    55  	ch, err = c.CacheImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}})
    56  	for c := range ch {
    57  		assert.True(t, c.Success)
    58  	}
    59  	store.AssertExpectations(t)
    60  	engine.AssertExpectations(t)
    61  }
    62  
    63  func TestRemoveImage(t *testing.T) {
    64  	c := NewTestCluster()
    65  	ctx := context.Background()
    66  	store := c.store.(*storemocks.Store)
    67  	// fail by validating
    68  	_, err := c.RemoveImage(ctx, &types.ImageOptions{Podname: ""})
    69  	assert.Error(t, err)
    70  	// fail by get nodes
    71  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    72  	_, err = c.RemoveImage(ctx, &types.ImageOptions{Podname: "podname"})
    73  	assert.Error(t, err)
    74  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
    75  	// fail 0 nodes
    76  	_, err = c.RemoveImage(ctx, &types.ImageOptions{Podname: "podname"})
    77  	assert.Error(t, err)
    78  	engine := &enginemocks.API{}
    79  	nodes := []*types.Node{
    80  		{
    81  			NodeMeta: types.NodeMeta{
    82  				Name: "test",
    83  			},
    84  			Engine: engine,
    85  		},
    86  	}
    87  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nodes, nil)
    88  	// fail remove
    89  	engine.On("ImageRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
    90  	ch, err := c.RemoveImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}})
    91  	for c := range ch {
    92  		assert.False(t, c.Success)
    93  	}
    94  	engine.On("ImageRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{"xx"}, nil)
    95  	// success remove but prune fail
    96  	engine.On("ImagesPrune", mock.Anything).Return(types.ErrMockError).Once()
    97  	ch, err = c.RemoveImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}, Prune: true})
    98  	for c := range ch {
    99  		assert.True(t, c.Success)
   100  	}
   101  	engine.On("ImagesPrune", mock.Anything).Return(nil)
   102  	ch, err = c.RemoveImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}, Prune: true})
   103  	for c := range ch {
   104  		assert.True(t, c.Success)
   105  	}
   106  	store.AssertExpectations(t)
   107  	engine.AssertExpectations(t)
   108  }
   109  
   110  func TestListImage(t *testing.T) {
   111  	c := NewTestCluster()
   112  	ctx := context.Background()
   113  	store := c.store.(*storemocks.Store)
   114  	// fail by get nodes
   115  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
   116  	_, err := c.ListImage(ctx, &types.ImageOptions{Podname: "podname"})
   117  	assert.Error(t, err)
   118  	// fail 0 nodes
   119  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once()
   120  	_, err = c.ListImage(ctx, &types.ImageOptions{Podname: "podname"})
   121  	assert.Error(t, err)
   122  	engine := &enginemocks.API{}
   123  	nodes := []*types.Node{
   124  		{
   125  			NodeMeta: types.NodeMeta{
   126  				Name: "test",
   127  			},
   128  			Engine: engine,
   129  		},
   130  	}
   131  	store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nodes, nil)
   132  	// fail by ImageList
   133  	engine.On("ImageList", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once()
   134  	ch, err := c.ListImage(ctx, &types.ImageOptions{Podname: "podname"})
   135  	msg := <-ch
   136  	assert.Error(t, msg.Error)
   137  	// success
   138  	engine.On("ImageList", mock.Anything, mock.Anything).Return(
   139  		[]*enginetypes.Image{{ID: "123"}}, nil,
   140  	)
   141  	ch, err = c.ListImage(ctx, &types.ImageOptions{Podname: "podname"})
   142  	msg = <-ch
   143  	assert.Equal(t, msg.Images[0].ID, "123")
   144  	store.AssertExpectations(t)
   145  	engine.AssertExpectations(t)
   146  }