-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tracker_test.go
More file actions
97 lines (85 loc) · 2.62 KB
/
Copy pathrun_tracker_test.go
File metadata and controls
97 lines (85 loc) · 2.62 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
package main
import "testing"
func TestRunTrackerLifecycle(t *testing.T) {
tracker := newRunTracker(8)
payload := pushPayload{
Repo: "pipe",
Ref: "refs/heads/main",
}
runID := tracker.enqueue(payload, ".pipe.yml", "pipe.log")
if runID == "" {
t.Fatal("expected run id")
}
snap := tracker.snapshot(10)
if snap.Queued != 1 || snap.Running != 0 {
t.Fatalf("unexpected queue/running counters: %#v", snap)
}
if len(snap.Items) != 1 {
t.Fatalf("expected one item, got %d", len(snap.Items))
}
if snap.Items[0].Status != string(jobStatusQueued) {
t.Fatalf("unexpected initial status: %q", snap.Items[0].Status)
}
tracker.markRunning(runID)
snap = tracker.snapshot(10)
if snap.Queued != 0 || snap.Running != 1 {
t.Fatalf("unexpected queue/running counters after running: %#v", snap)
}
if snap.Items[0].Status != string(jobStatusRunning) {
t.Fatalf("unexpected running status: %q", snap.Items[0].Status)
}
tracker.finish(runID, jobStatusOK, "pipeline passed", "abc123")
rec, ok := tracker.get(runID)
if !ok {
t.Fatal("expected run to exist")
}
if rec.Status != string(jobStatusOK) {
t.Fatalf("unexpected final status: %q", rec.Status)
}
if rec.Detail != "pipeline passed" || rec.Commit != "abc123" {
t.Fatalf("unexpected final detail/commit: %#v", rec)
}
if rec.StartedAt.IsZero() || rec.FinishedAt.IsZero() {
t.Fatalf("expected timestamps to be set: %#v", rec)
}
}
func TestRunTrackerTrim(t *testing.T) {
tracker := newRunTracker(2)
payload := pushPayload{
Repo: "pipe",
Ref: "refs/heads/main",
}
first := tracker.enqueue(payload, ".pipe/first.yml", "first.log")
second := tracker.enqueue(payload, ".pipe/second.yml", "second.log")
third := tracker.enqueue(payload, ".pipe/third.yml", "third.log")
if _, ok := tracker.get(first); ok {
t.Fatalf("expected oldest run %q to be trimmed", first)
}
if _, ok := tracker.get(second); !ok {
t.Fatalf("expected second run %q to exist", second)
}
if _, ok := tracker.get(third); !ok {
t.Fatalf("expected third run %q to exist", third)
}
snap := tracker.snapshot(10)
if len(snap.Items) != 2 {
t.Fatalf("expected 2 runs in snapshot, got %d", len(snap.Items))
}
if snap.Items[0].ID != third || snap.Items[1].ID != second {
t.Fatalf("unexpected run order: %#v", snap.Items)
}
}
func TestRunTrackerTagRefName(t *testing.T) {
tracker := newRunTracker(4)
runID := tracker.enqueue(pushPayload{
Repo: "pipe",
Ref: "refs/tags/v1.2.3",
}, ".pipe/release.yml", "release.log")
rec, ok := tracker.get(runID)
if !ok {
t.Fatal("expected run to exist")
}
if rec.Branch != "v1.2.3" {
t.Fatalf("expected tracked ref name for tag, got %q", rec.Branch)
}
}