github.com/argoproj/argo-cd/v3@v3.2.1/reposerver/cache/mocks/reposervercache.go (about)

     1  package mocks
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/alicebob/miniredis/v2"
     8  	"github.com/redis/go-redis/v9"
     9  	"github.com/stretchr/testify/mock"
    10  
    11  	cacheutil "github.com/argoproj/argo-cd/v3/util/cache"
    12  	cacheutilmocks "github.com/argoproj/argo-cd/v3/util/cache/mocks"
    13  )
    14  
    15  type MockCacheType int
    16  
    17  const (
    18  	MockCacheTypeRedis MockCacheType = iota
    19  	MockCacheTypeInMem
    20  )
    21  
    22  type MockRepoCache struct {
    23  	mock.Mock
    24  	RedisClient       *cacheutilmocks.MockCacheClient
    25  	StopRedisCallback func()
    26  }
    27  
    28  type MockCacheOptions struct {
    29  	RepoCacheExpiration     time.Duration
    30  	RevisionCacheExpiration time.Duration
    31  	ReadDelay               time.Duration
    32  	WriteDelay              time.Duration
    33  }
    34  
    35  type CacheCallCounts struct {
    36  	ExternalSets    int
    37  	ExternalGets    int
    38  	ExternalDeletes int
    39  	ExternalRenames int
    40  }
    41  
    42  // Checks that the cache was called the expected number of times
    43  func (mockCache *MockRepoCache) AssertCacheCalledTimes(t *testing.T, calls *CacheCallCounts) {
    44  	t.Helper()
    45  	mockCache.RedisClient.AssertNumberOfCalls(t, "Get", calls.ExternalGets)
    46  	mockCache.RedisClient.AssertNumberOfCalls(t, "Set", calls.ExternalSets)
    47  	mockCache.RedisClient.AssertNumberOfCalls(t, "Delete", calls.ExternalDeletes)
    48  	mockCache.RedisClient.AssertNumberOfCalls(t, "Rename", calls.ExternalRenames)
    49  }
    50  
    51  func (mockCache *MockRepoCache) ConfigureDefaultCallbacks() {
    52  	mockCache.RedisClient.On("Get", mock.Anything, mock.Anything).Return(nil)
    53  	mockCache.RedisClient.On("Set", mock.Anything).Return(nil)
    54  	mockCache.RedisClient.On("Delete", mock.Anything).Return(nil)
    55  	mockCache.RedisClient.On("Rename", mock.Anything, mock.Anything, mock.Anything).Return(nil)
    56  }
    57  
    58  func NewInMemoryRedis() (*redis.Client, func()) {
    59  	cacheutil.NewInMemoryCache(5 * time.Second)
    60  	mr, err := miniredis.Run()
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	return redis.NewClient(&redis.Options{Addr: mr.Addr()}), mr.Close
    65  }
    66  
    67  func NewMockRepoCache(cacheOpts *MockCacheOptions) *MockRepoCache {
    68  	redisClient, stopRedis := NewInMemoryRedis()
    69  	redisCacheClient := &cacheutilmocks.MockCacheClient{
    70  		ReadDelay:  cacheOpts.ReadDelay,
    71  		WriteDelay: cacheOpts.WriteDelay,
    72  		BaseCache:  cacheutil.NewRedisCache(redisClient, cacheOpts.RepoCacheExpiration, cacheutil.RedisCompressionNone),
    73  	}
    74  	newMockCache := &MockRepoCache{RedisClient: redisCacheClient, StopRedisCallback: stopRedis}
    75  	newMockCache.ConfigureDefaultCallbacks()
    76  	return newMockCache
    77  }