diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml new file mode 100644 index 0000000..6d66c07 --- /dev/null +++ b/.github/workflows/e2e-test.yml @@ -0,0 +1,40 @@ +name: E2E Test +on: + push: + branches: + - main + pull_request: + workflow_call: + +jobs: + test: + runs-on: ubuntu-24.04 + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Build Image + id: build-image + uses: redhat-actions/buildah-build@v2 + with: + image: slack_server_mock + tags: latest + containerfiles: | + ./Containerfile + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r test/requirements.txt + + - name: Run tests + run: | + pytest test diff --git a/test/requirements.txt b/test/requirements.txt index 42250e4..236df67 100644 --- a/test/requirements.txt +++ b/test/requirements.txt @@ -1 +1,3 @@ slack_bolt +pytest +requests diff --git a/test/slackbot.py b/test/slackbot.py index 86d9ecd..df32952 100644 --- a/test/slackbot.py +++ b/test/slackbot.py @@ -13,25 +13,28 @@ class KnowledgeBaseSlackBot(): # pylint:disable=R0903 """ Slackbot application backend """ - def __init__(self) -> None: - self._client = WebClient(token=bot_token, base_url="http://localhost:8888") + def __init__(self, base_url="http://localhost:8888") -> None: + self._client = WebClient(token=bot_token, base_url=base_url) self._handler = SocketModeHandler(App(client=self._client), app_token) self._handler.app.message()(self._got_message) - def run(self): + def run(self, block=False): """ Start the Slackbot backend application """ - self._handler.start() + if block: + self._handler.start() + else: + self._handler.connect() def _got_message(self, message, say): print("Got message {}".format(message['text'])) - say("Hello right back at ya") + say(message['text']) + + def is_connected(self) -> bool: + return self._handler.client.is_connected() + + def stop(self): + self._handler.close() if __name__ == "__main__": - bot = KnowledgeBaseSlackBot() - t = Thread(target=bot.run) - t.start() - while not bot._handler.client.is_connected(): - time.sleep(1) - # bot._handler.client.send_message("hello") - t.join() + KnowledgeBaseSlackBot().run(block=True) diff --git a/test/test_echo.py b/test/test_echo.py new file mode 100644 index 0000000..fcd43df --- /dev/null +++ b/test/test_echo.py @@ -0,0 +1,53 @@ +from pathlib import Path +import time +import subprocess + +import pytest +import requests +from slackbot import KnowledgeBaseSlackBot + + +# Fixture to manage Podman container lifecycle +@pytest.fixture(scope="module") +def podman_container(): + container_name = "slack_server_mock" + image = "quay.io/yblum/slack_server_mock:0.0.1" + ports = [3001, 8080, 8888] + project_root_path = Path(__file__).parents[1] + volumes = [f"{project_root_path}/settings.yaml:/app/settings.yaml:z"] + + podman_run_command = ["podman", "run", "--name", container_name, "-d", "--rm"] + for port in ports: + podman_run_command.extend(["--publish", f"{port}:{port}"]) + for volume in volumes: + podman_run_command.extend(["--volume", volume]) + podman_run_command.append(image) + # Start the container + print("Starting Podman container...") + subprocess.run(podman_run_command, check=True) + + # Wait a bit for the container to be fully up and running + time.sleep(5) + + # Provide the container name to the test + yield container_name + + # Stop and remove the container after tests + print("Stopping Podman container...") + subprocess.run(["podman", "kill", container_name], check=True) + + +@pytest.fixture +def bot(): + bot = KnowledgeBaseSlackBot() + bot.run() + while not bot.is_connected(): + time.sleep(1) + yield bot + bot.stop() + + +def test_echo(bot, podman_container): + msg = "foo" + res = requests.post(url="http://localhost:8080/message", json={"message": msg}) + assert res.json().get('answer') == msg