-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransaction_test.go
More file actions
134 lines (124 loc) · 3.8 KB
/
Copy pathtransaction_test.go
File metadata and controls
134 lines (124 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package hpatch
import (
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestCommitFailureRollsBack(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "a.txt", "a\n", 0o644)
writeTestFile(t, root, "b.txt", "b\n", 0o644)
operations := &failingFileOperations{
fileOperations: testRootOperations(t, root),
failRename: map[int]error{4: errors.New("injected install failure")},
}
err := commitChanges(updateChanges(), operations)
if err == nil || !strings.Contains(err.Error(), "rollback succeeded") {
t.Fatalf("commitChanges() error = %v", err)
}
want := map[string]string{"a.txt": "a\n", "b.txt": "b\n"}
if got := readTree(t, root); !reflect.DeepEqual(got, want) {
t.Fatalf("tree after rollback = %#v, want %#v", got, want)
}
}
func TestRollbackFailureIsReportedAndBackupPreserved(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "a.txt", "a\n", 0o644)
writeTestFile(t, root, "b.txt", "b\n", 0o644)
operations := &failingFileOperations{
fileOperations: testRootOperations(t, root),
failRename: map[int]error{
4: errors.New("injected install failure"),
5: errors.New("injected restore failure"),
},
}
err := commitChanges(updateChanges(), operations)
if err == nil || !strings.Contains(err.Error(), "rollback also failed") || !strings.Contains(err.Error(), "b.txt") {
t.Fatalf("commitChanges() error = %v", err)
}
backups, globErr := filepath.Glob(filepath.Join(root, ".b.txt.hpatch-backup-*"))
if globErr != nil || len(backups) != 1 {
t.Fatalf("preserved backups = %v, error %v", backups, globErr)
}
content, readErr := os.ReadFile(backups[0])
if readErr != nil || string(content) != "b\n" {
t.Fatalf("backup content = %q, error %v", content, readErr)
}
}
func TestStagingCleanupFailureReportsRetainedArtifact(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "a.txt", "a\n", 0o644)
operations := &failingFileOperations{
fileOperations: testRootOperations(t, root),
failRemove: map[int]error{
1: errors.New("injected reservation cleanup failure"),
2: errors.New("injected staging cleanup failure"),
},
}
err := commitChanges(updateChanges()[:1], operations)
if err == nil || !strings.Contains(err.Error(), "cleanup also failed") || !strings.Contains(err.Error(), ".a.txt.hpatch-backup-") {
t.Fatalf("commitChanges() error = %v", err)
}
artifacts, globErr := filepath.Glob(filepath.Join(root, ".a.txt.hpatch-backup-*"))
if globErr != nil || len(artifacts) != 1 {
t.Fatalf("retained artifacts = %v, error %v", artifacts, globErr)
}
}
type failingFileOperations struct {
fileOperations
renameCalls int
failRename map[int]error
removeCalls int
failRemove map[int]error
}
func (f *failingFileOperations) Rename(oldPath, newPath string) error {
f.renameCalls++
if err := f.failRename[f.renameCalls]; err != nil {
return err
}
return f.fileOperations.Rename(oldPath, newPath)
}
func (f *failingFileOperations) Remove(path string) error {
f.removeCalls++
if err := f.failRemove[f.removeCalls]; err != nil {
return err
}
return f.fileOperations.Remove(path)
}
func updateChanges() []change {
changes := make([]change, 0, 2)
for _, entry := range []struct {
path string
before string
after string
}{
{path: "a.txt", before: "a\n", after: "A\n"},
{path: "b.txt", before: "b\n", after: "B\n"},
} {
changes = append(changes, change{
kind: changeUpdate,
originalPath: entry.path,
path: entry.path,
original: entry.before,
content: entry.after,
mode: 0o644,
})
}
return changes
}
func testRootOperations(t *testing.T, path string) rootFileOperations {
t.Helper()
root, err := os.OpenRoot(path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := root.Close(); err != nil {
t.Errorf("closing root: %v", err)
}
})
return rootFileOperations{root: root}
}