@@ -2,6 +2,7 @@ package registry
22
33import (
44 "fmt"
5+ "strings"
56
67 utilerrors "k8s.io/apimachinery/pkg/util/errors"
78 "k8s.io/apimachinery/pkg/util/sets"
@@ -68,6 +69,12 @@ func NewResolver(stepsByName ReferenceByName, chainsByName ChainByName, workflow
6869
6970func (r * registry ) Resolve (name string , config api.MultiStageTestConfiguration ) (api.MultiStageTestConfigurationLiteral , error ) {
7071 var overridden [][]api.TestStep
72+
73+ // Validate workflow overrides before merging
74+ if err := r .validateWorkflowOverrides (config ); err != nil {
75+ return api.MultiStageTestConfigurationLiteral {}, err
76+ }
77+
7178 if config .Workflow != nil {
7279 var errs []error
7380 overridden , errs = r .mergeWorkflow (& config )
@@ -78,6 +85,35 @@ func (r *registry) Resolve(name string, config api.MultiStageTestConfiguration)
7885 return r .resolveTest (config , stackForTest (name , config .Environment , config .Dependencies , config .DNSConfig , config .NodeArchitecture ), overridden )
7986}
8087
88+ // validateWorkflowOverrides validates that tests properly acknowledge when they override workflow pre/post steps
89+ func (r * registry ) validateWorkflowOverrides (config api.MultiStageTestConfiguration ) error {
90+ if config .Workflow == nil {
91+ return nil
92+ }
93+
94+ workflow , ok := r .workflowsByName [* config .Workflow ]
95+ if ! ok {
96+ // This will be caught by mergeWorkflow, no need to duplicate the error
97+ return nil
98+ }
99+
100+ hasPreOverride := config .Pre != nil && len (workflow .Pre ) > 0
101+ hasPostOverride := config .Post != nil && len (workflow .Post ) > 0
102+
103+ if (hasPreOverride || hasPostOverride ) && (config .AllowPrePostStepOverrides == nil || ! * config .AllowPrePostStepOverrides ) {
104+ var overriddenSections []string
105+ if hasPreOverride {
106+ overriddenSections = append (overriddenSections , "pre" )
107+ }
108+ if hasPostOverride {
109+ overriddenSections = append (overriddenSections , "post" )
110+ }
111+ return fmt .Errorf ("test configuration overrides %s steps from workflow %q but does not have 'allow_pre_post_step_overrides: true' set. This flag is required to prevent accidental overriding of critical setup and teardown steps that could cause resource leaks" , strings .Join (overriddenSections , " and " ), * config .Workflow )
112+ }
113+
114+ return nil
115+ }
116+
81117func (r * registry ) mergeWorkflow (config * api.MultiStageTestConfiguration ) ([][]api.TestStep , []error ) {
82118 var overridden [][]api.TestStep
83119 workflow , ok := r .workflowsByName [* config .Workflow ]
@@ -103,6 +139,7 @@ func (r *registry) mergeWorkflow(config *api.MultiStageTestConfiguration) ([][]a
103139 } else {
104140 overridden = append (overridden , workflow .Post )
105141 }
142+
106143 config .Environment = mergeEnvironments (workflow .Environment , config .Environment )
107144 config .Dependencies = mergeDependencies (workflow .Dependencies , config .Dependencies )
108145 config .DependencyOverrides = mergeDependencyOverrides (workflow .DependencyOverrides , config .DependencyOverrides )
0 commit comments