From 511387b805977b3524ff89e55301d4f10991ed26 Mon Sep 17 00:00:00 2001 From: Stanislav Jakuschevskij Date: Fri, 18 Apr 2025 11:12:36 +0200 Subject: [PATCH] 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 --- .../pipeline/v1/container_validation_test.go | 139 +++++++++++++++++ pkg/apis/pipeline/v1/task_validation_test.go | 143 ------------------ 2 files changed, 139 insertions(+), 143 deletions(-) diff --git a/pkg/apis/pipeline/v1/container_validation_test.go b/pkg/apis/pipeline/v1/container_validation_test.go index e488043662a..e14601bbe0e 100644 --- a/pkg/apis/pipeline/v1/container_validation_test.go +++ b/pkg/apis/pipeline/v1/container_validation_test.go @@ -508,6 +508,145 @@ func TestStepValidateErrorWithArtifactsRefFlagNotEnabled(t *testing.T) { } } +func TestStepValidateErrorWithStepActionRef(t *testing.T) { + tests := []struct { + name string + Step v1.Step + expectedError apis.FieldError + }{ + { + name: "Cannot use image with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Image: "foo", + }, + expectedError: apis.FieldError{ + Message: "image cannot be used with Ref", + Paths: []string{"image"}, + }, + }, { + name: "Cannot use command with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Command: []string{"foo"}, + }, + expectedError: apis.FieldError{ + Message: "command cannot be used with Ref", + Paths: []string{"command"}, + }, + }, { + name: "Cannot use args with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Args: []string{"foo"}, + }, + expectedError: apis.FieldError{ + Message: "args cannot be used with Ref", + Paths: []string{"args"}, + }, + }, { + name: "Cannot use script with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Script: "echo hi", + }, + expectedError: apis.FieldError{ + Message: "script cannot be used with Ref", + Paths: []string{"script"}, + }, + }, { + name: "Cannot use workingDir with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + WorkingDir: "/workspace", + }, + expectedError: apis.FieldError{ + Message: "working dir cannot be used with Ref", + Paths: []string{"workingDir"}, + }, + }, { + name: "Cannot use env with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Env: []corev1.EnvVar{{ + Name: "env1", + Value: "value1", + }}, + }, + expectedError: apis.FieldError{ + Message: "env cannot be used with Ref", + Paths: []string{"env"}, + }, + }, { + name: "Cannot use params without Ref", + Step: v1.Step{ + Image: "my-image", + Params: v1.Params{{ + Name: "param", + }}, + }, + expectedError: apis.FieldError{ + Message: "params cannot be used without Ref", + Paths: []string{"params"}, + }, + }, { + name: "Cannot use volumeMounts with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "$(params.foo)", + MountPath: "/registry-config", + }}, + }, + expectedError: apis.FieldError{ + Message: "volumeMounts cannot be used with Ref", + Paths: []string{"volumeMounts"}, + }, + }, { + name: "Cannot use results with Ref", + Step: v1.Step{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Results: []v1.StepResult{{ + Name: "result", + }}, + }, + expectedError: apis.FieldError{ + Message: "results cannot be used with Ref", + Paths: []string{"results"}, + }, + }, + } + for _, st := range tests { + t.Run(st.name, func(t *testing.T) { + ctx := t.Context() + ctx = apis.WithinCreate(ctx) + err := st.Step.Validate(ctx) + if err == nil { + t.Fatalf("Expected an error, got nothing for %v", st.Step) + } + if d := cmp.Diff(st.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" { + t.Errorf("Step.Validate() errors diff %s", diff.PrintWantGot(d)) + } + }) + } +} + func TestSidecarValidate(t *testing.T) { tests := []struct { name string diff --git a/pkg/apis/pipeline/v1/task_validation_test.go b/pkg/apis/pipeline/v1/task_validation_test.go index a533e8ebbcc..a6c68d199f4 100644 --- a/pkg/apis/pipeline/v1/task_validation_test.go +++ b/pkg/apis/pipeline/v1/task_validation_test.go @@ -1364,149 +1364,6 @@ func TestTaskSpecValidateError(t *testing.T) { } } -func TestTaskSpecValidateErrorWithStepActionRef(t *testing.T) { - tests := []struct { - name string - Steps []v1.Step - expectedError apis.FieldError - }{ - { - name: "Cannot use image with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Image: "foo", - }}, - expectedError: apis.FieldError{ - Message: "image cannot be used with Ref", - Paths: []string{"steps[0].image"}, - }, - }, { - name: "Cannot use command with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Command: []string{"foo"}, - }}, - expectedError: apis.FieldError{ - Message: "command cannot be used with Ref", - Paths: []string{"steps[0].command"}, - }, - }, { - name: "Cannot use args with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Args: []string{"foo"}, - }}, - expectedError: apis.FieldError{ - Message: "args cannot be used with Ref", - Paths: []string{"steps[0].args"}, - }, - }, { - name: "Cannot use script with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Script: "echo hi", - }}, - expectedError: apis.FieldError{ - Message: "script cannot be used with Ref", - Paths: []string{"steps[0].script"}, - }, - }, { - name: "Cannot use workingDir with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - WorkingDir: "/workspace", - }}, - expectedError: apis.FieldError{ - Message: "working dir cannot be used with Ref", - Paths: []string{"steps[0].workingDir"}, - }, - }, { - name: "Cannot use env with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Env: []corev1.EnvVar{{ - Name: "env1", - Value: "value1", - }}, - }}, - expectedError: apis.FieldError{ - Message: "env cannot be used with Ref", - Paths: []string{"steps[0].env"}, - }, - }, { - name: "Cannot use params without Ref", - Steps: []v1.Step{{ - Image: "my-image", - Params: v1.Params{{ - Name: "param", - }}, - }}, - expectedError: apis.FieldError{ - Message: "params cannot be used without Ref", - Paths: []string{"steps[0].params"}, - }, - }, { - name: "Cannot use volumeMounts with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - VolumeMounts: []corev1.VolumeMount{{ - Name: "$(params.foo)", - MountPath: "/registry-config", - }}, - }}, - expectedError: apis.FieldError{ - Message: "volumeMounts cannot be used with Ref", - Paths: []string{"steps[0].volumeMounts"}, - }, - }, { - name: "Cannot use results with Ref", - Steps: []v1.Step{{ - Ref: &v1.Ref{ - Name: "stepAction", - }, - Results: []v1.StepResult{{ - Name: "result", - }}, - }}, - expectedError: apis.FieldError{ - Message: "results cannot be used with Ref", - Paths: []string{"steps[0].results"}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - ts := v1.TaskSpec{ - Steps: tt.Steps, - } - ctx := t.Context() - ctx = apis.WithinCreate(ctx) - ts.SetDefaults(ctx) - err := ts.Validate(ctx) - if err == nil { - t.Fatalf("Expected an error, got nothing for %v", ts) - } - if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" { - t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d)) - } - }) - } -} - func TestTaskSpecValidateErrorWithStepResultRef(t *testing.T) { tests := []struct { name string