forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_runner_label_test.go
279 lines (269 loc) · 7.9 KB
/
rule_runner_label_test.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package actionlint
import (
"strings"
"testing"
)
func TestRuleRunnerLabelCheckLabels(t *testing.T) {
testCases := []struct {
what string
labels []string
matrix []string
known []string
errs []string
}{
// Normal cases
{
what: "simple GH-hosted Linux runner label",
labels: []string{"ubuntu-20.04"},
},
{
what: "simple GH-hosted Windows runner label",
labels: []string{"windows-latest"},
},
{
what: "simple GH-hosted macOS runner label",
labels: []string{"macos-11.0"},
},
{
what: "simple GH-hosted runner label in upper case",
labels: []string{"macOS-11"},
},
{
what: "self-hosted Linux runner",
labels: []string{"self-hosted", "linux"},
},
{
what: "self-hosted all Linux runner labels",
labels: []string{"self-hosted", "linux", "ubuntu-20.04", "ubuntu-latest"},
},
{
what: "self-hosted all macOS runner labels",
labels: []string{"self-hosted", "macOS", "macOS-latest", "macOS-11.0", "macOS-11"},
},
{
what: "self-hosted Linux runner in upper case",
labels: []string{"SELF-HOSTED", "LINUX"},
},
{
what: "self-hosted macOS runner",
labels: []string{"self-hosted", "macos", "arm64"},
},
{
what: "self-hosted runner with GH-hosted runner label",
labels: []string{"self-hosted", "ubuntu-20.04"},
},
{
what: "multiple labels for GH-hosted runner",
labels: []string{"ubuntu-latest", "ubuntu-20.04"},
},
{
what: "user-defined labels",
labels: []string{"self-hosted", "foo", "bar", "linux"},
known: []string{"foo", "bar"},
},
{
what: "matrix",
labels: []string{"${{matrix.os}}"},
matrix: []string{"ubuntu-latest", "windows-latest"},
},
{
what: "matrix at second label",
labels: []string{"self-hosted", "${{matrix.os}}"},
matrix: []string{"linux", "windows"},
},
{
what: "matrix at first label",
labels: []string{"${{matrix.os}}", "ubuntu-20.04"},
matrix: []string{"ubuntu-latest"},
},
{
what: "user-defined label with matrix",
labels: []string{"self-hosted", "${{matrix.os}}"},
matrix: []string{"foo", "bar"},
known: []string{"foo", "bar"},
},
{
what: "cannot check label: prefix",
labels: []string{"foo-${{matrix.os}}"},
matrix: []string{"ubuntu-latest"},
},
{
what: "cannot check label: siffux",
labels: []string{"${{matrix.os}}-bar"},
matrix: []string{"ubuntu-latest"},
},
{
what: "cannot check label: not a matrix",
labels: []string{"${{fromJSON(env.FOO).os}}"},
matrix: []string{"ubuntu-latest"},
},
{
what: "cannot check label: not a matrix 2",
labels: []string{"${{env.os}}"},
matrix: []string{"ubuntu-latest"},
},
{
what: "cannot check label: matrix object",
labels: []string{"${{matrix}}"},
matrix: []string{"ubuntu-latest"},
},
{
what: "cannot parse expression",
labels: []string{"${{}}"},
matrix: []string{"ubuntu-latest"},
},
{
what: "give up checking matrix value containing expression",
labels: []string{"${{matrix.os}}"},
matrix: []string{"ubuntu-latest", "${{env.OS}}"},
},
{
what: "use matrix value but no matrix exist",
labels: []string{"${{matrix.os}}"},
},
// TODO: Add tests for 'include:'
// TODO: Check matrix with 'include:'
// Error cases
{
what: "undefined label",
labels: []string{"linux-latest"},
errs: []string{`"linux-latest" is unknown`},
},
{
what: "undefined self-hosted label",
labels: []string{"self-hosted", "foo"},
errs: []string{`"foo" is unknown`},
},
{
what: "GH-hosted runner labels conflict",
labels: []string{"ubuntu-latest", "windows-latest"},
errs: []string{`label "windows-latest" conflicts with label "ubuntu-latest"`},
},
{
what: "self-hosted runner labels conflict",
labels: []string{"self-hosted", "windows", "linux"},
errs: []string{`label "linux" conflicts with label "windows"`},
},
{
what: "self-hosted runner labels conflict with GH-hosted runner label",
labels: []string{"self-hosted", "windows", "macOS-latest"},
errs: []string{`label "macOS-latest" conflicts with label "windows"`},
},
{
what: "GH-hosted labels multiple conflicts",
labels: []string{"ubuntu-latest", "windows-latest", "macos-latest"},
errs: []string{
`label "windows-latest" conflicts with label "ubuntu-latest"`,
`label "macos-latest" conflicts with label "ubuntu-latest"`,
},
},
{
what: "GH-hosted labels conflict mixed with self-hosted runner labels",
labels: []string{"self-hosted", "ubuntu-latest", "x64", "windows-latest", "foo"},
known: []string{"foo"},
errs: []string{`label "windows-latest" conflicts with label "ubuntu-latest"`},
},
{
what: "GH-hosted labels conflict ignore case",
labels: []string{"macOS-latest", "Windows-latest"},
errs: []string{`label "Windows-latest" conflicts with label "macOS-latest"`},
},
{
what: "GH-hosted labels conflict with matrix at second label",
labels: []string{"ubuntu-latest", "${{matrix.os}}"},
matrix: []string{"windows-latest", "macos-latest"},
errs: []string{
`label "windows-latest" conflicts with label "ubuntu-latest"`,
`label "macos-latest" conflicts with label "ubuntu-latest"`,
},
},
{
what: "GH-hosted labels conflict with matrix at first label",
labels: []string{"${{matrix.os}}", "ubuntu-latest"},
matrix: []string{"windows-latest", "macos-latest"},
errs: []string{`label "ubuntu-latest" conflicts with label`},
},
{
what: "GH-hosted labels conflicts with multiple matrixes",
labels: []string{"${{matrix.os}}", "${{matrix.os}}"},
matrix: []string{"windows-latest", "macos-latest"},
errs: []string{
`label "windows-latest" conflicts with label "macos-latest"`,
`label "macos-latest" conflicts with label "windows-latest"`,
},
},
// TODO: Add error tests for 'include:'
}
for _, tc := range testCases {
t.Run(tc.what, func(t *testing.T) {
pos := &Pos{}
labels := make([]*String, 0, len(tc.labels))
for _, l := range tc.labels {
labels = append(labels, &String{l, false, pos})
}
node := &Job{
RunsOn: &Runner{
Labels: labels,
},
}
if tc.matrix != nil {
n := &String{"os", false, pos}
row := make([]RawYAMLValue, 0, len(tc.matrix))
for _, m := range tc.matrix {
row = append(row, &RawYAMLString{m, pos})
}
st := &Strategy{
Matrix: &Matrix{
Rows: map[string]*MatrixRow{
"os": {
Name: n,
Values: row,
},
},
},
Pos: pos,
}
node.Strategy = st
}
rule := NewRuleRunnerLabel(tc.known)
if err := rule.VisitJobPre(node); err != nil {
t.Fatal(err)
}
errs := rule.Errs()
if len(errs) != len(tc.errs) {
t.Fatalf("%d error(s) are wanted but got %d error(s) actually: %v", len(tc.errs), len(errs), errs)
}
for i, want := range tc.errs {
have := errs[i].Error()
if !strings.Contains(have, want) {
t.Fatalf("%q is not contained in error message of errs[%d]: %q", want, i, have)
}
}
})
}
}
func TestRuleRunnerLabelDoNothingOnNoRunsOn(t *testing.T) {
rule := NewRuleRunnerLabel([]string{})
if err := rule.VisitJobPre(&Job{}); err != nil {
t.Fatal(err)
}
if errs := rule.Errs(); len(errs) > 0 {
t.Fatalf("%d error(s) occurred: %v", len(errs), errs)
}
}
func TestRuleRunnerLabelAllGitHubHostedRunnerLabels(t *testing.T) {
all := []string{}
all = append(all, allGitHubHostedRunnerLabels...)
all = append(all, selfHostedRunnerPresetOSLabels...)
if len(all) != len(defaultRunnerOSCompats) {
t.Errorf("%d elements in allGitHubHostedRunnerLabels but %d elements in githubHostedRunnerCompats", len(all), len(defaultRunnerOSCompats))
}
for _, l := range all {
if l != strings.ToLower(l) {
t.Errorf("label %q in allGitHubHostedRunnerLabels is not in lower-case", l)
}
if _, ok := defaultRunnerOSCompats[l]; !ok {
t.Errorf("%q is included in allGitHubHostedRunnerLabels but not included in githubHostedRunnerCompats", l)
}
}
}