1919# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
2020#
2121
22+ import logging
2223import unittest
2324from unittest import mock
2425
25- import pcie_test
26+ from pcie_test import PCIeTester , _run_command , init_logger , main
2627
2728
2829class TestPCIeTester (unittest .TestCase ):
@@ -32,7 +33,7 @@ class TestPCIeTester(unittest.TestCase):
3233
3334 def setUp (self ):
3435 """Set up test fixtures"""
35- self .tester = pcie_test . PCIeTester ()
36+ self .tester = PCIeTester ()
3637
3738 @mock .patch ("pcie_test.subprocess.run" )
3839 def test_run_command_success (self , mock_run ):
@@ -41,7 +42,7 @@ def test_run_command_success(self, mock_run):
4142 mock_result .stdout = "Test output"
4243 mock_run .return_value = mock_result
4344
44- result = pcie_test . _run_command (["lspci" ])
45+ result = _run_command (["lspci" ])
4546 self .assertEqual (result , "Test output" )
4647 self .assertEqual (mock_run .call_count , 1 )
4748
@@ -51,7 +52,7 @@ def test_run_command_file_not_found(self, mock_run):
5152 mock_run .side_effect = FileNotFoundError ()
5253
5354 with self .assertRaises (RuntimeError ) as cm :
54- pcie_test . _run_command (["lspci" ])
55+ _run_command (["lspci" ])
5556 self .assertIn ("command not found" , str (cm .exception ))
5657
5758 @mock .patch ("pcie_test.subprocess.run" )
@@ -64,7 +65,7 @@ def test_run_command_called_process_error(self, mock_run):
6465 )
6566
6667 with self .assertRaises (RuntimeError ) as cm :
67- pcie_test . _run_command (["lspci" ])
68+ _run_command (["lspci" ])
6869 self .assertIn ("Error executing command" , str (cm .exception ))
6970
7071 def test_parse_link_info_valid (self ):
@@ -253,30 +254,30 @@ class TestInitLogger(unittest.TestCase):
253254
254255 def test_init_logger (self ):
255256 """Test that init_logger creates a logger"""
256- logger = pcie_test . init_logger ()
257+ logger = init_logger ()
257258 self .assertIsNotNone (logger )
258- self .assertEqual (logger .level , pcie_test . logging .INFO )
259+ self .assertEqual (logger .level , logging .INFO )
259260
260261
261262class TestMain (unittest .TestCase ):
262263 """Test the main function"""
263264
264265 @mock .patch ("pcie_test.sys.exit" )
265266 @mock .patch ("pcie_test.init_logger" )
266- @mock .patch .object (pcie_test . PCIeTester , "list_resources" )
267+ @mock .patch .object (PCIeTester , "list_resources" )
267268 @mock .patch ("pcie_test.sys.argv" , ["pcie_test.py" , "resource" ])
268269 def test_main_resource_command (self , mock_list , mock_logger , mock_exit ):
269270 """Test main with resource command"""
270271 mock_list .return_value = 0
271272 mock_logger .return_value = mock .Mock ()
272- pcie_test . main ()
273+ main ()
273274 self .assertEqual (mock_list .call_count , 1 )
274275 mock_exit .assert_called_with (0 )
275276 self .assertEqual (mock_exit .call_count , 1 )
276277
277278 @mock .patch ("pcie_test.sys.exit" )
278279 @mock .patch ("pcie_test.init_logger" )
279- @mock .patch .object (pcie_test . PCIeTester , "check_link_state" )
280+ @mock .patch .object (PCIeTester , "check_link_state" )
280281 @mock .patch (
281282 "pcie_test.sys.argv" , ["pcie_test.py" , "check_speed" , "-s" , "00:00.0" ]
282283 )
@@ -286,15 +287,15 @@ def test_main_check_speed_command(
286287 """Test main with check_speed command"""
287288 mock_check .return_value = 0
288289 mock_logger .return_value = mock .Mock ()
289- pcie_test . main ()
290+ main ()
290291 mock_check .assert_called_with ("00:00.0" , force = False )
291292 self .assertEqual (mock_check .call_count , 1 )
292293 mock_exit .assert_called_with (0 )
293294 self .assertEqual (mock_exit .call_count , 1 )
294295
295296 @mock .patch ("pcie_test.sys.exit" )
296297 @mock .patch ("pcie_test.init_logger" )
297- @mock .patch .object (pcie_test . PCIeTester , "check_link_state" )
298+ @mock .patch .object (PCIeTester , "check_link_state" )
298299 @mock .patch (
299300 "pcie_test.sys.argv" ,
300301 ["pcie_test.py" , "check_speed" , "-s" , "00:00.0" , "--force" ],
@@ -305,31 +306,31 @@ def test_main_check_speed_with_force(
305306 """Test main with check_speed command and --force flag"""
306307 mock_check .return_value = 1
307308 mock_logger .return_value = mock .Mock ()
308- pcie_test . main ()
309+ main ()
309310 mock_check .assert_called_with ("00:00.0" , force = True )
310311 self .assertEqual (mock_check .call_count , 1 )
311312 mock_exit .assert_called_with (1 )
312313 self .assertEqual (mock_exit .call_count , 1 )
313314
314315 @mock .patch ("pcie_test.sys.exit" )
315316 @mock .patch ("pcie_test.init_logger" )
316- @mock .patch .object (pcie_test . PCIeTester , "check_aspm_state" )
317+ @mock .patch .object (PCIeTester , "check_aspm_state" )
317318 @mock .patch (
318319 "pcie_test.sys.argv" , ["pcie_test.py" , "check_aspm" , "-s" , "00:00.0" ]
319320 )
320321 def test_main_check_aspm_command (self , mock_check , mock_logger , mock_exit ):
321322 """Test main with check_aspm command"""
322323 mock_check .return_value = 0
323324 mock_logger .return_value = mock .Mock ()
324- pcie_test . main ()
325+ main ()
325326 mock_check .assert_called_with ("00:00.0" , force = False )
326327 self .assertEqual (mock_check .call_count , 1 )
327328 mock_exit .assert_called_with (0 )
328329 self .assertEqual (mock_exit .call_count , 1 )
329330
330331 @mock .patch ("pcie_test.sys.exit" )
331332 @mock .patch ("pcie_test.init_logger" )
332- @mock .patch .object (pcie_test . PCIeTester , "check_aspm_state" )
333+ @mock .patch .object (PCIeTester , "check_aspm_state" )
333334 @mock .patch (
334335 "pcie_test.sys.argv" ,
335336 ["pcie_test.py" , "check_aspm" , "-s" , "00:00.0" , "--force" ],
@@ -340,25 +341,23 @@ def test_main_check_aspm_with_force(
340341 """Test main with check_aspm command and --force flag"""
341342 mock_check .return_value = 1
342343 mock_logger .return_value = mock .Mock ()
343- pcie_test . main ()
344+ main ()
344345 mock_check .assert_called_with ("00:00.0" , force = True )
345346 self .assertEqual (mock_check .call_count , 1 )
346347 mock_exit .assert_called_with (1 )
347348 self .assertEqual (mock_exit .call_count , 1 )
348349
349350 @mock .patch ("pcie_test.sys.exit" )
350351 @mock .patch ("pcie_test.init_logger" )
351- @mock .patch .object (pcie_test . PCIeTester , "list_resources" )
352+ @mock .patch .object (PCIeTester , "list_resources" )
352353 @mock .patch ("pcie_test.sys.argv" , ["pcie_test.py" , "--debug" , "resource" ])
353354 def test_main_with_debug_flag (self , mock_list , mock_logger , mock_exit ):
354355 """Test main with --debug flag"""
355356 mock_list .return_value = 0
356357 mock_logger_instance = mock .Mock ()
357358 mock_logger .return_value = mock_logger_instance
358- pcie_test .main ()
359- mock_logger_instance .setLevel .assert_called_with (
360- pcie_test .logging .DEBUG
361- )
359+ main ()
360+ mock_logger_instance .setLevel .assert_called_with (logging .DEBUG )
362361 self .assertEqual (mock_logger_instance .setLevel .call_count , 1 )
363362
364363
0 commit comments