-
Notifications
You must be signed in to change notification settings - Fork 163
/
rule_workflow_call_test.go
385 lines (358 loc) · 9.91 KB
/
rule_workflow_call_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package actionlint
import (
"fmt"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRuleWorkflowCallCheckWorkflowCallUsesFormat(t *testing.T) {
tests := []struct {
uses string
ok bool
}{
{"owner/repo/x.yml@ref", true},
{"owner/repo/x.yml@@", true},
{"owner/repo/x.yml@release/v1", true},
{"./path/to/x.yml", true},
{"${{ env.FOO }}", true},
{"./path/to/x.yml@ref", false},
{"/path/to/x.yml@ref", false},
{"./", false},
{".", false},
{"owner/x.yml@ref", false},
{"owner/repo@ref", false},
{"owner/repo/x.yml", false},
{"/repo/x.yml@ref", false},
{"owner//x.yml@ref", false},
{"owner/repo/@ref", false},
{"owner/repo/x.yml@", false},
}
for _, tc := range tests {
t.Run(tc.uses, func(t *testing.T) {
c := NewLocalReusableWorkflowCache(nil, "", nil)
r := NewRuleWorkflowCall("", c)
j := &Job{
WorkflowCall: &WorkflowCall{
Uses: &String{
Value: tc.uses,
Pos: &Pos{},
},
},
}
err := r.VisitJobPre(j)
if err != nil {
t.Fatal(err)
}
errs := r.Errs()
if tc.ok && len(errs) > 0 {
t.Fatalf("Error occurred: %v", errs)
}
if !tc.ok {
if len(errs) > 2 || len(errs) == 0 {
t.Fatalf("Wanted one error but have: %v", errs)
}
}
})
}
}
func TestRuleWorkflowCallNestedWorkflowCalls(t *testing.T) {
w := &Workflow{
On: []Event{
&WorkflowCallEvent{
Pos: &Pos{},
},
},
}
j := &Job{
WorkflowCall: &WorkflowCall{
Uses: &String{
Value: "o/r/w.yaml@r",
Pos: &Pos{},
},
},
}
c := NewLocalReusableWorkflowCache(nil, "", nil)
r := NewRuleWorkflowCall("", c)
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
if err := r.VisitJobPre(j); err != nil {
t.Fatal(err)
}
errs := r.Errs()
if len(errs) > 0 {
t.Fatal("unexpected errors:", errs)
}
}
func TestRuleWorkflowCallWriteEventNodeToMetadataCache(t *testing.T) {
s := func(v string) *String {
return &String{Value: v, Pos: &Pos{}}
}
w := &Workflow{
On: []Event{
&WorkflowCallEvent{
Inputs: []*WorkflowCallEventInput{
{
Name: s("input1"),
Type: WorkflowCallEventInputTypeString,
ID: "input1",
},
},
Outputs: map[string]*WorkflowCallEventOutput{
"output1": {Name: s("output1")},
},
Secrets: map[string]*WorkflowCallEventSecret{
"secret1": {Name: s("secret1")},
},
Pos: &Pos{},
},
},
}
cwd := filepath.Join("path", "to", "project")
c := NewLocalReusableWorkflowCache(&Project{cwd, nil}, cwd, nil)
r := NewRuleWorkflowCall("test-workflow.yaml", c)
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
errs := r.Errs()
if len(errs) > 0 {
t.Fatal(errs)
}
m, ok := c.readCache("./test-workflow.yaml")
if !ok {
t.Fatal("no metadata was created")
}
want := &ReusableWorkflowMetadata{
Inputs: ReusableWorkflowMetadataInputs{
"input1": {"input1", false, StringType{}},
},
Outputs: ReusableWorkflowMetadataOutputs{
"output1": {"output1"},
},
Secrets: ReusableWorkflowMetadataSecrets{
"secret1": {"secret1", false},
},
}
if diff := cmp.Diff(want, m); diff != "" {
t.Fatal(diff)
}
}
func TestRuleWorkflowCallCheckReusableWorkflowCall(t *testing.T) {
cwd := filepath.Join("testdata", "reusable_workflow_metadata")
cache := NewLocalReusableWorkflowCache(&Project{cwd, nil}, cwd, nil)
for i, md := range []*ReusableWorkflowMetadata{
// workflow0.yaml
{
Inputs: ReusableWorkflowMetadataInputs{
"optional_input": {"optional_input", false, StringType{}},
"required_input": {"required_input", true, StringType{}},
},
Outputs: ReusableWorkflowMetadataOutputs{
"output": {"output"},
},
Secrets: ReusableWorkflowMetadataSecrets{
"optional_secret": {"optional_secret", false},
"required_secret": {"required_secret", true},
},
},
// workflow1.yaml: Inputs and outputs in upper case (#216)
{
Inputs: ReusableWorkflowMetadataInputs{
"optional_input": {"OPTIONAL_INPUT", false, StringType{}},
"required_input": {"REQUIRED_INPUT", true, StringType{}},
},
Outputs: ReusableWorkflowMetadataOutputs{
"output": {"OUTPUT"},
},
Secrets: ReusableWorkflowMetadataSecrets{
"optional_secret": {"OPTIONAL_SECRET", false},
"required_secret": {"REQUIRED_SECRET", true},
},
},
// workflow2.yaml: No input and secret are defined
{
Inputs: ReusableWorkflowMetadataInputs{},
Outputs: ReusableWorkflowMetadataOutputs{},
Secrets: ReusableWorkflowMetadataSecrets{},
},
} {
cache.writeCache(fmt.Sprintf("./workflow%d.yaml", i), md)
}
tests := []struct {
what string
uses string
inputs []string
secrets []string
inheritSecrets bool
errs []string
}{
{
what: "all",
uses: "./workflow0.yaml",
inputs: []string{"optional_input", "required_input"},
secrets: []string{"optional_secret", "required_secret"},
},
{
what: "only required",
uses: "./workflow0.yaml",
inputs: []string{"required_input"},
secrets: []string{"required_secret"},
},
{
what: "unknown workflow",
uses: "./unknown-workflow.yaml",
inputs: []string{"aaa", "bbb"},
secrets: []string{"xxx", "yyy"},
errs: []string{
"could not read reusable workflow file for \"./unknown-workflow.yaml\":",
},
},
{
what: "missing required input and secret",
uses: "./workflow0.yaml",
inputs: []string{"optional_input"},
secrets: []string{"optional_secret"},
errs: []string{
"input \"required_input\" is required",
"secret \"required_secret\" is required",
},
},
{
what: "undefined input and secret",
uses: "./workflow0.yaml",
inputs: []string{"required_input", "unknown_input"},
secrets: []string{"required_secret", "unknown_secret"},
errs: []string{
"input \"unknown_input\" is not defined in \"./workflow0.yaml\" reusable workflow. defined inputs are \"optional_input\", \"required_input\"",
"secret \"unknown_secret\" is not defined in \"./workflow0.yaml\" reusable workflow. defined secrets are \"optional_secret\", \"required_secret\"",
},
},
{
what: "inherit secrets",
uses: "./workflow0.yaml",
inputs: []string{"required_input"},
secrets: []string{"unknown_secret", "optional_secret"},
inheritSecrets: true,
},
{
what: "read workflow",
uses: "./ok.yaml", // Defined in testdata/reusable_workflow_metadata/ok.yaml
inputs: []string{"input2"},
secrets: []string{"secret2"},
},
{
what: "read broken workflow",
uses: "./broken.yaml", // Defined in testdata/reusable_workflow_metadata/broken.yaml
errs: []string{
"error while parsing reusable workflow \"./broken.yaml\"",
},
},
{
what: "external workflow call with no input and no secret",
uses: "owner/repo/path/to/workflow@main",
},
{
what: "external workflow call with inputs and secrets",
uses: "owner/repo/path/to/workflow@main",
inputs: []string{"aaa", "bbb"},
secrets: []string{"xxx", "yyy"},
},
{
what: "call in upper case and workflow in lower case",
uses: "./workflow0.yaml",
inputs: []string{"OPTIONAL_INPUT", "REQUIRED_INPUT"},
secrets: []string{"OPTIONAL_SECRET", "REQUIRED_SECRET"},
},
{
what: "call in lower case and workflow in upper case",
uses: "./workflow1.yaml",
inputs: []string{"optional_input", "required_input"},
secrets: []string{"optional_secret", "required_secret"},
},
{
what: "call in upper case and workflow in upper case",
uses: "./workflow1.yaml",
inputs: []string{"OPTIONAL_INPUT", "REQUIRED_INPUT"},
secrets: []string{"OPTIONAL_SECRET", "REQUIRED_SECRET"},
},
{
what: "undefined upper input and secret",
uses: "./workflow0.yaml",
inputs: []string{"required_input", "UNKNOWN_INPUT"},
secrets: []string{"required_secret", "UNKNOWN_SECRET"},
errs: []string{
"input \"UNKNOWN_INPUT\" is not defined in \"./workflow0.yaml\"",
"secret \"UNKNOWN_SECRET\" is not defined in \"./workflow0.yaml\"",
},
},
{
what: "no input and secret defined",
uses: "./workflow2.yaml",
inputs: []string{"unknown_input"},
secrets: []string{"unknown_secret"},
errs: []string{
"input \"unknown_input\" is not defined in \"./workflow2.yaml\" reusable workflow. no input is defined",
"secret \"unknown_secret\" is not defined in \"./workflow2.yaml\" reusable workflow. no secret is defined",
},
},
}
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
r := NewRuleWorkflowCall("this-workflow.yaml", cache)
w := &Workflow{
On: []Event{
&WorkflowCallEvent{
Pos: &Pos{},
},
},
}
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
c := &WorkflowCall{
Uses: &String{Value: tc.uses, Pos: &Pos{}},
Inputs: map[string]*WorkflowCallInput{},
Secrets: map[string]*WorkflowCallSecret{},
InheritSecrets: tc.inheritSecrets,
}
for _, i := range tc.inputs {
c.Inputs[strings.ToLower(i)] = &WorkflowCallInput{
Name: &String{Value: i, Pos: &Pos{}},
Value: &String{Value: "", Pos: &Pos{}},
}
}
for _, s := range tc.secrets {
c.Secrets[strings.ToLower(s)] = &WorkflowCallSecret{
Name: &String{Value: s, Pos: &Pos{}},
Value: &String{Value: "", Pos: &Pos{}},
}
}
j := &Job{WorkflowCall: c}
if err := r.VisitJobPre(j); err != nil {
t.Fatal(err)
}
errs := []string{}
for _, err := range r.Errs() {
errs = append(errs, err.Error())
}
sort.Strings(errs)
if len(errs) != len(tc.errs) {
t.Fatalf(
"Number of errors is unexpected. %d errors was expected but got %d errors. Expected errors are %v but actual errors are %v",
len(tc.errs),
len(errs),
tc.errs,
errs,
)
}
for i, have := range errs {
want := tc.errs[i]
if !strings.Contains(have, want) {
t.Errorf("%d-th error is unexpected. %q should be contained in error message %q", i, want, have)
}
}
})
}
}