|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 | from fastapi.testclient import TestClient |
| 10 | +from freezegun import freeze_time |
10 | 11 | from httpx import AsyncClient |
11 | 12 | from sqlalchemy import select |
12 | 13 | from sqlalchemy.ext.asyncio import AsyncSession |
|
28 | 29 | InstanceType, |
29 | 30 | Resources, |
30 | 31 | ) |
| 32 | +from dstack._internal.core.models.profiles import Schedule |
31 | 33 | from dstack._internal.core.models.resources import Range |
32 | 34 | from dstack._internal.core.models.runs import ( |
33 | 35 | ApplyRunPlanInput, |
@@ -1370,6 +1372,41 @@ async def test_updates_run(self, test_db, session: AsyncSession, client: AsyncCl |
1370 | 1372 | assert run.run_spec.configuration.replicas == Range(min=1, max=1) |
1371 | 1373 | assert updated_run.run_spec.configuration.replicas == Range(min=2, max=2) |
1372 | 1374 |
|
| 1375 | + @pytest.mark.asyncio |
| 1376 | + @pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True) |
| 1377 | + async def test_creates_pending_run_if_run_is_scheduled( |
| 1378 | + self, test_db, session: AsyncSession, client: AsyncClient |
| 1379 | + ): |
| 1380 | + user = await create_user(session=session, global_role=GlobalRole.USER) |
| 1381 | + project = await create_project(session=session, owner=user) |
| 1382 | + await add_project_member( |
| 1383 | + session=session, project=project, user=user, project_role=ProjectRole.USER |
| 1384 | + ) |
| 1385 | + repo = await create_repo(session=session, project_id=project.id) |
| 1386 | + run_spec = get_run_spec( |
| 1387 | + run_name="test-run", |
| 1388 | + repo_id=repo.name, |
| 1389 | + ) |
| 1390 | + run_spec.configuration.schedule = Schedule(cron=["5 * * * *", "10 * * * *"]) |
| 1391 | + with freeze_time(datetime(2023, 1, 2, 3, 9, tzinfo=timezone.utc)): |
| 1392 | + response = await client.post( |
| 1393 | + f"/api/project/{project.name}/runs/apply", |
| 1394 | + headers=get_auth_headers(user.token), |
| 1395 | + json={ |
| 1396 | + "plan": { |
| 1397 | + "run_spec": json.loads(run_spec.json()), |
| 1398 | + "current_resource": None, |
| 1399 | + }, |
| 1400 | + "force": False, |
| 1401 | + }, |
| 1402 | + ) |
| 1403 | + assert response.status_code == 200, response.json() |
| 1404 | + res = await session.execute(select(RunModel)) |
| 1405 | + run = res.scalar() |
| 1406 | + assert run is not None |
| 1407 | + assert run.status == RunStatus.PENDING |
| 1408 | + assert run.next_triggered_at == datetime(2023, 1, 2, 3, 10) |
| 1409 | + |
1373 | 1410 |
|
1374 | 1411 | class TestSubmitRun: |
1375 | 1412 | @pytest.mark.asyncio |
|
0 commit comments