From 4578362eb503372be06781539842ec8411ec54ef Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:32:32 -0700 Subject: [PATCH 01/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 95 ++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index ffc653c8..39a6d620 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -1,8 +1,9 @@ name: Scheduled Tests on: - schedule: - - cron: '0 */3 * * *' + workflow_dispatch: + # schedule: + # - cron: '0 */3 * * *' jobs: run_app_tests: @@ -25,6 +26,11 @@ jobs: - name: Upgrade pip run: pip install --upgrade pip + - name: Create python virtual environment + run: | + python -m venv .venv + source .venv/bin/activate + - name: Install dependencies run: pip install -r requirements_web.txt @@ -34,7 +40,90 @@ jobs: - name: Install pytest-slack run: pip install pytest-slack + - name: Create conftest.py for Slack Notifications + uses: andstor/create-file-action@v1 + with: + path: src/tests/conftest.py + content: | + import pytest + import requests + from _pytest.terminal import TerminalReporter + + @pytest.hookimpl(tryfirst=True) + def pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: int, config): + """Customize pytest terminal summary and send to Slack.""" + + SLACK_WEBHOOK_URL = config.getoption('--slack-webhook-url') + + # Collect test summary information + total_tests = terminalreporter.stats.get('passed', []) + \ + terminalreporter.stats.get('failed', []) + \ + terminalreporter.stats.get('error', []) + \ + terminalreporter.stats.get('skipped', []) + + passed_tests = len(terminalreporter.stats.get('passed', [])) + failed_tests = len(terminalreporter.stats.get('failed', [])) + error_tests = len(terminalreporter.stats.get('error', [])) + skipped_tests = len(terminalreporter.stats.get('skipped', [])) + + # Prepare error details + error_details = "" + for test in terminalreporter.stats.get('failed', []) + terminalreporter.stats.get('error', []): + error_details += f"• *Test*: `{test.nodeid}`\n" + error_details += f" _Details:_\n```\n{''.join(test.longreprtext.splitlines(keepends=True)[-10:])}```\n\n" + error_details = "*Error Details:*\n" + error_details if error_details else "" + + # Determine status emoji and color + status_emoji = ":thumbsup:" if failed_tests == 0 and error_tests == 0 else ":thumbsdown:" + bug_emoji = "" if failed_tests == 0 and error_tests == 0 else ":bug-happy:" + status_color = "good" if failed_tests == 0 and error_tests == 0 else "danger" + + # Check if there's anything to report + if len(total_tests) == 0: + terminalreporter.write("No tests were run, skipping Slack notification.\n") + return + + # Create the payload for Slack + slack_data = { + "channel": "#observability-test", + "username": "Mygeneset.Info Tests", + "icon_emoji": f"{status_emoji}", + "attachments": [ + { + "color": status_color, + "title": f"{bug_emoji} Mygeneset Pytest Summary", + "text": f"Total Tests: *{len(total_tests)}*\n" + f"Passed: *{passed_tests}* :white_check_mark:\n" + f"Failed: *{failed_tests}* :x:\n" + f"Errors: *{error_tests}* :exclamation:\n" + f"Skipped: *{skipped_tests}* :fast_forward:\n\n" + f"{error_details}" + } + ] + } + + # Send to Slack + webhook_url = SLACK_WEBHOOK_URL # config.getoption('--slack-webhook-url') + if webhook_url: + response = requests.post(webhook_url, json=slack_data) + if response.status_code == 200: + terminalreporter.write("Slack notification sent successfully.\n") + else: + terminalreporter.write(f"Failed to send message to Slack: {response.status_code}, {response.text}\n") + else: + terminalreporter.write("Slack webhook URL not provided, skipping notification.\n") + + @pytest.hookimpl(tryfirst=True) + def pytest_addoption(parser): + """Add command-line options for Slack integration.""" + parser.addoption("--slack-webhook-url", action="store", default=None, help="Slack webhook URL to send messages") + parser.addoption("--slack-channel", action="store", default="#general", help="Slack channel to send messages") + parser.addoption("--slack-username", action="store", default="pytest-bot", help="Slack username to send messages as") + + # ### How to run manually: + # python -m pytest src/tests/test_remote.py --slack-webhook-url=$SLACK_WEBHOOK_URL + - name: Run pytest and generate report - run: pytest src/tests/test_remote.py --slack_hook=${{ secrets.SLACK_WEBHOOK_URL }} --slack_channel=observability-test --slack_username="mygeneset.info tests" + run: python -m pytest src/tests/test_remote.py --slack-webhook-url=${{ secrets.SLACK_WEBHOOK_URL }} env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} From 3588c172d177a4e744b5f851d6cb4b9486ff826e Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:34:43 -0700 Subject: [PATCH 02/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 39a6d620..828d4c7c 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -4,7 +4,9 @@ on: workflow_dispatch: # schedule: # - cron: '0 */3 * * *' - + pull_request: + branches: + - github-action-schedule-remote-tests jobs: run_app_tests: runs-on: ubuntu-latest From 543de845a4fd5dc2aa02c0029bcbdab62af14fe8 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:37:15 -0700 Subject: [PATCH 03/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 828d4c7c..91a80a64 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -125,7 +125,7 @@ jobs: # ### How to run manually: # python -m pytest src/tests/test_remote.py --slack-webhook-url=$SLACK_WEBHOOK_URL - - name: Run pytest and generate report + - name: Run pytest and send report to Slack run: python -m pytest src/tests/test_remote.py --slack-webhook-url=${{ secrets.SLACK_WEBHOOK_URL }} env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} From c6d3050280ed8b674fd86fe698708bca7bc1fd9d Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:39:40 -0700 Subject: [PATCH 04/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 91a80a64..3b42e92e 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -129,3 +129,7 @@ jobs: run: python -m pytest src/tests/test_remote.py --slack-webhook-url=${{ secrets.SLACK_WEBHOOK_URL }} env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + + - name: Setup tmate debug session on failure + if: ${{ failure() }} + uses: mxschmitt/action-tmate@v3 From 8b050c414b831919f502dd96b857d780f4f118b8 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:40:08 -0700 Subject: [PATCH 05/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 3b42e92e..010ef614 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -1,7 +1,7 @@ name: Scheduled Tests on: - workflow_dispatch: + # workflow_dispatch: # schedule: # - cron: '0 */3 * * *' pull_request: From 9ccc770866085e4cdd1437c6f9fc02cb53a9a737 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:42:19 -0700 Subject: [PATCH 06/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 010ef614..9e1ba987 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -4,6 +4,7 @@ on: # workflow_dispatch: # schedule: # - cron: '0 */3 * * *' + push: {} pull_request: branches: - github-action-schedule-remote-tests From 491e82e338a5a3fd3cc174f91498f0a76e5d33e6 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:48:22 -0700 Subject: [PATCH 07/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 9e1ba987..d531a9fc 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -44,10 +44,11 @@ jobs: run: pip install pytest-slack - name: Create conftest.py for Slack Notifications - uses: andstor/create-file-action@v1 + uses: DamianReeves/write-file-action@master with: - path: src/tests/conftest.py - content: | + path: conftest.py + write-mode: override + contents: | import pytest import requests from _pytest.terminal import TerminalReporter From 485b0f7aeaf7b1d29a7492d5a6b3bbeac12b65e6 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:49:15 -0700 Subject: [PATCH 08/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index d531a9fc..85dc1783 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -47,7 +47,7 @@ jobs: uses: DamianReeves/write-file-action@master with: path: conftest.py - write-mode: override + write-mode: overwrite contents: | import pytest import requests From 48f7d13dfabb2443678c4f9aeafc66d7fd397b2d Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:50:36 -0700 Subject: [PATCH 09/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 85dc1783..38336643 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -132,6 +132,6 @@ jobs: env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - - name: Setup tmate debug session on failure - if: ${{ failure() }} - uses: mxschmitt/action-tmate@v3 + # - name: Setup tmate debug session on failure + # if: ${{ failure() }} + # uses: mxschmitt/action-tmate@v3 From f5089fdee0b07d7d64ecf2ff85e6e51fac7a1f84 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:51:35 -0700 Subject: [PATCH 10/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 38336643..856a45de 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -89,7 +89,7 @@ jobs: # Create the payload for Slack slack_data = { - "channel": "#observability-test", + "channel": "#observability", "username": "Mygeneset.Info Tests", "icon_emoji": f"{status_emoji}", "attachments": [ From 82baf8b7aa1ca588e2e93d4cb23b9f306327e49c Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:52:51 -0700 Subject: [PATCH 11/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 856a45de..fbeb8cc4 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -89,7 +89,7 @@ jobs: # Create the payload for Slack slack_data = { - "channel": "#observability", + "channel": "#logger---web", "username": "Mygeneset.Info Tests", "icon_emoji": f"{status_emoji}", "attachments": [ From dbc25d6ebb74653d01cf9c2308d72e035d0a9c3f Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:58:38 -0700 Subject: [PATCH 12/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index fbeb8cc4..73ffb674 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -1,6 +1,9 @@ name: Scheduled Tests on: + workflow_dispatch: # Manual trigger + schedule: # Scheduled trigger + - cron: "0 18 * * 1-5" # Runs every weekday at 6 AM UTC # workflow_dispatch: # schedule: # - cron: '0 */3 * * *' From 0cdb9af0b7995403a190934e8fc41c1aa27b6429 Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:59:10 -0700 Subject: [PATCH 13/13] Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index 73ffb674..08fd88f6 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -4,13 +4,6 @@ on: workflow_dispatch: # Manual trigger schedule: # Scheduled trigger - cron: "0 18 * * 1-5" # Runs every weekday at 6 AM UTC - # workflow_dispatch: - # schedule: - # - cron: '0 */3 * * *' - push: {} - pull_request: - branches: - - github-action-schedule-remote-tests jobs: run_app_tests: runs-on: ubuntu-latest