Skip to content

Commit d096b49

Browse files
committed
refactor: move Step artifacts flag tests
Now that `Step` implements the `Validatable` interface the tests for the `Step` validation are moved from `task_validation_test.go` to `container_validation_test.go`. The following two tests are moved and renamed: - `TestTaskSpecValidateErrorWithArtifactsRefFlagNotEnabled` - > `TestStepValidateErrorWithArtifactsRefFlagNotEnabled` - `TestTaskSpecValidateSuccessWithArtifactsRefFlagEnabled` - > `TestStepValidateSuccessWithArtifactsRefFlagEnabled` Issue #8700. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent c12a36e commit d096b49

File tree

2 files changed

+204
-218
lines changed

2 files changed

+204
-218
lines changed

pkg/apis/pipeline/v1/container_validation_test.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,210 @@ func TestStepValidateError(t *testing.T) {
304304
}
305305
}
306306

307+
func TestStepValidateSuccessWithArtifactsRefFlagEnabled(t *testing.T) {
308+
tests := []struct {
309+
name string
310+
Step v1.Step
311+
}{
312+
{
313+
name: "reference step artifacts in Env",
314+
Step: v1.Step{
315+
Image: "busybox",
316+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
317+
},
318+
},
319+
{
320+
name: "reference step artifacts path in Env",
321+
Step: v1.Step{
322+
Image: "busybox",
323+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
324+
},
325+
},
326+
{
327+
name: "reference step artifacts in Script",
328+
Step: v1.Step{
329+
Image: "busybox",
330+
Script: "echo $(steps.aaa.inputs.bbb)",
331+
},
332+
},
333+
{
334+
name: "reference step artifacts path in Script",
335+
Step: v1.Step{
336+
Image: "busybox",
337+
Script: "echo 123 >> $(step.artifacts.path)",
338+
},
339+
},
340+
{
341+
name: "reference step artifacts in Command",
342+
Step: v1.Step{
343+
Image: "busybox",
344+
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
345+
},
346+
},
347+
{
348+
name: "reference step artifacts path in Command",
349+
Step: v1.Step{
350+
Image: "busybox",
351+
Command: []string{"echo", "$(step.artifacts.path)"},
352+
},
353+
},
354+
{
355+
name: "reference step artifacts in Args",
356+
Step: v1.Step{
357+
Image: "busybox",
358+
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
359+
},
360+
},
361+
{
362+
name: "reference step artifacts path in Args",
363+
Step: v1.Step{
364+
Image: "busybox",
365+
Args: []string{"echo", "$(step.artifacts.path)"},
366+
},
367+
},
368+
}
369+
for _, st := range tests {
370+
t.Run(st.name, func(t *testing.T) {
371+
ctx := config.ToContext(t.Context(), &config.Config{
372+
FeatureFlags: &config.FeatureFlags{
373+
EnableStepActions: true,
374+
EnableArtifacts: true,
375+
},
376+
})
377+
ctx = apis.WithinCreate(ctx)
378+
err := st.Step.Validate(ctx)
379+
if err != nil {
380+
t.Fatalf("Expected no errors, got err for %v", err)
381+
}
382+
})
383+
}
384+
}
385+
386+
func TestStepValidateSuccessWithArtifactsRefFlagNotEnabled(t *testing.T) {
387+
tests := []struct {
388+
name string
389+
Step v1.Step
390+
}{
391+
{
392+
name: "script without reference to a step artifact",
393+
Step: v1.Step{
394+
Image: "busybox",
395+
Script: "echo 123",
396+
},
397+
},
398+
}
399+
for _, st := range tests {
400+
t.Run(st.name, func(t *testing.T) {
401+
ctx := config.ToContext(t.Context(), &config.Config{
402+
FeatureFlags: nil,
403+
})
404+
ctx = apis.WithinCreate(ctx)
405+
err := st.Step.Validate(ctx)
406+
if err != nil {
407+
t.Fatalf("Expected no errors, got err for %v", err)
408+
}
409+
})
410+
}
411+
}
412+
413+
func TestStepValidateErrorWithArtifactsRefFlagNotEnabled(t *testing.T) {
414+
tests := []struct {
415+
name string
416+
Step v1.Step
417+
expectedError apis.FieldError
418+
}{
419+
{
420+
name: "Cannot reference step artifacts in Env without setting enable-artifacts to true",
421+
Step: v1.Step{
422+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
423+
},
424+
expectedError: apis.FieldError{
425+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
426+
},
427+
},
428+
{
429+
name: "Cannot reference step artifacts path in Env without setting enable-artifacts to true",
430+
Step: v1.Step{
431+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
432+
},
433+
expectedError: apis.FieldError{
434+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
435+
},
436+
},
437+
{
438+
name: "Cannot reference step artifacts in Script without setting enable-artifacts to true",
439+
Step: v1.Step{
440+
Script: "echo $(steps.aaa.inputs.bbb)",
441+
},
442+
expectedError: apis.FieldError{
443+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
444+
},
445+
},
446+
{
447+
name: "Cannot reference step artifacts path in Script without setting enable-artifacts to true",
448+
Step: v1.Step{
449+
Script: "echo 123 >> $(step.artifacts.path)",
450+
},
451+
expectedError: apis.FieldError{
452+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
453+
},
454+
},
455+
{
456+
name: "Cannot reference step artifacts in Command without setting enable-artifacts to true",
457+
Step: v1.Step{
458+
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
459+
},
460+
expectedError: apis.FieldError{
461+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
462+
},
463+
},
464+
{
465+
name: "Cannot reference step artifacts path in Command without setting enable-artifacts to true",
466+
Step: v1.Step{
467+
Command: []string{"echo", "$(step.artifacts.path)"},
468+
},
469+
expectedError: apis.FieldError{
470+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
471+
},
472+
},
473+
{
474+
name: "Cannot reference step artifacts in Args without setting enable-artifacts to true",
475+
Step: v1.Step{
476+
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
477+
},
478+
expectedError: apis.FieldError{
479+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
480+
},
481+
},
482+
{
483+
name: "Cannot reference step artifacts path in Args without setting enable-artifacts to true",
484+
Step: v1.Step{
485+
Args: []string{"echo", "$(step.artifacts.path)"},
486+
},
487+
expectedError: apis.FieldError{
488+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
489+
},
490+
},
491+
}
492+
for _, st := range tests {
493+
t.Run(st.name, func(t *testing.T) {
494+
ctx := config.ToContext(t.Context(), &config.Config{
495+
FeatureFlags: &config.FeatureFlags{
496+
EnableStepActions: true,
497+
},
498+
})
499+
ctx = apis.WithinCreate(ctx)
500+
err := st.Step.Validate(ctx)
501+
if err == nil {
502+
t.Fatalf("Expected an error, got nothing for %v", st.Step)
503+
}
504+
if d := cmp.Diff(st.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
505+
t.Errorf("Step.Validate() errors diff %s", diff.PrintWantGot(d))
506+
}
507+
})
508+
}
509+
}
510+
307511
func TestSidecarValidate(t *testing.T) {
308512
tests := []struct {
309513
name string

0 commit comments

Comments
 (0)