diff --git a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/check_gpio.py b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/check_gpio.py index 4d153c221..7641d323f 100755 --- a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/check_gpio.py +++ b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/check_gpio.py @@ -2,6 +2,7 @@ import argparse import os from contextlib import contextmanager + # from checkbox_support.snap_utils.snapd import Snapd from checkbox_support.snap_utils.snapd import Snapd from checkbox_support.snap_utils.system import get_gadget_snap @@ -107,25 +108,16 @@ def check_gpio_list(gpio_list, config): @contextmanager def interface_test(gpio_slot, gadget_name, timeout=60): - snap = os.environ['SNAP_NAME'] - timeout = int(os.environ.get('SNAPD_TASK_TIMEOUT', timeout)) + snap = os.environ["SNAP_NAME"] + timeout = int(os.environ.get("SNAPD_TASK_TIMEOUT", timeout)) try: - connect_interface(gadget_name, - gpio_slot, - snap, - timeout) + connect_interface(gadget_name, gpio_slot, snap, timeout) yield finally: - disconnect_interface(gadget_name, - gpio_slot, - snap, - timeout) + disconnect_interface(gadget_name, gpio_slot, snap, timeout) -def connect_interface(gadget_name, - gpio_slot, - snap, - timeout): +def connect_interface(gadget_name, gpio_slot, snap, timeout): """ Connect GPIO plugs of checkbox to GPIO slots of gadget snap. @@ -141,21 +133,14 @@ def connect_interface(gadget_name, print("Attempting connect GPIO to {}:{}".format(gadget_name, gpio_slot)) try: Snapd(task_timeout=timeout).connect( - gadget_name, - gpio_slot, - snap, - "gpio" - ) + gadget_name, gpio_slot, snap, "gpio") print("Success") except requests.HTTPError: print("Failed to connect {}".format(gpio_slot)) raise SystemExit(1) -def disconnect_interface(gadget_name, - gpio_slot, - snap, - timeout): +def disconnect_interface(gadget_name, gpio_slot, snap, timeout): """ Connect GPIO plugs of checkbox to GPIO slots of gadget snap. @@ -172,11 +157,7 @@ def disconnect_interface(gadget_name, format(gadget_name, gpio_slot)) try: Snapd(task_timeout=timeout).disconnect( - gadget_name, - gpio_slot, - snap, - "gpio" - ) + gadget_name, gpio_slot, snap, "gpio") print("Success") except requests.HTTPError: print("Failed to disconnect {}".format(gpio_slot)) @@ -204,9 +185,9 @@ def check_node(num): def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers( - dest='action', + dest="action", help="Action in check-gpio, check-node and dump", - ) + ) check_gpio_subparser = subparsers.add_parser("check-gpio") check_gpio_subparser.add_argument( "-c", @@ -214,7 +195,7 @@ def main(): required=True, help="Checkbox config include expected GPIO\ e.g. 499:500:501:502", - ) + ) check_node_subparser = subparsers.add_parser("check-node") check_node_subparser.add_argument( "-n", @@ -222,18 +203,15 @@ def main(): type=int, required=True, help="GPIO number to check if node exported", - ) + ) check_node_subparser.add_argument( "-s", "--slot", type=str, required=True, help="GPIO slot to connect.", - ) - subparsers.add_parser( - "dump", - help="Dump GPIO slots from gadget" - ) + ) + subparsers.add_parser("dump", help="Dump GPIO slots from gadget") args = parser.parse_args() snapd = Snapd() gadget_name = get_gadget_snap() @@ -242,11 +220,8 @@ def main(): check_gpio_list(gpio_slots, args.config) if args.action == "dump": for x in gpio_slots: - print( - "slot: {}\ngpio_number: {}\n".format( - x, gpio_slots[x]["number"] - ) - ) + print("slot: {}\ngpio_number: {}\n". + format(x, gpio_slots[x]["number"])) if args.action == "check-node": with interface_test(args.slot, gadget_name): check_node(args.num) diff --git a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/tests/test_check_gpio.py b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/tests/test_check_gpio.py index 95fd5c96c..c45916484 100755 --- a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/tests/test_check_gpio.py +++ b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/tests/test_check_gpio.py @@ -14,13 +14,14 @@ def test_parse_single_ports(self): def test_parse_port_ranges(self): # Test parsing port ranges - self.assertEqual(check_gpio.parse_config("5:8,10:12"), - [5, 6, 7, 8, 10, 11, 12]) + self.assertEqual( + check_gpio.parse_config("5:8,10:12"), [5, 6, 7, 8, 10, 11, 12]) def test_parse_mixed(self): # Test parsing mixed single ports and port ranges - self.assertEqual(check_gpio.parse_config("1,3:5,7,9:10"), - [1, 3, 4, 5, 7, 9, 10]) + self.assertEqual( + check_gpio.parse_config("1,3:5,7,9:10"), [1, 3, 4, 5, 7, 9, 10] + ) def test_parse_empty_string(self): # Test parsing an empty string @@ -38,54 +39,65 @@ def test_parse_invalid_input(self): def test_list_gpio_slots_exist(self): snapd_mock = MagicMock() - gadget_name = 'test_gadget' + gadget_name = "test_gadget" snapd_mock.interfaces.return_value = { "slots": [ - {"snap": gadget_name, - "slot": "gpio-0", - "interface": "gpio", - "attrs": {"number": "10"}}, - {"snap": "other-snap", - "slot": "other-slot", - "interface": "other-interface", - "attrs": {}}, + { + "snap": gadget_name, + "slot": "gpio-0", + "interface": "gpio", + "attrs": {"number": "10"}, + }, + { + "snap": "other-snap", + "slot": "other-slot", + "interface": "other-interface", + "attrs": {}, + }, ] } expected_result = {"gpio-0": {"number": "10"}} - self.assertEqual(check_gpio.list_gpio_slots(snapd_mock, gadget_name), - expected_result) + self.assertEqual( + check_gpio.list_gpio_slots( + snapd_mock, gadget_name), expected_result + ) def test_list_gpio_slots_not_exist(self): snapd_mock = MagicMock() - gadget_name = 'test_gadget' + gadget_name = "test_gadget" snapd_mock.interfaces.return_value = { "slots": [ - {"snap": "other-snap", - "slot": "other-slot", - "interface": "other-interface", - "attrs": {}}, + { + "snap": "other-snap", + "slot": "other-slot", + "interface": "other-interface", + "attrs": {}, + }, ] } expected_result = {} - self.assertEqual(check_gpio.list_gpio_slots(snapd_mock, gadget_name), - expected_result) + self.assertEqual( + check_gpio.list_gpio_slots(snapd_mock, gadget_name), + expected_result + ) - @patch('builtins.print') + @patch("builtins.print") def test_check_gpio_list_all_defined(self, mock_print): gpio_list = { 1: {"number": 499}, 2: {"number": 500}, 3: {"number": 501}, - 4: {"number": 502} + 4: {"number": 502}, } config = "499,500:502" check_gpio.check_gpio_list(gpio_list, config) # Assert that "All expected GPIO slots have been defined # in gadget snap." is printed mock_print.assert_called_with( - "All expected GPIO slots have been defined in gadget snap.") + "All expected GPIO slots have been defined in gadget snap." + ) - @patch('builtins.print') + @patch("builtins.print") def test_check_gpio_list_missing(self, mock_print): gpio_list = { 1: {"number": 499}, @@ -100,33 +112,28 @@ def test_check_gpio_list_missing(self, mock_print): # Assert that the proper error message is printed for the # missing GPIO slot mock_print.assert_called_with( - "Error: Slot of GPIO 502 is not defined in gadget snap") + "Error: Slot of GPIO 502 is not defined in gadget snap" + ) # Assert that SystemExit is raised with exit code 1 self.assertEqual(context.exception.code, 1) - @patch('builtins.print') - @patch('check_gpio.Snapd') + @patch("builtins.print") + @patch("check_gpio.Snapd") def test_connect_interface_success(self, mock_snapd, mock_print): mock_snapd.return_value.connect.side_effect = None gpio_slot = "gpio-499" gadget_name = "gadget_snap" snap = "checkbox_snap" timeout = 30 - check_gpio.connect_interface(gadget_name, - gpio_slot, - snap, - timeout) - - expected_calls = [call(gadget_name, - gpio_slot, - snap, - 'gpio')] + check_gpio.connect_interface(gadget_name, gpio_slot, snap, timeout) + + expected_calls = [call(gadget_name, gpio_slot, snap, "gpio")] mock_snapd.return_value.connect.assert_has_calls(expected_calls) mock_print.assert_called_with("Success") - @patch('builtins.print') - @patch('check_gpio.Snapd') + @patch("builtins.print") + @patch("check_gpio.Snapd") def test_connect_interface_fail(self, mock_snapd, mock_print): mock_snapd.return_value.connect.side_effect = requests.HTTPError gpio_slot = "gpio-499" @@ -134,41 +141,29 @@ def test_connect_interface_fail(self, mock_snapd, mock_print): snap = "checkbox_snap" timeout = 30 with self.assertRaises(SystemExit) as err: - check_gpio.connect_interface(gadget_name, - gpio_slot, - snap, - timeout) - - expected_calls = [call(gadget_name, - gpio_slot, - snap, - 'gpio')] + check_gpio.connect_interface(gadget_name, gpio_slot, snap, timeout) + + expected_calls = [call(gadget_name, gpio_slot, snap, "gpio")] mock_snapd.return_value.connect.assert_has_calls(expected_calls) mock_print.assert_called_with("Failed to connect gpio-499") self.assertEqual(err.exception.code, 1) - @patch('builtins.print') - @patch('check_gpio.Snapd') + @patch("builtins.print") + @patch("check_gpio.Snapd") def test_disconnect_interface_success(self, mock_snapd, mock_print): mock_snapd.return_value.disconnect.side_effect = None gpio_slot = "gpio-499" gadget_name = "gadget_snap" snap = "checkbox_snap" timeout = 30 - check_gpio.disconnect_interface(gadget_name, - gpio_slot, - snap, - timeout) - - expected_calls = [call(gadget_name, - gpio_slot, - snap, - 'gpio')] + check_gpio.disconnect_interface(gadget_name, gpio_slot, snap, timeout) + + expected_calls = [call(gadget_name, gpio_slot, snap, "gpio")] mock_snapd.return_value.disconnect.assert_has_calls(expected_calls) mock_print.assert_called_with("Success") - @patch('builtins.print') - @patch('check_gpio.Snapd') + @patch("builtins.print") + @patch("check_gpio.Snapd") def test_disconnect_interface_fail(self, mock_snapd, mock_print): mock_snapd.return_value.disconnect.side_effect = requests.HTTPError gpio_slot = "gpio-499" @@ -176,57 +171,48 @@ def test_disconnect_interface_fail(self, mock_snapd, mock_print): snap = "checkbox_snap" timeout = 30 with self.assertRaises(SystemExit) as err: - check_gpio.disconnect_interface(gadget_name, - gpio_slot, - snap, - timeout) - - expected_calls = [call(gadget_name, - gpio_slot, - snap, - 'gpio')] + check_gpio.disconnect_interface( + gadget_name, gpio_slot, snap, timeout) + + expected_calls = [call(gadget_name, gpio_slot, snap, "gpio")] mock_snapd.return_value.disconnect.assert_has_calls(expected_calls) mock_print.assert_called_with("Failed to disconnect gpio-499") self.assertEqual(err.exception.code, 1) - @patch('builtins.print') + @patch("builtins.print") def test_check_node_exists(self, mock_print): # Mocking os.path.exists to return True - with patch('os.path.exists', return_value=True): + with patch("os.path.exists", return_value=True): check_gpio.check_node(499) # Assert that "GPIO node of 499 exist!" is printed mock_print.assert_called_with("GPIO node of 499 exist!") def test_check_node_not_exist(self): # Mocking os.path.exists to return False - with patch('os.path.exists', return_value=False): + with patch("os.path.exists", return_value=False): with self.assertRaises(SystemExit) as context: check_gpio.check_node(499) # Assert that SystemExit is raised with the correct message self.assertEqual(context.exception.args[0], "GPIO node of 499 not exist!") - @patch.dict(os.environ, {'SNAP_NAME': 'checkbox_snap'}) - @patch.dict(os.environ, {'SNAPD_TASK_TIMEOUT': '30'}) - @patch('check_gpio.connect_interface') - @patch('check_gpio.disconnect_interface') - def test_interface_test(self, - mock_disconnect, - mock_connect): + @patch.dict(os.environ, {"SNAP_NAME": "checkbox_snap"}) + @patch.dict(os.environ, {"SNAPD_TASK_TIMEOUT": "30"}) + @patch("check_gpio.connect_interface") + @patch("check_gpio.disconnect_interface") + def test_interface_test(self, mock_disconnect, mock_connect): gadget_name = "gadget" gpio_slot = "gpio-499" mock_connect.side_effect = None mock_disconnect.side_effect = None with check_gpio.interface_test(gpio_slot, gadget_name): - mock_connect.assert_called_once_with(gadget_name, - gpio_slot, - 'checkbox_snap', - 30) - mock_disconnect.assert_called_once_with(gadget_name, - gpio_slot, - 'checkbox_snap', - 30) + mock_connect.assert_called_once_with( + gadget_name, gpio_slot, "checkbox_snap", 30 + ) + mock_disconnect.assert_called_once_with( + gadget_name, gpio_slot, "checkbox_snap", 30 + ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()