-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaper_test.go
More file actions
90 lines (79 loc) · 1.95 KB
/
Copy pathreaper_test.go
File metadata and controls
90 lines (79 loc) · 1.95 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
package artifact
import (
"context"
"os"
"path/filepath"
"testing"
"time"
)
func TestReaper_CleansExpiredArtifacts(t *testing.T) {
dir := t.TempDir()
fs := &FilesystemTmpStorage{BasePath: dir}
db := setupTestDB(t)
store := &Store{DB: db}
ctx := context.Background()
execID := createTestExecution(t, db)
// Write a file and create metadata
srcPath := filepath.Join(dir, "src.bin")
if err := os.WriteFile(srcPath, []byte("data"), 0644); err != nil {
t.Fatalf("os.WriteFile: %v", err)
}
key := "wf/" + execID + "/artifact/file.bin"
url, err := fs.Put(ctx, key, srcPath)
if err != nil {
t.Fatalf("fs.Put: %v", err)
}
if err := store.Create(ctx, &Artifact{
ExecutionID: execID,
StepName: "step-a",
Name: "test-artifact",
URL: url,
Size: 4,
}); err != nil {
t.Fatalf("store.Create: %v", err)
}
// Backdate the artifact so it appears expired
_, err = db.ExecContext(ctx, `
UPDATE execution_artifacts SET created_at = NOW() - interval '48 hours'
WHERE execution_id = $1
`, execID)
if err != nil {
t.Fatalf("backdate: %v", err)
}
reaper := &Reaper{
Store: store,
TmpStorage: fs,
Retention: 24 * time.Hour,
}
cleaned, err := reaper.Sweep(ctx)
if err != nil {
t.Fatalf("Sweep: %v", err)
}
if cleaned != 1 {
t.Errorf("cleaned = %d, want 1", cleaned)
}
// Metadata should be gone
arts, listErr := store.ListByExecution(ctx, execID)
if listErr != nil {
t.Fatalf("ListByExecution: %v", listErr)
}
if len(arts) != 0 {
t.Errorf("expected 0 artifacts after sweep, got %d", len(arts))
}
}
func TestReaper_SkipsWhenRetentionZero(t *testing.T) {
dir := t.TempDir()
fs := &FilesystemTmpStorage{BasePath: dir}
reaper := &Reaper{
Store: &Store{}, // won't be called
TmpStorage: fs,
Retention: 0,
}
cleaned, err := reaper.Sweep(context.Background())
if err != nil {
t.Fatalf("Sweep: %v", err)
}
if cleaned != 0 {
t.Errorf("cleaned = %d, want 0 when retention is 0", cleaned)
}
}