Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62719f4bd1 |
No files matched your search
@@ -83,12 +83,12 @@ func (v *Vaultik) checkDestinationMarker() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// markDestination checks the backup destination store with
|
// markDestination refuses a local index bound to a different
|
||||||
// checkStorageBinding and checkDestinationMarker, writes the marker, and
|
// destination, checks the backup destination store for its marker,
|
||||||
// then binds the local index to the destination with
|
// writes the marker, and then records the binding. A first backup marks
|
||||||
// EnsureStorageBinding. A first backup marks its destination and a later
|
// its destination and a later one writes the same text again. The
|
||||||
// one writes the same text again. Binding after the write means a backup
|
// binding is recorded last, so a first backup that cannot write to the
|
||||||
// that cannot write to the destination leaves the local index unbound.
|
// destination leaves the local index unbound.
|
||||||
func (v *Vaultik) markDestination() error {
|
func (v *Vaultik) markDestination() error {
|
||||||
err := v.checkStorageBinding()
|
err := v.checkStorageBinding()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ var errStorageBindingMismatch = errors.New(
|
|||||||
// snapshots whose manifests reference blobs that aren't on the new
|
// snapshots whose manifests reference blobs that aren't on the new
|
||||||
// destination).
|
// destination).
|
||||||
//
|
//
|
||||||
// It records nothing: only a backup binds the local index, through
|
// It records nothing. Only a backup binds the local index, through
|
||||||
// EnsureStorageBinding, and only once it has written to the destination.
|
// EnsureStorageBinding, and it does so before adding anything to it. An
|
||||||
// An unbound local index therefore holds nothing from any destination.
|
// unbound local index therefore holds nothing from any destination.
|
||||||
//
|
//
|
||||||
// Read-only inspection commands (remote info, snapshot list, etc.)
|
// Read-only inspection commands (remote info, snapshot list, etc.)
|
||||||
// deliberately don't call this: they can be run against a bare
|
// deliberately don't call this: they can be run against a bare
|
||||||
@@ -53,31 +53,32 @@ func (v *Vaultik) checkStorageBinding() error {
|
|||||||
errStorageBindingMismatch, buildBindingMismatchMessage(stored, configured))
|
errStorageBindingMismatch, buildBindingMismatchMessage(stored, configured))
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureStorageBinding refuses as checkStorageBinding does and, when the
|
// EnsureStorageBinding records the configured destination (storage_url,
|
||||||
// local index is not bound yet, records the configured destination
|
// or the s3.* endpoint, bucket and prefix) as the binding of an unbound
|
||||||
// (storage_url, or the s3.* endpoint, bucket and prefix) as its binding.
|
// local index. It refuses as checkStorageBinding does when the local
|
||||||
// A backup calls it right after writing the destination marker, its
|
// index is bound to a different destination.
|
||||||
// first write to the destination, so a backup that fails before then
|
|
||||||
// (a mistyped bucket, a refused credential) leaves the local index
|
|
||||||
// unbound and a corrected config is enough to go on.
|
|
||||||
func (v *Vaultik) EnsureStorageBinding() error {
|
func (v *Vaultik) EnsureStorageBinding() error {
|
||||||
err := v.checkStorageBinding()
|
|
||||||
if err != nil || v.Repositories == nil || v.Config == nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
stored, configured, err := v.storageBinding()
|
stored, configured, err := v.storageBinding()
|
||||||
if err != nil || stored != "" {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = v.Repositories.LocalMeta.Set(
|
if stored == "" {
|
||||||
v.ctx, database.LocalMetaKeyStorageURL, configured)
|
err = v.Repositories.LocalMeta.Set(
|
||||||
if err != nil {
|
v.ctx, database.LocalMetaKeyStorageURL, configured)
|
||||||
return fmt.Errorf("recording local storage binding: %w", err)
|
if err != nil {
|
||||||
|
return fmt.Errorf("recording local storage binding: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Bound local index to storage destination", "storage_url", configured)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Bound local index to storage destination", "storage_url", configured)
|
if stored != configured {
|
||||||
|
return fmt.Errorf("%w\n%s",
|
||||||
|
errStorageBindingMismatch, buildBindingMismatchMessage(stored, configured))
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"sneak.berlin/go/vaultik/internal/config"
|
"sneak.berlin/go/vaultik/internal/config"
|
||||||
"sneak.berlin/go/vaultik/internal/database"
|
"sneak.berlin/go/vaultik/internal/database"
|
||||||
"sneak.berlin/go/vaultik/internal/log"
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
|
"sneak.berlin/go/vaultik/internal/storage"
|
||||||
"sneak.berlin/go/vaultik/internal/storage/faultstore"
|
"sneak.berlin/go/vaultik/internal/storage/faultstore"
|
||||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||||
)
|
)
|
||||||
@@ -55,17 +56,78 @@ func TestFailedFirstBackupLeavesIndexUnbound(t *testing.T) {
|
|||||||
assert.Equal(t, "file://"+storeDir, storageBindingOf(ctx, t, repos))
|
assert.Equal(t, "file://"+storeDir, storageBindingOf(ctx, t, repos))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPruneLeavesIndexUnbound checks that a command other than a backup
|
// TestBackupToOtherDestinationWritesNoMarker checks that a backup
|
||||||
// does not bind an unbound local index.
|
// refuses a local index bound to a different destination before it
|
||||||
func TestPruneLeavesIndexUnbound(t *testing.T) {
|
// writes the marker to the configured one.
|
||||||
|
func TestBackupToOtherDestinationWritesNoMarker(t *testing.T) {
|
||||||
log.Initialize(log.Config{})
|
log.Initialize(log.Config{})
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
v, repos := newFileDestinationVaultik(ctx, t, t.TempDir())
|
firstDir := t.TempDir()
|
||||||
|
v, _ := newFileDestinationVaultik(ctx, t, firstDir)
|
||||||
|
v.Config.StorageURL = "file://" + firstDir
|
||||||
|
|
||||||
require.NoError(t, v.Prune(&vaultik.PruneOptions{Force: true}))
|
require.NoError(t, backUp(v, "first"))
|
||||||
assert.Empty(t, storageBindingOf(ctx, t, repos))
|
|
||||||
|
secondDir := t.TempDir()
|
||||||
|
secondStore, err := storage.NewFileStorer(secondDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
v.Storage = secondStore
|
||||||
|
v.Config.StorageURL = "file://" + secondDir
|
||||||
|
|
||||||
|
require.ErrorContains(t, backUp(v, "first"),
|
||||||
|
"local index is bound to a different backup destination")
|
||||||
|
assert.NoFileExists(t, filepath.Join(secondDir, markerFile))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCommandsOtherThanBackupLeaveIndexUnbound checks that commands
|
||||||
|
// other than a backup do not bind an unbound local index, whether or not
|
||||||
|
// they fail. Each runs on a fresh local index.
|
||||||
|
func TestCommandsOtherThanBackupLeaveIndexUnbound(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
run func(v *vaultik.Vaultik) error
|
||||||
|
}{
|
||||||
|
{"prune", func(v *vaultik.Vaultik) error {
|
||||||
|
return v.Prune(&vaultik.PruneOptions{Force: true})
|
||||||
|
}},
|
||||||
|
{"snapshot purge", func(v *vaultik.Vaultik) error {
|
||||||
|
return v.PurgeSnapshotsWithOptions(
|
||||||
|
&vaultik.SnapshotPurgeOptions{KeepLatest: true, Force: true})
|
||||||
|
}},
|
||||||
|
{"snapshot remove", func(v *vaultik.Vaultik) error {
|
||||||
|
_, err := v.RemoveSnapshot("host_first_2026-01-02T03:04:05Z",
|
||||||
|
&vaultik.RemoveOptions{Force: true})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}},
|
||||||
|
{"snapshot remove --all", func(v *vaultik.Vaultik) error {
|
||||||
|
_, err := v.RemoveAllSnapshots(&vaultik.RemoveOptions{Force: true})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}},
|
||||||
|
{"remote nuke", func(v *vaultik.Vaultik) error {
|
||||||
|
return v.NukeRemote(true)
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
v, repos := newFileDestinationVaultik(ctx, t, t.TempDir())
|
||||||
|
|
||||||
|
_ = tt.run(v)
|
||||||
|
|
||||||
|
assert.Empty(t, storageBindingOf(ctx, t, repos))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildBindTestVaultik returns a minimal Vaultik wired with a real
|
// buildBindTestVaultik returns a minimal Vaultik wired with a real
|
||||||
|
|||||||
Reference in new issue
Block a user