Skip to content

Commit

Permalink
Testing - add a pytest based test
Browse files Browse the repository at this point in the history
Add GH workflow

Signed-off-by: Ygal Blum <[email protected]>
  • Loading branch information
ygalblum committed Jul 4, 2024
1 parent e84d5ca commit 41d4703
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
slack_bolt
pytest
requests
27 changes: 15 additions & 12 deletions test/slackbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
53 changes: 53 additions & 0 deletions test/test_echo.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 41d4703

Please sign in to comment.