Skip to content

Commit 0ab3ab9

Browse files
saiarcot895lguohan
authored andcommitted
Fix test execution on Bookworm (#3041)
* Fix test execution on Bookworm Running tests with `python3 -m pytest` instead of `python setup.py test` causes some breakages on Bookworm. Some of this has to do with what module searc paths get added, while others appear to be related to changes in the `mock` module between Python 3.9 and Python 3.11. Fix this and make sure it works on both Bullseye and Bookworm. * Fix mclag_test.py having different results for some test cases The test_mclag_add_mclag_member_to_nonexisting_domain test case passes when run on Bullseye, but fails on Bookworm. This is because some test cases modify the value of `mclag.ADHOC_VALIDATION`, but this value may persist for other test cases as well, and if test cases happen to run in a different order, then it may unexpectedly fail. For now, fix `test_mclag_add_mclag_member_to_nonexisting_domain` by setting the value there. * Add check_output parameter to the setup function due to the patch Since there is a patched function specified as an attribute, newer versions of mock expect that the object can be passed in as a parameter to the function. However, the `setup` functions didn't accept it as a parameter. Modify the `setup` functions to accept a parameter for this object. Signed-off-by: Saikrishna Arcot <[email protected]> --------- Signed-off-by: Saikrishna Arcot <[email protected]>
1 parent ef8f6f8 commit 0ab3ab9

File tree

7 files changed

+173
-730
lines changed

7 files changed

+173
-730
lines changed

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[pytest]
22
addopts = --cov-config=.coveragerc --cov --cov-report html --cov-report term --cov-report xml --junitxml=test-results.xml -vv
3+
pythonpath = .

tests/chassis_modules_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import os
33
from click.testing import CliRunner
44

5-
test_path = os.path.dirname(os.path.abspath(__file__))
6-
modules_path = os.path.dirname(test_path)
7-
sys.path.insert(0, modules_path)
8-
95
import show.main as show
106
import config.main as config
117
import tests.mock_tables.dbconnector

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
'snmp.timer',
6161
'telemetry.timer']
6262

63+
@pytest.fixture(autouse=True)
64+
def setup_env():
65+
# This is needed because we call scripts from this module as a separate
66+
# process when running tests, and so the PYTHONPATH needs to be set
67+
# correctly for those scripts to run.
68+
if "PYTHONPATH" not in os.environ:
69+
os.environ["PYTHONPATH"] = os.getcwd()
6370

6471
@pytest.fixture
6572
def get_cmd_module():

tests/debug_test.py

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

77
class TestDebugFrr(object):
88
@patch('subprocess.check_output', MagicMock(return_value='FRRouting'))
9-
def setup(self):
9+
def setup(self, check_output = None):
1010
print('SETUP')
1111
import debug.main as debug
1212
import undebug.main as undebug
@@ -379,7 +379,7 @@ def test_undebug_zebra_vxlan(self, run_command):
379379

380380
class TestDebugQuagga(object):
381381
@patch('subprocess.check_output', MagicMock(return_value='quagga'))
382-
def setup(self):
382+
def setup(self, check_output = None):
383383
print('SETUP')
384384
import debug.main as debug
385385
import undebug.main as undebug

tests/decode_syseeprom_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
decode_syseeprom_path = os.path.join(scripts_path, 'decode-syseeprom')
20-
decode_syseeprom = load_module_from_source('decode-syseeprom', decode_syseeprom_path)
20+
decode_syseeprom = load_module_from_source('decode_syseeprom', decode_syseeprom_path)
2121

2222
# Replace swsscommon objects with mocked objects
2323
decode_syseeprom.SonicV2Connector = dbconnector.SonicV2Connector
@@ -195,8 +195,9 @@ def test_print_model(self, capsys):
195195

196196
@mock.patch('os.geteuid', lambda: 0)
197197
@mock.patch('sonic_py_common.device_info.get_platform', lambda: 'arista')
198-
@mock.patch('decode-syseeprom.read_and_print_eeprom')
199-
@mock.patch('decode-syseeprom.read_eeprom_from_db')
198+
@mock.patch.object(sys, 'argv', ["decode-syseeprom"])
199+
@mock.patch('decode_syseeprom.read_and_print_eeprom')
200+
@mock.patch('decode_syseeprom.read_eeprom_from_db')
200201
def test_support_platforms_not_db_based(self, mockDbBased, mockNotDbBased):
201202
decode_syseeprom.main()
202203
assert mockNotDbBased.called

tests/mclag_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ def test_mclag_session_timeout(self):
360360

361361

362362
def test_mclag_add_mclag_member_to_nonexisting_domain(self):
363+
# This is required for the add command to fail; otherwise, it incorrectly passes
364+
mclag.ADHOC_VALIDATION = True
363365
runner = CliRunner()
364366
db = Db()
365367
obj = {'db':db.cfgdb}

tests/muxcable_test.py

Lines changed: 157 additions & 721 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)