Debian Codegraph @

internal/repository/upgrade_repo_test.go

raw symbols SCIP
1 package repository
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "sync"
8 "testing"
9
10 "github.com/restic/restic/internal/backend"
11 "github.com/restic/restic/internal/errors"
12 rtest "github.com/restic/restic/internal/test"
13 )
14
15 func TestUpgradeRepoV2(t *testing.T) {
16 repo, _, _ := TestRepositoryWithVersion(t, 1)
17 if repo.Config().Version != 1 {
18 t.Fatal("test repo has wrong version")
19 }
20
21 err := UpgradeRepo(context.Background(), repo)
22 rtest.OK(t, err)
23 }
24
25 type failBackend struct {
26 backend.Backend
27
28 mu sync.Mutex
29 ConfigFileSavesUntilError uint
30 }
31
32 func (be *failBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
33 if h.Type != backend.ConfigFile {
34 return be.Backend.Save(ctx, h, rd)
35 }
36
37 be.mu.Lock()
38 if be.ConfigFileSavesUntilError == 0 {
39 be.mu.Unlock()
40 return errors.New("failure induced for testing")
41 }
42
43 be.ConfigFileSavesUntilError--
44 be.mu.Unlock()
45
46 return be.Backend.Save(ctx, h, rd)
47 }
48
49 func TestUpgradeRepoV2Failure(t *testing.T) {
50 be := TestBackend(t)
51
52 // wrap backend so that it fails upgrading the config after the initial write
53 be = &failBackend{
54 ConfigFileSavesUntilError: 1,
55 Backend: be,
56 }
57
58 repo, _ := TestRepositoryWithBackend(t, be, 1, Options{})
59 if repo.Config().Version != 1 {
60 t.Fatal("test repo has wrong version")
61 }
62
63 err := UpgradeRepo(context.Background(), repo)
64 if err == nil {
65 t.Fatal("expected error returned from Apply(), got nil")
66 }
67
68 upgradeErr := err.(*upgradeRepoV2Error)
69 if upgradeErr.UploadNewConfigError == nil {
70 t.Fatal("expected upload error, got nil")
71 }
72
73 if upgradeErr.ReuploadOldConfigError == nil {
74 t.Fatal("expected reupload error, got nil")
75 }
76
77 if upgradeErr.BackupFilePath == "" {
78 t.Fatal("no backup file path found")
79 }
80 rtest.OK(t, os.Remove(upgradeErr.BackupFilePath))
81 rtest.OK(t, os.Remove(filepath.Dir(upgradeErr.BackupFilePath)))
82 }