From 24676a6fc968f78c5e178b01658d9ffe64a2c38b Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 16:25:47 -0400 Subject: [PATCH 01/12] showcapture no --- .github/workflows/distro_tests.yml | 2 +- .github/workflows/tests.yml | 2 +- bbot/test/test_step_1/test_events.py | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distro_tests.yml b/.github/workflows/distro_tests.yml index 395a38f5c..283c7e9e9 100644 --- a/.github/workflows/distro_tests.yml +++ b/.github/workflows/distro_tests.yml @@ -61,4 +61,4 @@ jobs: export BBOT_DISTRO_TESTS=true poetry env use python3.11 poetry install - poetry run pytest --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO . + poetry run pytest --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO . diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab04a94e4..531d47d9a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -48,7 +48,7 @@ jobs: poetry install - name: Run tests run: | - poetry run pytest --exitfirst --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . + poetry run pytest --exitfirst --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . - name: Upload Debug Logs uses: actions/upload-artifact@v3 with: diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.py index 00ce75ff8..97601a1e2 100644 --- a/bbot/test/test_step_1/test_events.py +++ b/bbot/test/test_step_1/test_events.py @@ -925,3 +925,9 @@ def test_event_closest_host(): vuln = scan.make_event( {"path": "/tmp/asdf.txt", "description": "test", "severity": "HIGH"}, "VULNERABILITY", parent=event3 ) + + +def test_test_output(): + import traceback + + assert False, traceback.format_stack() From d348d9611d4fe6678d1b74faf61fcab49b0ac350 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 16:44:36 -0400 Subject: [PATCH 02/12] silence live logging --- bbot/test/conftest.py | 7 +++++++ bbot/test/test_step_1/test_events.py | 6 ------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bbot/test/conftest.py b/bbot/test/conftest.py index 516c0fec2..7759a4964 100644 --- a/bbot/test/conftest.py +++ b/bbot/test/conftest.py @@ -47,6 +47,13 @@ def assert_all_responses_were_requested() -> bool: return False +@pytest.fixture(autouse=True) +def silence_live_logging(): + for handler in logging.getLogger().handlers: + if type(handler).__name__ == '_LiveLoggingStreamHandler': + handler.setLevel(logging.CRITICAL) + + @pytest.fixture def bbot_httpserver(): server = HTTPServer(host="127.0.0.1", port=8888, threaded=True) diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.py index 97601a1e2..00ce75ff8 100644 --- a/bbot/test/test_step_1/test_events.py +++ b/bbot/test/test_step_1/test_events.py @@ -925,9 +925,3 @@ def test_event_closest_host(): vuln = scan.make_event( {"path": "/tmp/asdf.txt", "description": "test", "severity": "HIGH"}, "VULNERABILITY", parent=event3 ) - - -def test_test_output(): - import traceback - - assert False, traceback.format_stack() From 905cc0430ae288907909fc730f7adb00e55ad795 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 16:47:15 -0400 Subject: [PATCH 03/12] blacked --- bbot/test/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bbot/test/conftest.py b/bbot/test/conftest.py index 7759a4964..dab778ea4 100644 --- a/bbot/test/conftest.py +++ b/bbot/test/conftest.py @@ -50,7 +50,7 @@ def assert_all_responses_were_requested() -> bool: @pytest.fixture(autouse=True) def silence_live_logging(): for handler in logging.getLogger().handlers: - if type(handler).__name__ == '_LiveLoggingStreamHandler': + if type(handler).__name__ == "_LiveLoggingStreamHandler": handler.setLevel(logging.CRITICAL) From 3f3c43bdced6ed301a717532d40a5ab82f6baab5 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 16:57:59 -0400 Subject: [PATCH 04/12] test tests --- .github/workflows/tests.yml | 2 +- bbot/test/test_step_1/test_events.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 531d47d9a..d47d7b830 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -48,7 +48,7 @@ jobs: poetry install - name: Run tests run: | - poetry run pytest --exitfirst --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . + poetry run pytest --exitfirst -k test_test_output --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . - name: Upload Debug Logs uses: actions/upload-artifact@v3 with: diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.py index 00ce75ff8..baf5fb8f4 100644 --- a/bbot/test/test_step_1/test_events.py +++ b/bbot/test/test_step_1/test_events.py @@ -925,3 +925,34 @@ def test_event_closest_host(): vuln = scan.make_event( {"path": "/tmp/asdf.txt", "description": "test", "severity": "HIGH"}, "VULNERABILITY", parent=event3 ) + + +def test_test_output(): + + import sys + import logging + + root = logging.getLogger() + print("Root logger handlers:") + for h in root.handlers: + print(f" Handler: {type(h).__name__}") + print(f" Stream: {getattr(h, 'stream', 'no stream')}") + print(f" Level: {logging.getLevelName(h.level)}") + print(f" Formatter: {h.formatter._fmt if h.formatter else 'None'}") + print() + + # Check other loggers too + for name in logging.root.manager.loggerDict: + logger = logging.getLogger(name) + if logger.handlers: + print(f"Logger '{name}' handlers:") + for h in logger.handlers: + print(f" Handler: {type(h).__name__}") + print(f" Stream: {getattr(h, 'stream', 'no stream')}") + print(f" Level: {logging.getLevelName(h.level)}") + print(f" Formatter: {h.formatter._fmt if h.formatter else 'None'}") + print() + + import traceback + + assert False, traceback.format_stack() From ffb8c09105d16ddda543df4a96f1556c56099737 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 16:58:23 -0400 Subject: [PATCH 05/12] remove thing --- .github/workflows/distro_tests.yml | 2 +- .github/workflows/tests.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distro_tests.yml b/.github/workflows/distro_tests.yml index 283c7e9e9..395a38f5c 100644 --- a/.github/workflows/distro_tests.yml +++ b/.github/workflows/distro_tests.yml @@ -61,4 +61,4 @@ jobs: export BBOT_DISTRO_TESTS=true poetry env use python3.11 poetry install - poetry run pytest --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO . + poetry run pytest --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO . diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d47d7b830..70d380022 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -48,7 +48,7 @@ jobs: poetry install - name: Run tests run: | - poetry run pytest --exitfirst -k test_test_output --show-capture=no --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . + poetry run pytest --exitfirst -k test_test_output --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . - name: Upload Debug Logs uses: actions/upload-artifact@v3 with: From 38d3f4911113e0c43c8ed3a0438774eb2f25d8fa Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 17:00:22 -0400 Subject: [PATCH 06/12] flaked --- bbot/test/test_step_1/test_events.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.py index baf5fb8f4..1d8cfd14c 100644 --- a/bbot/test/test_step_1/test_events.py +++ b/bbot/test/test_step_1/test_events.py @@ -929,7 +929,6 @@ def test_event_closest_host(): def test_test_output(): - import sys import logging root = logging.getLogger() From 16e82e8265d61268f29dcb7680e0d4d54f7db9d5 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 17:05:13 -0400 Subject: [PATCH 07/12] done --- .github/workflows/tests.yml | 2 +- bbot/test/test_step_1/test_events.py | 30 ---------------------------- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 70d380022..ab04a94e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -48,7 +48,7 @@ jobs: poetry install - name: Run tests run: | - poetry run pytest --exitfirst -k test_test_output --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . + poetry run pytest --exitfirst --reruns 2 -o timeout_func_only=true --timeout 1200 --disable-warnings --log-cli-level=INFO --cov-config=bbot/test/coverage.cfg --cov-report xml:cov.xml --cov=bbot . - name: Upload Debug Logs uses: actions/upload-artifact@v3 with: diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.py index 1d8cfd14c..00ce75ff8 100644 --- a/bbot/test/test_step_1/test_events.py +++ b/bbot/test/test_step_1/test_events.py @@ -925,33 +925,3 @@ def test_event_closest_host(): vuln = scan.make_event( {"path": "/tmp/asdf.txt", "description": "test", "severity": "HIGH"}, "VULNERABILITY", parent=event3 ) - - -def test_test_output(): - - import logging - - root = logging.getLogger() - print("Root logger handlers:") - for h in root.handlers: - print(f" Handler: {type(h).__name__}") - print(f" Stream: {getattr(h, 'stream', 'no stream')}") - print(f" Level: {logging.getLevelName(h.level)}") - print(f" Formatter: {h.formatter._fmt if h.formatter else 'None'}") - print() - - # Check other loggers too - for name in logging.root.manager.loggerDict: - logger = logging.getLogger(name) - if logger.handlers: - print(f"Logger '{name}' handlers:") - for h in logger.handlers: - print(f" Handler: {type(h).__name__}") - print(f" Stream: {getattr(h, 'stream', 'no stream')}") - print(f" Level: {logging.getLevelName(h.level)}") - print(f" Formatter: {h.formatter._fmt if h.formatter else 'None'}") - print() - - import traceback - - assert False, traceback.format_stack() From 0207fd08250e6ee81996e80524078d2e8f5f73d9 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 17:16:30 -0400 Subject: [PATCH 08/12] stop logger on exit --- bbot/core/config/logger.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bbot/core/config/logger.py b/bbot/core/config/logger.py index 4723d4237..10cdd7b2e 100644 --- a/bbot/core/config/logger.py +++ b/bbot/core/config/logger.py @@ -1,9 +1,11 @@ import sys +import atexit import logging from copy import copy import multiprocessing import logging.handlers from pathlib import Path +from contextlib import suppress from ..helpers.misc import mkdir, error_and_exit from ...logger import colorize, loglevel_mapping @@ -70,9 +72,14 @@ def __init__(self, core): # Start the QueueListener self.listener = logging.handlers.QueueListener(self.queue, *self.log_handlers.values()) self.listener.start() + atexit.register(self.listener.stop) self.log_level = logging.INFO + def stop_listener(self): + with suppress(Exception): + self.listener.stop() + def setup_queue_handler(self, logging_queue=None, log_level=logging.DEBUG): if logging_queue is None: logging_queue = self.queue From ad8802441b2bc1e794afac65f556e34cbc84fe4f Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 17:26:19 -0400 Subject: [PATCH 09/12] better log cleanup --- bbot/core/config/logger.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/bbot/core/config/logger.py b/bbot/core/config/logger.py index 10cdd7b2e..31fe1b10f 100644 --- a/bbot/core/config/logger.py +++ b/bbot/core/config/logger.py @@ -72,11 +72,28 @@ def __init__(self, core): # Start the QueueListener self.listener = logging.handlers.QueueListener(self.queue, *self.log_handlers.values()) self.listener.start() - atexit.register(self.listener.stop) + atexit.register(self.cleanup_logging) self.log_level = logging.INFO - def stop_listener(self): + def cleanup_logging(self): + # Close the queue handler + self.queue_handler.close() + + # Clean root logger + root_logger = logging.getLogger() + for handler in list(root_logger.handlers): + root_logger.removeHandler(handler) + handler.close() + + # Clean all other loggers + for logger in logging.Logger.manager.loggerDict.values(): + if hasattr(logger, 'handlers'): # Logger, not PlaceHolder + for handler in list(logger.handlers): + logger.removeHandler(handler) + handler.close() + + # Stop queue listener with suppress(Exception): self.listener.stop() From 60ab921912e0e065b70fab18123141ef0d3d803a Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 18:35:15 -0400 Subject: [PATCH 10/12] rename tests --- .github/workflows/distro_tests.yml | 2 +- .github/workflows/tests.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distro_tests.yml b/.github/workflows/distro_tests.yml index 395a38f5c..7b2bd51dd 100644 --- a/.github/workflows/distro_tests.yml +++ b/.github/workflows/distro_tests.yml @@ -1,4 +1,4 @@ -name: Linux Distro Tests +name: Tests (Linux Distros) on: pull_request: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab04a94e4..78b0cbd93 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,4 +1,4 @@ -name: tests +name: Tests (Python Versions) on: push: branches: From e8c27f4f2a221bac5f68cf1a96d70ac24d7b1aa0 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 18:36:06 -0400 Subject: [PATCH 11/12] don't skip dastardly tests --- bbot/test/test_step_2/module_tests/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bbot/test/test_step_2/module_tests/base.py b/bbot/test/test_step_2/module_tests/base.py index bb63b57e5..6bccf32dc 100644 --- a/bbot/test/test_step_2/module_tests/base.py +++ b/bbot/test/test_step_2/module_tests/base.py @@ -90,8 +90,8 @@ async def module_test( self, httpx_mock, bbot_httpserver, bbot_httpserver_ssl, monkeypatch, request, caplog, capsys ): # Skip dastardly test if we're in the distro tests (because dastardly uses docker) - if os.getenv("BBOT_DISTRO_TESTS") and self.name == "dastardly": - pytest.skip("Skipping module_test for dastardly module due to BBOT_DISTRO_TESTS environment variable") + # if os.getenv("BBOT_DISTRO_TESTS") and self.name == "dastardly": + # pytest.skip("Skipping module_test for dastardly module due to BBOT_DISTRO_TESTS environment variable") self.log.info(f"Starting {self.name} module test") module_test = self.ModuleTest( From 1837f0a85b605570ac510de11caabf8ef96580fd Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 28 Oct 2024 19:54:56 -0400 Subject: [PATCH 12/12] unskip dastardly tests --- bbot/core/config/logger.py | 17 +++++++++++------ bbot/test/test_step_2/module_tests/base.py | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/bbot/core/config/logger.py b/bbot/core/config/logger.py index 31fe1b10f..2e42ef8de 100644 --- a/bbot/core/config/logger.py +++ b/bbot/core/config/logger.py @@ -78,20 +78,25 @@ def __init__(self, core): def cleanup_logging(self): # Close the queue handler - self.queue_handler.close() + with suppress(Exception): + self.queue_handler.close() # Clean root logger root_logger = logging.getLogger() for handler in list(root_logger.handlers): - root_logger.removeHandler(handler) - handler.close() + with suppress(Exception): + root_logger.removeHandler(handler) + with suppress(Exception): + handler.close() # Clean all other loggers for logger in logging.Logger.manager.loggerDict.values(): - if hasattr(logger, 'handlers'): # Logger, not PlaceHolder + if hasattr(logger, "handlers"): # Logger, not PlaceHolder for handler in list(logger.handlers): - logger.removeHandler(handler) - handler.close() + with suppress(Exception): + logger.removeHandler(handler) + with suppress(Exception): + handler.close() # Stop queue listener with suppress(Exception): diff --git a/bbot/test/test_step_2/module_tests/base.py b/bbot/test/test_step_2/module_tests/base.py index 6bccf32dc..bb63b57e5 100644 --- a/bbot/test/test_step_2/module_tests/base.py +++ b/bbot/test/test_step_2/module_tests/base.py @@ -90,8 +90,8 @@ async def module_test( self, httpx_mock, bbot_httpserver, bbot_httpserver_ssl, monkeypatch, request, caplog, capsys ): # Skip dastardly test if we're in the distro tests (because dastardly uses docker) - # if os.getenv("BBOT_DISTRO_TESTS") and self.name == "dastardly": - # pytest.skip("Skipping module_test for dastardly module due to BBOT_DISTRO_TESTS environment variable") + if os.getenv("BBOT_DISTRO_TESTS") and self.name == "dastardly": + pytest.skip("Skipping module_test for dastardly module due to BBOT_DISTRO_TESTS environment variable") self.log.info(f"Starting {self.name} module test") module_test = self.ModuleTest(