Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 23 configure bluesky logging #114

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If you want to run these tests on a developer machine, clone this repo into `C:\


Once these files are in place, run the tests with `run_tests.bat`

You may have to start the IBEX server if it complains - `C:/Instrument/Apps/Epics/start_ibex_server.bat`

### Running all tests

Expand Down
47 changes: 47 additions & 0 deletions test_bluesky.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
import unittest

import bluesky.plan_stubs as bps
Expand All @@ -18,6 +20,7 @@
PeriodGoodFramesNormalizer,
)
from ibex_bluesky_core.devices.simpledae.waiters import GoodFramesWaiter, PeriodGoodFramesWaiter
from ibex_bluesky_core.logger import logger
from ibex_bluesky_core.run_engine import get_run_engine
from ophyd_async.plan_stubs import ensure_connected

Expand All @@ -31,6 +34,11 @@
P3_INIT_VALUE: float = 123.456
P5_INIT_VALUE: float = 987.654321

LOG_FOLDER = os.path.join("C:\\", "instrument", "var", "logs", "bluesky")
LOG_MESSAGE = "Logging something to "
LOG_ENV_PATH = "BLUESKY_LOGS"
LOG_FILE_NAME = "blueskylogs.log"


class TestBluesky(unittest.TestCase):
def setUp(self) -> None:
Expand Down Expand Up @@ -181,6 +189,45 @@ def _plan():
# Assert we successfully set npoints periods
self.assertEqual(g.get_number_periods(), npoints)

def test_GIVEN_logging_is_requested_THEN_the_log_folder_exists(self) -> None:
this_function_name = sys._getframe().f_code.co_name
message = LOG_MESSAGE + this_function_name
# Log invocation.
logger.blueskylogger.info(message)
if LOG_ENV_PATH in os.environ:
assert not os.path.exists(os.environ[LOG_ENV_PATH])

if LOG_ENV_PATH not in os.environ:
assert os.path.exists(LOG_FOLDER)

def test_GIVEN_logging_is_requested_THEN_the_log_file_exists(self) -> None:
log_path = LOG_FOLDER
if LOG_ENV_PATH in os.environ:
log_path = os.environ[LOG_ENV_PATH]

# Log invocation.
this_function_name = sys._getframe().f_code.co_name
message = LOG_MESSAGE + this_function_name
logger.blueskylogger.info(message)
qualified_log_filename = os.path.join(log_path, LOG_FILE_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think LOG_FILE_NAME needs defining similar to the other environment variables at the top of the file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added missed constant

assert os.path.exists(qualified_log_filename)

def test_GIVEN_logging_is_requested_THEN_the_log_file_contains_the_message(self) -> None:
log_path = LOG_FOLDER
if LOG_ENV_PATH in os.environ:
log_path = os.environ[LOG_ENV_PATH]

# Log invocation.
this_function_name = sys._getframe().f_code.co_name
message = LOG_MESSAGE + this_function_name
logger.blueskylogger.info(message)
qualified_log_filename = os.path.join(log_path, LOG_FILE_NAME)
assert os.path.exists(qualified_log_filename)
# Open the log file and read its content.
with open(qualified_log_filename, "r") as f:
content = f.read()
assert content.__contains__(message)


if __name__ == "__main__":
unittest.main()