-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
231 lines (209 loc) · 6.17 KB
/
Copy pathmain_test.go
File metadata and controls
231 lines (209 loc) · 6.17 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
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
package main
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"
)
// mkScenarios creates the given directory names under a fresh temp dir and
// returns its path. Also creates a "not_a_dir" plain file to verify it's
// filtered out.
func mkScenarios(t *testing.T, names []string) string {
t.Helper()
root := t.TempDir()
for _, n := range names {
if err := os.Mkdir(filepath.Join(root, n), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", n, err)
}
}
if err := os.WriteFile(filepath.Join(root, "not_a_dir"), []byte("x"), 0o644); err != nil {
t.Fatalf("write not_a_dir: %v", err)
}
return root
}
func TestRun_ChunksAlphabetically(t *testing.T) {
root := mkScenarios(t, []string{
// Intentionally unsorted to verify run() sorts before chunking.
"python_basic_idle_3.12",
"python_asyncio_3.12",
"python_basic_memory_3.12",
"python_cpu",
"python_deep_stack_3.12",
"node_heap", // must be filtered out by the pattern
})
got, err := run("python.*", "", root, 3)
if err != nil {
t.Fatal(err)
}
want := []matrixEntry{
{
Shard: "1/2",
Regex: "(^|/)(python_asyncio_3.12|python_basic_idle_3.12|python_basic_memory_3.12)$",
Names: "python_asyncio_3.12, python_basic_idle_3.12, python_basic_memory_3.12",
},
{
Shard: "2/2",
Regex: "(^|/)(python_cpu|python_deep_stack_3.12)$",
Names: "python_cpu, python_deep_stack_3.12",
},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v\nwant %#v", got, want)
}
}
func TestRun_SingleChunkWhenSmall(t *testing.T) {
root := mkScenarios(t, []string{"dotnet_wall", "dotnet_alloc"})
got, err := run("dotnet.*", "", root, 3)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("expected 1 chunk, got %d: %#v", len(got), got)
}
if got[0].Shard != "1/1" {
t.Errorf("shard: got %q want 1/1", got[0].Shard)
}
if got[0].Regex != "(^|/)(dotnet_alloc|dotnet_wall)$" {
t.Errorf("regex: got %q", got[0].Regex)
}
if got[0].Names != "dotnet_alloc, dotnet_wall" {
t.Errorf("names: got %q", got[0].Names)
}
}
func TestRun_AnchoringRejectsSubstringMatches(t *testing.T) {
// Pattern "python_cpu" must NOT pick up "python_cpu_sleep_sync_3.12".
root := mkScenarios(t, []string{
"python_cpu",
"python_cpu_sleep_sync_3.12",
})
got, err := run("python_cpu", "", root, 3)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Regex != "(^|/)(python_cpu)$" {
t.Fatalf("expected only python_cpu, got %#v", got)
}
}
func TestRun_ExactChunkSizeBoundary(t *testing.T) {
// 6 scenarios with chunk size 3 → exactly 2 chunks of 3 (no remainder).
root := mkScenarios(t, []string{
"a", "b", "c", "d", "e", "f",
})
got, err := run(".*", "", root, 3)
if err != nil {
t.Fatal(err)
}
if len(got) != 2 {
t.Fatalf("expected 2 chunks, got %d", len(got))
}
if got[0].Shard != "1/2" || got[1].Shard != "2/2" {
t.Errorf("shard labels: %q %q", got[0].Shard, got[1].Shard)
}
}
func TestRun_ExcludeDropsMatchedNames(t *testing.T) {
// Mirrors the CI python gate: run everything python except the wheel-only
// variants (every *_3.15 plus python_live_heap_3.14).
root := mkScenarios(t, []string{
"python_cpu",
"python_lock_3.14",
"python_lock_3.15",
"python_mem_domain_3.14",
"python_mem_domain_3.15",
"python_live_heap_3.14",
"python_live_heap_3.15",
})
got, err := run("python.*", `_3\.15$|^python_live_heap_3\.14$`, root, 3)
if err != nil {
t.Fatal(err)
}
var names []string
for _, e := range got {
names = append(names, e.Names)
}
joined := strings.Join(names, ", ")
want := "python_cpu, python_lock_3.14, python_mem_domain_3.14, python_live_heap_3.14" # DEMO_FAIL
if joined != want {
t.Fatalf("excluded set wrong:\n got %q\nwant %q", joined, want)
}
}
func TestRun_InvalidExcludeIsError(t *testing.T) {
root := mkScenarios(t, []string{"python_cpu"})
if _, err := run("python.*", "[invalid", root, 3); err == nil {
t.Fatal("expected error on invalid exclude regex")
}
}
func TestRun_NoMatchIsError(t *testing.T) {
root := mkScenarios(t, []string{"python_cpu"})
_, err := run("ruby.*", "", root, 3)
if err == nil {
t.Fatal("expected error when no scenarios match")
}
}
func TestRun_InvalidPatternIsError(t *testing.T) {
root := mkScenarios(t, []string{"python_cpu"})
if _, err := run("[invalid", "", root, 3); err == nil {
t.Fatal("expected error on invalid regex")
}
}
// TestRun_RegexMatchesIntendedDirAndOnlyThat verifies the produced regex
// behaves correctly against the Go test runner's matching surface, which is
// filepath.Dir of each Dockerfile (e.g. "scenarios/python_cpu") — substring
// matches must NOT occur.
func TestRun_RegexMatchesIntendedDirAndOnlyThat(t *testing.T) {
root := mkScenarios(t, []string{
"python_cpu",
"python_cpu_sleep_sync_3.12",
"python_basic_idle_3.12",
})
got, err := run("python.*", "", root, 3)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("expected 1 chunk, got %d", len(got))
}
re := regexp.MustCompile(got[0].Regex)
cases := []struct {
path string
want bool
}{
{"scenarios/python_cpu", true},
{"scenarios/python_cpu_sleep_sync_3.12", true},
{"scenarios/python_basic_idle_3.12", true},
// substring within another name must NOT match
{"scenarios/python_cpu_something_else", false},
// completely unrelated
{"scenarios/ruby_basic", false},
// bare name (no "scenarios/" prefix) must also match — the alternation
// anchor is (^|/) so a leading match works too.
{"python_cpu", true},
}
for _, c := range cases {
if got := re.MatchString(c.path); got != c.want {
t.Errorf("MatchString(%q) = %v, want %v (regex=%s)", c.path, got, c.want, re)
}
}
}
// TestRun_OutputIsValidJSON encodes the output the same way main() does and
// verifies it round-trips to the expected shape.
func TestRun_OutputIsValidJSON(t *testing.T) {
root := mkScenarios(t, []string{"a", "b", "c", "d"})
got, err := run(".*", "", root, 3)
if err != nil {
t.Fatal(err)
}
buf, err := json.Marshal(got)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var decoded []matrixEntry
if err := json.Unmarshal(buf, &decoded); err != nil {
t.Fatalf("unmarshal: %v\njson: %s", err, buf)
}
if !reflect.DeepEqual(decoded, got) {
t.Fatalf("round-trip mismatch:\n got=%#v\nwant=%#v", decoded, got)
}
}