Skip to content

Commit

Permalink
chore: proper fix integration tests (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamagalhaes authored Jan 29, 2025
1 parent 763dca2 commit 0563dc2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Run Pytest unit tests
env:
CUSTOMER_IDENTIFIER: "ci-unit-tests"
INTEGRATION_TEST_URL: "https://plugin-testing.canvasmedical.com"
INTEGRATION_TEST_URL: "https://api-test-clinic.canvasmedical.com"
INTEGRATION_TEST_CLIENT_ID: ${{ secrets.INTEGRATION_TEST_CLIENT_ID }}
INTEGRATION_TEST_CLIENT_SECRET: ${{ secrets.INTEGRATION_TEST_CLIENT_SECRET }}
run: poetry run pytest -m "not integtest" --ff --verbosity 2
Expand All @@ -33,7 +33,7 @@ jobs:
if: matrix.version == '3.12'
env:
CUSTOMER_IDENTIFIER: "ci-integration-tests"
INTEGRATION_TEST_URL: "https://plugin-testing.canvasmedical.com"
INTEGRATION_TEST_URL: "https://api-test-clinic.canvasmedical.com"
INTEGRATION_TEST_CLIENT_ID: ${{ secrets.INTEGRATION_TEST_CLIENT_ID }}
INTEGRATION_TEST_CLIENT_SECRET: ${{ secrets.INTEGRATION_TEST_CLIENT_SECRET }}
run: poetry run pytest -m "integtest" --ff --verbosity 2
Expand Down
14 changes: 12 additions & 2 deletions canvas_sdk/commands/tests/protocol/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from collections.abc import Generator
from datetime import datetime
from typing import cast

import pytest

import settings
from canvas_sdk.commands.tests.test_utils import (
COMMANDS,
MaskedValue,
Expand All @@ -12,6 +14,7 @@
get_token,
install_plugin,
trigger_plugin_event,
wait_for_log,
write_protocol_code,
)

Expand Down Expand Up @@ -40,10 +43,18 @@ def write_and_install_protocol_and_clean_up(
) -> Generator[None, None, None]:
"""Write the protocol code, install the plugin, and clean up after the test."""
write_protocol_code(new_note["externallyExposableId"], plugin_name, COMMANDS)
message_received_event, thread, ws = wait_for_log(
cast(str, settings.INTEGRATION_TEST_URL),
token.value,
f"Loading plugin '{plugin_name}",
)
install_plugin(plugin_name, token)
message_received_event.wait(timeout=5.0)

yield

ws.close()
thread.join()
clean_up_files_and_plugins(plugin_name, token)


Expand All @@ -57,8 +68,7 @@ def test_protocol_that_inserts_every_command(
commands_in_body = get_original_note_body_commands(new_note["id"], token)

# TODO: Temporary workaround to ignore the updateGoal command until the integration test instance is fixed.
command_keys = [c.Meta.key for c in COMMANDS if c.Meta.key != "updateGoal"]

command_keys = [c.Meta.key for c in COMMANDS]
assert len(command_keys) == len(commands_in_body)
for i, command_key in enumerate(command_keys):
assert commands_in_body[i] == command_key
13 changes: 4 additions & 9 deletions canvas_sdk/commands/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ def compute(self):
def install_plugin(plugin_name: str, token: MaskedValue) -> None:
"""Install a plugin."""
with open(_build_package(Path(f"./custom-plugins/{plugin_name}")), "rb") as package:
message_received_event = wait_for_log(
cast(str, settings.INTEGRATION_TEST_URL),
token.value,
f"Loading plugin '{plugin_name}",
)
response = requests.post(
plugin_url(cast(str, settings.INTEGRATION_TEST_URL)),
data={"is_enabled": True},
Expand All @@ -203,8 +198,6 @@ def install_plugin(plugin_name: str, token: MaskedValue) -> None:
)
response.raise_for_status()

message_received_event.wait(timeout=5.0)


def trigger_plugin_event(token: MaskedValue) -> None:
"""Trigger a plugin event."""
Expand Down Expand Up @@ -328,7 +321,9 @@ def get_token() -> MaskedValue:
return MaskedValue(response.json()["access_token"])


def wait_for_log(host: str, token: str, message: str) -> threading.Event:
def wait_for_log(
host: str, token: str, message: str
) -> tuple[threading.Event, threading.Thread, websocket.WebSocketApp]:
"""Wait for a specific log message."""
hostname = cast(str, urlparse(host).hostname)
instance = hostname.removesuffix(".canvasmedical.com")
Expand Down Expand Up @@ -362,4 +357,4 @@ def _on_error(ws: websocket.WebSocket, error: str) -> None:

connected_event.wait(timeout=5.0)

return message_received_event
return message_received_event, thread, ws
7 changes: 5 additions & 2 deletions canvas_sdk/effects/banner_alert/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def compute(self):
protocol.write(protocol_code)

with open(_build_package(Path(f"./custom-plugins/{plugin_name}")), "rb") as package:
message_received_event = wait_for_log(
message_received_event, thread, ws = wait_for_log(
settings.INTEGRATION_TEST_URL,
token.value,
f"Loading plugin '{plugin_name}:{plugin_name}.protocols.my_protocol:Protocol'",
f"Loading plugin '{plugin_name}",
)

# install the plugin
Expand All @@ -111,6 +111,9 @@ def compute(self):

yield

ws.close()
thread.join()

# clean up
if Path(f"./custom-plugins/{plugin_name}").exists():
shutil.rmtree(Path(f"./custom-plugins/{plugin_name}"))
Expand Down

0 comments on commit 0563dc2

Please # to comment.