Skip to content

Commit fb60939

Browse files
twoGiantstekton-robot
authored andcommitted
refactor: move Step Ref 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: - `TestTaskSpecValidateErrorWithStepActionRef` - > `TestStepValidateErrorWithStepActionRef` Issue #8700. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent fe0df5f commit fb60939

File tree

2 files changed

+139
-143
lines changed

2 files changed

+139
-143
lines changed

pkg/apis/pipeline/v1/container_validation_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,145 @@ func TestStepValidateErrorWithArtifactsRefFlagNotEnabled(t *testing.T) {
508508
}
509509
}
510510

511+
func TestStepValidateErrorWithStepActionRef(t *testing.T) {
512+
tests := []struct {
513+
name string
514+
Step v1.Step
515+
expectedError apis.FieldError
516+
}{
517+
{
518+
name: "Cannot use image with Ref",
519+
Step: v1.Step{
520+
Ref: &v1.Ref{
521+
Name: "stepAction",
522+
},
523+
Image: "foo",
524+
},
525+
expectedError: apis.FieldError{
526+
Message: "image cannot be used with Ref",
527+
Paths: []string{"image"},
528+
},
529+
}, {
530+
name: "Cannot use command with Ref",
531+
Step: v1.Step{
532+
Ref: &v1.Ref{
533+
Name: "stepAction",
534+
},
535+
Command: []string{"foo"},
536+
},
537+
expectedError: apis.FieldError{
538+
Message: "command cannot be used with Ref",
539+
Paths: []string{"command"},
540+
},
541+
}, {
542+
name: "Cannot use args with Ref",
543+
Step: v1.Step{
544+
Ref: &v1.Ref{
545+
Name: "stepAction",
546+
},
547+
Args: []string{"foo"},
548+
},
549+
expectedError: apis.FieldError{
550+
Message: "args cannot be used with Ref",
551+
Paths: []string{"args"},
552+
},
553+
}, {
554+
name: "Cannot use script with Ref",
555+
Step: v1.Step{
556+
Ref: &v1.Ref{
557+
Name: "stepAction",
558+
},
559+
Script: "echo hi",
560+
},
561+
expectedError: apis.FieldError{
562+
Message: "script cannot be used with Ref",
563+
Paths: []string{"script"},
564+
},
565+
}, {
566+
name: "Cannot use workingDir with Ref",
567+
Step: v1.Step{
568+
Ref: &v1.Ref{
569+
Name: "stepAction",
570+
},
571+
WorkingDir: "/workspace",
572+
},
573+
expectedError: apis.FieldError{
574+
Message: "working dir cannot be used with Ref",
575+
Paths: []string{"workingDir"},
576+
},
577+
}, {
578+
name: "Cannot use env with Ref",
579+
Step: v1.Step{
580+
Ref: &v1.Ref{
581+
Name: "stepAction",
582+
},
583+
Env: []corev1.EnvVar{{
584+
Name: "env1",
585+
Value: "value1",
586+
}},
587+
},
588+
expectedError: apis.FieldError{
589+
Message: "env cannot be used with Ref",
590+
Paths: []string{"env"},
591+
},
592+
}, {
593+
name: "Cannot use params without Ref",
594+
Step: v1.Step{
595+
Image: "my-image",
596+
Params: v1.Params{{
597+
Name: "param",
598+
}},
599+
},
600+
expectedError: apis.FieldError{
601+
Message: "params cannot be used without Ref",
602+
Paths: []string{"params"},
603+
},
604+
}, {
605+
name: "Cannot use volumeMounts with Ref",
606+
Step: v1.Step{
607+
Ref: &v1.Ref{
608+
Name: "stepAction",
609+
},
610+
VolumeMounts: []corev1.VolumeMount{{
611+
Name: "$(params.foo)",
612+
MountPath: "/registry-config",
613+
}},
614+
},
615+
expectedError: apis.FieldError{
616+
Message: "volumeMounts cannot be used with Ref",
617+
Paths: []string{"volumeMounts"},
618+
},
619+
}, {
620+
name: "Cannot use results with Ref",
621+
Step: v1.Step{
622+
Ref: &v1.Ref{
623+
Name: "stepAction",
624+
},
625+
Results: []v1.StepResult{{
626+
Name: "result",
627+
}},
628+
},
629+
expectedError: apis.FieldError{
630+
Message: "results cannot be used with Ref",
631+
Paths: []string{"results"},
632+
},
633+
},
634+
}
635+
for _, st := range tests {
636+
t.Run(st.name, func(t *testing.T) {
637+
ctx := t.Context()
638+
ctx = apis.WithinCreate(ctx)
639+
err := st.Step.Validate(ctx)
640+
if err == nil {
641+
t.Fatalf("Expected an error, got nothing for %v", st.Step)
642+
}
643+
if d := cmp.Diff(st.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
644+
t.Errorf("Step.Validate() errors diff %s", diff.PrintWantGot(d))
645+
}
646+
})
647+
}
648+
}
649+
511650
func TestSidecarValidate(t *testing.T) {
512651
tests := []struct {
513652
name string

pkg/apis/pipeline/v1/task_validation_test.go

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,149 +1364,6 @@ func TestTaskSpecValidateError(t *testing.T) {
13641364
}
13651365
}
13661366

1367-
func TestTaskSpecValidateErrorWithStepActionRef(t *testing.T) {
1368-
tests := []struct {
1369-
name string
1370-
Steps []v1.Step
1371-
expectedError apis.FieldError
1372-
}{
1373-
{
1374-
name: "Cannot use image with Ref",
1375-
Steps: []v1.Step{{
1376-
Ref: &v1.Ref{
1377-
Name: "stepAction",
1378-
},
1379-
Image: "foo",
1380-
}},
1381-
expectedError: apis.FieldError{
1382-
Message: "image cannot be used with Ref",
1383-
Paths: []string{"steps[0].image"},
1384-
},
1385-
}, {
1386-
name: "Cannot use command with Ref",
1387-
Steps: []v1.Step{{
1388-
Ref: &v1.Ref{
1389-
Name: "stepAction",
1390-
},
1391-
Command: []string{"foo"},
1392-
}},
1393-
expectedError: apis.FieldError{
1394-
Message: "command cannot be used with Ref",
1395-
Paths: []string{"steps[0].command"},
1396-
},
1397-
}, {
1398-
name: "Cannot use args with Ref",
1399-
Steps: []v1.Step{{
1400-
Ref: &v1.Ref{
1401-
Name: "stepAction",
1402-
},
1403-
Args: []string{"foo"},
1404-
}},
1405-
expectedError: apis.FieldError{
1406-
Message: "args cannot be used with Ref",
1407-
Paths: []string{"steps[0].args"},
1408-
},
1409-
}, {
1410-
name: "Cannot use script with Ref",
1411-
Steps: []v1.Step{{
1412-
Ref: &v1.Ref{
1413-
Name: "stepAction",
1414-
},
1415-
Script: "echo hi",
1416-
}},
1417-
expectedError: apis.FieldError{
1418-
Message: "script cannot be used with Ref",
1419-
Paths: []string{"steps[0].script"},
1420-
},
1421-
}, {
1422-
name: "Cannot use workingDir with Ref",
1423-
Steps: []v1.Step{{
1424-
Ref: &v1.Ref{
1425-
Name: "stepAction",
1426-
},
1427-
WorkingDir: "/workspace",
1428-
}},
1429-
expectedError: apis.FieldError{
1430-
Message: "working dir cannot be used with Ref",
1431-
Paths: []string{"steps[0].workingDir"},
1432-
},
1433-
}, {
1434-
name: "Cannot use env with Ref",
1435-
Steps: []v1.Step{{
1436-
Ref: &v1.Ref{
1437-
Name: "stepAction",
1438-
},
1439-
Env: []corev1.EnvVar{{
1440-
Name: "env1",
1441-
Value: "value1",
1442-
}},
1443-
}},
1444-
expectedError: apis.FieldError{
1445-
Message: "env cannot be used with Ref",
1446-
Paths: []string{"steps[0].env"},
1447-
},
1448-
}, {
1449-
name: "Cannot use params without Ref",
1450-
Steps: []v1.Step{{
1451-
Image: "my-image",
1452-
Params: v1.Params{{
1453-
Name: "param",
1454-
}},
1455-
}},
1456-
expectedError: apis.FieldError{
1457-
Message: "params cannot be used without Ref",
1458-
Paths: []string{"steps[0].params"},
1459-
},
1460-
}, {
1461-
name: "Cannot use volumeMounts with Ref",
1462-
Steps: []v1.Step{{
1463-
Ref: &v1.Ref{
1464-
Name: "stepAction",
1465-
},
1466-
VolumeMounts: []corev1.VolumeMount{{
1467-
Name: "$(params.foo)",
1468-
MountPath: "/registry-config",
1469-
}},
1470-
}},
1471-
expectedError: apis.FieldError{
1472-
Message: "volumeMounts cannot be used with Ref",
1473-
Paths: []string{"steps[0].volumeMounts"},
1474-
},
1475-
}, {
1476-
name: "Cannot use results with Ref",
1477-
Steps: []v1.Step{{
1478-
Ref: &v1.Ref{
1479-
Name: "stepAction",
1480-
},
1481-
Results: []v1.StepResult{{
1482-
Name: "result",
1483-
}},
1484-
}},
1485-
expectedError: apis.FieldError{
1486-
Message: "results cannot be used with Ref",
1487-
Paths: []string{"steps[0].results"},
1488-
},
1489-
},
1490-
}
1491-
for _, tt := range tests {
1492-
t.Run(tt.name, func(t *testing.T) {
1493-
ts := v1.TaskSpec{
1494-
Steps: tt.Steps,
1495-
}
1496-
ctx := t.Context()
1497-
ctx = apis.WithinCreate(ctx)
1498-
ts.SetDefaults(ctx)
1499-
err := ts.Validate(ctx)
1500-
if err == nil {
1501-
t.Fatalf("Expected an error, got nothing for %v", ts)
1502-
}
1503-
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
1504-
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
1505-
}
1506-
})
1507-
}
1508-
}
1509-
15101367
func TestTaskSpecValidateErrorWithStepResultRef(t *testing.T) {
15111368
tests := []struct {
15121369
name string

0 commit comments

Comments
 (0)