Skip to content

Commit 68f292d

Browse files
authored
fix(compiler): prevent same-name stages in pipeline configs (#1360)
* fix(compiler): prevent same-name stages in pipeline configs * lintfix
1 parent 6bdd986 commit 68f292d

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

compiler/native/validate.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,17 @@ func validatePipelineStages(s pipeline.StageSlice) error {
166166
reportMap := make(map[string]string)
167167
reportCount := 0
168168

169-
nameMap := make(map[string]bool)
169+
stageNameMap := make(map[string]bool)
170+
stepNameMap := make(map[string]bool)
170171

171172
for _, stage := range s {
172-
err := validatePipelineContainers(stage.Steps, &reportCount, reportMap, nameMap, stage.Name)
173+
if _, ok := stageNameMap[stage.Name]; ok {
174+
return fmt.Errorf("stage `%s` is already defined", stage.Name)
175+
}
176+
177+
stageNameMap[stage.Name] = true
178+
179+
err := validatePipelineContainers(stage.Steps, &reportCount, reportMap, stepNameMap, stage.Name)
173180
if err != nil {
174181
return err
175182
}

compiler/native/validate_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,51 @@ func TestNative_Validate_Stages(t *testing.T) {
256256
}
257257
}
258258

259+
func TestNative_Validate_StagesSameName(t *testing.T) {
260+
// setup types
261+
strFoo := "foo"
262+
strBar := "bar"
263+
264+
p := &pipeline.Build{
265+
Version: "v1",
266+
Stages: pipeline.StageSlice{
267+
&pipeline.Stage{
268+
Name: strFoo,
269+
Steps: pipeline.ContainerSlice{
270+
&pipeline.Container{
271+
Commands: raw.StringSlice{"echo hello"},
272+
Image: "alpine",
273+
Name: strFoo,
274+
Pull: "always",
275+
},
276+
},
277+
},
278+
&pipeline.Stage{
279+
Name: strFoo,
280+
Steps: pipeline.ContainerSlice{
281+
&pipeline.Container{
282+
Commands: raw.StringSlice{"echo hello"},
283+
Image: "alpine",
284+
Name: strBar,
285+
Pull: "always",
286+
},
287+
},
288+
},
289+
},
290+
}
291+
292+
// run test
293+
compiler, err := FromCLICommand(context.Background(), testCommand(t, "http://foo.example.com"))
294+
if err != nil {
295+
t.Errorf("Unable to create new compiler: %v", err)
296+
}
297+
298+
err = compiler.ValidatePipeline(p)
299+
if err == nil {
300+
t.Errorf("Validate should have returned err")
301+
}
302+
}
303+
259304
func TestNative_Validate_Stages_NoName(t *testing.T) {
260305
// setup types
261306
str := "foo"

router/middleware/tracing_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestMiddleware_TracingInstrumentation(t *testing.T) {
6767
if !reflect.DeepEqual(got, trace.SpanContext{}) {
6868
return errors.New("span context is not empty")
6969
}
70+
7071
return nil
7172
},
7273
},
@@ -82,6 +83,7 @@ func TestMiddleware_TracingInstrumentation(t *testing.T) {
8283
if reflect.DeepEqual(got, trace.SpanContext{}) {
8384
return errors.New("span context is empty")
8485
}
86+
8587
return nil
8688
},
8789
},

0 commit comments

Comments
 (0)