Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
seankingyang committed Apr 12, 2024
1 parent c53badf commit 40a5997
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 105 deletions.
34 changes: 21 additions & 13 deletions providers/resource/bin/WIFI_phy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def parse_iw_dev_output():
"""
Parses the output of 'iw dev' to extract PHY and interface mappings.
Parses the output of "iw dev" to extract PHY and interface mappings.
"""
cmd = "iw dev"
output = check_output(cmd, shell=True, universal_newlines=True)
Expand All @@ -17,7 +17,7 @@ def parse_iw_dev_output():

def parse_phy_info_output(output):
"""
Parses the output of 'iw phy info' to extract bands and STA support.
Parses the output of "iw phy info" to extract bands and STA support.
"""
bands_data = output.split("Supported commands:")[0]
bands = {}
Expand All @@ -32,8 +32,11 @@ def parse_phy_info_output(output):
freqs_raw = band_content.split("Frequencies:")[1]
freq_ptn = r"(\d+ MHz.*)"
freq_compile = re.compile(freq_ptn)
freqs = [freq for freq in freq_compile.findall(freqs_raw)
if "disabled" not in freq]
freqs = [
freq
for freq in freq_compile.findall(freqs_raw)
if "disabled" not in freq
]
bands[band_num] = freqs
return bands

Expand All @@ -46,9 +49,10 @@ def check_sta_support(phy_info_output):
# Supported STAs and their keywords
supported_stas = {"BE": "EHT", "AX": "HE RX MCS", "AC": "VHT RX MCS"}

sta_supported = {sta: "supported" if sta_keyword in phy_info_output
else "unsupported"
for sta, sta_keyword in supported_stas.items()}
sta_supported = {
sta: "supported" if sta_keyword in phy_info_output else "unsupported"
for sta, sta_keyword in supported_stas.items()
}
return sta_supported


Expand All @@ -60,8 +64,11 @@ def check_freq_support(bands):
supported_freqs = {"2.4GHz": "1", "5GHz": "2", "6GHz": "4"}

freq_supported = {
freq: "supported"
if band in bands and len(bands[band]) > 0 else "unsupported"
freq: (
"supported"
if band in bands and len(bands[band]) > 0
else "unsupported"
)
for freq, band in supported_freqs.items()
}
return freq_supported
Expand All @@ -74,22 +81,23 @@ def create_phy_interface_mapping(phy_interface):
phy_interface_mapping = {}
for phy, interface in phy_interface:
cmd = "iw {} info".format(phy)
phy_info_output = check_output(cmd, shell=True,
universal_newlines=True)
phy_info_output = check_output(
cmd, shell=True, universal_newlines=True
)
bands = parse_phy_info_output(phy_info_output)
freq_supported = check_freq_support(bands)
sta_supported = check_sta_support(phy_info_output)
phy_interface_mapping[interface] = {
"PHY": phy,
"Bands": bands,
"FREQ_Supported": freq_supported,
"STA_Supported": sta_supported
"STA_Supported": sta_supported,
}
return phy_interface_mapping


def main():
# Read and parse 'iw dev' output
# Read and parse "iw dev" output
phy_interface = parse_iw_dev_output()

# Create mapping with interface, PHY, bands, and supported STAs
Expand Down
193 changes: 101 additions & 92 deletions providers/resource/tests/test_WIFI_phy.py
Original file line number Diff line number Diff line change
@@ -1,126 +1,135 @@
import os
from unittest.mock import patch
import unittest
from WIFI_phy import (parse_iw_dev_output, parse_phy_info_output,
check_sta_support, check_freq_support,
create_phy_interface_mapping, main)
from WIFI_phy import (
parse_iw_dev_output,
parse_phy_info_output,
check_sta_support,
check_freq_support,
create_phy_interface_mapping,
main,
)

class WIFIphyData():

class WIFIphyData:
@staticmethod
def get_text(filenmae):
full_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_WIFI_phy_data",
filenmae
filenmae,
)
with open(full_path, 'r', encoding='UTF-8') as stream:
with open(full_path, "r", encoding="UTF-8") as stream:
return stream.read()


class WIFITest(unittest.TestCase, WIFIphyData):

def setUp(self):
self.iw_dev_file_list = ["iw_dev.log", "iw_dev2.log"]
self.phy_info_file_list = ["RTL8821CE_AC.log", "QCNFA765_AX_6G.log",
"BE200_AX.log", "BE200_BE.log"]
self.phy_info_file_list = [
"RTL8821CE_AC.log",
"QCNFA765_AX_6G.log",
"BE200_AX.log",
"BE200_BE.log",
]

@patch('WIFI_phy.check_output')
@patch("WIFI_phy.check_output")
def test_parse_iw_dev_output(self, mock_check_output):
expected_result = {
"iw_dev.log": [('phy#0', 'wlp2s0')],
"iw_dev2.log": [('phy#0', 'wlp3s0f0'),
('phy#1', 'wlp3s0f1')]
"iw_dev.log": [("phy#0", "wlp2s0")],
"iw_dev2.log": [("phy#0", "wlp3s0f0"), ("phy#1", "wlp3s0f1")],
}

for file in self.iw_dev_file_list:
mock_check_output.return_value = WIFIphyData.get_text(file)
result = parse_iw_dev_output()
self.assertEqual(result, expected_result[file])

@patch('WIFI_phy.check_output')
@patch("WIFI_phy.check_output")
def test_create_phy_interface_mapping(self, mock_check_output):
iw_dev_file = self.iw_dev_file_list[0]
iw_dev_output = WIFIphyData.get_text(iw_dev_file)
excvected_result = {
"RTL8821CE_AC.log": {
'wlp2s0': {
'PHY': 'phy#0',
'Bands': {
'1': ['2412 MHz [1] (30.0 dBm)'],
'2': ['5180 MHz [36] (23.0 dBm)']
"wlp2s0": {
"PHY": "phy#0",
"Bands": {
"1": ["2412 MHz [1] (30.0 dBm)"],
"2": ["5180 MHz [36] (23.0 dBm)"],
},
'FREQ_Supported': {
'2.4GHz': 'supported',
'5GHz': 'supported',
'6GHz': 'unsupported'
"FREQ_Supported": {
"2.4GHz": "supported",
"5GHz": "supported",
"6GHz": "unsupported",
},
"STA_Supported": {
"BE": "unsupported",
"AX": "unsupported",
"AC": "supported",
},
'STA_Supported': {
'BE': 'unsupported',
'AX': 'unsupported',
'AC': 'supported'
}
}
},
"QCNFA765_AX_6G.log": {
'wlp2s0': {
'PHY': 'phy#0',
'Bands': {
'1': ['2412 MHz [1] (30.0 dBm)'],
'2': ['5180 MHz [36] (24.0 dBm)'],
'4': ['5955 MHz [1] (30.0 dBm)']
"wlp2s0": {
"PHY": "phy#0",
"Bands": {
"1": ["2412 MHz [1] (30.0 dBm)"],
"2": ["5180 MHz [36] (24.0 dBm)"],
"4": ["5955 MHz [1] (30.0 dBm)"],
},
"FREQ_Supported": {
"2.4GHz": "supported",
"5GHz": "supported",
"6GHz": "supported",
},
'FREQ_Supported': {
'2.4GHz': 'supported',
'5GHz': 'supported',
'6GHz': 'supported'
"STA_Supported": {
"BE": "unsupported",
"AX": "supported",
"AC": "supported",
},
'STA_Supported': {
'BE': 'unsupported',
'AX': 'supported',
'AC': 'supported'
}
}
},
"BE200_AX.log": {
'wlp2s0': {
'PHY': 'phy#0',
'Bands': {
'1': ['2412 MHz [1] (22.0 dBm)'],
'2': ['5180 MHz [36] (22.0 dBm)'],
'4': []
"wlp2s0": {
"PHY": "phy#0",
"Bands": {
"1": ["2412 MHz [1] (22.0 dBm)"],
"2": ["5180 MHz [36] (22.0 dBm)"],
"4": [],
},
'FREQ_Supported': {
'2.4GHz': 'supported',
'5GHz': 'supported',
'6GHz': 'unsupported'
"FREQ_Supported": {
"2.4GHz": "supported",
"5GHz": "supported",
"6GHz": "unsupported",
},
"STA_Supported": {
"BE": "unsupported",
"AX": "supported",
"AC": "supported",
},
'STA_Supported': {
'BE': 'unsupported',
'AX': 'supported',
'AC': 'supported'
}
}
},
"BE200_BE.log": {
'wlp2s0': {
'PHY': 'phy#0',
'Bands': {
'1': ['2412 MHz [1] (22.0 dBm)'],
'2': ['5180 MHz [36] (22.0 dBm)'],
'4': []
"wlp2s0": {
"PHY": "phy#0",
"Bands": {
"1": ["2412 MHz [1] (22.0 dBm)"],
"2": ["5180 MHz [36] (22.0 dBm)"],
"4": [],
},
"FREQ_Supported": {
"2.4GHz": "supported",
"5GHz": "supported",
"6GHz": "unsupported",
},
'FREQ_Supported': {
'2.4GHz': 'supported',
'5GHz': 'supported',
'6GHz': 'unsupported'
"STA_Supported": {
"BE": "supported",
"AX": "supported",
"AC": "supported",
},
'STA_Supported': {
'BE': 'supported',
'AX': 'supported',
'AC': 'supported'
}
}
}
},
}
for file in self.phy_info_file_list:
phy_info_output = WIFIphyData.get_text(file)
Expand All @@ -134,8 +143,8 @@ def test_parse_phy_info_output(self):
result = parse_phy_info_output(phy_info_output)

expected_result = {
'1': ['2412 MHz [1] (30.0 dBm)'],
'2': ['5180 MHz [36] (23.0 dBm)']
"1": ["2412 MHz [1] (30.0 dBm)"],
"2": ["5180 MHz [36] (23.0 dBm)"],
}
self.assertDictEqual(result, expected_result)

Expand All @@ -144,42 +153,42 @@ def test_check_sta_support(self):
result = check_sta_support(phy_info_output)

expected_result = {
'BE': 'unsupported',
'AX': 'unsupported',
'AC': 'supported'
"BE": "unsupported",
"AX": "unsupported",
"AC": "supported",
}
self.assertDictEqual(result, expected_result)

def test_check_freq_support(self):
bands = {
'1': ['2412 MHz [1] (30.0 dBm)'],
'2': ['5180 MHz [36] (23.0 dBm)']
"1": ["2412 MHz [1] (30.0 dBm)"],
"2": ["5180 MHz [36] (23.0 dBm)"],
}
result = check_freq_support(bands)
expected_result = {
'2.4GHz': 'supported',
'5GHz': 'supported',
'6GHz': 'unsupported'
"2.4GHz": "supported",
"5GHz": "supported",
"6GHz": "unsupported",
}
self.assertDictEqual(result, expected_result)

@patch('WIFI_phy.print')
@patch('WIFI_phy.check_output')
@patch("WIFI_phy.print")
@patch("WIFI_phy.check_output")
def test_main(self, mock_check_output, mock_print):
iw_dev_output = WIFIphyData.get_text(self.iw_dev_file_list[0])
phy_info_output = WIFIphyData.get_text(self.phy_info_file_list[0])
mock_check_output.side_effect = [iw_dev_output, phy_info_output]
main()
expected_calls = [
unittest.mock.call('wlp2s0_2.4GHz: supported'),
unittest.mock.call('wlp2s0_5GHz: supported'),
unittest.mock.call('wlp2s0_6GHz: unsupported'),
unittest.mock.call('wlp2s0_be: unsupported'),
unittest.mock.call('wlp2s0_ax: unsupported'),
unittest.mock.call('wlp2s0_ac: supported')
unittest.mock.call("wlp2s0_2.4GHz: supported"),
unittest.mock.call("wlp2s0_5GHz: supported"),
unittest.mock.call("wlp2s0_6GHz: unsupported"),
unittest.mock.call("wlp2s0_be: unsupported"),
unittest.mock.call("wlp2s0_ax: unsupported"),
unittest.mock.call("wlp2s0_ac: supported"),
]
mock_print.assert_has_calls(expected_calls, any_order=True)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit 40a5997

Please sign in to comment.