|
| 1 | +from pathlib import Path |
| 2 | +from typing import Optional |
| 3 | +from unittest.mock import ANY, Mock, patch |
| 4 | +from urllib.parse import urlparse |
| 5 | + |
| 6 | +from pytest import mark |
| 7 | + |
| 8 | +from murfey.instrument_server.api import ( |
| 9 | + GainReference, |
| 10 | + _get_murfey_url, |
| 11 | + upload_gain_reference, |
| 12 | +) |
| 13 | +from murfey.util import posix_path |
| 14 | + |
| 15 | +test_get_murfey_url_params_matrix = ( |
| 16 | + # Server URL to use |
| 17 | + ("default",), |
| 18 | + ("0.0.0.0:8000",), |
| 19 | + ("murfey_server",), |
| 20 | + ("http://murfey_server:8000",), |
| 21 | + ("http://murfey_server:8080/api",), |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@mark.parametrize("test_params", test_get_murfey_url_params_matrix) |
| 26 | +def test_get_murfey_url( |
| 27 | + test_params: tuple[str], |
| 28 | + mock_client_configuration, # From conftest.py |
| 29 | +): |
| 30 | + # Unpack test_params |
| 31 | + (server_url_to_test,) = test_params |
| 32 | + |
| 33 | + # Replace the server URL from the fixture with other ones for testing |
| 34 | + if server_url_to_test != "default": |
| 35 | + mock_client_configuration["Murfey"]["server"] = server_url_to_test |
| 36 | + |
| 37 | + # Mock the module-wide config variable with the fixture value |
| 38 | + # The fixture is only loaded within the test function, so this patch |
| 39 | + # has to happen inside the function instead of as a decorator |
| 40 | + with patch("murfey.instrument_server.api.config", mock_client_configuration): |
| 41 | + known_server = _get_murfey_url() |
| 42 | + |
| 43 | + # Prepend 'http://' to config URLs that don't have it for the comparison |
| 44 | + # Otherwise, urlparse stores it under the 'path' attribute |
| 45 | + original_url = str(mock_client_configuration["Murfey"].get("server")) |
| 46 | + if not original_url.startswith(("http://", "https://")): |
| 47 | + original_url = f"http://{original_url}" |
| 48 | + |
| 49 | + # Check that the components of the result match those in the config |
| 50 | + parsed_original = urlparse(original_url) |
| 51 | + parsed_server = urlparse(known_server) |
| 52 | + assert parsed_server.scheme in ("http", "https") |
| 53 | + assert parsed_server.hostname == parsed_original.hostname |
| 54 | + assert parsed_server.port == parsed_original.port |
| 55 | + assert parsed_server.netloc == parsed_original.netloc |
| 56 | + assert parsed_server.path == parsed_original.path |
| 57 | + |
| 58 | + |
| 59 | +test_upload_gain_reference_params_matrix = ( |
| 60 | + # Rsync URL settings |
| 61 | + ("http://1.1.1.1",), # When rsync_url is provided |
| 62 | + ("",), # When rsync_url is blank |
| 63 | + (None,), # When rsync_url not provided |
| 64 | +) |
| 65 | + |
| 66 | + |
| 67 | +@mark.parametrize("test_params", test_upload_gain_reference_params_matrix) |
| 68 | +@patch("murfey.instrument_server.api.subprocess") |
| 69 | +@patch("murfey.instrument_server.api.tokens") |
| 70 | +@patch("murfey.instrument_server.api._get_murfey_url") |
| 71 | +@patch("murfey.instrument_server.api.requests") |
| 72 | +def test_upload_gain_reference( |
| 73 | + mock_request, |
| 74 | + mock_get_server_url, |
| 75 | + mock_tokens, |
| 76 | + mock_subprocess, |
| 77 | + test_params: tuple[Optional[str]], |
| 78 | +): |
| 79 | + |
| 80 | + # Unpack test parameters and define other ones |
| 81 | + (rsync_url_setting,) = test_params |
| 82 | + server_url = "http://0.0.0.0:8000" |
| 83 | + instrument_name = "murfey" |
| 84 | + session_id = 1 |
| 85 | + |
| 86 | + # Create a mock machine config base on the test params |
| 87 | + rsync_module = "data" |
| 88 | + gain_ref_dir = "C:/ProgramData/Gatan/Gain Reference" |
| 89 | + mock_machine_config = { |
| 90 | + "rsync_module": rsync_module, |
| 91 | + "gain_reference_directory": gain_ref_dir, |
| 92 | + } |
| 93 | + if rsync_url_setting is not None: |
| 94 | + mock_machine_config["rsync_url"] = rsync_url_setting |
| 95 | + |
| 96 | + # Assign expected values to the mock objects |
| 97 | + mock_response = Mock() |
| 98 | + mock_response.status_code = 200 |
| 99 | + mock_response.json.return_value = mock_machine_config |
| 100 | + mock_request.get.return_value = mock_response |
| 101 | + mock_get_server_url.return_value = server_url |
| 102 | + mock_subprocess.run.return_value = Mock(returncode=0) |
| 103 | + |
| 104 | + # Construct payload and pass request to function |
| 105 | + gain_ref_file = f"{gain_ref_dir}/gain.mrc" |
| 106 | + visit_path = "2025/aa00000-0" |
| 107 | + gain_dest_dir = "processing" |
| 108 | + payload = { |
| 109 | + "gain_path": gain_ref_file, |
| 110 | + "visit_path": visit_path, |
| 111 | + "gain_destination_dir": gain_dest_dir, |
| 112 | + } |
| 113 | + result = upload_gain_reference( |
| 114 | + instrument_name=instrument_name, |
| 115 | + session_id=session_id, |
| 116 | + gain_reference=GainReference( |
| 117 | + **payload, |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + # Check that the machine config request was called |
| 122 | + machine_config_url = f"{server_url}/instruments/{instrument_name}/machine" |
| 123 | + mock_request.get.assert_called_once_with( |
| 124 | + machine_config_url, |
| 125 | + headers={"Authorization": ANY}, |
| 126 | + ) |
| 127 | + |
| 128 | + # Check that the subprocess was run with the expected arguments |
| 129 | + # If no rsync_url key is provided, or rsync_url key is empty, |
| 130 | + # It should default to the server URL |
| 131 | + expected_rsync_url = ( |
| 132 | + urlparse(server_url) if not rsync_url_setting else urlparse(rsync_url_setting) |
| 133 | + ) |
| 134 | + expected_rsync_path = f"{expected_rsync_url.hostname}::{rsync_module}/{visit_path}/{gain_dest_dir}/gain.mrc" |
| 135 | + expected_rsync_cmd = [ |
| 136 | + "rsync", |
| 137 | + posix_path(Path(gain_ref_file)), |
| 138 | + expected_rsync_path, |
| 139 | + ] |
| 140 | + mock_subprocess.run.assert_called_once_with( |
| 141 | + expected_rsync_cmd, |
| 142 | + capture_output=True, |
| 143 | + text=True, |
| 144 | + ) |
| 145 | + |
| 146 | + # Check that the function ran through to completion successfully |
| 147 | + assert result == {"success": True} |
0 commit comments