diff --git a/cli/actions/run_test_action.go b/cli/actions/run_test_action.go index d507a0d8ef..791d63bb87 100644 --- a/cli/actions/run_test_action.go +++ b/cli/actions/run_test_action.go @@ -35,6 +35,7 @@ type runTestAction struct { logger *zap.Logger client *openapi.APIClient environmentActions environmentsActions + cliExit func(int) } var _ Action[RunResourceArgs] = &runTestAction{} @@ -48,8 +49,8 @@ type runDefParams struct { EnvironmentVariables map[string]string } -func NewRunTestAction(config config.Config, logger *zap.Logger, client *openapi.APIClient, environmentActions environmentsActions) runTestAction { - return runTestAction{config, logger, client, environmentActions} +func NewRunTestAction(config config.Config, logger *zap.Logger, client *openapi.APIClient, environmentActions environmentsActions, cliExit func(int)) runTestAction { + return runTestAction{config, logger, client, environmentActions, cliExit} } func (a runTestAction) Run(ctx context.Context, args RunResourceArgs) error { @@ -349,7 +350,7 @@ func (a runTestAction) testRun(ctx context.Context, test openapi.Test, runID int allPassed := tro.Run.Result.GetAllPassed() if params.WaitForResult && !allPassed { // It failed, so we have to return an error status - os.Exit(1) + a.cliExit(1) } return nil @@ -385,13 +386,13 @@ func (a runTestAction) transactionRun(ctx context.Context, transaction openapi.T if params.WaitForResult { if utils.RunStateIsFailed(tro.Run.GetState()) { // It failed, so we have to return an error status - os.Exit(1) + a.cliExit(1) } for _, step := range tro.Run.Steps { if !step.Result.GetAllPassed() { // if any test doesn't pass, fail the transaction execution - os.Exit(1) + a.cliExit(1) } } } diff --git a/cli/cmd/test_run_cmd.go b/cli/cmd/test_run_cmd.go index d58bbcf0b8..6cdb907e09 100644 --- a/cli/cmd/test_run_cmd.go +++ b/cli/cmd/test_run_cmd.go @@ -31,7 +31,7 @@ var testRunCmd = &cobra.Command{ environmentOptions := append(baseOptions, actions.WithClient(utils.GetResourceAPIClient("environments", cliConfig))) environmentActions := actions.NewEnvironmentsActions(environmentOptions...) - runTestAction := actions.NewRunTestAction(cliConfig, cliLogger, client, environmentActions) + runTestAction := actions.NewRunTestAction(cliConfig, cliLogger, client, environmentActions, ExitCLI) actionArgs := actions.RunResourceArgs{ DefinitionFile: runTestFileDefinition, EnvID: runTestEnvID, diff --git a/cli/formatters/test_run.go b/cli/formatters/test_run.go index be56ddf014..190dac0cc2 100644 --- a/cli/formatters/test_run.go +++ b/cli/formatters/test_run.go @@ -111,6 +111,10 @@ func (f testRun) formatSuccessfulTest(test openapi.Test, run openapi.TestRun) st buffer.WriteString(message) for i, specResult := range run.Result.Results { + if len(test.Specs) <= i { + break // guard clause: this means that the server sent more results than specs + } + title := f.getTestSpecTitle(test.Specs[i].GetName(), specResult) message := f.formatMessage("\t%s %s\n", PASSED_TEST_ICON, title) message = f.getColoredText(true, message) @@ -145,7 +149,6 @@ func (f testRun) formatFailedTest(test openapi.Test, run openapi.TestRun) string allPassed := true for _, result := range specResult.Results { - for _, spanResult := range result.SpanResults { // meta assertions such as tracetest.selected_spans.count don't have a spanID, // so they will be treated differently. To overcome them, we will place all @@ -182,6 +185,10 @@ func (f testRun) formatFailedTest(test openapi.Test, run openapi.TestRun) string } } + if len(test.Specs) <= i { + break // guard clause: this means that the server sent more results than specs + } + title := f.getTestSpecTitle(test.Specs[i].GetName(), specResult) icon := f.getStateIcon(allPassed) message := f.formatMessage("\t%s %s\n", icon, title) diff --git a/server/testdb/runs.go b/server/testdb/runs.go index aa1565e072..7976b80877 100644 --- a/server/testdb/runs.go +++ b/server/testdb/runs.go @@ -334,12 +334,12 @@ SELECT "environment", -- transaction run - transaction_run.transaction_run_id, - transaction_run.transaction_run_transaction_id -FROM test_runs -LEFT OUTER JOIN - transaction_run_steps transaction_run -ON transaction_run.test_run_id = id AND transaction_run.test_run_test_id = test_id + transaction_run_steps.transaction_run_id, + transaction_run_steps.transaction_run_transaction_id +FROM + test_runs + LEFT OUTER JOIN transaction_run_steps + ON transaction_run_steps.test_run_id = test_runs.id AND transaction_run_steps.test_run_test_id = test_runs.test_id ` func (td *postgresDB) GetRun(ctx context.Context, testID id.ID, runID int) (model.Run, error) { @@ -554,11 +554,12 @@ func readRunRow(row scanner) (model.Run, error) { } func (td *postgresDB) GetTransactionRunSteps(ctx context.Context, tr tests.TransactionRun) ([]model.Run, error) { - // stmt, err := td.db.Prepare(selectRunQuery + " WHERE id = $1 AND test_id = $2") - stmt, err := td.db.Prepare(selectRunQuery + ` INNER JOIN - transaction_run_steps trs ON - test_runs.id = trs.test_run_id AND test_runs.test_id = trs.test_run_test_id - WHERE trs.transaction_run_id = $1 AND trs.transaction_run_transaction_id = $2`) + query := selectRunQuery + ` +WHERE transaction_run_steps.transaction_run_id = $1 AND transaction_run_steps.transaction_run_transaction_id = $2 +ORDER BY test_runs.completed_at ASC +` + + stmt, err := td.db.Prepare(query) if err != nil { return []model.Run{}, fmt.Errorf("prepare: %w", err) }