Skip to content

Commit eb04d89

Browse files
diningPhilosopher64Prabhakar Kumar
authored andcommitted
Includes HOSTNAME in the directory structure into which matlab-proxy stores logs. This fixes issues related to mounted HOME folders in multi-host environments.
fixes #27
1 parent 5ee4d96 commit eb04d89

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

matlab_proxy/settings.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,22 @@ def get_ws_env_settings():
132132
def get_mwi_config_folder(dev=False):
133133
if dev:
134134
return get_test_temp_dir()
135+
135136
else:
136-
return Path.home() / ".matlab" / "MWI"
137+
config_folder_path = Path.home() / ".matlab" / "MWI"
138+
# In multi-host environments, Path.home() can be the same for
139+
# multiple hosts and can cause issues when different hosts launch
140+
# matlab-proxy on the same port.
141+
# Using hostname to be part of the path of the config folder would avoid collisions.
142+
hostname = socket.gethostname()
143+
if hostname:
144+
config_folder_path = config_folder_path / "hosts" / hostname
145+
146+
logger.debug(
147+
f"{'Hostname could not be determined. ' if not hostname else '' }Using the folder: {config_folder_path} for storing all matlab-proxy related session information"
148+
)
149+
150+
return config_folder_path
137151

138152

139153
def get_mwi_logs_root_dir(dev=False):

tests/unit/test_devel.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ def matlab_log_dir_fixture(monkeypatch, tmp_path):
3434
monkeypatch : A built-in pytest fixture.
3535
tmp_path: tmp_path fixture provides a temporary directory unique to the test invocation.
3636
"""
37-
matlab_log_dir = str(tmp_path)
38-
monkeypatch.setenv("MATLAB_LOG_DIR", matlab_log_dir)
37+
hostname = socket.gethostname()
38+
matlab_log_dir = Path(tmp_path)
39+
40+
if hostname:
41+
matlab_log_dir = matlab_log_dir / "hosts" / hostname
42+
matlab_log_dir.mkdir(parents=True, exist_ok=True)
43+
44+
monkeypatch.setenv("MATLAB_LOG_DIR", str(matlab_log_dir))
45+
3946
return matlab_log_dir
4047

4148

4249
@pytest.fixture(name="matlab_ready_file")
43-
def matlab_ready_file_fixture(matlab_log_dir, monkeypatch):
50+
def matlab_ready_file_fixture(matlab_log_dir):
4451
"""A pytest fixture to create the matlab_ready_file.
4552
4653
This fixture creates the matlab readyfile path based on the matlab_log_dir fixture output.
@@ -124,8 +131,7 @@ def matlab_process_valid_nlm_fixture(matlab_log_dir, matlab_process_setup, valid
124131
"""
125132

126133
matlab_process = subprocess.Popen(
127-
matlab_process_setup.matlab_cmd,
128-
stderr=subprocess.PIPE,
134+
matlab_process_setup.matlab_cmd, stderr=subprocess.PIPE
129135
)
130136

131137
yield

tests/unit/test_settings.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import time
5+
import tempfile
56

67
import matlab_proxy
78
import matlab_proxy.settings as settings
@@ -324,3 +325,51 @@ def test_get_process_timeout(timeout_value, monkeypatch):
324325

325326
# Assert
326327
assert expected_timeout == actual_timeout
328+
329+
330+
def test_get_mwi_config_folder_dev():
331+
## Arrange
332+
expected_config_dir = Path(tempfile.gettempdir()) / "MWI" / "tests"
333+
334+
# Act
335+
actual_config_dir = settings.get_mwi_config_folder(dev=True)
336+
337+
# Assert
338+
assert expected_config_dir == actual_config_dir
339+
340+
341+
@pytest.mark.parametrize(
342+
"hostname, home, expected_mwi_config_dir",
343+
[
344+
(
345+
"bob",
346+
Path("/home/bob"),
347+
Path("/home/bob") / ".matlab" / "MWI" / "hosts" / "bob",
348+
),
349+
(
350+
"bob",
351+
Path("/home/CommonProject"),
352+
Path("/home/CommonProject") / ".matlab" / "MWI" / "hosts" / "bob",
353+
),
354+
(
355+
None,
356+
Path("/home/CommonProject"),
357+
Path("/home/CommonProject") / ".matlab" / "MWI",
358+
),
359+
],
360+
ids=[
361+
"Single host machine with unique $HOME per host",
362+
"Multi-host machine with common $HOME for multiple hosts",
363+
"default directory when hostname is missing",
364+
],
365+
)
366+
def test_get_mwi_config_folder(mocker, hostname, home, expected_mwi_config_dir):
367+
# Arrange
368+
mocker.patch("matlab_proxy.settings.Path.home", return_value=home)
369+
mocker.patch("matlab_proxy.settings.socket.gethostname", return_value=hostname)
370+
371+
# Act
372+
actual_config_dir = settings.get_mwi_config_folder()
373+
374+
# Assert
375+
assert expected_mwi_config_dir == actual_config_dir

0 commit comments

Comments
 (0)