Skip to content

Commit 99896aa

Browse files
author
Prabhakar Kumar
committed
Merge branch 'Add_tests' into 'main'
Modified settings.py to return test specific settings. Added tests to test_app.py See merge request cit/mathworks/jupyter-matlab-proxy!20
2 parents 26c1005 + 690a243 commit 99896aa

File tree

6 files changed

+319
-123
lines changed

6 files changed

+319
-123
lines changed

jupyter_matlab_proxy/app_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def reserve_matlab_port(self):
294294
# FIXME Because of https://github.com/http-party/node-http-proxy/issues/1342 the
295295
# node application in development mode always uses port 31515 to bypass the
296296
# reverse proxy. Once this is addressed, remove this special case.
297-
if os.getenv("DEV") == "true":
297+
if os.getenv("DEV") == "true" and os.getenv("TEST", "false").lower() != "true":
298298
self.matlab_port = 31515
299299
else:
300300

jupyter_matlab_proxy/settings.py

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,91 @@ def get_matlab_version(matlab_path):
2626
return root.find("release").text
2727

2828

29-
def get(dev=False):
30-
devel_file = Path(__file__).resolve().parent / "./devel.py"
31-
matlab_startup_file = str(Path(__file__).resolve().parent / "matlab" / "startup.m")
32-
matlab_ready_file = Path(tempfile.mkstemp()[1])
29+
def get_ws_env_settings():
3330
ws_env = (os.getenv("WS_ENV") or "").lower()
3431
ws_env_suffix = f"-{ws_env}" if "integ" in ws_env else ""
3532

33+
return ws_env, ws_env_suffix
34+
35+
36+
def get_dev_settings():
37+
devel_file = Path(__file__).resolve().parent / "./devel.py"
38+
matlab_ready_file = Path(tempfile.mkstemp()[1])
39+
ws_env, ws_env_suffix = get_ws_env_settings()
40+
41+
return {
42+
"matlab_path": Path(),
43+
"matlab_version": "R2020b",
44+
"matlab_cmd": [
45+
"python",
46+
"-u",
47+
str(devel_file),
48+
"matlab",
49+
"--ready-file",
50+
str(matlab_ready_file),
51+
],
52+
"xvfb_cmd": [
53+
"python",
54+
"-u",
55+
str(devel_file),
56+
"xvfb",
57+
"--ready-file",
58+
str(Path(tempfile.gettempdir()) / ".X11-unix" / "X1"),
59+
],
60+
"matlab_ready_file": matlab_ready_file,
61+
"base_url": os.environ.get("BASE_URL", ""),
62+
"app_port": os.environ.get("APP_PORT", 8000),
63+
"host_interface": os.environ.get("APP_HOST", "127.0.0.1"),
64+
"mwapikey": str(uuid.uuid4()),
65+
"matlab_protocol": "http",
66+
"matlab_display": ":1",
67+
"nlm_conn_str": os.environ.get("MLM_LICENSE_FILE"),
68+
"matlab_config_file": Path(tempfile.gettempdir())
69+
/ ".matlab"
70+
/ "proxy_app_config.json",
71+
"ws_env": ws_env,
72+
"mwa_api_endpoint": f"https://login{ws_env_suffix}.mathworks.com/authenticationws/service/v4",
73+
"mhlm_api_endpoint": f"https://licensing{ws_env_suffix}.mathworks.com/mls/service/v1/entitlement/list",
74+
"mwa_login": f"https://login{ws_env_suffix}.mathworks.com",
75+
}
76+
77+
78+
def get(dev=False):
79+
"""Method which returns the settings specific to the environment in which the server is running in
80+
If the environment variable 'TEST' is set to true, will make some changes to the dev settings.
81+
82+
Args:
83+
dev (bool, optional): development environment. Defaults to False.
84+
85+
Returns:
86+
Dict: Containing data on how to start MATLAB among other information.
87+
"""
88+
3689
if dev:
37-
return {
38-
"matlab_path": Path(),
39-
"matlab_version": "R2020b",
40-
"matlab_cmd": [
41-
"python",
42-
"-u",
43-
str(devel_file),
44-
"matlab",
45-
"--ready-file",
46-
str(matlab_ready_file),
47-
],
48-
"xvfb_cmd": [
49-
"python",
50-
"-u",
51-
str(devel_file),
52-
"xvfb",
53-
"--ready-file",
54-
str(Path(tempfile.gettempdir()) / ".X11-unix" / "X1"),
55-
],
56-
"matlab_ready_file": matlab_ready_file,
57-
"base_url": os.environ.get("BASE_URL", ""),
58-
"app_port": os.environ.get("APP_PORT", 8000),
59-
"host_interface": os.environ.get("APP_HOST", "127.0.0.1"),
60-
"mwapikey": str(uuid.uuid4()),
61-
"matlab_protocol": "http",
62-
"matlab_display": ":1",
63-
"nlm_conn_str": os.environ.get("MLM_LICENSE_FILE"),
64-
"matlab_config_file": Path(tempfile.gettempdir())
65-
/ ".matlab"
66-
/ "proxy_app_config.json",
67-
"ws_env": ws_env,
68-
"mwa_api_endpoint": f"https://login{ws_env_suffix}.mathworks.com/authenticationws/service/v4",
69-
"mhlm_api_endpoint": f"https://licensing{ws_env_suffix}.mathworks.com/mls/service/v1/entitlement/list",
70-
"mwa_login": f"https://login{ws_env_suffix}.mathworks.com",
71-
}
90+
settings = get_dev_settings()
91+
92+
# If running tests using Pytest, it will set environment variable TEST to true before running tests.
93+
# Will make test env specific changes before returning the settings.
94+
if os.environ.get("TEST", "False").lower() == "true":
95+
96+
# Set ready_delay value to 0 for faster fake MATLAB startup.
97+
ready_delay = ["--ready-delay", "0"]
98+
matlab_cmd = settings["matlab_cmd"]
99+
matlab_cmd[4:4] = ready_delay
100+
settings["matlab_cmd"] = matlab_cmd
101+
102+
# Set NLM Connection string. Server will start using this connection string for licensing
103+
settings["nlm_conn_str"] = "abc@nlm"
104+
105+
return settings
106+
72107
else:
108+
matlab_startup_file = str(
109+
Path(__file__).resolve().parent / "matlab" / "startup.m"
110+
)
73111
matlab_path = get_matlab_path()
112+
ws_env, ws_env_suffix = get_ws_env_settings()
113+
74114
return {
75115
"matlab_path": matlab_path,
76116
"matlab_version": get_matlab_version(matlab_path),
@@ -102,7 +142,8 @@ def get(dev=False):
102142
"host_interface": os.environ.get("APP_HOST"),
103143
"mwapikey": str(uuid.uuid4()),
104144
"matlab_protocol": "https",
105-
"matlab_display": ":1",
145+
# TODO: Uncomment this ?
146+
# "matlab_display": ":1",
106147
"nlm_conn_str": os.environ.get("MLM_LICENSE_FILE"),
107148
"matlab_config_file": Path.home() / ".matlab" / "proxy_app_config.json",
108149
"ws_env": ws_env,

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[pytest]
22
env =
33
D:DEV=true
4+
D:TEST=true
45
testpaths =
56
tests
67

tests/conftest.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,3 @@ def pre_test_cleanup():
2222
os.remove(matlab_config_file)
2323
except:
2424
pass
25-
26-
27-
@pytest.fixture(name="mock_settings_get_custom_ready_delay")
28-
def mock_settings_get_custom_ready_delay_fixture(mocker):
29-
"""A pytest fixture which mocks settings.get() method.
30-
31-
The ready_delay for the fake_matlab_process is 10 (by default)
32-
The 'matlab_cmd' returned within settings.get() method does not
33-
contain a --ready-delay option for running the command. So it defaults to 10.
34-
35-
This fixture mocks the settings.get() method and then modifies only the
36-
'matlab_cmd' key.
37-
38-
It adds the --ready-delay param and sets its value to 0.
39-
40-
Args:
41-
mocker : A built in pytest fixture
42-
dev_settings (Dict): A Dict which contains dev-mode settings
43-
"""
44-
45-
# Grab the settings:
46-
custom_settings = settings.get(dev=True)
47-
48-
# Add the --ready-delay param with value 0
49-
ready_delay = ["--ready-delay", "0"]
50-
matlab_cmd = custom_settings["matlab_cmd"]
51-
matlab_cmd[4:4] = ready_delay
52-
53-
# Update the matlab_cmd key
54-
custom_settings["matlab_cmd"] = matlab_cmd
55-
56-
# Patch it to return the custom settings.
57-
mocker.patch("jupyter_matlab_proxy.settings.get", return_value=custom_settings)

0 commit comments

Comments
 (0)