From afa8d649c2e001dd5af8228e10bb1d9c3b1291b5 Mon Sep 17 00:00:00 2001 From: stanley31huang Date: Tue, 8 Oct 2024 15:57:47 +0800 Subject: [PATCH] add wwan reconnection test add wwan reconnection test --- providers/base/bin/wwan_tests.py | 39 +++++++----- providers/base/tests/test_wwan_tests.py | 81 +++++++++++++++++++++++++ providers/base/units/wwan/jobs.pxu | 11 +++- providers/base/units/wwan/test-plan.pxu | 1 + 4 files changed, 116 insertions(+), 16 deletions(-) diff --git a/providers/base/bin/wwan_tests.py b/providers/base/bin/wwan_tests.py index 4e33f09e00..92536ae6db 100755 --- a/providers/base/bin/wwan_tests.py +++ b/providers/base/bin/wwan_tests.py @@ -334,7 +334,7 @@ def _ping_test(if_name): class ThreeGppConnection: - def invoked(self): + def register_argument(self): parser = argparse.ArgumentParser() parser.add_argument( "hw_id", type=str, help="The hardware ID of the modem" @@ -353,22 +353,25 @@ def invoked(self): default=30, help="delay before ping test", ) - args = parser.parse_args(sys.argv[2:]) + return parser.parse_args(sys.argv[2:]) - mm = MMCLI() - mm_id = mm.equipment_id_to_mm_id(args.hw_id) - wwan_control_if = mm.get_primary_port(mm_id) + def invoked(self): + + args = self.register_argument() ret_code = 1 try: - _create_3gpp_connection(wwan_control_if, args.apn) - _wwan_radio_on() - time.sleep(args.wwan_setup_time) - ret_code = _ping_test(args.wwan_net_if) + with WWANTestCtx(args.hw_id, True, True) as ctx: + wwan_control_if = ctx.mm_obj.get_primary_port( + str(ctx.modem_idx) + ) + _create_3gpp_connection(wwan_control_if, args.apn) + time.sleep(args.wwan_setup_time) + ret_code = _ping_test(args.wwan_net_if) except subprocess.SubprocessError: pass _destroy_3gpp_connection() - _wwan_radio_off() + sys.exit(ret_code) @@ -453,25 +456,33 @@ def invoked(self): class Resources: - def invoked(self): + def register_arguments(self): parser = argparse.ArgumentParser() parser.add_argument( "--use-cli", action="store_true", help="Use mmcli for all calls rather than dbus", ) - args = parser.parse_args(sys.argv[2:]) + return parser.parse_args(sys.argv[2:]) + + def invoked(self): + args = self.register_arguments() if args.use_cli: mm = MMCLI() else: mm = MMDbus() + for m in mm.get_modem_ids(): print("mm_id: {}".format(m)) print("hw_id: {}".format(mm.get_equipment_id(m))) print("manufacturer: {}".format(mm.get_manufacturer(m))) print("model: {}".format(mm.get_model_name(m))) - print("firmware_revision: {}".format(mm.get_firmware_revision(m))) - print("hardware_revision: {}".format(mm.get_hardware_revision(m))) + print( + "firmware_revision: {}".format(mm.get_firmware_revision(m)) + ) + print( + "hardware_revision: {}".format(mm.get_hardware_revision(m)) + ) print() diff --git a/providers/base/tests/test_wwan_tests.py b/providers/base/tests/test_wwan_tests.py index 06ae054c2c..ac237ac3e8 100644 --- a/providers/base/tests/test_wwan_tests.py +++ b/providers/base/tests/test_wwan_tests.py @@ -264,3 +264,84 @@ def test_invoked_call_error(self, mock_arg, mock_mmctx, mock_run): stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) + + +class TestThreeGppConnectionTest(unittest.TestCase): + + def test_register_argument(self): + + sys.argv = [ + "wwan_tests.py", + "3gpp-connection", + "hw_id", + "wwan_net_if", + "apn", + "30", + ] + obj_3gppscan = wwan_tests.ThreeGppConnection() + ret_args = obj_3gppscan.register_argument() + self.assertEqual(ret_args.hw_id, "hw_id") + self.assertEqual(ret_args.wwan_net_if, "wwan_net_if") + self.assertEqual(ret_args.apn, "apn") + self.assertEqual(ret_args.wwan_setup_time, 30) + + @patch("wwan_tests._ping_test") + @patch("wwan_tests._destroy_3gpp_connection") + @patch("wwan_tests._create_3gpp_connection") + @patch("wwan_tests.WWANTestCtx") + @patch("wwan_tests.ThreeGppConnection.register_argument") + def test_invoked_successfully( + self, mock_arg, mock_mmctx, mock_create_conn, mock_rm_conn, mock_ping + ): + mock_arg.return_value = argparse.Namespace( + hw_id="2", wwan_net_if="wwan0", apn="internet", wwan_setup_time=0.1 + ) + mock_get_primary_port = Mock() + mmcli_instance = Mock() + mmcli_instance.modem_idx = "0" + mmcli_instance.mm_obj = Mock(get_primary_port=mock_get_primary_port) + mock_mmctx.return_value.__enter__.return_value = mmcli_instance + mock_ping.return_value = 0 + + with redirect_stdout(StringIO()): + with self.assertRaises(SystemExit) as context: + obj_3gppscan = wwan_tests.ThreeGppConnection() + obj_3gppscan.invoked() + + mock_mmctx.assert_called_with("2", True, True) + self.assertEqual(mock_arg.call_count, 1) + self.assertEqual(mock_get_primary_port.call_count, 1) + self.assertEqual(mock_ping.call_count, 1) + self.assertEqual(mock_create_conn.call_count, 1) + self.assertEqual(mock_rm_conn.call_count, 1) + self.assertEqual(context.exception.code, 0) + + @patch("wwan_tests._ping_test") + @patch("wwan_tests._destroy_3gpp_connection") + @patch("wwan_tests._create_3gpp_connection") + @patch("wwan_tests.WWANTestCtx") + @patch("wwan_tests.ThreeGppConnection.register_argument") + def test_invoked_failed_exit_code( + self, mock_arg, mock_mmctx, mock_create_conn, mock_rm_conn, mock_ping + ): + mock_arg.return_value = argparse.Namespace( + hw_id="2", wwan_net_if="wwan0", apn="internet", wwan_setup_time=0.1 + ) + mock_get_primary_port = Mock() + mmcli_instance = Mock() + mmcli_instance.modem_idx = "0" + mmcli_instance.mm_obj = Mock(get_primary_port=mock_get_primary_port) + mock_mmctx.return_value.__enter__.return_value = mmcli_instance + mock_ping.return_value = 1 + + with redirect_stdout(StringIO()): + with self.assertRaises(SystemExit) as context: + obj_3gppscan = wwan_tests.ThreeGppConnection() + obj_3gppscan.invoked() + + mock_mmctx.assert_called_with("2", True, True) + self.assertEqual(mock_arg.call_count, 1) + self.assertEqual(mock_ping.call_count, 1) + self.assertEqual(mock_create_conn.call_count, 1) + self.assertEqual(mock_rm_conn.call_count, 1) + self.assertEqual(context.exception.code, 1) diff --git a/providers/base/units/wwan/jobs.pxu b/providers/base/units/wwan/jobs.pxu index 7f799cba13..6cb68b49ef 100644 --- a/providers/base/units/wwan/jobs.pxu +++ b/providers/base/units/wwan/jobs.pxu @@ -32,9 +32,10 @@ template-unit: job id: wwan/gsm-connection-{manufacturer}-{model}-{hw_id}-auto template-id: wwan/gsm-connection-manufacturer-model-hw_id-auto _summary: Verify a GSM broadband modem can create a data connection +_template-summary: Verify a GSM broadband modem can create a data connection multiple times _purpose: - Any modems discovered by the resource job that list GSM support - will be tested to ensure a data connection can be made. + Any modems discovered by the resource job that list GSM support + will be tested to ensure a data connection can be made. plugin: shell command: BEGIN_CONNECTION_TEST_TS=$(date '+%Y-%m-%d %H:%M:%S') @@ -56,6 +57,12 @@ requires: manifest.has_wwan_module == 'True' manifest.has_sim_card == 'True' snap.name == 'modem-manager' or package.name == 'modemmanager' +_siblings: + [{{ + "id": "wwan/gsm-reconnection-{manufacturer}-{model}-{hw_id}-auto", + "_summary": "Verify a GSM broadband modem can recreate a data connection", + "depends": "wwan/gsm-connection-{manufacturer}-{model}-{hw_id}-auto" + }}] unit: template template-resource: wwan_resource diff --git a/providers/base/units/wwan/test-plan.pxu b/providers/base/units/wwan/test-plan.pxu index 0e2959c076..bc1780174b 100644 --- a/providers/base/units/wwan/test-plan.pxu +++ b/providers/base/units/wwan/test-plan.pxu @@ -18,6 +18,7 @@ include: wwan/detect certification-status=blocker wwan/3gpp-scan-manufacturer-model-hw_id-auto wwan/gsm-connection-.*-auto certification-status=blocker + wwan/gsm-reconnection-.*-auto certification-status=blocker wwan/check-sim-present-.*-auto bootstrap_include: wwan_resource