Skip to content

Commit

Permalink
Modify logic and add more unittest to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rickwu666666 committed Aug 8, 2024
1 parent eae7e80 commit e4fd5a8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
37 changes: 20 additions & 17 deletions providers/resource/bin/wifi_interface_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ def run_command(command: List[str]) -> str:
)
return result.stdout.decode("utf-8")
except Exception as e:
raise SystemError("An unexpected error occurred: {}", e)
raise SystemError("An unexpected error occurred: {}".format(e))


def get_interfaces() -> List[Tuple[str, str]]:
output = run_command(["iw", "dev"])
interface_pattern = r"phy#(\d+).*?Interface (\S+)"
device_pattern = r"phy#(\d+).*?Interface (\S+)"
if output:
matches = re.findall(interface_pattern, output, re.DOTALL)
return [(phy, interface) for phy, interface in matches]
return []
matches = re.findall(device_pattern, output, re.DOTALL)
return [
(device_id, interface_name)
for device_id, interface_name in matches
]
raise SystemExit(0)


def get_wiphy_info() -> List[Tuple[str, List[str]]]:
# We can not use command "iw phy0 info" to get the infomation of sepcific
# interface.
# Because of "phy0" in command could be other pattern like "mwiphy0".
# And such pattern as "mwiphy0" will only show in the output of command
# "iw phy".
output = run_command(["iw", "phy"])
if output:
contents = re.split(r"Wiphy\s+", output)
Expand All @@ -44,26 +52,21 @@ def get_wiphy_info() -> List[Tuple[str, List[str]]]:
modes = re.findall(r"\*\s*([\w/-]+)", match_modes.group(1))

wiphy_info.append((match_index, modes))
return wiphy_info
return wiphy_info


def print_supported_modes(
interfaces: List[Tuple[str, str]], wiphy_info: List[Tuple[str, List[str]]]
) -> str:
def print_supported_modes() -> str:
interfaces = get_interfaces()
wiphy_info = get_wiphy_info()
for wiphy_index, modes in wiphy_info:
for interface_index, interface in interfaces:
if wiphy_index == interface_index:
for device_index, interface in interfaces:
if wiphy_index == device_index:
for mode in modes:
print("{}_{}: supported".format(interface, mode))


def main():
interfaces = get_interfaces()
if not interfaces:
print("")
return
wiphy_info = get_wiphy_info()
print_supported_modes(interfaces, wiphy_info)
print_supported_modes()

Check warning on line 69 in providers/resource/bin/wifi_interface_mode.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/wifi_interface_mode.py#L69

Added line #L69 was not covered by tests


if __name__ == "__main__":
Expand Down
48 changes: 29 additions & 19 deletions providers/resource/tests/test_wifi_interface_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get_interfaces,
get_wiphy_info,
print_supported_modes,
main,
)


Expand All @@ -22,9 +23,14 @@ def test_run_command_success(self, mock_run):
@patch("subprocess.run")
def test_run_command_failure(self, mock_run):
mock_run.side_effect = Exception("Command failed")
with self.assertRaises(SystemError):
with self.assertRaises(SystemError) as context:
run_command(["echo", "hello"])

self.assertIn(
"An unexpected error occurred: Command failed",
str(context.exception),
)

@patch("wifi_interface_mode.run_command")
def test_get_interfaces(self, mock_run_command):
mock_run_command.return_value = """
Expand All @@ -37,6 +43,15 @@ def test_get_interfaces(self, mock_run_command):
result = get_interfaces()
self.assertEqual(result, expected)

@patch("wifi_interface_mode.run_command")
def test_get_interfaces_no_wifi_interface(self, mock_run_command):
mock_run_command.return_value = ""

with self.assertRaises(SystemExit) as cm:
get_interfaces()

self.assertEqual(cm.exception.code, 0)

@patch("wifi_interface_mode.run_command")
def test_get_wiphy_info_with_one_phy(self, mock_run_command):
mock_run_command.return_value = """Wiphy phy0
Expand Down Expand Up @@ -97,29 +112,24 @@ def test_get_wiphy_info_with_two_phy(self, mock_run_command):
result = get_wiphy_info()
self.assertEqual(result, expected)

@patch("wifi_interface_mode.run_command")
def test_print_supported_modes(self, mock_run_command):
interfaces = [("0", "wlan0"), ("1", "wlan1")]
wiphy_ids = [
(
"0",
["managed", "AP/VLAN", "monitor"],
),
(
"1",
["managed", "AP/VLAN", "P2P-client"],
),
@patch("wifi_interface_mode.get_interfaces")
@patch("wifi_interface_mode.get_wiphy_info")
def test_print_supported_modes(
self, mock_get_wiphy_info, mock_get_interfaces
):
# Mock the return values
mock_get_interfaces.return_value = [("0", "wlan0"), ("1", "wlan1")]
mock_get_wiphy_info.return_value = [
("0", ["IBSS", "AP/VLAN"]),
("1", ["AP", "P2P-client"]),
]

# Mock print to capture output
with patch("builtins.print") as mock_print:
print_supported_modes(interfaces, wiphy_ids)
print_supported_modes()
expected_output = [
"wlan0_managed: supported",
"wlan0_IBSS: supported",
"wlan0_AP/VLAN: supported",
"wlan0_monitor: supported",
"wlan1_managed: supported",
"wlan1_AP/VLAN: supported",
"wlan1_AP: supported",
"wlan1_P2P-client: supported",
]
for output in expected_output:
Expand Down

0 comments on commit e4fd5a8

Please sign in to comment.