aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/cmd_prune_test.go
blob: e3b93981b3c84a7f0f6570756c10d51553618fff (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package cli

import (
	"context"
	"errors"
	"sort"
	"strings"
	"testing"
	"time"

	"tsne.dev/hopper/internal/bunny"
)

func runPrune(source string, api storageAPI, older duration, now time.Time) error {
	cmd := &pruneCommand{concurrency: 8, olderThan: older}
	return cmd.prune(context.Background(), api, source, now)
}

func days(n int) duration { return duration{days: n} }

func sortedDeletes(f *fakeAPI) []string {
	f.mu.Lock()
	defer f.mu.Unlock()
	out := append([]string(nil), f.deletes...)
	sort.Strings(out)
	return out
}

// TestPruneSelectsCandidates covers the core §10 correctness properties: an
// orphan older than the cutoff is deleted; a file still in the release is kept
// even with an old LastChanged; an orphan newer than the cutoff is kept.
func TestPruneSelectsCandidates(t *testing.T) {
	arc := makeArchive(t, []tarEntry{
		{name: "index.html", body: "<html>"},
		{name: "assets/live.js", body: "x"},
	})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)
	recent := now.AddDate(0, 0, -5)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "index.html", LastChanged: old}, // live, old -> kept
		{Path: "assets", IsDirectory: true},
		{Path: "orphan-old.js", Size: 3, LastChanged: old}, // orphan, old -> deleted
		{Path: "orphan-new.js", LastChanged: recent},       // orphan, new -> kept
	}
	fc.lists["assets"] = []bunny.Object{
		{Path: "assets/live.js", LastChanged: old},          // live asset, old -> kept
		{Path: "assets/gone.js", Size: 4, LastChanged: old}, // orphan, old -> deleted
	}
	captureLogs(t)

	if err := runPrune(arc, fc, days(30), now); err != nil {
		t.Fatalf("prune: %v", err)
	}

	got := sortedDeletes(fc)
	want := []string{"assets/gone.js", "orphan-old.js"}
	if strings.Join(got, ",") != strings.Join(want, ",") {
		t.Fatalf("deletes = %v, want %v", got, want)
	}
}

func TestPruneAppliesPrefix(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "app.js", body: "x"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists["foo"] = []bunny.Object{
		{Path: "foo/app.js", LastChanged: old},   // live (prefixed) -> kept
		{Path: "foo/stale.js", LastChanged: old}, // orphan -> deleted
	}
	captureLogs(t)

	cmd := &pruneCommand{concurrency: 8, prefix: "foo", olderThan: days(30)}
	if err := cmd.prune(context.Background(), fc, arc, now); err != nil {
		t.Fatalf("prune: %v", err)
	}
	if got := sortedDeletes(fc); len(got) != 1 || got[0] != "foo/stale.js" {
		t.Fatalf("deletes = %v, want [foo/stale.js]", got)
	}
}

func TestPruneEmptyLiveSetIsFatal(t *testing.T) {
	arc := makeArchive(t, nil)
	fc := newFakeAPI()
	// A List failure would be reported if reached; the empty-guard must fire
	// first, so no Bunny call happens.
	fc.listErr = errors.New("should not be called")
	captureLogs(t)

	err := runPrune(arc, fc, days(30), time.Now())
	if err == nil || !strings.Contains(err.Error(), "empty source") {
		t.Fatalf("expected empty-live-set error, got %v", err)
	}
	if len(fc.deletes) != 0 {
		t.Fatalf("expected no deletes, got %v", fc.deletes)
	}
}

func TestPruneListFailureIsFatal(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "a.js", body: "x"}})
	fc := newFakeAPI()
	fc.listErr = errors.New("boom")
	captureLogs(t)

	if err := runPrune(arc, fc, days(30), time.Now()); err == nil {
		t.Fatal("expected error")
	}
	if len(fc.deletes) != 0 {
		t.Fatalf("expected no deletes, got %v", fc.deletes)
	}
}

func TestPruneDrainAndReport(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "keep.js", body: "x"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "keep.js", LastChanged: old},
		{Path: "a.js", LastChanged: old},
		{Path: "b.js", LastChanged: old},
		{Path: "c.js", LastChanged: old},
	}
	fc.failErr = errors.New("boom")
	fc.failWhen = func(path string) bool { return path == "b.js" }
	_, errb := captureLogs(t)

	err := runPrune(arc, fc, days(30), now)
	var exit exitError
	if !errors.As(err, &exit) {
		t.Fatalf("expected exitError (non-zero exit) on delete failure, got %v", err)
	}
	// One failed delete must not stop the others: all three orphans are attempted.
	if got := sortedDeletes(fc); strings.Join(got, ",") != "a.js,b.js,c.js" {
		t.Fatalf("all candidates must be attempted, got %v", got)
	}
	if !strings.Contains(errb.String(), "b.js") {
		t.Fatalf("failure report must name b.js, got %v", errb.String())
	}
}

// TestPruneRemovesEmptyDirsBottomUp covers §10 step 5: a directory whose only
// file is deleted is removed, and its now-empty parent is removed too.
func TestPruneRemovesEmptyDirsBottomUp(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "index.html", body: "<html>"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "index.html", LastChanged: old},
		{Path: "a", IsDirectory: true},
	}
	fc.lists["a"] = []bunny.Object{{Path: "a/b", IsDirectory: true}}
	fc.lists["a/b"] = []bunny.Object{{Path: "a/b/gone.js", Size: 3, LastChanged: old}}
	captureLogs(t)

	if err := runPrune(arc, fc, days(30), now); err != nil {
		t.Fatalf("prune: %v", err)
	}

	got := sortedDeletes(fc)
	want := []string{"a", "a/b/gone.js"}
	if strings.Join(got, ",") != strings.Join(want, ",") {
		t.Fatalf("deletes = %v, want %v", got, want)
	}
}

// TestPruneKeepsDirWithFailedChild covers §10 step 5: a directory whose file
// delete FAILED must not be removed, because the node survives.
func TestPruneKeepsDirWithFailedChild(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "index.html", body: "<html>"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "index.html", LastChanged: old},
		{Path: "a", IsDirectory: true},
	}
	fc.lists["a"] = []bunny.Object{{Path: "a/gone.js", Size: 3, LastChanged: old}}
	fc.failWhen = func(path string) bool { return path == "a/gone.js" }
	fc.failErr = errors.New("boom")
	captureLogs(t)

	if err := runPrune(arc, fc, days(30), now); err == nil {
		t.Fatal("expected non-zero exit on file-delete failure")
	}
	for _, d := range fc.deletes {
		if d == "a" {
			t.Fatalf("dir with failed child must not be removed, got %v", fc.deletes)
		}
	}
}

// TestPruneEmptyDirFailureNonFatal covers §10 step 6: an empty-dir removal
// failure is warned and counted but never flips the exit code.
func TestPruneEmptyDirFailureNonFatal(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "index.html", body: "<html>"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "index.html", LastChanged: old},
		{Path: "a", IsDirectory: true},
	}
	fc.lists["a"] = []bunny.Object{{Path: "a/gone.js", Size: 3, LastChanged: old}}
	fc.failWhen = func(path string) bool { return path == "a" }
	fc.failErr = errors.New("boom")
	_, errb := captureLogs(t)

	if err := runPrune(arc, fc, days(30), now); err != nil {
		t.Fatalf("empty-dir failure must be non-fatal, got %v", err)
	}
	if !strings.Contains(errb.String(), "Error:") {
		t.Fatalf("expected error on empty-dir failure, got %q", errb.String())
	}
}

func TestPruneDryRunDeletesNothing(t *testing.T) {
	arc := makeArchive(t, []tarEntry{{name: "keep.js", body: "x"}})
	now := time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC)
	old := now.AddDate(0, 0, -100)

	fc := newFakeAPI()
	fc.lists[""] = []bunny.Object{
		{Path: "keep.js", LastChanged: old},
		{Path: "stale.js", LastChanged: old},
	}
	dryRun = true

	if err := runPrune(arc, dryRunStorageAPI{fc}, days(30), now); err != nil {
		t.Fatalf("prune: %v", err)
	}
	if len(fc.deletes) != 0 {
		t.Fatalf("dry-run must delete nothing, got %v", fc.deletes)
	}
}