Skip to content

Commit 77dc707

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 <stas@two-giants.com>
1 parent 8da7441 commit 77dc707

File tree

2 files changed

+178
-191
lines changed

2 files changed

+178
-191
lines changed

pkg/apis/pipeline/v1/container_validation_test.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
2929
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
3030
"github.com/tektoncd/pipeline/test/diff"
31+
corev1 "k8s.io/api/core/v1"
3132
"knative.dev/pkg/apis"
3233
)
3334

@@ -171,6 +172,183 @@ func TestRef_Invalid(t *testing.T) {
171172
}
172173
}
173174

175+
func TestStepValidateSuccessWithArtifactsRefFlagEnabled(t *testing.T) {
176+
tests := []struct {
177+
name string
178+
Step v1.Step
179+
}{
180+
{
181+
name: "reference step artifacts in Env",
182+
Step: v1.Step{
183+
Image: "busybox",
184+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
185+
},
186+
},
187+
{
188+
name: "reference step artifacts path in Env",
189+
Step: v1.Step{
190+
Image: "busybox",
191+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
192+
},
193+
},
194+
{
195+
name: "reference step artifacts in Script",
196+
Step: v1.Step{
197+
Image: "busybox",
198+
Script: "echo $(steps.aaa.inputs.bbb)",
199+
},
200+
},
201+
{
202+
name: "reference step artifacts path in Script",
203+
Step: v1.Step{
204+
Image: "busybox",
205+
Script: "echo 123 >> $(step.artifacts.path)",
206+
},
207+
},
208+
{
209+
name: "reference step artifacts in Command",
210+
Step: v1.Step{
211+
Image: "busybox",
212+
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
213+
},
214+
},
215+
{
216+
name: "reference step artifacts path in Command",
217+
Step: v1.Step{
218+
Image: "busybox",
219+
Command: []string{"echo", "$(step.artifacts.path)"},
220+
},
221+
},
222+
{
223+
name: "reference step artifacts in Args",
224+
Step: v1.Step{
225+
Image: "busybox",
226+
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
227+
},
228+
},
229+
{
230+
name: "reference step artifacts path in Args",
231+
Step: v1.Step{
232+
Image: "busybox",
233+
Args: []string{"echo", "$(step.artifacts.path)"},
234+
},
235+
},
236+
}
237+
for _, st := range tests {
238+
t.Run(st.name, func(t *testing.T) {
239+
ctx := config.ToContext(context.Background(), &config.Config{
240+
FeatureFlags: &config.FeatureFlags{
241+
EnableStepActions: true,
242+
EnableArtifacts: true,
243+
},
244+
})
245+
ctx = apis.WithinCreate(ctx)
246+
err := st.Step.Validate(ctx)
247+
if err != nil {
248+
t.Fatalf("Expected no errors, got err for %v", err)
249+
}
250+
})
251+
}
252+
}
253+
254+
func TestStepValidateErrorWithArtifactsRefFlagNotEnabled(t *testing.T) {
255+
tests := []struct {
256+
name string
257+
Step v1.Step
258+
expectedError apis.FieldError
259+
}{
260+
{
261+
name: "Cannot reference step artifacts in Env without setting enable-artifacts to true",
262+
Step: v1.Step{
263+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
264+
},
265+
expectedError: apis.FieldError{
266+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
267+
},
268+
},
269+
{
270+
name: "Cannot reference step artifacts path in Env without setting enable-artifacts to true",
271+
Step: v1.Step{
272+
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
273+
},
274+
expectedError: apis.FieldError{
275+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
276+
},
277+
},
278+
{
279+
name: "Cannot reference step artifacts in Script without setting enable-artifacts to true",
280+
Step: v1.Step{
281+
Script: "echo $(steps.aaa.inputs.bbb)",
282+
},
283+
expectedError: apis.FieldError{
284+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
285+
},
286+
},
287+
{
288+
name: "Cannot reference step artifacts path in Script without setting enable-artifacts to true",
289+
Step: v1.Step{
290+
Script: "echo 123 >> $(step.artifacts.path)",
291+
},
292+
expectedError: apis.FieldError{
293+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
294+
},
295+
},
296+
{
297+
name: "Cannot reference step artifacts in Command without setting enable-artifacts to true",
298+
Step: v1.Step{
299+
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
300+
},
301+
expectedError: apis.FieldError{
302+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
303+
},
304+
},
305+
{
306+
name: "Cannot reference step artifacts path in Command without setting enable-artifacts to true",
307+
Step: v1.Step{
308+
Command: []string{"echo", "$(step.artifacts.path)"},
309+
},
310+
expectedError: apis.FieldError{
311+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
312+
},
313+
},
314+
{
315+
name: "Cannot reference step artifacts in Args without setting enable-artifacts to true",
316+
Step: v1.Step{
317+
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
318+
},
319+
expectedError: apis.FieldError{
320+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
321+
},
322+
},
323+
{
324+
name: "Cannot reference step artifacts path in Args without setting enable-artifacts to true",
325+
Step: v1.Step{
326+
Args: []string{"echo", "$(step.artifacts.path)"},
327+
},
328+
expectedError: apis.FieldError{
329+
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
330+
},
331+
},
332+
}
333+
for _, st := range tests {
334+
t.Run(st.name, func(t *testing.T) {
335+
ctx := config.ToContext(context.Background(), &config.Config{
336+
FeatureFlags: &config.FeatureFlags{
337+
EnableStepActions: true,
338+
},
339+
})
340+
ctx = apis.WithinCreate(ctx)
341+
err := st.Step.Validate(ctx)
342+
if err == nil {
343+
t.Fatalf("Expected an error, got nothing for %v", st.Step)
344+
}
345+
if d := cmp.Diff(st.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
346+
t.Errorf("Step.Validate() errors diff %s", diff.PrintWantGot(d))
347+
}
348+
})
349+
}
350+
}
351+
174352
func TestSidecarValidate(t *testing.T) {
175353
tests := []struct {
176354
name string

pkg/apis/pipeline/v1/task_validation_test.go

Lines changed: 0 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,197 +1806,6 @@ func TestTaskSpecValidateErrorWithStepResultRef(t *testing.T) {
18061806
})
18071807
}
18081808
}
1809-
func TestTaskSpecValidateSuccessWithArtifactsRefFlagEnabled(t *testing.T) {
1810-
tests := []struct {
1811-
name string
1812-
Steps []v1.Step
1813-
}{
1814-
{
1815-
name: "reference step artifacts in Env",
1816-
Steps: []v1.Step{{
1817-
Image: "busybox",
1818-
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
1819-
}},
1820-
},
1821-
{
1822-
name: "reference step artifacts path in Env",
1823-
Steps: []v1.Step{{
1824-
Image: "busybox",
1825-
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
1826-
}},
1827-
},
1828-
{
1829-
name: "reference step artifacts in Script",
1830-
Steps: []v1.Step{{
1831-
Image: "busybox",
1832-
Script: "echo $(steps.aaa.inputs.bbb)",
1833-
}},
1834-
},
1835-
{
1836-
name: "reference step artifacts path in Script",
1837-
Steps: []v1.Step{{
1838-
Image: "busybox",
1839-
Script: "echo 123 >> $(step.artifacts.path)",
1840-
}},
1841-
},
1842-
{
1843-
name: "reference step artifacts in Command",
1844-
Steps: []v1.Step{{
1845-
Image: "busybox",
1846-
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
1847-
}},
1848-
},
1849-
{
1850-
name: "reference step artifacts path in Command",
1851-
Steps: []v1.Step{{
1852-
Image: "busybox",
1853-
Command: []string{"echo", "$(step.artifacts.path)"},
1854-
}},
1855-
},
1856-
{
1857-
name: "reference step artifacts in Args",
1858-
Steps: []v1.Step{{
1859-
Image: "busybox",
1860-
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
1861-
}},
1862-
},
1863-
{
1864-
name: "reference step artifacts path in Args",
1865-
Steps: []v1.Step{{
1866-
Image: "busybox",
1867-
Args: []string{"echo", "$(step.artifacts.path)"},
1868-
}},
1869-
},
1870-
}
1871-
for _, tt := range tests {
1872-
t.Run(tt.name, func(t *testing.T) {
1873-
ts := v1.TaskSpec{
1874-
Steps: tt.Steps,
1875-
}
1876-
ctx := config.ToContext(context.Background(), &config.Config{
1877-
FeatureFlags: &config.FeatureFlags{
1878-
EnableStepActions: true,
1879-
EnableArtifacts: true,
1880-
},
1881-
})
1882-
ctx = apis.WithinCreate(ctx)
1883-
ts.SetDefaults(ctx)
1884-
err := ts.Validate(ctx)
1885-
if err != nil {
1886-
t.Fatalf("Expected no errors, got err for %v", err)
1887-
}
1888-
})
1889-
}
1890-
}
1891-
func TestTaskSpecValidateErrorWithArtifactsRefFlagNotEnabled(t *testing.T) {
1892-
tests := []struct {
1893-
name string
1894-
Steps []v1.Step
1895-
expectedError apis.FieldError
1896-
}{
1897-
{
1898-
name: "Cannot reference step artifacts in Env without setting enable-artifacts to true",
1899-
Steps: []v1.Step{{
1900-
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(steps.aaa.outputs.image)"}},
1901-
}},
1902-
expectedError: apis.FieldError{
1903-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1904-
Paths: []string{"steps[0]"},
1905-
},
1906-
},
1907-
{
1908-
name: "Cannot reference step artifacts path in Env without setting enable-artifacts to true",
1909-
Steps: []v1.Step{{
1910-
Env: []corev1.EnvVar{{Name: "AAA", Value: "$(step.artifacts.path)"}},
1911-
}},
1912-
expectedError: apis.FieldError{
1913-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1914-
Paths: []string{"steps[0]"},
1915-
},
1916-
},
1917-
{
1918-
name: "Cannot reference step artifacts in Script without setting enable-artifacts to true",
1919-
Steps: []v1.Step{{
1920-
Script: "echo $(steps.aaa.inputs.bbb)",
1921-
}},
1922-
expectedError: apis.FieldError{
1923-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1924-
Paths: []string{"steps[0]"},
1925-
},
1926-
},
1927-
{
1928-
name: "Cannot reference step artifacts path in Script without setting enable-artifacts to true",
1929-
Steps: []v1.Step{{
1930-
Script: "echo 123 >> $(step.artifacts.path)",
1931-
}},
1932-
expectedError: apis.FieldError{
1933-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1934-
Paths: []string{"steps[0]"},
1935-
},
1936-
},
1937-
{
1938-
name: "Cannot reference step artifacts in Command without setting enable-artifacts to true",
1939-
Steps: []v1.Step{{
1940-
Command: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
1941-
}},
1942-
expectedError: apis.FieldError{
1943-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1944-
Paths: []string{"steps[0]"},
1945-
},
1946-
},
1947-
{
1948-
name: "Cannot reference step artifacts path in Command without setting enable-artifacts to true",
1949-
Steps: []v1.Step{{
1950-
Command: []string{"echo", "$(step.artifacts.path)"},
1951-
}},
1952-
expectedError: apis.FieldError{
1953-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1954-
Paths: []string{"steps[0]"},
1955-
},
1956-
},
1957-
{
1958-
name: "Cannot reference step artifacts in Args without setting enable-artifacts to true",
1959-
Steps: []v1.Step{{
1960-
Args: []string{"echo", "$(steps.aaa.outputs.bbbb)"},
1961-
}},
1962-
expectedError: apis.FieldError{
1963-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1964-
Paths: []string{"steps[0]"},
1965-
},
1966-
},
1967-
{
1968-
name: "Cannot reference step artifacts path in Args without setting enable-artifacts to true",
1969-
Steps: []v1.Step{{
1970-
Args: []string{"echo", "$(step.artifacts.path)"},
1971-
}},
1972-
expectedError: apis.FieldError{
1973-
Message: fmt.Sprintf("feature flag %s should be set to true to use artifacts feature.", config.EnableArtifacts),
1974-
Paths: []string{"steps[0]"},
1975-
},
1976-
},
1977-
}
1978-
for _, tt := range tests {
1979-
t.Run(tt.name, func(t *testing.T) {
1980-
ts := v1.TaskSpec{
1981-
Steps: tt.Steps,
1982-
}
1983-
ctx := config.ToContext(context.Background(), &config.Config{
1984-
FeatureFlags: &config.FeatureFlags{
1985-
EnableStepActions: true,
1986-
},
1987-
})
1988-
ctx = apis.WithinCreate(ctx)
1989-
ts.SetDefaults(ctx)
1990-
err := ts.Validate(ctx)
1991-
if err == nil {
1992-
t.Fatalf("Expected an error, got nothing for %v", ts)
1993-
}
1994-
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
1995-
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
1996-
}
1997-
})
1998-
}
1999-
}
20001809

20011810
func TestTaskSpecValidateErrorWithArtifactsRef(t *testing.T) {
20021811
tests := []struct {

0 commit comments

Comments
 (0)