Skip to content

Commit e1c3bac

Browse files
pranaverma92Prabhakar Kumar
authored andcommitted
Improves test logging infrastructure.
1 parent ca68bbf commit e1c3bac

File tree

7 files changed

+113
-16
lines changed

7 files changed

+113
-16
lines changed

.github/workflows/run-integration-tests.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# Workflow to run MATLAB-Proxy integration tests
44
name: Integration testing for MATLAB Proxy
55

6+
env:
7+
MWI_INTEG_TESTS_LOG_FILE_PATH: '${{ github.workspace }}/tests/integration/integ_test_logs.log'
8+
69
on:
710
# Reusable workflow
811
# Trigger on workflow call
@@ -26,20 +29,20 @@ jobs:
2629

2730
steps:
2831
- name: Checkout
29-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
3033

3134
- name: Setup MATLAB ${{ matrix.matlab-release }}
32-
uses: matlab-actions/setup-matlab@v2-beta
35+
uses: matlab-actions/setup-matlab@v2
3336
with:
3437
release: ${{ matrix.matlab-release }}
3538

3639
- name: Setup nodejs
37-
uses: actions/setup-node@v3
40+
uses: actions/setup-node@v4
3841
with:
3942
node-version: 18
4043

4144
- name: Set up Python ${{ matrix.python-version }}
42-
uses: actions/setup-python@v4
45+
uses: actions/setup-python@v5
4346
with:
4447
python-version: ${{ matrix.python-version }}
4548

@@ -57,3 +60,16 @@ jobs:
5760
env:
5861
TEST_USERNAME: ${{secrets.TEST_USERNAME}}
5962
TEST_PASSWORD: ${{secrets.TEST_PASSWORD}}
63+
64+
- name: Copy Log File
65+
if: ${{ always() }}
66+
run: |
67+
cp ${{ github.workspace }}/tests/integration/integ_test_logs.log ${{ matrix.os }}${{ matrix.matlab-release }}${{ matrix.python-version }}.log
68+
69+
- name: Make logs files available for downloading
70+
if: ${{ always() }}
71+
uses: actions/upload-artifact@v4
72+
with:
73+
name: MATLAB Proxy Integration Tests Log File ${{ matrix.os }}${{ matrix.matlab-release }}${{ matrix.python-version }}
74+
path: ${{ matrix.os }}${{ matrix.matlab-release }}${{ matrix.python-version }}.log
75+
retention-days: 7

tests/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ Python package.
7272
```powershell
7373
$env:TEST_USERNAME="some-username"; $env:TEST_PASSWORD="some-password"
7474
```
75-
75+
* If you need the tests logs to be available in a file, set MWI_INTEG_TESTS_LOG_FILE_PATH
76+
to the intended path of the log file.
77+
- Bash (Linux/macOS):
78+
```bash
79+
export MWI_INTEG_TESTS_LOG_FILE_PATH="Path intended"
80+
```
81+
- Powershell (Windows):
82+
```powershell
83+
$env:MWI_INTEG_TESTS_LOG_FILE_PATH="Path intended"
84+
```
7685
* Run the following command from the root directory of the project:
7786
```
7887
python3 -m pytest tests/integration -vs

tests/integration/integration_tests_utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import urllib3
99
import requests
1010
import json
11+
from logging_util import create_test_logger
12+
13+
_logger = create_test_logger(__name__, log_file_path = os.getenv("MWI_INTEG_TESTS_LOG_FILE_PATH"))
1114

1215

1316
def perform_basic_checks():
@@ -114,13 +117,13 @@ def wait_matlab_proxy_ready(matlab_proxy_url):
114117
):
115118
time.sleep(1)
116119

117-
print("MATLAB PROXY IS READY...")
118-
119120
try:
120121
if not os.path.exists(str(get_matlab_config_file())):
121122
raise FileNotFoundError("Config file is not present on the path")
122-
except FileNotFoundError:
123-
print("File does not exist")
123+
124+
except FileNotFoundError as e:
125+
_logger.exception("Config file does not exist")
126+
_logger.exception(e)
124127

125128

126129
def get_random_free_port() -> str:
@@ -151,8 +154,6 @@ def wait_server_info_ready(port_number):
151154
config_folder = settings.get_mwi_config_folder()
152155
_path = config_folder / "ports" / port_number / "mwi_server.info"
153156

154-
print("_path in wait_server_info_ready ", _path)
155-
156157
while time.time() - start_time < timeout_value:
157158
if _path.exists():
158159
return True
@@ -287,7 +288,7 @@ def get_connection_string(port_number):
287288
conn_string = _file.readline().rstrip()
288289

289290
except FileNotFoundError:
290-
print(f"{_path} does not exist")
291+
_logger.exception(f"{_path} does not exist")
291292

292293
return conn_string
293294

tests/integration/integration_tests_with_license/conftest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import shutil
88
from matlab_proxy.util import system
99
from matlab_proxy.util.mwi import environment_variables as mwi_env
10+
from logging_util import create_test_logger
11+
12+
_logger = create_test_logger(__name__, log_file_path = os.getenv("MWI_INTEG_TESTS_LOG_FILE_PATH"))
1013

1114

1215
@pytest.fixture(scope="module", name="module_monkeypatch")
@@ -88,9 +91,10 @@ def matlab_proxy_fixture(module_monkeypatch, request):
8891

8992
# Run matlab-proxy in the background in an event loop
9093
proc = loop.run_until_complete(utils.start_matlab_proxy_app(input_env=input_env))
94+
_logger.info("Started MATLAB Proxy process")
9195

9296
utils.wait_server_info_ready(mwi_app_port)
93-
print("Server file is available")
97+
_logger.info("Server file is available")
9498

9599
matlab_proxy_url = utils.get_connection_string(mwi_app_port)
96100

@@ -104,14 +108,15 @@ def matlab_proxy_fixture(module_monkeypatch, request):
104108
requests.exceptions.SSLError,
105109
),
106110
)
107-
print("In matlab_proxy_fixture MATLAB proxy URL responded")
111+
_logger.info("MATLAB proxy URL responded")
108112

109113
# License matlab-proxy using playwright UI automation
110114
utils.license_matlab_proxy(matlab_proxy_url)
111-
print("In matlab_proxy_fixture Proxy is licensed")
115+
_logger.info("Successfully licensed MATLAB Proxy")
112116

113117
# Wait for matlab-proxy to be up and running
114118
utils.wait_matlab_proxy_ready(matlab_proxy_url)
119+
_logger.info("MATLAB Proxy is ready")
115120

116121
# Get the location of ".matlab"
117122
matlab_config_file = str(

tests/integration/integration_tests_with_license/test_http_end_points.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import re
1212
from requests.adapters import HTTPAdapter, Retry
1313
from urllib.parse import urlparse
14+
from logging_util import create_test_logger
15+
16+
_logger = create_test_logger(__name__, log_file_path = os.getenv("MWI_INTEG_TESTS_LOG_FILE_PATH"))
1417

1518
# Timeout for polling the matlab-proxy http endpoints
1619
# matlab proxy in Mac machines takes more time to be 'up'
@@ -80,7 +83,8 @@ def _move(source, destination):
8083
try:
8184
shutil.move(source, destination)
8285
except shutil.Error as err:
83-
print(f"Error in moving {source}", err)
86+
_logger.error(f"Error in moving {source}")
87+
_logger.exception(err)
8488

8589

8690
def _send_http_get_request(uri, connection_scheme, http_endpoint=""):
@@ -156,6 +160,7 @@ def test_stop_matlab(matlab_proxy_app_fixture):
156160
matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
157161
"""
158162

163+
_logger.info("Testing stopping")
159164
status = _check_matlab_status(
160165
matlab_proxy_app_fixture.connection_scheme,
161166
"up",
@@ -180,6 +185,7 @@ def test_stop_matlab(matlab_proxy_app_fixture):
180185
matlab_proxy_app_fixture.url,
181186
)
182187
assert status == "down"
188+
_logger.info("stop_matlab test passed")
183189

184190

185191
async def test_print_message(matlab_proxy_app_fixture):

tests/integration/integration_tests_without_license/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import pytest
44
from integration import integration_tests_utils as utils
55
import requests
6+
from logging_util import create_test_logger
7+
import os
8+
9+
_logger = create_test_logger(__name__, log_file_path = os.getenv("MWI_INTEG_TESTS_LOG_FILE_PATH"))
610

711

812
@pytest.fixture(scope="module", name="module_monkeypatch")
@@ -42,6 +46,7 @@ def start_matlab_proxy_fixture(module_monkeypatch):
4246

4347
# Run matlab-proxy in the background in an event loop
4448
proc = loop.run_until_complete(utils.start_matlab_proxy_app(input_env=input_env))
49+
_logger.info("Started MATLAB Proxy process")
4550

4651
# Wait for mwi_server.info file to be ready
4752
utils.wait_server_info_ready(mwi_app_port)

tests/logging_util.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2024 The MathWorks, Inc.
2+
3+
import logging
4+
from pathlib import Path
5+
6+
7+
def create_test_logger(log_name, log_level=logging.INFO, log_file_path=None):
8+
"""
9+
Returns a logger with specified name and level
10+
11+
Args:
12+
log_file_path (string): Log file path to which the logs should be written
13+
log_level (logger level attribute): Log level to be set for the logger
14+
log_name (string): Name of the logger to be used
15+
"""
16+
17+
# Create a logger with the name 'TEST'
18+
logger = logging.getLogger("TEST - " + log_name)
19+
20+
# Create a formatter
21+
formatter = logging.Formatter(
22+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
23+
)
24+
25+
# Set the logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL)
26+
logger.setLevel(log_level)
27+
28+
if log_file_path:
29+
file_path = Path(log_file_path)
30+
31+
# Create a file handler
32+
file_handler = logging.FileHandler(filename=file_path, mode="a")
33+
34+
# Set a logging level for the file
35+
file_handler.setLevel(log_level)
36+
37+
# Set the formatter for the console handler
38+
file_handler.setFormatter(formatter)
39+
40+
# Add the console handler to the logger
41+
logger.addHandler(file_handler)
42+
43+
# Create a console handler
44+
console_handler = logging.StreamHandler()
45+
46+
# Set a logging level for the console handler
47+
console_handler.setLevel(log_level)
48+
49+
# Set the formatter for the console handler
50+
console_handler.setFormatter(formatter)
51+
52+
# Add the console handler to the logger
53+
logger.addHandler(console_handler)
54+
55+
return logger

0 commit comments

Comments
 (0)