diff --git a/pkg/remote/oci.go b/pkg/remote/oci.go index 1108dff1a..df0fb4302 100644 --- a/pkg/remote/oci.go +++ b/pkg/remote/oci.go @@ -168,6 +168,7 @@ func (g *ociRemoteLoader) pullComposeArtifact(ctx context.Context, path string) if err != nil { return "", fmt.Errorf("initializing remote resource cache: %w", err) } + cache = filepath.Join(cache, cacheOCIDir) local := filepath.Join(cache, descriptor.Digest.Hex()) if cacheEntryIsComplete(local) { @@ -213,6 +214,12 @@ func (g *ociRemoteLoader) pullComposeArtifact(ctx context.Context, path string) return local, nil } +// cacheOCIDir is the directory of the cache which holds the extracted OCI +// artifacts, one directory for each digest, as cacheGitDir does for the checkouts of +// Git repositories. Earlier releases kept them at the root of the cache, where they +// are no longer looked for: an artifact is pulled again the first time it is used. +const cacheOCIDir = "oci" + // cacheEntryComplete is the file which marks a directory of the cache as a // complete entry, written once everything the entry holds is in place. const cacheEntryComplete = ".complete" diff --git a/pkg/remote/oci_cache_entry_test.go b/pkg/remote/oci_cache_entry_test.go index 188cd13e4..46fd3ecf9 100644 --- a/pkg/remote/oci_cache_entry_test.go +++ b/pkg/remote/oci_cache_entry_test.go @@ -99,7 +99,7 @@ func setCacheRoot(t *testing.T) string { t.Helper() root := t.TempDir() t.Setenv("XDG_CACHE_HOME", root) - return filepath.Join(root, "docker-compose") + return filepath.Join(root, "docker-compose", cacheOCIDir) } // An entry of the cache of extracted projects is complete or is not there: it @@ -253,3 +253,24 @@ func TestMoveCacheEntryDoesNotRestoreADirectoryWhichIsNotAnEntry(t *testing.T) { assert.NilError(t, err) assert.Equal(t, len(entries), 0, "neither the directory nor anything aside is left") } + +// The extracted artifacts are kept in a directory of the cache for OCI artifacts: an +// entry, even a complete one, which an earlier release left at the root of the +// cache is not looked for, and the artifact is extracted again in the directory. +func TestOCILoaderKeepsEntriesInTheDirectoryForOCI(t *testing.T) { + registry := newCacheTestRegistry(t) + host := registry.serve(t) + cache := setCacheRoot(t) + legacy := filepath.Join(filepath.Dir(cache), registry.digest.Hex()) + assert.NilError(t, os.MkdirAll(legacy, 0o700)) + assert.NilError(t, os.WriteFile(filepath.Join(legacy, "compose.yaml"), []byte("services: {}\n"), 0o600)) + assert.NilError(t, os.WriteFile(filepath.Join(legacy, cacheEntryComplete), nil, 0o600)) + + file, err := newCacheTestLoader(host).Load(t.Context(), registry.ref(host)) + assert.NilError(t, err) + + assert.Equal(t, file, filepath.Join(cache, registry.digest.Hex(), "compose.yaml")) + content, err := os.ReadFile(file) + assert.NilError(t, err) + assert.Equal(t, string(content), string(registry.compose)) +}