@@ -26,7 +26,6 @@ import (
2626 "path"
2727 "path/filepath"
2828 "regexp"
29- "sync"
3029 "testing"
3130
3231 ecc "github.com/conforma/crds/api/v1alpha1"
@@ -163,7 +162,7 @@ func TestInlineDataSource(t *testing.T) {
163162 t .Run (tt .name , func (t * testing.T ) {
164163 // Clear download cache for each test
165164 t .Cleanup (func () {
166- downloadCache = sync. Map {}
165+ ClearDownloadCache ()
167166 })
168167
169168 s := InlineData (tt .inputData )
@@ -276,7 +275,7 @@ func TestInlineDataGetPolicy(t *testing.T) {
276275 t .Run (tt .name , func (t * testing.T ) {
277276 // Clear download cache for each test
278277 t .Cleanup (func () {
279- downloadCache = sync. Map {}
278+ ClearDownloadCache ()
280279 })
281280
282281 s := InlineData (tt .inputData )
@@ -539,7 +538,7 @@ func (m mockPolicySource) Type() PolicyType {
539538func TestGetPolicyThroughCache (t * testing.T ) {
540539 test := func (t * testing.T , fs afero.Fs , expectedDownloads int ) {
541540 t .Cleanup (func () {
542- downloadCache = sync. Map {}
541+ ClearDownloadCache ()
543542 })
544543
545544 ctx := utils .WithFS (context .Background (), fs )
@@ -604,7 +603,7 @@ func TestGetPolicyThroughCache(t *testing.T) {
604603// causing Rego compile issue
605604func TestDownloadCacheWorkdirMismatch (t * testing.T ) {
606605 t .Cleanup (func () {
607- downloadCache = sync. Map {}
606+ ClearDownloadCache ()
608607 })
609608 tmp := t .TempDir ()
610609
@@ -634,12 +633,55 @@ func TestDownloadCacheWorkdirMismatch(t *testing.T) {
634633 assert .Equal (t , destination1 , destination2 )
635634}
636635
636+ // TestGetPolicyPinnedURLCacheConsistency verifies that URL pinning doesn't
637+ // cause duplicate policy directories. After the first GetPolicy call, the URL
638+ // is pinned (e.g. github.com/org/repo -> git::github.com/org/repo?ref=abc123),
639+ // which changes the cache key. Without the fix, the second call would miss the
640+ // cache and re-download into a new directory, causing OPA duplicate package errors.
641+ func TestGetPolicyPinnedURLCacheConsistency (t * testing.T ) {
642+ t .Cleanup (ClearDownloadCache )
643+
644+ workDir := t .TempDir ()
645+ policyDir := filepath .Join (workDir , "policy" )
646+ require .NoError (t , os .MkdirAll (policyDir , 0o755 ))
647+
648+ originalUrl := "github.com/org/repo//policy"
649+ p := & PolicyUrl {Url : originalUrl , Kind : PolicyKind }
650+
651+ dl := & mockDownloader {}
652+ dl .On ("Download" , mock .Anything , mock .Anything , originalUrl , false ).
653+ Run (func (args mock.Arguments ) {
654+ dest := args .String (1 )
655+ require .NoError (t , os .MkdirAll (dest , 0o755 ))
656+ }).
657+ Return (& gitMetadata.GitMetadata {LatestCommit : "abc123def456" }, nil )
658+
659+ ctx := usingDownloader (context .TODO (), dl )
660+
661+ // First call: downloads and pins URL
662+ dest1 , err := p .GetPolicy (ctx , workDir , false )
663+ require .NoError (t , err )
664+ assert .NotEqual (t , originalUrl , p .Url , "URL should be pinned after first call" )
665+
666+ // Second call: should hit cache despite pinned URL
667+ dest2 , err := p .GetPolicy (ctx , workDir , false )
668+ require .NoError (t , err )
669+ assert .Equal (t , dest1 , dest2 , "second call should return same directory" )
670+
671+ // Verify only one directory exists under policy/
672+ entries , err := os .ReadDir (policyDir )
673+ require .NoError (t , err )
674+ assert .Len (t , entries , 1 , "only one policy directory should exist, not a duplicate from pinned URL" )
675+
676+ dl .AssertNumberOfCalls (t , "Download" , 1 )
677+ }
678+
637679// TestConcurrentPolicyCachingRaceCondition reproduces the "file exists" error
638680// that occurs when multiple workers simultaneously try to create symlinks from
639681// cached policy downloads to their individual work directories
640682func TestConcurrentPolicyCachingRaceCondition (t * testing.T ) {
641683 t .Cleanup (func () {
642- downloadCache = sync. Map {}
684+ ClearDownloadCache ()
643685 })
644686
645687 tmp := t .TempDir ()
0 commit comments