Skip to content

Commit

Permalink
Fix compiler notification tests (PR #5080)
Browse files Browse the repository at this point in the history
Pull request opened by the merge tool on behalf of #5080
  • Loading branch information
wouterdb authored and inmantaci committed Nov 7, 2022
1 parent 3052db1 commit 1415b67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
3 changes: 3 additions & 0 deletions changelogs/unreleased/fix_compiler_notification_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: Fix compiler notification tests
change-type: patch
destination-branches: [master, iso5]
21 changes: 15 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,25 +1420,34 @@ def __init__(
self.request = request
self.version: Optional[int] = None
self._make_compile_fail = make_compile_fail
self._make_pull_fail = False
self._runner_queue = runner_queue
self.block = False

async def run(self, force_update: Optional[bool] = False) -> Tuple[bool, None]:
now = datetime.datetime.now()

if self._runner_queue is not None:
self._runner_queue.put(self)
self.block = True
while self.block:
await asyncio.sleep(0.1)

returncode = 1 if self._make_compile_fail else 0
report = data.Report(
compile=self.request.id, started=now, name="CompileRunnerMock", command="", completed=now, returncode=returncode
)
await report.insert()

if self._make_pull_fail:
report = data.Report(
compile=self.request.id, started=now, name="Pulling updates", command="", completed=now, returncode=1
)
await report.insert()

self.version = int(time.time())
success = not self._make_compile_fail

if self._runner_queue is not None:
self._runner_queue.put(self)
self.block = True
while self.block:
await asyncio.sleep(0.1)

return success, None


Expand Down
52 changes: 26 additions & 26 deletions tests/server/test_compilerservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import uuid
from asyncio import Semaphore
from collections import abc
from typing import AsyncIterator, List, Optional, Tuple
from typing import TYPE_CHECKING, AsyncIterator, List, Optional, Tuple

import pkg_resources
import pytest
Expand All @@ -51,6 +51,9 @@

logger = logging.getLogger("inmanta.test.server.compilerservice")

if TYPE_CHECKING:
from conftest import CompileRunnerMock


@pytest.fixture
async def compilerservice(server_config, init_dataclasses_and_load_schema):
Expand Down Expand Up @@ -786,7 +789,11 @@ async def is_compiling() -> None:


async def run_compile_and_wait_until_compile_is_done(
compiler_service: CompilerService, compiler_queue: queue.Queue, env_id: uuid.UUID
compiler_service: CompilerService,
compiler_queue: queue.Queue["CompileRunnerMock"],
env_id: uuid.UUID,
fail: Optional[bool] = None,
fail_on_pull=False,
) -> None:
"""
Unblock the first compile in the compiler queue and wait until the compile finishes.
Expand All @@ -796,6 +803,9 @@ async def run_compile_and_wait_until_compile_is_done(
# prevent race conditions where compile request is not yet in queue
await retry_limited(lambda: not compiler_queue.empty(), timeout=10)
run = compiler_queue.get(block=True)
if fail is not None:
run._make_compile_fail = fail
run._make_pull_fail = fail_on_pull
run.block = False

def _is_compile_finished() -> bool:
Expand Down Expand Up @@ -1228,16 +1238,16 @@ async def test_compileservice_api(client, environment):
["a custom message", None],
)
async def test_notification_failed_compile_with_message(
server, client, environment_factory: EnvironmentFactory, message: Optional[str]
server, client, environment, mocked_compiler_service_block, message: Optional[str]
) -> None:
compilerservice = server.get_slice(SLICE_COMPILER)

env = await environment_factory.create_environment("x=0 x=1")

result = await client.list_notifications(env.id)
result = await client.list_notifications(environment)
assert result.code == 200
assert len(result.result["data"]) == 0

env = await data.Environment.get_by_id(environment)

compile_id, _ = await compilerservice.request_recompile(
env,
force_update=False,
Expand All @@ -1247,19 +1257,15 @@ async def test_notification_failed_compile_with_message(
failed_compile_message=message,
)

async def compile_done() -> bool:
res = await compilerservice.is_compiling(env.id)
return res == 204

await retry_limited(compile_done, timeout=10)
await run_compile_and_wait_until_compile_is_done(compilerservice, mocked_compiler_service_block, env.id, True)

async def notification_logged() -> bool:
result = await client.list_notifications(env.id)
result = await client.list_notifications(environment)
assert result.code == 200
return len(result.result["data"]) > 0

await retry_limited(notification_logged, timeout=10)
result = await client.list_notifications(env.id)
result = await client.list_notifications(environment)
assert result.code == 200
compile_failed_notification = next((item for item in result.result["data"] if item["title"] == "Compilation failed"), None)
assert compile_failed_notification
Expand All @@ -1270,7 +1276,9 @@ async def notification_logged() -> bool:
assert "A compile has failed" in compile_failed_notification["message"]


async def test_notification_on_failed_exporting_compile(server, client, environment: str) -> None:
async def test_notification_on_failed_exporting_compile(
server, client, environment: str, mocked_compiler_service_failing_compile
) -> None:
compilerservice = server.get_slice(SLICE_COMPILER)
env = await data.Environment.get_by_id(uuid.UUID(environment))

Expand Down Expand Up @@ -1300,10 +1308,9 @@ async def notification_logged() -> bool:


async def test_notification_on_failed_pull_during_compile(
server, client, environment_factory: EnvironmentFactory, tmp_path
server, client, environment: str, mocked_compiler_service_block
) -> None:
env = await environment_factory.create_environment("")
project_dir = os.path.join(server.get_slice(SLICE_SERVER)._server_storage["environments"], str(env.id))
env = await data.Environment.get_by_id(uuid.UUID(environment))

compilerservice = server.get_slice(SLICE_COMPILER)

Expand All @@ -1314,19 +1321,12 @@ async def test_notification_on_failed_pull_during_compile(
# Do a compile
compile_id, _ = await compilerservice.request_recompile(env, force_update=True, do_export=False, remote_id=uuid.uuid4())

async def compile_done() -> bool:
res = await compilerservice.is_compiling(env.id)
return res == 204

await retry_limited(compile_done, timeout=10)

# Change the remote to an invalid value
subprocess.check_output(["git", "remote", "set-url", "origin", str(tmp_path)], cwd=project_dir)
await run_compile_and_wait_until_compile_is_done(compilerservice, mocked_compiler_service_block, env.id)

# During the next compile, the pull should fail
compile_id, _ = await compilerservice.request_recompile(env, force_update=True, do_export=False, remote_id=uuid.uuid4())

await retry_limited(compile_done, timeout=10)
await run_compile_and_wait_until_compile_is_done(compilerservice, mocked_compiler_service_block, env.id, fail_on_pull=True)

async def notification_logged() -> bool:
result = await client.list_notifications(env.id)
Expand Down

0 comments on commit 1415b67

Please sign in to comment.