diff --git a/cmd/testworkflow-init/main.go b/cmd/testworkflow-init/main.go index 74300096c1..75a7384e64 100644 --- a/cmd/testworkflow-init/main.go +++ b/cmd/testworkflow-init/main.go @@ -150,7 +150,8 @@ func main() { delayedPauses := make([]string, 0) // Interpret the operations - for _, action := range state.GetActions(int(groupIndex)) { + actions := state.GetActions(int(groupIndex)) + for i, action := range actions { switch action.Type() { case lite.ActionTypeDeclare: state.GetStep(action.Declare.Ref). @@ -175,7 +176,19 @@ func main() { case lite.ActionTypeContainerTransition: orchestration.Setup.SetConfig(action.Container.Config) - orchestration.Setup.AdvanceEnv() + err := orchestration.Setup.AdvanceEnv() + // Attach the error to the next consecutive step + if err != nil { + for _, next := range actions[i:] { + if next.Type() != lite.ActionTypeStart || *next.Start == "" { + continue + } + step := state.GetStep(*next.Start) + orchestration.Start(step) + break + } + output.ExitErrorf(data.CodeInputError, err.Error()) + } stdout.SetSensitiveWords(orchestration.Setup.GetSensitiveWords()) currentContainer = *action.Container @@ -225,10 +238,13 @@ func main() { orchestration.End(step) case lite.ActionTypeSetup: - orchestration.Setup.UseEnv(constants.EnvGroupDebug) + err := orchestration.Setup.UseEnv(constants.EnvGroupDebug) + if err != nil { + output.ExitErrorf(data.CodeInputError, err.Error()) + } stdout.SetSensitiveWords(orchestration.Setup.GetSensitiveWords()) step := state.GetStep(data.InitStepName) - err := commands.Setup(*action.Setup) + err = commands.Setup(*action.Setup) if err == nil { step.SetStatus(data.StepStatusPassed) } else { @@ -257,7 +273,10 @@ func main() { } // Configure the environment - orchestration.Setup.UseCurrentEnv() + err := orchestration.Setup.UseCurrentEnv() + if err != nil { + output.ExitErrorf(data.CodeInputError, err.Error()) + } if action.Execute.Toolkit { serialized, _ := json.Marshal(state.InternalConfig) _ = os.Setenv("TK_CFG", string(serialized)) diff --git a/cmd/testworkflow-init/orchestration/setup.go b/cmd/testworkflow-init/orchestration/setup.go index 503430d83e..09ea0b497c 100644 --- a/cmd/testworkflow-init/orchestration/setup.go +++ b/cmd/testworkflow-init/orchestration/setup.go @@ -8,6 +8,8 @@ import ( "regexp" "strings" + "github.com/pkg/errors" + "github.com/kubeshop/testkube/cmd/testworkflow-init/constants" "github.com/kubeshop/testkube/cmd/testworkflow-init/data" "github.com/kubeshop/testkube/cmd/testworkflow-init/output" @@ -168,7 +170,7 @@ func (c *setup) GetInternalConfig() (config testworkflowconfig.InternalConfig) { return config } -func (c *setup) UseEnv(group string) { +func (c *setup) UseEnv(group string) error { c.UseBaseEnv() c.envSelectedGroup = group @@ -226,20 +228,21 @@ func (c *setup) UseEnv(group string) { for name, expr := range envTemplates { value, err := expressions.CompileAndResolveTemplate(expr, localEnvMachine, addonMachine, expressions.FinalizerFail) if err != nil { - output.ExitErrorf(data.CodeInputError, "failed to compute '%s' environment variable: %s", name, err.Error()) + return errors.Wrapf(err, "failed to compute '%s' environment variable", name) } str, _ := value.Static().StringValue() os.Setenv(name, str) } + return nil } -func (c *setup) UseCurrentEnv() { - c.UseEnv(fmt.Sprintf("%d", c.envCurrentGroup)) +func (c *setup) UseCurrentEnv() error { + return c.UseEnv(fmt.Sprintf("%d", c.envCurrentGroup)) } -func (c *setup) AdvanceEnv() { +func (c *setup) AdvanceEnv() error { c.envCurrentGroup++ - c.UseCurrentEnv() + return c.UseCurrentEnv() } func (c *setup) SetWorkingDir(workingDir string) {