Skip to content

Commit 9de916c

Browse files
Kumar Pallavkrisctl
authored andcommitted
Adds tests for custom MATLAB startup code execution feature.
1 parent fb04706 commit 9de916c

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

tests/integration/integration_tests_with_license/test_http_end_points.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ async def __aenter__(self):
198198

199199
self.temp_dir_name = "temp_dir"
200200
self.mwi_base_url = "/matlab-test"
201+
self.mwi_matlab_startup_script = "c=12"
201202

202203
# Environment variables to launch matlab proxy
203204
input_env = {
204205
"MWI_APP_PORT": self.mwi_app_port,
205206
"MWI_BASE_URL": self.mwi_base_url,
207+
"MWI_MATLAB_STARTUP_SCRIPT": self.mwi_matlab_startup_script,
206208
}
207209

208210
self.proc = await utils.start_matlab_proxy_app(
@@ -399,3 +401,92 @@ def test_download_file_from_matlab(
399401
# Once MATLAB is up, we can then attempt to download
400402
result = _download_test_file(matlab_proxy_app_fixture, test_file)
401403
assert result == test_file_contents
404+
405+
406+
def test_matlab_startup_script_execution(matlab_proxy_app_fixture):
407+
"""Test that the MATLAB startup script is executed and creates the expected output file.
408+
409+
This test verifies:
410+
1. MATLAB is up and running
411+
2. The startup script (c=12) was executed
412+
3. The output file exists at the expected location
413+
4. The file contains the expected content
414+
5. After stopping MATLAB, the output file is deleted
415+
416+
Args:
417+
matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
418+
"""
419+
# Arrange
420+
# Ensure MATLAB is up
421+
status = _check_matlab_status(matlab_proxy_app_fixture, "up")
422+
assert status == "up"
423+
424+
# Read the logs to find the path of the startup code output file
425+
read_descriptor, write_descriptor = matlab_proxy_app_fixture.dpipe
426+
number_of_bytes = 2000
427+
428+
if read_descriptor:
429+
line = os.read(read_descriptor, number_of_bytes).decode("utf-8")
430+
process_logs = line.strip()
431+
# Find the path of the startup code output file
432+
match = re.search(
433+
r"The results of executing MWI_MATLAB_STARTUP_SCRIPT are stored at: \s*(.*?startup_code_output\.txt)",
434+
process_logs,
435+
)
436+
if match:
437+
output_file_path = match.group(1)
438+
_logger.info(f"Found startup code output file path: {output_file_path}")
439+
else:
440+
log_excerpt = (
441+
process_logs[:1500] + "..."
442+
if len(process_logs) > 1500
443+
else process_logs
444+
)
445+
error_message = (
446+
f"Could not find startup code output file path in logs. "
447+
f"Expected to find a message containing 'When MATLAB starts, you can see the output for your startup code at:' "
448+
f"followed by a path to 'startup_code_output.txt'. "
449+
f"Log excerpt: {log_excerpt}"
450+
)
451+
assert False, error_message
452+
453+
# Assert
454+
# Verify the file exists
455+
assert os.path.exists(
456+
output_file_path
457+
), f"Startup code output file not found at {output_file_path}"
458+
459+
# Read the file content and verify it contains the expected output
460+
with open(output_file_path, "r") as f:
461+
content = f.read()
462+
normalized_content = "".join(content.split())
463+
assert (
464+
"c=12" in normalized_content
465+
), f"Expected value 'c=12' in file content, but got: {normalized_content}"
466+
467+
# Act
468+
# Stop MATLAB
469+
http_endpoint_to_test = "/stop_matlab"
470+
stop_url = matlab_proxy_app_fixture.url + http_endpoint_to_test
471+
472+
with requests.Session() as s:
473+
retries = Retry(total=10, backoff_factor=0.1)
474+
s.mount(
475+
f"{matlab_proxy_app_fixture.connection_scheme}://",
476+
HTTPAdapter(max_retries=retries),
477+
)
478+
s.delete(stop_url, headers=matlab_proxy_app_fixture.headers, verify=False)
479+
480+
status = _check_matlab_status(matlab_proxy_app_fixture, "down")
481+
482+
# Assert
483+
assert status == "down"
484+
# Verify the file has been deleted
485+
assert not os.path.exists(
486+
output_file_path
487+
), f"Startup code output file was not deleted after stopping MATLAB: {output_file_path}"
488+
489+
# Cleanup
490+
# Close the read and write descriptors
491+
os.close(read_descriptor)
492+
os.close(write_descriptor)

tests/matlab-tests/TestEvaluateUserMatlabCodeScript.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% Copyright 2024 The MathWorks, Inc.
1+
% Copyright 2024-2025 The MathWorks, Inc.
22

33
classdef TestEvaluateUserMatlabCodeScript < matlab.unittest.TestCase
44
% TestEvaluateUserMatlabCodeScript contains unit tests for the complete function
@@ -81,5 +81,16 @@ function testInvalidScript(testCase)
8181
testCase.verifyTrue(contains(logContent, 'An error occurred in the following code:'));
8282
testCase.verifyTrue(contains(logContent, 'MATLAB:UndefinedFunction'));
8383
end
84+
85+
function testVariableCreationInBaseWorkspace(testCase)
86+
% Test that a variable created in the startup script exists in the workspace
87+
setenv('MWI_MATLAB_STARTUP_SCRIPT', 'myStartupVar = 10;');
88+
evaluateUserMatlabCode();
89+
% Check variable existence and value
90+
varExists = eval("exist('myStartupVar', 'var')");
91+
assert(varExists == 1, 'myStartupVar should exist in workspace');
92+
varValue = eval("myStartupVar");
93+
assert(varValue == 10, 'myStartupVar should have value 10');
94+
end
8495
end
8596
end

0 commit comments

Comments
 (0)