github.com/kubeshop/testkube@v1.17.23/pkg/imageinspector/configmapstorage_test.go (about)

     1  package imageinspector
     2  
     3  import (
     4  	"context"
     5  	"maps"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/stretchr/testify/assert"
    10  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    11  	"k8s.io/apimachinery/pkg/runtime/schema"
    12  
    13  	"github.com/kubeshop/testkube/pkg/configmap"
    14  )
    15  
    16  func mustMarshalInfo(v Info) string {
    17  	s, e := marshalInfo(v)
    18  	if e != nil {
    19  		panic(e)
    20  	}
    21  	return s
    22  }
    23  
    24  func TestConfigMapStorageGet(t *testing.T) {
    25  	ctrl := gomock.NewController(t)
    26  	client := configmap.NewMockInterface(ctrl)
    27  	m := NewConfigMapStorage(client, "dummy", false)
    28  	value := map[string]string{
    29  		string(hash(req1.Registry, req1.Image)): mustMarshalInfo(info1),
    30  		string(hash(req2.Registry, req2.Image)): mustMarshalInfo(info2),
    31  	}
    32  
    33  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
    34  
    35  	v1, err1 := m.Get(context.Background(), req1)
    36  	assert.NoError(t, err1)
    37  	assert.Equal(t, &info1, v1)
    38  }
    39  
    40  func TestConfigMapStorageGetEmpty(t *testing.T) {
    41  	ctrl := gomock.NewController(t)
    42  	client := configmap.NewMockInterface(ctrl)
    43  	m := NewConfigMapStorage(client, "dummy", false)
    44  
    45  	client.EXPECT().Get(gomock.Any(), "dummy").
    46  		Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "dummy"))
    47  
    48  	v1, err1 := m.Get(context.Background(), req1)
    49  	assert.NoError(t, err1)
    50  	assert.Equal(t, noInfoPtr, v1)
    51  }
    52  
    53  func TestConfigMapStorageStore(t *testing.T) {
    54  	ctrl := gomock.NewController(t)
    55  	client := configmap.NewMockInterface(ctrl)
    56  	m := NewConfigMapStorage(client, "dummy", false)
    57  	value := map[string]string{
    58  		string(hash(req1.Registry, req1.Image)): mustMarshalInfo(info1),
    59  	}
    60  	expected := map[string]string{
    61  		string(hash(req2.Registry, req2.Image)): mustMarshalInfo(info2),
    62  	}
    63  	maps.Copy(expected, value)
    64  
    65  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
    66  	client.EXPECT().Apply(gomock.Any(), "dummy", expected).Return(nil)
    67  
    68  	err1 := m.Store(context.Background(), req2, info2)
    69  	assert.NoError(t, err1)
    70  }
    71  
    72  func TestConfigMapStorageStoreTooLarge(t *testing.T) {
    73  	ctrl := gomock.NewController(t)
    74  	client := configmap.NewMockInterface(ctrl)
    75  	m := NewConfigMapStorage(client, "dummy", false)
    76  	value := map[string]string{
    77  		string(hash(req1.Registry, req1.Image)):     mustMarshalInfo(info1),
    78  		string(hash(req1.Registry+"A", req1.Image)): mustMarshalInfo(info1),
    79  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
    80  		string(hash(req2.Registry+"A", req2.Image)): mustMarshalInfo(info2),
    81  	}
    82  	initial := map[string]string{
    83  		string(hash(req1.Registry, req1.Image)):     mustMarshalInfo(info1),
    84  		string(hash(req1.Registry+"A", req1.Image)): mustMarshalInfo(info1),
    85  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
    86  		string(hash(req2.Registry+"A", req2.Image)): mustMarshalInfo(info2),
    87  		string(hash(req3.Registry, req3.Image)):     mustMarshalInfo(info3),
    88  	}
    89  	expected := map[string]string{
    90  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
    91  		string(hash(req2.Registry+"A", req2.Image)): mustMarshalInfo(info2),
    92  		string(hash(req3.Registry, req3.Image)):     mustMarshalInfo(info3),
    93  	}
    94  
    95  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
    96  	client.EXPECT().Apply(gomock.Any(), "dummy", initial).Return(k8serrors.NewRequestEntityTooLargeError("test"))
    97  	client.EXPECT().Apply(gomock.Any(), "dummy", expected).Return(nil)
    98  
    99  	err1 := m.Store(context.Background(), req3, info3)
   100  	assert.NoError(t, err1)
   101  }
   102  
   103  func TestConfigMapStorageStoreMany(t *testing.T) {
   104  	ctrl := gomock.NewController(t)
   105  	client := configmap.NewMockInterface(ctrl)
   106  	m := NewConfigMapStorage(client, "dummy", false)
   107  	value := map[string]string{
   108  		string(hash(req1.Registry, req1.Image)): mustMarshalInfo(info1),
   109  	}
   110  	expected := map[string]string{
   111  		string(hash(req2.Registry, req2.Image)): mustMarshalInfo(info2),
   112  		string(hash(req3.Registry, req3.Image)): mustMarshalInfo(info3),
   113  	}
   114  	maps.Copy(expected, value)
   115  
   116  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
   117  	client.EXPECT().Apply(gomock.Any(), "dummy", expected).Return(nil)
   118  
   119  	err1 := m.StoreMany(context.Background(), map[Hash]Info{
   120  		hash(req2.Registry, req2.Image): info2,
   121  		hash(req3.Registry, req3.Image): info3,
   122  	})
   123  	assert.NoError(t, err1)
   124  }
   125  
   126  func TestConfigMapStorageStoreManyTooLarge(t *testing.T) {
   127  	ctrl := gomock.NewController(t)
   128  	client := configmap.NewMockInterface(ctrl)
   129  	m := NewConfigMapStorage(client, "dummy", false)
   130  	value := map[string]string{
   131  		string(hash(req1.Registry, req1.Image)):     mustMarshalInfo(info1),
   132  		string(hash(req1.Registry+"A", req1.Image)): mustMarshalInfo(info1),
   133  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
   134  	}
   135  	initial := map[string]string{
   136  		string(hash(req1.Registry, req1.Image)):     mustMarshalInfo(info1),
   137  		string(hash(req1.Registry+"A", req1.Image)): mustMarshalInfo(info1),
   138  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
   139  		string(hash(req2.Registry+"A", req2.Image)): mustMarshalInfo(info2),
   140  		string(hash(req3.Registry, req3.Image)):     mustMarshalInfo(info3),
   141  	}
   142  	expected := map[string]string{
   143  		string(hash(req2.Registry, req2.Image)):     mustMarshalInfo(info2),
   144  		string(hash(req2.Registry+"A", req2.Image)): mustMarshalInfo(info2),
   145  		string(hash(req3.Registry, req3.Image)):     mustMarshalInfo(info3),
   146  	}
   147  
   148  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
   149  	client.EXPECT().Apply(gomock.Any(), "dummy", initial).Return(k8serrors.NewRequestEntityTooLargeError("test"))
   150  	client.EXPECT().Apply(gomock.Any(), "dummy", expected).Return(nil)
   151  
   152  	err1 := m.StoreMany(context.Background(), map[Hash]Info{
   153  		hash(req2.Registry+"A", req2.Image): info2,
   154  		hash(req3.Registry, req3.Image):     info3,
   155  	})
   156  	assert.NoError(t, err1)
   157  }
   158  
   159  func TestConfigMapStorageCopyTo(t *testing.T) {
   160  	ctrl := gomock.NewController(t)
   161  	client := configmap.NewMockInterface(ctrl)
   162  	s := NewMockStorageWithTransfer(ctrl)
   163  	m := NewConfigMapStorage(client, "dummy", false)
   164  	value := map[string]string{
   165  		string(hash(req1.Registry, req1.Image)): mustMarshalInfo(info1),
   166  		string(hash(req2.Registry, req2.Image)): mustMarshalInfo(info2),
   167  		string(hash(req3.Registry, req3.Image)): mustMarshalInfo(info3),
   168  	}
   169  	expected := map[Hash]Info{
   170  		hash(req1.Registry, req1.Image): info1,
   171  		hash(req2.Registry, req2.Image): info2,
   172  		hash(req3.Registry, req3.Image): info3,
   173  	}
   174  	client.EXPECT().Get(gomock.Any(), "dummy").Return(value, nil)
   175  	s.EXPECT().StoreMany(gomock.Any(), expected).Return(nil)
   176  
   177  	err1 := m.CopyTo(context.Background(), s)
   178  	assert.NoError(t, err1)
   179  }