Skip to content

Commit b73856b

Browse files
committed
refactor: move Step onError and API version 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`. `TestStepOnError` is moved. `TestIncompatibleAPIVersions` is broken down and the Step fields related tests i.e. "windows script support", "stdout stream support" and "stderr stream support" are moved to a new test `TestStepIncompatibleAPIVersions`. Issue #8700. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent b0ca6fe commit b73856b

File tree

2 files changed

+142
-99
lines changed

2 files changed

+142
-99
lines changed

pkg/apis/pipeline/v1/container_validation_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,148 @@ func TestRef_Invalid(t *testing.T) {
171171
}
172172
}
173173

174+
func TestStepOnError(t *testing.T) {
175+
tests := []struct {
176+
name string
177+
params []v1.ParamSpec
178+
step v1.Step
179+
expectedError *apis.FieldError
180+
}{{
181+
name: "valid step - valid onError usage - set to continue",
182+
step: v1.Step{
183+
OnError: v1.Continue,
184+
Image: "image",
185+
Args: []string{"arg"},
186+
},
187+
}, {
188+
name: "valid step - valid onError usage - set to stopAndFail",
189+
step: v1.Step{
190+
OnError: v1.StopAndFail,
191+
Image: "image",
192+
Args: []string{"arg"},
193+
},
194+
}, {
195+
name: "valid step - valid onError usage - set to a task parameter",
196+
params: []v1.ParamSpec{{
197+
Name: "CONTINUE",
198+
Default: &v1.ParamValue{Type: v1.ParamTypeString, StringVal: string(v1.Continue)},
199+
}},
200+
step: v1.Step{
201+
OnError: "$(params.CONTINUE)",
202+
Image: "image",
203+
Args: []string{"arg"},
204+
},
205+
}, {
206+
name: "invalid step - onError set to invalid value",
207+
step: v1.Step{
208+
OnError: "onError",
209+
Image: "image",
210+
Args: []string{"arg"},
211+
},
212+
expectedError: &apis.FieldError{
213+
Message: `invalid value: "onError"`,
214+
Paths: []string{"onError"},
215+
Details: `Task step onError must be either "continue" or "stopAndFail"`,
216+
},
217+
}}
218+
for _, st := range tests {
219+
t.Run(st.name, func(t *testing.T) {
220+
ctx := context.Background()
221+
err := st.step.Validate(ctx)
222+
if st.expectedError == nil && err != nil {
223+
t.Errorf("No error expected from Step.Validate() but got = %v", err)
224+
} else if st.expectedError != nil {
225+
if err == nil {
226+
t.Errorf("Expected error from Step.Validate() = %v, but got none", st.expectedError)
227+
} else if d := cmp.Diff(st.expectedError.Error(), err.Error()); d != "" {
228+
t.Errorf("returned error from Step.Validate() does not match with the expected error: %s", diff.PrintWantGot(d))
229+
}
230+
}
231+
})
232+
}
233+
}
234+
235+
// TestStepIncompatibleAPIVersions exercises validation of fields in a Step
236+
// that require a specific feature gate version in order to work.
237+
func TestStepIncompatibleAPIVersions(t *testing.T) {
238+
versions := []string{"alpha", "beta", "stable"}
239+
isStricterThen := func(first, second string) bool {
240+
// assume values are in order alpha (less strict), beta, stable (strictest)
241+
// return true if first is stricter then second
242+
switch first {
243+
case second, "alpha":
244+
return false
245+
case "stable":
246+
return true
247+
default:
248+
// first is beta, true is second is alpha, false is second is stable
249+
return second == "alpha"
250+
}
251+
}
252+
253+
for _, st := range []struct {
254+
name string
255+
requiredVersion string
256+
step v1.Step
257+
}{
258+
{
259+
name: "windows script support requires alpha",
260+
requiredVersion: "alpha",
261+
step: v1.Step{
262+
Image: "my-image",
263+
Script: `
264+
#!win powershell -File
265+
script-1`,
266+
},
267+
}, {
268+
name: "stdout stream support requires alpha",
269+
requiredVersion: "alpha",
270+
step: v1.Step{
271+
Image: "foo",
272+
StdoutConfig: &v1.StepOutputConfig{
273+
Path: "/tmp/stdout.txt",
274+
},
275+
},
276+
}, {
277+
name: "stderr stream support requires alpha",
278+
requiredVersion: "alpha",
279+
step: v1.Step{
280+
Image: "foo",
281+
StderrConfig: &v1.StepOutputConfig{
282+
Path: "/tmp/stderr.txt",
283+
},
284+
},
285+
},
286+
} {
287+
for _, version := range versions {
288+
testName := fmt.Sprintf("(using %s) %s", version, st.name)
289+
t.Run(testName, func(t *testing.T) {
290+
ctx := context.Background()
291+
if version == "alpha" {
292+
ctx = cfgtesting.EnableAlphaAPIFields(ctx)
293+
}
294+
if version == "beta" {
295+
ctx = cfgtesting.EnableBetaAPIFields(ctx)
296+
}
297+
if version == "stable" {
298+
ctx = cfgtesting.EnableStableAPIFields(ctx)
299+
}
300+
err := st.step.Validate(ctx)
301+
302+
// If the configured version is stricter than the required one, we expect an error
303+
if isStricterThen(version, st.requiredVersion) && err == nil {
304+
t.Fatalf("no error received even though version required is %q while feature gate is %q", st.requiredVersion, version)
305+
}
306+
307+
// If the configured version is more permissive than the required one, we expect no error
308+
if isStricterThen(st.requiredVersion, version) && err != nil {
309+
t.Fatalf("error received despite required version and feature gate matching %q: %v", version, err)
310+
}
311+
})
312+
}
313+
}
314+
}
315+
174316
func TestSidecarValidate(t *testing.T) {
175317
tests := []struct {
176318
name string

pkg/apis/pipeline/v1/task_validation_test.go

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,72 +2142,6 @@ func TestStepAndSidecarWorkspacesErrors(t *testing.T) {
21422142
}
21432143
}
21442144

2145-
func TestStepOnError(t *testing.T) {
2146-
tests := []struct {
2147-
name string
2148-
params []v1.ParamSpec
2149-
steps []v1.Step
2150-
expectedError *apis.FieldError
2151-
}{{
2152-
name: "valid step - valid onError usage - set to continue",
2153-
steps: []v1.Step{{
2154-
OnError: v1.Continue,
2155-
Image: "image",
2156-
Args: []string{"arg"},
2157-
}},
2158-
}, {
2159-
name: "valid step - valid onError usage - set to stopAndFail",
2160-
steps: []v1.Step{{
2161-
OnError: v1.StopAndFail,
2162-
Image: "image",
2163-
Args: []string{"arg"},
2164-
}},
2165-
}, {
2166-
name: "valid step - valid onError usage - set to a task parameter",
2167-
params: []v1.ParamSpec{{
2168-
Name: "CONTINUE",
2169-
Default: &v1.ParamValue{Type: v1.ParamTypeString, StringVal: string(v1.Continue)},
2170-
}},
2171-
steps: []v1.Step{{
2172-
OnError: "$(params.CONTINUE)",
2173-
Image: "image",
2174-
Args: []string{"arg"},
2175-
}},
2176-
}, {
2177-
name: "invalid step - onError set to invalid value",
2178-
steps: []v1.Step{{
2179-
OnError: "onError",
2180-
Image: "image",
2181-
Args: []string{"arg"},
2182-
}},
2183-
expectedError: &apis.FieldError{
2184-
Message: `invalid value: "onError"`,
2185-
Paths: []string{"steps[0].onError"},
2186-
Details: `Task step onError must be either "continue" or "stopAndFail"`,
2187-
},
2188-
}}
2189-
for _, tt := range tests {
2190-
t.Run(tt.name, func(t *testing.T) {
2191-
ts := &v1.TaskSpec{
2192-
Params: tt.params,
2193-
Steps: tt.steps,
2194-
}
2195-
ctx := context.Background()
2196-
ts.SetDefaults(ctx)
2197-
err := ts.Validate(ctx)
2198-
if tt.expectedError == nil && err != nil {
2199-
t.Errorf("No error expected from TaskSpec.Validate() but got = %v", err)
2200-
} else if tt.expectedError != nil {
2201-
if err == nil {
2202-
t.Errorf("Expected error from TaskSpec.Validate() = %v, but got none", tt.expectedError)
2203-
} else if d := cmp.Diff(tt.expectedError.Error(), err.Error()); d != "" {
2204-
t.Errorf("returned error from TaskSpec.Validate() does not match with the expected error: %s", diff.PrintWantGot(d))
2205-
}
2206-
}
2207-
})
2208-
}
2209-
}
2210-
22112145
// TestIncompatibleAPIVersions exercises validation of fields that
22122146
// require a specific feature gate version in order to work.
22132147
func TestIncompatibleAPIVersions(t *testing.T) {
@@ -2262,39 +2196,6 @@ func TestIncompatibleAPIVersions(t *testing.T) {
22622196
}},
22632197
}},
22642198
},
2265-
}, {
2266-
name: "windows script support requires alpha",
2267-
requiredVersion: "alpha",
2268-
spec: v1.TaskSpec{
2269-
Steps: []v1.Step{{
2270-
Image: "my-image",
2271-
Script: `
2272-
#!win powershell -File
2273-
script-1`,
2274-
}},
2275-
},
2276-
}, {
2277-
name: "stdout stream support requires alpha",
2278-
requiredVersion: "alpha",
2279-
spec: v1.TaskSpec{
2280-
Steps: []v1.Step{{
2281-
Image: "foo",
2282-
StdoutConfig: &v1.StepOutputConfig{
2283-
Path: "/tmp/stdout.txt",
2284-
},
2285-
}},
2286-
},
2287-
}, {
2288-
name: "stderr stream support requires alpha",
2289-
requiredVersion: "alpha",
2290-
spec: v1.TaskSpec{
2291-
Steps: []v1.Step{{
2292-
Image: "foo",
2293-
StderrConfig: &v1.StepOutputConfig{
2294-
Path: "/tmp/stderr.txt",
2295-
},
2296-
}},
2297-
},
22982199
},
22992200
} {
23002201
for _, version := range versions {

0 commit comments

Comments
 (0)