Files
vaultik/internal/vaultik/prune_manifest_safety_test.go
T
clawbot 86361c8b50
check / check (pull_request) Successful in 1m20s
check / check (push) Successful in 2m42s
Fail closed on unreadable manifests instead of losing blobs (closes #157)
Prune learned which blobs are in use by reading every snapshot's manifest, but merely logged and skipped one it could not download or decode. Blobs referenced only by that snapshot then looked unreferenced and were deleted, with a zero exit -- and snapshot create --prune runs this unattended. collectReferencedBlobs now errors, naming the remote key, so prune deletes nothing and exits non-zero.

Manifest generation likewise skipped a blob whose lookup failed or was missing, yielding a manifest short of what the snapshot needs; it now fails. Deep verify only warned when the manifest omitted a database blob; it now fails on any divergence. Docs corrected.

Model: opus-4-8
2026-09-22 11:45:30 +02:00

48 lines
1.6 KiB
Go

package vaultik_test
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sneak.berlin/go/vaultik/internal/log"
"sneak.berlin/go/vaultik/internal/vaultik"
)
// TestPruneBlobs_UnreadableManifestDeletesNothing is the regression guard
// for issue #157: prune identifies referenced blobs by reading every
// snapshot's manifest, and a manifest it cannot decode used to be logged
// and skipped. Blobs referenced only by that snapshot then looked
// unreferenced and were deleted, with a zero exit — silent backup loss,
// made worse by `snapshot create --prune` running unattended with force.
//
// The single blob here is referenced only by the snapshot whose manifest
// is corrupt, so the old behaviour would delete it and succeed. Prune
// must instead delete nothing and return an error.
func TestPruneBlobs_UnreadableManifestDeletesNothing(t *testing.T) {
log.Initialize(log.Config{})
t.Parallel()
env := newListEnv(t)
ctx := context.Background()
blobKey := "blobs/" + testBlobHashA[:2] + "/" + testBlobHashA[2:4] +
"/" + testBlobHashA
require.NoError(t, env.store.Put(ctx, blobKey,
bytes.NewReader([]byte("blob-bytes"))))
// A manifest at the path prune reads, but with contents it cannot
// decode.
require.NoError(t, env.store.Put(ctx,
"metadata/corruptkey/manifest.json.zst",
bytes.NewReader([]byte("not a valid manifest"))))
err := env.v.PruneBlobs(&vaultik.PruneOptions{Force: true})
require.Error(t, err, "prune must fail when a manifest cannot be read")
assert.True(t, env.store.hasKey(blobKey),
"no blob may be deleted when a manifest is unreadable")
}