Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/api/api_v1/endpoints/test_run_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

from app import crud, log_utils, models, schemas
from app.api import DEFAULT_404_MESSAGE
from app.crud.crud_project import validate_config
from app.crud.crud_test_run_execution import ImportError
from app.db.session import get_db
from app.default_environment_config import default_environment_config
from app.models.project import Project
from app.models.test_run_execution import TestRunExecution
from app.pics.pics_writer import PICSWriter
from app.schemas.test_environment_config import TestEnvironmentConfigError
from app.schemas.test_run_execution import TestRunExecutionUpdate
from app.test_engine import TEST_ENGINE_ABORTING_TESTING_MESSAGE
from app.test_engine.test_runner import AbortError, LoadingError, TestRunner
Expand Down Expand Up @@ -188,6 +190,15 @@ def create_cli_test_run_execution(
The created TestRunExecution.
"""

# Reject an invalid config up front, before any project or execution is created
if execution_config is not None:
try:
validate_config(execution_config)
except TestEnvironmentConfigError as e:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY, detail=str(e)
)

# Retrieve or create the CLI project
cli_project = _cli_project(db, test_run_execution_in.project_id)
test_run_execution_in.project_id = cli_project.id
Expand Down
17 changes: 13 additions & 4 deletions app/crud/crud_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
from app.utils import program_class


def validate_config(config: Dict[str, Any]) -> None:
"""Validate a test environment config against the program's config model.

Instantiates the program class, which raises TestEnvironmentConfigError when
the config is not in accordance with the model. Does nothing when no program
class is available.
"""
if program_class:
program_class(**config)


class CRUDProject(
CRUDBaseRead[Project],
CRUDBaseDelete[Project],
Expand Down Expand Up @@ -85,8 +96,7 @@ def create(self, db: Session, *, obj_in: ProjectCreate) -> Project:
obj_in.config = default_environment_config.__dict__
json_obj_in = jsonable_encoder(obj_in)
# Try to instantiate the program class in order to validate the input data
if program_class:
program_class(**json_obj_in["config"])
validate_config(json_obj_in["config"])

obj_in_data = json_obj_in
db_obj = Project(**obj_in_data)
Expand Down Expand Up @@ -115,8 +125,7 @@ def update(
setattr(db_obj, field, update_data[field])

# Try to instantiate the program class in order to validate the input data
if program_class:
program_class(**jsonable_encoder(db_obj)["config"])
validate_config(jsonable_encoder(db_obj)["config"])

db.add(db_obj)
db.commit()
Expand Down
70 changes: 70 additions & 0 deletions app/tests/api/api_v1/test_test_run_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,76 @@ def test_create_cli_test_run_execution_does_not_update_existing_project_no_pics(
assert execution_in.execution_pics == {}


def test_create_cli_test_run_execution_rejects_invalid_execution_config(
mock_db, test_run_execution_create, test_selection, default_config
):
"""An invalid execution_config is rejected with 422 before anything is created."""
invalid_config = {**default_config, "dut_config": {"pairing_mode": "not-a-mode"}}

with patch(
"app.api.api_v1.endpoints.test_run_executions.get_db", return_value=mock_db
), patch(
"app.api.api_v1.endpoints.test_run_executions._cli_project"
) as mock_cli_project, patch(
"app.api.api_v1.endpoints.test_run_executions.crud.test_run_execution.create"
) as mock_create:
response = client.post(
f"{settings.API_V1_STR}/test_run_executions/cli",
json={
"test_run_execution_in": test_run_execution_create.dict(),
"selected_tests": test_selection,
"execution_config": invalid_config,
},
)

assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert "invalid properties" in response.json()["detail"]
mock_cli_project.assert_not_called()
mock_create.assert_not_called()


def test_create_cli_test_run_execution_without_execution_config_skips_validation(
mock_db, test_run_execution_create, test_selection
):
"""An absent execution_config is not validated (the project config is used)."""
with patch(
"app.api.api_v1.endpoints.test_run_executions.get_db", return_value=mock_db
), patch(
"app.api.api_v1.endpoints.test_run_executions.validate_config"
) as mock_validate, patch(
"app.api.api_v1.endpoints.test_run_executions._cli_project"
) as mock_cli_project, patch(
"app.api.api_v1.endpoints.test_run_executions._reject_invalid_test_parameters"
), patch(
"app.api.api_v1.endpoints.test_run_executions.crud.test_run_execution.create"
) as mock_create:
mock_cli_project.return_value = Project(id=1, name="CLI Project", config={})
mock_create.return_value = TestRunExecution(
id=1,
title=test_run_execution_create.title,
description=test_run_execution_create.description,
project_id=1,
operator_id=1,
certification_mode=False,
state=TestStateEnum.PENDING,
started_at=None,
completed_at=None,
imported_at=None,
archived_at=None,
)
response = client.post(
f"{settings.API_V1_STR}/test_run_executions/cli",
json={
"test_run_execution_in": test_run_execution_create.dict(),
"selected_tests": test_selection,
},
)

assert response.status_code == 200
mock_validate.assert_not_called()
mock_create.assert_called_once()


_RUNNER_OPTIONS_PATH = (
"app.api.api_v1.endpoints.test_run_executions.load_runner_options"
)
Expand Down
Loading