Let snapshot remove accept a remote-only snapshot's remote key (closes #400) #458
No files matched your search
@@ -504,6 +504,36 @@ func TestRemoveSnapshot_ByRemoteKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRemoveSnapshot_ByRemoteKeyInLocalIndex checks that a snapshot the
|
||||||
|
// local index holds, named by its remote key, is removed from the local
|
||||||
|
// index as well as from the destination store.
|
||||||
|
func TestRemoveSnapshot_ByRemoteKeyInLocalIndex(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
store := newTestStorer()
|
||||||
|
addManifest(t, store, "snapshot-001", nil)
|
||||||
|
|
||||||
|
// A backup wrote to this destination, so it carries the marker.
|
||||||
|
require.NoError(t, store.Put(context.Background(), markerFile,
|
||||||
|
bytes.NewReader(nil)))
|
||||||
|
|
||||||
|
tv := vaultik.NewForTesting(store)
|
||||||
|
tv.Repositories = newLocalIndex(t, "snapshot-001")
|
||||||
|
|
||||||
|
result, err := tv.RemoveSnapshot(snapshot.RemoteSnapshotKey("snapshot-001"),
|
||||||
|
&vaultik.RemoveOptions{Force: true})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, result.RemoteRemoved)
|
||||||
|
assert.False(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
||||||
|
|
||||||
|
snap, err := tv.Repositories.Snapshots.GetByID(context.Background(),
|
||||||
|
"snapshot-001")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Nil(t, snap)
|
||||||
|
}
|
||||||
|
|
||||||
// TestRemoveSnapshot_NotInLocalIndexDeleteFails checks that removing a
|
// TestRemoveSnapshot_NotInLocalIndexDeleteFails checks that removing a
|
||||||
// snapshot only the destination store holds fails when its metadata
|
// snapshot only the destination store holds fails when its metadata
|
||||||
// cannot be deleted there, under --force and under --json: nothing was
|
// cannot be deleted there, under --force and under --json: nothing was
|
||||||
|
|||||||
@@ -1467,7 +1467,7 @@ func (v *Vaultik) RemoveSnapshot(
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteKey, inLocalIndex, err := v.findSnapshotToRemove(
|
snapshotID, remoteKey, inLocalIndex, err := v.findSnapshotToRemove(
|
||||||
snapshotID, opts.LocalOnly)
|
snapshotID, opts.LocalOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
@@ -1531,22 +1531,39 @@ func (v *Vaultik) RemoveSnapshot(
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// findSnapshotToRemove returns the remote key of the snapshot that
|
// findSnapshotToRemove returns the ID of the snapshot that identifier
|
||||||
// identifier names, and whether the local index holds it. A remote key,
|
// names, its remote key, and whether the local index holds it. A remote
|
||||||
// or the leading part of one, names a snapshot only on the destination
|
// key, or the leading part of one, is resolved on the destination store;
|
||||||
// store, so the local index never holds it.
|
// when the local index holds the snapshot with that key, its ID is
|
||||||
|
// returned, so it is removed from both.
|
||||||
func (v *Vaultik) findSnapshotToRemove(
|
func (v *Vaultik) findSnapshotToRemove(
|
||||||
identifier string, localOnly bool,
|
identifier string, localOnly bool,
|
||||||
) (string, bool, error) {
|
) (string, string, bool, error) {
|
||||||
if isRemoteKeyOrPrefix(identifier) && !localOnly {
|
if !isRemoteKeyOrPrefix(identifier) || localOnly {
|
||||||
remoteKey, err := v.resolveSnapshotRemoteKey(identifier)
|
|
||||||
|
|
||||||
return remoteKey, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
inLocalIndex, err := v.checkSnapshotExists(identifier, localOnly)
|
inLocalIndex, err := v.checkSnapshotExists(identifier, localOnly)
|
||||||
|
|
||||||
return snapshot.RemoteSnapshotKey(identifier), inLocalIndex, err
|
return identifier, snapshot.RemoteSnapshotKey(identifier),
|
||||||
|
inLocalIndex, err
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteKey, err := v.resolveSnapshotRemoteKey(identifier)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
localIDs, err := v.localSnapshotIDs()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", false, fmt.Errorf(
|
||||||
|
"looking up snapshot in local index: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, id := range localIDs {
|
||||||
|
if snapshot.RemoteSnapshotKey(id) == remoteKey {
|
||||||
|
return id, remoteKey, true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return identifier, remoteKey, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkSnapshotExists returns errSnapshotNotFound when the local index
|
// checkSnapshotExists returns errSnapshotNotFound when the local index
|
||||||
|
|||||||
Reference in new issue
Block a user