Skip to content

Commit

Permalink
feat: enabling stopping testrun + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
carere committed Dec 16, 2024
1 parent 074eb98 commit 36219b7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
15 changes: 15 additions & 0 deletions api/handle_scenario_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func handleGetScenarioTestRun(uc usecases.Usecases) func(c *gin.Context) {
}
}

func handleCancelScenarioTestRun(uc usecases.Usecases) func(c *gin.Context) {
return func(c *gin.Context) {
ctx := c.Request.Context()
testRunId := c.Param("test_run_id")

usecase := usecasesWithCreds(ctx, uc).NewScenarioTestRunUseCase()
testrun, err := usecase.CancelTestRunById(ctx, testRunId)
if presentError(ctx, c, err) {
return
}
result := dto.AdaptScenarioTestRunDto(testrun)
c.JSON(http.StatusOK, gin.H{"scenario_test_run": result})
}
}

func handleDecisionsDataByOutcomeAndScore(uc usecases.Usecases) func(c *gin.Context) {
return func(c *gin.Context) {
ctx := c.Request.Context()
Expand Down
1 change: 1 addition & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func addRoutes(r *gin.Engine, conf Configuration, uc usecases.Usecases, auth Aut
timeoutMiddleware(conf.BatchTimeout),
handleListRulesExecution(uc))
router.GET("/scenario-testruns/:test_run_id", tom, handleGetScenarioTestRun(uc))
router.POST("/scenario-testruns/:test_run_id/cancel", tom, handleCancelScenarioTestRun(uc))

router.GET("/scheduled-executions", tom, handleListScheduledExecution(uc))
router.GET("/scheduled-executions/:execution_id", tom, handleGetScheduledExecution(uc))
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
services:
db:
container_name: postgres
image: europe-west1-docker.pkg.dev/marble-infra/marble/postgresql-db:latest # custom image of postgres 15 with pg_cron extension added
#image: europe-west1-docker.pkg.dev/marble-infra/marble/postgresql-db:latest # custom image of postgres 15 with pg_cron extension added
image: postgres:15.2-alpine
shm_size: 1g
restart: always
environment:
Expand Down
20 changes: 20 additions & 0 deletions repositories/scenario_testrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ScenarioTestRunRepository interface {
testrunId string,
input models.ScenarioTestRunCreateDbInput,
) error
ListRunningTestRun(ctx context.Context, exec Executor) ([]models.ScenarioTestRun, error)
ListTestRunsByScenarioID(ctx context.Context, exec Executor, scenarioID string) ([]models.ScenarioTestRun, error)
GetTestRunByLiveVersionID(
ctx context.Context,
Expand Down Expand Up @@ -107,6 +108,25 @@ func (repo *MarbleDbRepository) GetTestRunByLiveVersionID(
return &testruns[0], nil
}

func (repo *MarbleDbRepository) ListRunningTestRun(
ctx context.Context, exec Executor,
) ([]models.ScenarioTestRun, error) {
if err := validateMarbleDbExecutor(exec); err != nil {
return nil, err
}
query := selectTestruns().
Where(squirrel.Eq{"status": models.Up}).
OrderBy("created_at DESC")
testruns, err := SqlToListOfModels(ctx, exec, query, dbmodels.AdaptScenarioTestrun)
if err != nil {
return nil, err
}
if len(testruns) == 0 {
return nil, nil
}
return testruns, nil
}

func (repo *MarbleDbRepository) ListTestRunsByScenarioID(ctx context.Context,
exec Executor, scenarioID string,
) ([]models.ScenarioTestRun, error) {
Expand Down
38 changes: 36 additions & 2 deletions usecases/scenario_testrun_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func (usecase *ScenarioTestRunUsecase) CreateScenarioTestRun(
}

// we should not have any existing testrun for this scenario
existingTestrun, err := usecase.repository.GetTestRunByLiveVersionID(ctx, exec, *scenario.LiveVersionID)
testRuns, err := usecase.repository.ListRunningTestRun(ctx, exec)
if err != nil {
return models.ScenarioTestRun{}, err
}
if existingTestrun != nil {
if len(testRuns) > 0 {
return models.ScenarioTestRun{}, errors.Wrapf(models.ErrTestRunAlreadyExist,
"the scenario %s has a running testrun", input.ScenarioId)
}
Expand Down Expand Up @@ -149,3 +149,37 @@ func (usecase *ScenarioTestRunUsecase) GetTestRunById(ctx context.Context,
}
return testrun, nil
}

func (usecase *ScenarioTestRunUsecase) CancelTestRunById(ctx context.Context,
testRunId string,
) (models.ScenarioTestRun, error) {
exec := usecase.executorFactory.NewExecutor()
testRun, err := usecase.repository.GetTestRunByID(
ctx,
exec,
testRunId)
if err != nil {
return models.ScenarioTestRun{}, err
}
if err := usecase.enforceSecurity.ReadTestRun(testRun.OrganizationId); err != nil {
return models.ScenarioTestRun{}, err
}
if testRun.Status == models.Down {
return testRun, nil
}
if testRun.Status == models.Up {
if err := usecase.repository.UpdateTestRunStatus(ctx,
exec, testRunId, models.Down); err != nil {
return models.ScenarioTestRun{}, err
}
updatedTestRun, err := usecase.repository.GetTestRunByID(
ctx,
exec,
testRunId)
if err != nil {
return models.ScenarioTestRun{}, err
}
return updatedTestRun, nil
}
return models.ScenarioTestRun{}, models.BadParameterError
}

0 comments on commit 36219b7

Please sign in to comment.