Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transaction run reporting #2617

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions cli/actions/run_test_action.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes on this file are unrelated to the PR's main objective, but they can help us in the future.
We are changing all calls to os.Exit here to a cliExit function to help us to mock os.Exit calls (which we use to debug CLI e2e tests).

Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actionArgs := actions.RunResourceArgs{
DefinitionFile: runTestFileDefinition,
EnvID: runTestEnvID,
Expand Down
9 changes: 8 additions & 1 deletion cli/formatters/test_run.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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