mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai
Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).
CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.
Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).
CGO_ENABLED=0 go build ./... verified green.
* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)
Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.
No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.
* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)
Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).
Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.
Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).
* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy
Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.
Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).
CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.
Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.
---------
Co-authored-by: Hanzo Dev <[email protected]>
889 lines
36 KiB
Go
889 lines
36 KiB
Go
package cache
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hanzoai/deploy/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/deploy/reposerver/apiclient"
|
|
"github.com/hanzoai/deploy/reposerver/cache/mocks"
|
|
cacheutil "github.com/hanzoai/deploy/util/cache"
|
|
)
|
|
|
|
type MockedCache struct {
|
|
mock.Mock
|
|
*Cache
|
|
}
|
|
|
|
type fixtures struct {
|
|
mockCache *mocks.MockRepoCache
|
|
cache *MockedCache
|
|
}
|
|
|
|
func newFixtures() *fixtures {
|
|
mockCache := mocks.NewMockRepoCache(&mocks.MockCacheOptions{RevisionCacheExpiration: 1 * time.Minute, RepoCacheExpiration: 1 * time.Minute})
|
|
newBaseCache := cacheutil.NewCache(mockCache.RedisClient)
|
|
baseCache := NewCache(newBaseCache, 1*time.Minute, 1*time.Minute, 10*time.Second)
|
|
return &fixtures{mockCache: mockCache, cache: &MockedCache{Cache: baseCache}}
|
|
}
|
|
|
|
func TestCache_GetRevisionMetadata(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
mockCache := fixtures.mockCache
|
|
// cache miss
|
|
_, err := cache.GetRevisionMetadata("my-repo-url", "my-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
mockCache.RedisClient.AssertCalled(t, "Get", mock.Anything, mock.Anything)
|
|
// populate cache
|
|
err = cache.SetRevisionMetadata("my-repo-url", "my-revision", &v1alpha1.RevisionMetadata{Message: "my-message"})
|
|
require.NoError(t, err)
|
|
// cache miss
|
|
_, err = cache.GetRevisionMetadata("other-repo-url", "my-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache miss
|
|
_, err = cache.GetRevisionMetadata("my-repo-url", "other-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache hit
|
|
value, err := cache.GetRevisionMetadata("my-repo-url", "my-revision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &v1alpha1.RevisionMetadata{Message: "my-message"}, value)
|
|
mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 4})
|
|
}
|
|
|
|
func TestCache_ListApps(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
mockCache := fixtures.mockCache
|
|
// cache miss
|
|
_, err := cache.ListApps("my-repo-url", "my-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// populate cache
|
|
err = cache.SetApps("my-repo-url", "my-revision", map[string]string{"foo": "bar"})
|
|
require.NoError(t, err)
|
|
// cache miss
|
|
_, err = cache.ListApps("other-repo-url", "my-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache miss
|
|
_, err = cache.ListApps("my-repo-url", "other-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache hit
|
|
value, err := cache.ListApps("my-repo-url", "my-revision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, map[string]string{"foo": "bar"}, value)
|
|
mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 4})
|
|
}
|
|
|
|
func TestCache_GetManifests(t *testing.T) {
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
mockCache := fixtures.mockCache
|
|
// cache miss
|
|
q := &apiclient.ManifestRequest{}
|
|
value := &CachedManifestResponse{}
|
|
newManifestCacheKeyData := func(revision string, appSource *v1alpha1.ApplicationSource, namespace, appLabelKey, appName string, refSourceCommitSHAs ResolvedRevisions) ManifestKey {
|
|
return ManifestKey{
|
|
Revision: revision,
|
|
AppSource: appSource,
|
|
RefSources: q.RefSources,
|
|
ClusterInfo: q,
|
|
Namespace: namespace,
|
|
AppLabelKey: appLabelKey,
|
|
AppName: appName,
|
|
RefSourceCommitSHAs: refSourceCommitSHAs,
|
|
}
|
|
}
|
|
err := cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "my-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// populate cache
|
|
res := &CachedManifestResponse{ManifestResponse: &apiclient.ManifestResponse{SourceType: "my-source-type"}}
|
|
err = cache.SetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "my-app-label-value", nil), res)
|
|
require.NoError(t, err)
|
|
t.Run("expect cache miss because of changed revision", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("other-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "my-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache miss because of changed path", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{Path: "other-path"}, "my-namespace", "my-app-label-key", "my-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache miss because of changed namespace", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "other-namespace", "my-app-label-key", "my-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache miss because of changed app label key", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "other-app-label-key", "my-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache miss because of changed app label value", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "other-app-label-value", nil), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache miss because of changed referenced source", func(t *testing.T) {
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "other-app-label-value", map[string]string{"my-referenced-source": "my-referenced-revision"}), value)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
})
|
|
t.Run("expect cache hit", func(t *testing.T) {
|
|
err = cache.SetManifests(
|
|
newManifestCacheKeyData("my-revision1", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "my-app-label-value", nil),
|
|
&CachedManifestResponse{ManifestResponse: &apiclient.ManifestResponse{SourceType: "my-source-type", Revision: "my-revision2"}})
|
|
require.NoError(t, err)
|
|
|
|
err = cache.GetManifests(newManifestCacheKeyData("my-revision1", &v1alpha1.ApplicationSource{}, "my-namespace", "my-app-label-key", "my-app-label-value", nil), value)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "my-source-type", value.ManifestResponse.SourceType)
|
|
assert.Equal(t, "my-revision1", value.ManifestResponse.Revision)
|
|
})
|
|
mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 2, ExternalGets: 8})
|
|
}
|
|
|
|
func TestCache_GetAppDetails(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
mockCache := fixtures.mockCache
|
|
// cache miss
|
|
value := &apiclient.RepoAppDetailsResponse{}
|
|
emptyRefSources := map[string]*v1alpha1.RefTarget{}
|
|
err := cache.GetAppDetails("my-revision", &v1alpha1.ApplicationSource{}, emptyRefSources, value, "", nil)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
res := &apiclient.RepoAppDetailsResponse{Type: "my-type"}
|
|
err = cache.SetAppDetails("my-revision", &v1alpha1.ApplicationSource{}, emptyRefSources, res, "", nil)
|
|
require.NoError(t, err)
|
|
// cache miss
|
|
err = cache.GetAppDetails("other-revision", &v1alpha1.ApplicationSource{}, emptyRefSources, value, "", nil)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache miss
|
|
err = cache.GetAppDetails("my-revision", &v1alpha1.ApplicationSource{Path: "other-path"}, emptyRefSources, value, "", nil)
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
// cache hit
|
|
err = cache.GetAppDetails("my-revision", &v1alpha1.ApplicationSource{}, emptyRefSources, value, "", nil)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &apiclient.RepoAppDetailsResponse{Type: "my-type"}, value)
|
|
mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 4})
|
|
}
|
|
|
|
func TestAddCacheFlagsToCmd(t *testing.T) {
|
|
t.Parallel()
|
|
cache, err := AddCacheFlagsToCmd(&cobra.Command{})()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 24*time.Hour, cache.repoCacheExpiration)
|
|
}
|
|
|
|
func TestCachedManifestResponse_HashBehavior(t *testing.T) {
|
|
t.Parallel()
|
|
inMemCache := cacheutil.NewInMemoryCache(1 * time.Hour)
|
|
|
|
repoCache := NewCache(
|
|
cacheutil.NewCache(inMemCache),
|
|
1*time.Minute,
|
|
1*time.Minute,
|
|
10*time.Second,
|
|
)
|
|
|
|
response := apiclient.ManifestResponse{
|
|
Namespace: "default",
|
|
Revision: "revision",
|
|
Manifests: []string{"sample-text"},
|
|
}
|
|
appSrc := &v1alpha1.ApplicationSource{}
|
|
appKey := "key"
|
|
appValue := "value"
|
|
|
|
// Set the value in the cache
|
|
store := &CachedManifestResponse{
|
|
FirstFailureTimestamp: 0,
|
|
ManifestResponse: &response,
|
|
MostRecentError: "",
|
|
NumberOfCachedResponsesReturned: 0,
|
|
NumberOfConsecutiveFailures: 0,
|
|
}
|
|
q := &apiclient.ManifestRequest{}
|
|
cacheKeyData := ManifestKey{
|
|
Revision: response.Revision,
|
|
AppSource: appSrc,
|
|
RefSources: q.RefSources,
|
|
ClusterInfo: q,
|
|
Namespace: response.Namespace,
|
|
AppLabelKey: appKey,
|
|
AppName: appValue,
|
|
}
|
|
err := repoCache.SetManifests(cacheKeyData, store)
|
|
require.NoError(t, err)
|
|
|
|
// Get the cache entry of the set value directly from the in memory cache, and check the values
|
|
var cacheKey string
|
|
var cmr *CachedManifestResponse
|
|
{
|
|
items := getInMemoryCacheContents(t, inMemCache)
|
|
|
|
assert.Len(t, items, 1)
|
|
|
|
for key, val := range items {
|
|
cmr = val
|
|
cacheKey = key
|
|
}
|
|
assert.NotEmpty(t, cmr.CacheEntryHash)
|
|
assert.NotNil(t, cmr.ManifestResponse)
|
|
assert.Equal(t, cmr.ManifestResponse, store.ManifestResponse)
|
|
|
|
regeneratedHash, err := cmr.generateCacheEntryHash()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cmr.CacheEntryHash, regeneratedHash)
|
|
}
|
|
|
|
// Retrieve the value using 'GetManifests' and confirm it works
|
|
retrievedVal := &CachedManifestResponse{}
|
|
err = repoCache.GetManifests(cacheKeyData, retrievedVal)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, retrievedVal, store)
|
|
|
|
// Corrupt the hash so that it doesn't match
|
|
{
|
|
newCmr := cmr.shallowCopy()
|
|
newCmr.CacheEntryHash = "!bad-hash!"
|
|
|
|
err := inMemCache.Set(&cacheutil.Item{
|
|
Key: cacheKey,
|
|
Object: &newCmr,
|
|
})
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Retrieve the value using GetManifests and confirm it returns a cache miss
|
|
retrievedVal = &CachedManifestResponse{}
|
|
err = repoCache.GetManifests(cacheKeyData, retrievedVal)
|
|
|
|
assert.Equal(t, err, cacheutil.ErrCacheMiss)
|
|
|
|
// Verify that the hash mismatch item has been deleted
|
|
items := getInMemoryCacheContents(t, inMemCache)
|
|
assert.Empty(t, items)
|
|
}
|
|
|
|
func getInMemoryCacheContents(t *testing.T, inMemCache *cacheutil.InMemoryCache) map[string]*CachedManifestResponse {
|
|
t.Helper()
|
|
items, err := inMemCache.Items(func() any { return &CachedManifestResponse{} })
|
|
require.NoError(t, err)
|
|
|
|
result := map[string]*CachedManifestResponse{}
|
|
for key, val := range items {
|
|
obj, ok := val.(*CachedManifestResponse)
|
|
require.True(t, ok, "Unexpected type in cache")
|
|
|
|
result[key] = obj
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func TestCachedManifestResponse_ShallowCopy(t *testing.T) {
|
|
t.Parallel()
|
|
pre := &CachedManifestResponse{
|
|
CacheEntryHash: "value",
|
|
FirstFailureTimestamp: 1,
|
|
ManifestResponse: &apiclient.ManifestResponse{
|
|
Manifests: []string{"one", "two"},
|
|
},
|
|
MostRecentError: "error",
|
|
NumberOfCachedResponsesReturned: 2,
|
|
NumberOfConsecutiveFailures: 3,
|
|
}
|
|
|
|
post := pre.shallowCopy()
|
|
assert.Equal(t, pre, post)
|
|
|
|
unequal := &CachedManifestResponse{
|
|
CacheEntryHash: "diff-value",
|
|
FirstFailureTimestamp: 1,
|
|
ManifestResponse: &apiclient.ManifestResponse{
|
|
Manifests: []string{"one", "two"},
|
|
},
|
|
MostRecentError: "error",
|
|
NumberOfCachedResponsesReturned: 2,
|
|
NumberOfConsecutiveFailures: 3,
|
|
}
|
|
assert.NotEqual(t, pre, unequal)
|
|
}
|
|
|
|
func TestCachedManifestResponse_ShallowCopyExpectedFields(t *testing.T) {
|
|
t.Parallel()
|
|
// Attempt to ensure that the developer updated CachedManifestResponse.shallowCopy(), by doing a sanity test of the structure here
|
|
|
|
val := &CachedManifestResponse{}
|
|
|
|
str, err := json.Marshal(val)
|
|
if err != nil {
|
|
assert.FailNow(t, "Unable to marshal", err)
|
|
return
|
|
}
|
|
|
|
jsonMap := map[string]any{}
|
|
err = json.Unmarshal(str, &jsonMap)
|
|
if err != nil {
|
|
assert.FailNow(t, "Unable to unmarshal", err)
|
|
return
|
|
}
|
|
|
|
expectedFields := []string{
|
|
"cacheEntryHash", "manifestResponse", "mostRecentError", "firstFailureTimestamp",
|
|
"numberOfConsecutiveFailures", "numberOfCachedResponsesReturned",
|
|
}
|
|
|
|
assert.Len(t, jsonMap, len(expectedFields))
|
|
|
|
// If this test failed, you probably also forgot to update CachedManifestResponse.shallowCopy(), so
|
|
// go do that first :)
|
|
|
|
for _, expectedField := range expectedFields {
|
|
assert.Containsf(t, string(str), "\""+expectedField+"\"", "Missing field: %s", expectedField)
|
|
}
|
|
}
|
|
|
|
func TestGetGitReferences(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("Valid args, nothing in cache, in-memory only", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
var references []*plumbing.Reference
|
|
lockOwner, err := cache.GetGitReferences("test-repo", &references)
|
|
require.NoError(t, err, "Error is cache miss handled inside function")
|
|
assert.Empty(t, lockOwner, "Lock owner should be empty")
|
|
assert.Nil(t, references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("Valid args, nothing in cache, external only", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
var references []*plumbing.Reference
|
|
lockOwner, err := cache.GetGitReferences("test-repo", &references)
|
|
require.NoError(t, err, "Error is cache miss handled inside function")
|
|
assert.Empty(t, lockOwner, "Lock owner should be empty")
|
|
assert.Nil(t, references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("Valid args, value in cache, in-memory only", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
err := cache.SetGitReferences("test-repo", *GitRefCacheItemToReferences([][2]string{{"test-repo", "ref: test"}}))
|
|
require.NoError(t, err)
|
|
var references []*plumbing.Reference
|
|
lockOwner, err := cache.GetGitReferences("test-repo", &references)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, lockOwner, "Lock owner should be empty")
|
|
assert.Len(t, references, 1)
|
|
assert.Equal(t, "test", (references)[0].Target().String())
|
|
assert.Equal(t, "test-repo", (references)[0].Name().String())
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("cache error", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
fixtures.mockCache.RedisClient.On("Get", mock.Anything, mock.Anything).Unset()
|
|
fixtures.mockCache.RedisClient.On("Get", mock.Anything, mock.Anything).Return(errors.New("test cache error"))
|
|
var references []*plumbing.Reference
|
|
lockOwner, err := cache.GetGitReferences("test-repo", &references)
|
|
require.ErrorContains(t, err, "test cache error", "Error should be propagated")
|
|
assert.Empty(t, lockOwner, "Lock owner should be empty")
|
|
assert.Nil(t, references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
}
|
|
|
|
func TestGitRefCacheItemToReferences_DataChecks(t *testing.T) {
|
|
t.Parallel()
|
|
references := *GitRefCacheItemToReferences(nil)
|
|
assert.Empty(t, references, "No data should be handled gracefully by returning an empty slice")
|
|
references = *GitRefCacheItemToReferences([][2]string{{"", ""}})
|
|
assert.Empty(t, references, "Empty data should be discarded")
|
|
references = *GitRefCacheItemToReferences([][2]string{{"test", ""}})
|
|
assert.Len(t, references, 1, "Just the key being set should not be discarded")
|
|
assert.Equal(t, "test", references[0].Name().String(), "Name should be set and equal test")
|
|
references = *GitRefCacheItemToReferences([][2]string{{"", "ref: test1"}})
|
|
assert.Len(t, references, 1, "Just the value being set should not be discarded")
|
|
assert.Equal(t, "test1", references[0].Target().String(), "Target should be set and equal test1")
|
|
references = *GitRefCacheItemToReferences([][2]string{{"test2", "ref: test2"}})
|
|
assert.Len(t, references, 1, "Valid data is should be preserved")
|
|
assert.Equal(t, "test2", references[0].Name().String(), "Name should be set and equal test2")
|
|
assert.Equal(t, "test2", references[0].Target().String(), "Target should be set and equal test2")
|
|
references = *GitRefCacheItemToReferences([][2]string{{"test3", "ref: test3"}, {"test4", "ref: test4"}})
|
|
assert.Len(t, references, 2, "Valid data is should be preserved")
|
|
assert.Equal(t, "test3", references[0].Name().String(), "Name should be set and equal test3")
|
|
assert.Equal(t, "test3", references[0].Target().String(), "Target should be set and equal test3")
|
|
assert.Equal(t, "test4", references[1].Name().String(), "Name should be set and equal test4")
|
|
assert.Equal(t, "test4", references[1].Target().String(), "Target should be set and equal test4")
|
|
}
|
|
|
|
func TestTryLockGitRefCache_OwnershipFlows(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
utilCache := cache.cache
|
|
var references []*plumbing.Reference
|
|
// Test setting the lock
|
|
_, err := cache.TryLockGitRefCache("my-repo-url", "my-lock-id", &references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 1})
|
|
require.NoError(t, err)
|
|
var output [][2]string
|
|
key := "git-refs|" + "my-repo-url"
|
|
err = utilCache.GetItem(key, &output)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 2})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "locked", output[0][0], "The lock should be set")
|
|
assert.Equal(t, "my-lock-id", output[0][1], "The lock should be set to the provided lock id")
|
|
// Test not being able to overwrite the lock
|
|
_, err = cache.TryLockGitRefCache("my-repo-url", "other-lock-id", &references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 2, ExternalGets: 3})
|
|
require.NoError(t, err)
|
|
err = utilCache.GetItem(key, &output)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 2, ExternalGets: 4})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "locked", output[0][0], "The lock should not have changed")
|
|
assert.Equal(t, "my-lock-id", output[0][1], "The lock should not have changed")
|
|
// Test can overwrite once there is nothing set
|
|
err = utilCache.SetItem(key, [][2]string{}, &cacheutil.CacheActionOpts{Expiration: 0, Delete: true})
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 2, ExternalGets: 4, ExternalDeletes: 1})
|
|
require.NoError(t, err)
|
|
_, err = cache.TryLockGitRefCache("my-repo-url", "other-lock-id", &references)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 3, ExternalGets: 5, ExternalDeletes: 1})
|
|
require.NoError(t, err)
|
|
err = utilCache.GetItem(key, &output)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "locked", output[0][0], "The lock should be set")
|
|
assert.Equal(t, "other-lock-id", output[0][1], "The lock id should have changed to other-lock-id")
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 3, ExternalGets: 6, ExternalDeletes: 1})
|
|
}
|
|
|
|
func TestGetOrLockGitReferences(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("Test cache lock get lock", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test-lock-id", lockId)
|
|
assert.NotEmpty(t, lockId, "Lock id should be set")
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 2})
|
|
})
|
|
|
|
t.Run("Test cache lock, cache hit local", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
err := cache.SetGitReferences("test-repo", *GitRefCacheItemToReferences([][2]string{{"test-repo", "ref: test"}}))
|
|
require.NoError(t, err)
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.NotEqual(t, "test-lock-id", lockId)
|
|
assert.Empty(t, lockId, "Lock id should not be set")
|
|
assert.Equal(t, "test-repo", references[0].Name().String())
|
|
assert.Equal(t, "test", references[0].Target().String())
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("Test cache lock, cache hit remote", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
err := fixtures.cache.cache.SetItem(
|
|
"git-refs|test-repo",
|
|
[][2]string{{"test-repo", "ref: test"}},
|
|
&cacheutil.CacheActionOpts{
|
|
Expiration: 30 * time.Second,
|
|
})
|
|
require.NoError(t, err)
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.NotEqual(t, "test-lock-id", lockId)
|
|
assert.Empty(t, lockId, "Lock id should not be set")
|
|
assert.Equal(t, "test-repo", references[0].Name().String())
|
|
assert.Equal(t, "test", references[0].Target().String())
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1, ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("Test miss, populated by external", func(t *testing.T) {
|
|
// Tests the case where another process populates the external cache when trying
|
|
// to obtain the lock
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
fixtures.mockCache.RedisClient.On("Get", mock.Anything, mock.Anything).Unset()
|
|
fixtures.mockCache.RedisClient.On("Get", mock.Anything, mock.Anything).Return(cacheutil.ErrCacheMiss).Once().Run(func(_ mock.Arguments) {
|
|
err := cache.SetGitReferences("test-repo", *GitRefCacheItemToReferences([][2]string{{"test-repo", "ref: test"}}))
|
|
require.NoError(t, err)
|
|
}).On("Get", mock.Anything, mock.Anything).Return(nil)
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.NotEqual(t, "test-lock-id", lockId)
|
|
assert.Empty(t, lockId, "Lock id should not be set")
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 2, ExternalGets: 2})
|
|
})
|
|
|
|
t.Run("Test cache lock timeout", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
// Create conditions for cache hit, which would result in false on updateCache if we weren't reaching the timeout
|
|
err := cache.SetGitReferences("test-repo", *GitRefCacheItemToReferences([][2]string{{"test-repo", "ref: test"}}))
|
|
require.NoError(t, err)
|
|
cache.revisionCacheLockTimeout = -1 * time.Second
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test-lock-id", lockId)
|
|
assert.NotEmpty(t, lockId, "Lock id should be set")
|
|
cache.revisionCacheLockTimeout = 10 * time.Second
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("Test cache lock error", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
fixtures.cache.revisionCacheLockTimeout = 10 * time.Second
|
|
fixtures.mockCache.RedisClient.On("Set", mock.Anything).Unset()
|
|
fixtures.mockCache.RedisClient.On("Set", mock.Anything).Return(errors.New("test cache error")).Once().
|
|
On("Set", mock.Anything).Return(nil)
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test-lock-id", lockId)
|
|
assert.NotEmpty(t, lockId, "Lock id should be set")
|
|
fixtures.mockCache.RedisClient.AssertNumberOfCalls(t, "Set", 2)
|
|
fixtures.mockCache.RedisClient.AssertNumberOfCalls(t, "Get", 4)
|
|
})
|
|
}
|
|
|
|
func TestUnlockGitReferences(t *testing.T) {
|
|
t.Run("Test not locked", func(t *testing.T) {
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
err := cache.UnlockGitReferences("test-repo", "")
|
|
// UnlockGitReferences returns nil when the key doesn't exist (cache miss is not an error in this path)
|
|
if err != nil {
|
|
assert.ErrorContains(t, err, "key is missing")
|
|
}
|
|
})
|
|
|
|
t.Run("Test unlock", func(t *testing.T) {
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
var references []*plumbing.Reference
|
|
lockId, err := cache.GetOrLockGitReferences("test-repo", "test-lock-id", &references)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test-lock-id", lockId)
|
|
assert.NotEmpty(t, lockId, "Lock id should be set")
|
|
// Release lock
|
|
err = cache.UnlockGitReferences("test-repo", lockId)
|
|
require.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
func TestSetHelmIndex(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("SetHelmIndex with valid data", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
err := fixtures.cache.SetHelmIndex("test-repo", []byte("test-data"))
|
|
require.NoError(t, err)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalSets: 1})
|
|
})
|
|
t.Run("SetHelmIndex with nil", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
err := fixtures.cache.SetHelmIndex("test-repo", nil)
|
|
require.Error(t, err, "nil data should not be cached")
|
|
var indexData []byte
|
|
err = fixtures.cache.GetHelmIndex("test-repo", &indexData)
|
|
require.Error(t, err)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
}
|
|
|
|
func TestRevisionChartDetails(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("GetRevisionChartDetails cache miss", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
details, err := fixtures.cache.GetRevisionChartDetails("test-repo", "test-revision", "v1.0.0")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
assert.Equal(t, &v1alpha1.ChartDetails{}, details)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
t.Run("GetRevisionChartDetails cache miss local", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := &v1alpha1.ChartDetails{
|
|
Description: "test-chart",
|
|
Home: "v1.0.0",
|
|
Maintainers: []string{"test-maintainer"},
|
|
}
|
|
err := cache.cache.SetItem(
|
|
revisionChartDetailsKey("test-repo", "test-revision", "v1.0.0"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
details, err := fixtures.cache.GetRevisionChartDetails("test-repo", "test-revision", "v1.0.0")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, details)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("GetRevisionChartDetails cache hit local", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := &v1alpha1.ChartDetails{
|
|
Description: "test-chart",
|
|
Home: "v1.0.0",
|
|
Maintainers: []string{"test-maintainer"},
|
|
}
|
|
err := cache.cache.SetItem(
|
|
revisionChartDetailsKey("test-repo", "test-revision", "v1.0.0"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
details, err := fixtures.cache.GetRevisionChartDetails("test-repo", "test-revision", "v1.0.0")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, details)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("SetRevisionChartDetails", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
expectedItem := &v1alpha1.ChartDetails{
|
|
Description: "test-chart",
|
|
Home: "v1.0.0",
|
|
Maintainers: []string{"test-maintainer"},
|
|
}
|
|
err := fixtures.cache.SetRevisionChartDetails("test-repo", "test-revision", "v1.0.0", expectedItem)
|
|
require.NoError(t, err)
|
|
details, err := fixtures.cache.GetRevisionChartDetails("test-repo", "test-revision", "v1.0.0")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, details)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
}
|
|
|
|
func TestOCIMetadata(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("GetOCIMetadata cache miss", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
metadata, err := fixtures.cache.GetOCIMetadata("test-repo", "sha256:abc")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
assert.Equal(t, &v1alpha1.OCIMetadata{}, metadata)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
|
|
t.Run("SetOCIMetadata round-trips", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
expectedItem := &v1alpha1.OCIMetadata{
|
|
CreatedAt: "2024-01-02T03:04:05Z",
|
|
Authors: "[email protected]",
|
|
Version: "1.2.3",
|
|
Description: "test description",
|
|
}
|
|
err := fixtures.cache.SetOCIMetadata("test-repo", "sha256:abc", expectedItem)
|
|
require.NoError(t, err)
|
|
metadata, err := fixtures.cache.GetOCIMetadata("test-repo", "sha256:abc")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, metadata)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
}
|
|
|
|
func TestGetGitDirectories(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("GetGitDirectories cache miss", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
directories, err := fixtures.cache.GetGitDirectories("test-repo", "test-revision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
assert.Empty(t, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
t.Run("GetGitDirectories cache miss local", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := []string{"test/dir", "test/dir2"}
|
|
err := cache.cache.SetItem(
|
|
gitDirectoriesKey("test-repo", "test-revision"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
directories, err := fixtures.cache.GetGitDirectories("test-repo", "test-revision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("GetGitDirectories cache hit local", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := []string{"test/dir", "test/dir2"}
|
|
err := cache.cache.SetItem(
|
|
gitDirectoriesKey("test-repo", "test-revision"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
directories, err := fixtures.cache.GetGitDirectories("test-repo", "test-revision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("SetGitDirectories", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
expectedItem := []string{"test/dir", "test/dir2"}
|
|
err := fixtures.cache.SetGitDirectories("test-repo", "test-revision", expectedItem)
|
|
require.NoError(t, err)
|
|
directories, err := fixtures.cache.GetGitDirectories("test-repo", "test-revision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
}
|
|
|
|
func TestGetGitFiles(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("GetGitFiles cache miss", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
directories, err := fixtures.cache.GetGitFiles("test-repo", "test-revision", "*.json")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
assert.Empty(t, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
t.Run("GetGitFiles cache hit", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := map[string][]byte{"test/file.json": []byte("\"test\":\"contents\""), "test/file1.json": []byte("\"test1\":\"contents1\"")}
|
|
err := cache.cache.SetItem(
|
|
gitFilesKey("test-repo", "test-revision", "*.json"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
files, err := fixtures.cache.GetGitFiles("test-repo", "test-revision", "*.json")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, files)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("SetGitFiles", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
expectedItem := map[string][]byte{"test/file.json": []byte("\"test\":\"contents\""), "test/file1.json": []byte("\"test1\":\"contents1\"")}
|
|
err := fixtures.cache.SetGitFiles("test-repo", "test-revision", "*.json", expectedItem)
|
|
require.NoError(t, err)
|
|
files, err := fixtures.cache.GetGitFiles("test-repo", "test-revision", "*.json")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, files)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
}
|
|
|
|
func TestGetGitFilesChanges(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("GetGitFilesChanges cache miss", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
directories, err := fixtures.cache.GetGitFilesChanges("test-repo", "test-revision", "syncedRevision")
|
|
require.ErrorIs(t, err, ErrCacheMiss)
|
|
assert.Empty(t, directories)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1})
|
|
})
|
|
t.Run("GetGitFilesChanges cache hit", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
cache := fixtures.cache
|
|
expectedItem := []string{"test/path/file-1.yaml", "test/path1/file-2.yaml"}
|
|
err := cache.cache.SetItem(
|
|
getGitFilesChangesKey("test-repo", "test-revision", "syncedRevision"),
|
|
expectedItem,
|
|
&cacheutil.CacheActionOpts{Expiration: 30 * time.Second})
|
|
require.NoError(t, err)
|
|
files, err := fixtures.cache.GetGitFilesChanges("test-repo", "test-revision", "syncedRevision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, files)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
|
|
t.Run("SetGitFilesChanges", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixtures := newFixtures()
|
|
t.Cleanup(fixtures.mockCache.StopRedisCallback)
|
|
expectedItem := []string{"test/path/file-1.yaml", "test/path1/file-2.yaml"}
|
|
err := fixtures.cache.SetGitFilesChanges("test-repo", "test-revision", "syncedRevision", expectedItem)
|
|
require.NoError(t, err)
|
|
files, err := fixtures.cache.GetGitFilesChanges("test-repo", "test-revision", "syncedRevision")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedItem, files)
|
|
fixtures.mockCache.AssertCacheCalledTimes(t, &mocks.CacheCallCounts{ExternalGets: 1, ExternalSets: 1})
|
|
})
|
|
}
|