Skip to content

Commit

Permalink
fix(server): fix transaction run reporting order (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbdias committed May 30, 2023
1 parent 7d1b985 commit e02077a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
11 changes: 6 additions & 5 deletions cli/actions/run_test_action.go
Expand Up @@ -35,6 +35,7 @@ type runTestAction struct {
logger *zap.Logger
client *openapi.APIClient
environmentActions environmentsActions
cliExit func(int)
}

var _ Action[RunResourceArgs] = &runTestAction{}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/test_run_cmd.go
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion cli/formatters/test_run.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions server/testdb/runs.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e02077a

Please sign in to comment.