Compare commits
5 Commits
add-compre
...
e8b4ef80ab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8b4ef80ab | ||
| 0b0398b3e9 | |||
|
|
09f23a7ef8 | ||
|
|
28614410a7 | ||
| b7c094c5e0 |
@@ -1,64 +0,0 @@
|
|||||||
package blobgen
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"crypto/rand"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// testRecipient is a static age recipient for tests.
|
|
||||||
const testRecipient = "age1cplgrwj77ta54dnmydvvmzn64ltk83ankxl5sww04mrtmu62kv3s89gmvv"
|
|
||||||
|
|
||||||
// TestCompressStreamNoDoubleClose is a regression test for issue #28.
|
|
||||||
// It verifies that CompressStream does not panic or return an error due to
|
|
||||||
// double-closing the underlying blobgen.Writer. Before the fix in PR #33,
|
|
||||||
// the explicit Close() on the happy path combined with defer Close() would
|
|
||||||
// cause a double close.
|
|
||||||
func TestCompressStreamNoDoubleClose(t *testing.T) {
|
|
||||||
input := []byte("regression test data for issue #28 double-close fix")
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
written, hash, err := CompressStream(&buf, bytes.NewReader(input), 3, []string{testRecipient})
|
|
||||||
require.NoError(t, err, "CompressStream should not return an error")
|
|
||||||
assert.True(t, written > 0, "expected bytes written > 0")
|
|
||||||
assert.NotEmpty(t, hash, "expected non-empty hash")
|
|
||||||
assert.True(t, buf.Len() > 0, "expected non-empty output")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCompressStreamLargeInput exercises CompressStream with a larger payload
|
|
||||||
// to ensure no double-close issues surface under heavier I/O.
|
|
||||||
func TestCompressStreamLargeInput(t *testing.T) {
|
|
||||||
data := make([]byte, 512*1024) // 512 KB
|
|
||||||
_, err := rand.Read(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
written, hash, err := CompressStream(&buf, bytes.NewReader(data), 3, []string{testRecipient})
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.True(t, written > 0)
|
|
||||||
assert.NotEmpty(t, hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCompressStreamEmptyInput verifies CompressStream handles empty input
|
|
||||||
// without double-close issues.
|
|
||||||
func TestCompressStreamEmptyInput(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
_, hash, err := CompressStream(&buf, strings.NewReader(""), 3, []string{testRecipient})
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCompressDataNoDoubleClose mirrors the stream test for CompressData,
|
|
||||||
// ensuring the explicit Close + error-path Close pattern is also safe.
|
|
||||||
func TestCompressDataNoDoubleClose(t *testing.T) {
|
|
||||||
input := []byte("CompressData regression test for double-close")
|
|
||||||
result, err := CompressData(input, 3, []string{testRecipient})
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.True(t, result.CompressedSize > 0)
|
|
||||||
assert.True(t, result.UncompressedSize == int64(len(input)))
|
|
||||||
assert.NotEmpty(t, result.SHA256)
|
|
||||||
}
|
|
||||||
@@ -90,6 +90,24 @@ func (v *Vaultik) CreateSnapshot(opts *SnapshotCreateOptions) error {
|
|||||||
v.printfStdout("\nAll %d snapshots completed in %s\n", len(snapshotNames), time.Since(overallStartTime).Round(time.Second))
|
v.printfStdout("\nAll %d snapshots completed in %s\n", len(snapshotNames), time.Since(overallStartTime).Round(time.Second))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prune old snapshots and unreferenced blobs if --prune was specified
|
||||||
|
if opts.Prune {
|
||||||
|
log.Info("Pruning enabled - deleting old snapshots and unreferenced blobs")
|
||||||
|
v.printlnStdout("\nPruning old snapshots (keeping latest)...")
|
||||||
|
|
||||||
|
if err := v.PurgeSnapshots(true, "", true); err != nil {
|
||||||
|
return fmt.Errorf("prune: purging old snapshots: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v.printlnStdout("Pruning unreferenced blobs...")
|
||||||
|
|
||||||
|
if err := v.PruneBlobs(&PruneOptions{Force: true}); err != nil {
|
||||||
|
return fmt.Errorf("prune: removing unreferenced blobs: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Pruning complete")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,11 +324,6 @@ func (v *Vaultik) createNamedSnapshot(opts *SnapshotCreateOptions, hostname, sna
|
|||||||
}
|
}
|
||||||
v.printfStdout("Duration: %s\n", formatDuration(snapshotDuration))
|
v.printfStdout("Duration: %s\n", formatDuration(snapshotDuration))
|
||||||
|
|
||||||
if opts.Prune {
|
|
||||||
log.Info("Pruning enabled - will delete old snapshots after snapshot")
|
|
||||||
// TODO: Implement pruning
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,7 +558,7 @@ func (v *Vaultik) PurgeSnapshots(keepLatest bool, olderThan string, force bool)
|
|||||||
if !force {
|
if !force {
|
||||||
v.printfStdout("\nDelete %d snapshot(s)? [y/N] ", len(toDelete))
|
v.printfStdout("\nDelete %d snapshot(s)? [y/N] ", len(toDelete))
|
||||||
var confirm string
|
var confirm string
|
||||||
if _, err := v.scanStdin(&confirm); err != nil {
|
if _, err := fmt.Scanln(&confirm); err != nil {
|
||||||
// Treat EOF or error as "no"
|
// Treat EOF or error as "no"
|
||||||
v.printlnStdout("Cancelled")
|
v.printlnStdout("Cancelled")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
23
internal/vaultik/snapshot_prune_test.go
Normal file
23
internal/vaultik/snapshot_prune_test.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package vaultik
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestSnapshotCreateOptions_PruneFlag verifies the Prune field exists on
|
||||||
|
// SnapshotCreateOptions and can be set.
|
||||||
|
func TestSnapshotCreateOptions_PruneFlag(t *testing.T) {
|
||||||
|
opts := &SnapshotCreateOptions{
|
||||||
|
Prune: true,
|
||||||
|
}
|
||||||
|
if !opts.Prune {
|
||||||
|
t.Error("Expected Prune to be true")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts2 := &SnapshotCreateOptions{
|
||||||
|
Prune: false,
|
||||||
|
}
|
||||||
|
if opts2.Prune {
|
||||||
|
t.Error("Expected Prune to be false")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -129,7 +129,7 @@ func (v *Vaultik) GetFilesystem() afero.Fs {
|
|||||||
return v.Fs
|
return v.Fs
|
||||||
}
|
}
|
||||||
|
|
||||||
// printfStdout writes formatted output to stdout.
|
// printfStdout writes formatted output to stdout for user-facing messages.
|
||||||
func (v *Vaultik) printfStdout(format string, args ...any) {
|
func (v *Vaultik) printfStdout(format string, args ...any) {
|
||||||
_, _ = fmt.Fprintf(v.Stdout, format, args...)
|
_, _ = fmt.Fprintf(v.Stdout, format, args...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user