aboutsummaryrefslogtreecommitdiff
path: root/internal/persistence/txn_test.go
blob: 10a809dcfbf837e537b1fc6bb6dad6b63c36b73b (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package persistence

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func writeTestFile(t *testing.T, path, content string) {
	t.Helper()
	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(path, []byte(content), 0644); err != nil {
		t.Fatal(err)
	}
}

func readTestFile(t *testing.T, path string) string {
	t.Helper()
	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	return string(data)
}

func TestTxnCommitAppliesAllOps(t *testing.T) {
	dir := t.TempDir()
	journalDir := filepath.Join(dir, ".journal")
	writeTestFile(t, filepath.Join(dir, "a.yaml"), "old-a")
	writeTestFile(t, filepath.Join(dir, "b.yaml"), "old-b")
	writeTestFile(t, filepath.Join(dir, "c.yaml"), "old-c")

	txn := NewTransaction(journalDir)
	if err := txn.Replace(filepath.Join(dir, "a.yaml"), []byte("new-a"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := txn.Delete(filepath.Join(dir, "b.yaml")); err != nil {
		t.Fatal(err)
	}
	if err := txn.Move(filepath.Join(dir, "c.yaml"), filepath.Join(dir, "d.yaml")); err != nil {
		t.Fatal(err)
	}
	if err := txn.Replace(filepath.Join(dir, "created.yaml"), []byte("created"), 0600); err != nil {
		t.Fatal(err)
	}

	if err := txn.Commit(); err != nil {
		t.Fatalf("commit: %v", err)
	}
	if got := readTestFile(t, filepath.Join(dir, "a.yaml")); got != "new-a" {
		t.Fatalf("replace failed: %q", got)
	}
	if _, err := os.Stat(filepath.Join(dir, "b.yaml")); !os.IsNotExist(err) {
		t.Fatalf("delete failed: %v", err)
	}
	if _, err := os.Stat(filepath.Join(dir, "c.yaml")); !os.IsNotExist(err) {
		t.Fatalf("move left source behind: %v", err)
	}
	if got := readTestFile(t, filepath.Join(dir, "d.yaml")); got != "old-c" {
		t.Fatalf("move lost content: %q", got)
	}
	if got := readTestFile(t, filepath.Join(dir, "created.yaml")); got != "created" {
		t.Fatalf("create failed: %q", got)
	}
	if entries, _ := ListJournalFiles(journalDir); len(entries) != 0 {
		t.Fatalf("journal not cleaned: %v", entries)
	}
}

func TestTxnRollbackOnFailure(t *testing.T) {
	dir := t.TempDir()
	journalDir := filepath.Join(dir, ".journal")
	writeTestFile(t, filepath.Join(dir, "a.yaml"), "old-a")
	// A file inside a read-only subdirectory makes the delete fail.
	sub := filepath.Join(dir, "sub")
	writeTestFile(t, filepath.Join(sub, "gone.yaml"), "old-gone")
	if err := os.Chmod(sub, 0500); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { os.Chmod(sub, 0700) })

	txn := NewTransaction(journalDir)
	if err := txn.Replace(filepath.Join(dir, "a.yaml"), []byte("new-a"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := txn.Delete(filepath.Join(sub, "gone.yaml")); err != nil {
		t.Fatal(err)
	}
	if err := txn.Commit(); err == nil {
		t.Fatal("expected commit to fail")
	}

	if got := readTestFile(t, filepath.Join(dir, "a.yaml")); got != "old-a" {
		t.Fatalf("rollback did not restore a.yaml: %q", got)
	}
	if got := readTestFile(t, filepath.Join(sub, "gone.yaml")); got != "old-gone" {
		t.Fatalf("rollback did not restore deleted file: %q", got)
	}
	if entries, _ := ListJournalFiles(journalDir); len(entries) != 0 {
		t.Fatalf("journal not cleaned after rollback: %v", entries)
	}
}

func TestRecoverRollsBackLeftoverJournal(t *testing.T) {
	// Simulate a crash: journal exists (it says a.yaml becomes new-a and
	// c.yaml is deleted) but the operations never ran.
	dir := t.TempDir()
	journalDir := filepath.Join(dir, ".journal")
	writeTestFile(t, filepath.Join(dir, "a.yaml"), "old-a")
	writeTestFile(t, filepath.Join(dir, "c.yaml"), "old-c")

	txn := NewTransaction(journalDir)
	if err := txn.Replace(filepath.Join(dir, "a.yaml"), []byte("new-a"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := txn.Delete(filepath.Join(dir, "c.yaml")); err != nil {
		t.Fatal(err)
	}
	journal, err := txn.MarshalJournal()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(journalDir, 0700); err != nil {
		t.Fatal(err)
	}
	if err := WriteFile(filepath.Join(journalDir, "txn-crash.json"), journal, 0600); err != nil {
		t.Fatal(err)
	}

	if err := Recover(journalDir); err != nil {
		t.Fatalf("recover: %v", err)
	}
	if got := readTestFile(t, filepath.Join(dir, "a.yaml")); got != "old-a" {
		t.Fatalf("recovery did not roll back a.yaml: %q", got)
	}
	if got := readTestFile(t, filepath.Join(dir, "c.yaml")); got != "old-c" {
		t.Fatalf("recovery resurrected a delete: %q", got)
	}
	if entries, _ := ListJournalFiles(journalDir); len(entries) != 0 {
		t.Fatalf("recovered journal not removed: %v", entries)
	}
}

func TestRecoverDropsCorruptJournal(t *testing.T) {
	dir := t.TempDir()
	journalDir := filepath.Join(dir, ".journal")
	if err := os.MkdirAll(journalDir, 0700); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(journalDir, "txn-bad.json"), []byte("{not json"), 0600); err != nil {
		t.Fatal(err)
	}
	if err := Recover(journalDir); err != nil {
		t.Fatalf("recover corrupt: %v", err)
	}
	if entries, _ := ListJournalFiles(journalDir); len(entries) != 0 {
		t.Fatalf("corrupt journal not removed: %v", entries)
	}
}

func TestTxnMoveOverwritesDestination(t *testing.T) {
	dir := t.TempDir()
	journalDir := filepath.Join(dir, ".journal")
	writeTestFile(t, filepath.Join(dir, "src.yaml"), "src-content")
	writeTestFile(t, filepath.Join(dir, "dst.yaml"), "dst-content")

	txn := NewTransaction(journalDir)
	if err := txn.Move(filepath.Join(dir, "src.yaml"), filepath.Join(dir, "dst.yaml")); err != nil {
		t.Fatal(err)
	}
	if err := txn.Commit(); err != nil {
		t.Fatalf("commit: %v", err)
	}
	if got := readTestFile(t, filepath.Join(dir, "dst.yaml")); got != "src-content" {
		t.Fatalf("move did not overwrite destination: %q", got)
	}
	if _, err := os.Stat(filepath.Join(dir, "src.yaml")); !os.IsNotExist(err) {
		t.Fatalf("source not removed: %v", err)
	}
	if !strings.HasSuffix(readTestFile(t, filepath.Join(dir, "dst.yaml")), "content") {
		t.Fatal("unreachable")
	}
}