tests: test_fst_config: Convert FstLauncher to context manager

Using __del__ for any kind of cleanup is not a good idea
as it's not guaranteed to be called at any particular time,
it's only called whenever the next garbage collect cycle
kicks in.

Use a context manager instead, which basically removes the
need for the try/finally and fixes the reliance on __del__.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
Johannes Berg 2021-05-11 21:35:47 +02:00 committed by Jouni Malinen
parent c8e2fc1fd5
commit fde38cac8a

View file

@ -111,7 +111,10 @@ class FstLauncher:
self.reg_ctrl = fst_test_common.HapdRegCtrl() self.reg_ctrl = fst_test_common.HapdRegCtrl()
self.test_is_supported() self.test_is_supported()
def __del__(self): def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.cleanup() self.cleanup()
@staticmethod @staticmethod
@ -303,19 +306,19 @@ def run_test_ap_configuration(apdev, test_params,
0 - no errors discovered, an error otherwise. The function is used for 0 - no errors discovered, an error otherwise. The function is used for
simplek "bad configuration" tests.""" simplek "bad configuration" tests."""
logdir = test_params['logdir'] logdir = test_params['logdir']
fst_launcher = FstLauncher(logdir) with FstLauncher(logdir) as fst_launcher:
ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_goodconf', 'a', ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_goodconf', 'a',
fst_test_common.fst_test_def_chan_a, fst_test_common.fst_test_def_chan_a,
fst_test_common.fst_test_def_group, fst_test_common.fst_test_def_group,
fst_test_common.fst_test_def_prio_low, fst_test_common.fst_test_def_prio_low,
fst_test_common.fst_test_def_llt) fst_test_common.fst_test_def_llt)
ap2 = FstLauncherConfigAP(apdev[1]['ifname'], 'fst_badconf', 'b', ap2 = FstLauncherConfigAP(apdev[1]['ifname'], 'fst_badconf', 'b',
fst_test_common.fst_test_def_chan_g, fst_group, fst_test_common.fst_test_def_chan_g, fst_group,
fst_pri, fst_llt) fst_pri, fst_llt)
fst_launcher.add_cfg(ap1) fst_launcher.add_cfg(ap1)
fst_launcher.add_cfg(ap2) fst_launcher.add_cfg(ap2)
res = fst_launcher.run_hostapd() res = fst_launcher.run_hostapd()
return res return res
def run_test_sta_configuration(test_params, def run_test_sta_configuration(test_params,
fst_group=fst_test_common.fst_test_def_group, fst_group=fst_test_common.fst_test_def_group,
@ -326,16 +329,16 @@ def run_test_sta_configuration(test_params,
the run: 0 - no errors discovered, an error otherwise. The function is used the run: 0 - no errors discovered, an error otherwise. The function is used
for simple "bad configuration" tests.""" for simple "bad configuration" tests."""
logdir = test_params['logdir'] logdir = test_params['logdir']
fst_launcher = FstLauncher(logdir) with FstLauncher(logdir) as fst_launcher:
sta1 = FstLauncherConfigSTA('wlan5', sta1 = FstLauncherConfigSTA('wlan5',
fst_test_common.fst_test_def_group, fst_test_common.fst_test_def_group,
fst_test_common.fst_test_def_prio_low, fst_test_common.fst_test_def_prio_low,
fst_test_common.fst_test_def_llt) fst_test_common.fst_test_def_llt)
sta2 = FstLauncherConfigSTA('wlan6', fst_group, fst_pri, fst_llt) sta2 = FstLauncherConfigSTA('wlan6', fst_group, fst_pri, fst_llt)
fst_launcher.add_cfg(sta1) fst_launcher.add_cfg(sta1)
fst_launcher.add_cfg(sta2) fst_launcher.add_cfg(sta2)
res = fst_launcher.run_wpa_supplicant() res = fst_launcher.run_wpa_supplicant()
return res return res
def test_fst_ap_config_llt_neg(dev, apdev, test_params): def test_fst_ap_config_llt_neg(dev, apdev, test_params):
"""FST AP configuration negative LLT""" """FST AP configuration negative LLT"""
@ -481,21 +484,21 @@ def test_fst_scan_mb(dev, apdev, test_params):
logdir = test_params['logdir'] logdir = test_params['logdir']
# Test valid MB IE in scan results # Test valid MB IE in scan results
fst_launcher = FstLauncher(logdir) with FstLauncher(logdir) as fst_launcher:
ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_11a', 'a', ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_11a', 'a',
fst_test_common.fst_test_def_chan_a, fst_test_common.fst_test_def_chan_a,
fst_test_common.fst_test_def_group, fst_test_common.fst_test_def_group,
fst_test_common.fst_test_def_prio_high) fst_test_common.fst_test_def_prio_high)
ap2 = FstLauncherConfigAP(apdev[1]['ifname'], 'fst_11g', 'b', ap2 = FstLauncherConfigAP(apdev[1]['ifname'], 'fst_11g', 'b',
fst_test_common.fst_test_def_chan_g, fst_test_common.fst_test_def_chan_g,
fst_test_common.fst_test_def_group, fst_test_common.fst_test_def_group,
fst_test_common.fst_test_def_prio_low) fst_test_common.fst_test_def_prio_low)
fst_launcher.add_cfg(ap1) fst_launcher.add_cfg(ap1)
fst_launcher.add_cfg(ap2) fst_launcher.add_cfg(ap2)
res = fst_launcher.run_hostapd() res = fst_launcher.run_hostapd()
if res != 0: if res != 0:
raise Exception("hostapd didn't start properly") raise Exception("hostapd didn't start properly")
try:
mbie1 = [] mbie1 = []
flags1 = '' flags1 = ''
mbie2 = [] mbie2 = []
@ -514,8 +517,6 @@ def test_fst_scan_mb(dev, apdev, test_params):
mbie2 = parse_ies(vals2['ie'], 0x9e) mbie2 = parse_ies(vals2['ie'], 0x9e)
if 'flags' in vals2: if 'flags' in vals2:
flags2 = vals2['flags'] flags2 = vals2['flags']
finally:
fst_launcher.cleanup()
if len(mbie1) == 0: if len(mbie1) == 0:
raise Exception("No MB IE created by 1st AP") raise Exception("No MB IE created by 1st AP")
@ -527,16 +528,16 @@ def test_fst_scan_nomb(dev, apdev, test_params):
logdir = test_params['logdir'] logdir = test_params['logdir']
# Test valid MB IE in scan results # Test valid MB IE in scan results
fst_launcher = FstLauncher(logdir) with FstLauncher(logdir) as fst_launcher:
ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_11a', 'a', ap1 = FstLauncherConfigAP(apdev[0]['ifname'], 'fst_11a', 'a',
fst_test_common.fst_test_def_chan_a, fst_test_common.fst_test_def_chan_a,
fst_test_common.fst_test_def_group, fst_test_common.fst_test_def_group,
fst_test_common.fst_test_def_prio_high) fst_test_common.fst_test_def_prio_high)
fst_launcher.add_cfg(ap1) fst_launcher.add_cfg(ap1)
res = fst_launcher.run_hostapd() res = fst_launcher.run_hostapd()
if res != 0: if res != 0:
raise Exception("Hostapd didn't start properly") raise Exception("Hostapd didn't start properly")
try:
time.sleep(2) time.sleep(2)
mbie1 = [] mbie1 = []
flags1 = '' flags1 = ''
@ -546,8 +547,6 @@ def test_fst_scan_nomb(dev, apdev, test_params):
mbie1 = parse_ies(vals1['ie'], 0x9e) mbie1 = parse_ies(vals1['ie'], 0x9e)
if 'flags' in vals1: if 'flags' in vals1:
flags1 = vals1['flags'] flags1 = vals1['flags']
finally:
fst_launcher.cleanup()
if len(mbie1) != 0: if len(mbie1) != 0:
raise Exception("MB IE exists with 1 AP") raise Exception("MB IE exists with 1 AP")