Files
restic/internal/repository/upgrade_repo_test.go
T
Rob MartinandMichael Eischer 2c9b353cbf repository: do not delete config on atomic backends during upgrade rollback (#22136)
In UpgradeRepo, if upgradeRepository fails, the error handler attempts
contingency rollback by re-uploading the original config file.

However, the contingency path unconditionally executed repo.be.Remove(ctx, h)
before calling repo.be.Save. On backends supporting atomic replacement (such
as local filesystems), a failed upgrade (e.g. out-of-disk-space or permissions
error while writing the temporary file) leaves the original, valid version 1
configuration file completely intact on disk.

Unconditionally removing the config file during rollback actively deletes
the working configuration. If the subsequent Save call also fails, the
repository is left permanently corrupted without a config file.

Condition the pre-removal on !repo.be.Properties().HasAtomicReplace, matching
the forward upgrade path in upgradeRepository. Add a unit test asserting that
aborted upgrades on atomic backends preserve the intact original configuration.

---------

Co-authored-by: Michael Eischer <michael.eischer@fau.de>
2026-11-15 18:57:01 +00:00

113 lines
2.9 KiB
Go

package repository
import (
"context"
"os"
"path/filepath"
"sync"
"testing"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/local"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
)
func TestUpgradeRepoV2(t *testing.T) {
repo, _, _ := TestRepositoryWithVersion(t, 1)
if repo.Config().Version != 1 {
t.Fatal("test repo has wrong version")
}
err := UpgradeRepo(context.Background(), repo, "")
rtest.OK(t, err)
}
type failBackend struct {
backend.Backend
mu sync.Mutex
ConfigFileSavesUntilError uint
}
func (be *failBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
if h.Type != backend.ConfigFile {
return be.Backend.Save(ctx, h, rd)
}
be.mu.Lock()
if be.ConfigFileSavesUntilError == 0 {
be.mu.Unlock()
return errors.New("failure induced for testing")
}
be.ConfigFileSavesUntilError--
be.mu.Unlock()
return be.Backend.Save(ctx, h, rd)
}
func TestUpgradeRepoV2FailurePreservesLocalConfig(t *testing.T) {
ctx := context.Background()
be, err := local.Create(ctx, local.Config{Path: t.TempDir()}, t.Logf)
rtest.OK(t, err)
// Both the upgrade and the recovery write fail after initialization.
repo, _ := TestRepositoryWithBackend(t, &failBackend{
ConfigFileSavesUntilError: 1,
Backend: be,
}, 1, Options{})
originalConfig, err := repo.LoadRaw(ctx, restic.ConfigFile, restic.ID{})
rtest.OK(t, err)
err = UpgradeRepo(ctx, repo, t.TempDir())
if err == nil {
t.Fatal("expected upgrade to fail")
}
upgradeErr := err.(*upgradeRepoV2Error)
if upgradeErr.ReuploadOldConfigError == nil {
t.Fatal("expected recovery write to fail")
}
// A failed recovery must not delete the intact original config.
preservedConfig, err := repo.LoadRaw(ctx, restic.ConfigFile, restic.ID{})
rtest.OK(t, err)
rtest.Equals(t, originalConfig, preservedConfig)
}
func TestUpgradeRepoV2Failure(t *testing.T) {
be := TestBackend(t)
// wrap backend so that it fails upgrading the config after the initial write
be = &failBackend{
ConfigFileSavesUntilError: 1,
Backend: be,
}
repo, _ := TestRepositoryWithBackend(t, be, 1, Options{})
if repo.Config().Version != 1 {
t.Fatal("test repo has wrong version")
}
err := UpgradeRepo(context.Background(), repo, "")
if err == nil {
t.Fatal("expected error returned from Apply(), got nil")
}
upgradeErr := err.(*upgradeRepoV2Error)
if upgradeErr.UploadNewConfigError == nil {
t.Fatal("expected upload error, got nil")
}
if upgradeErr.ReuploadOldConfigError == nil {
t.Fatal("expected reupload error, got nil")
}
if upgradeErr.BackupFilePath == "" {
t.Fatal("no backup file path found")
}
rtest.OK(t, os.Remove(upgradeErr.BackupFilePath))
rtest.OK(t, os.Remove(filepath.Dir(upgradeErr.BackupFilePath)))
}