Skip to content

Commit f3ee6a7

Browse files
author
Jan Musial
committed
Move prepare/teardown to TF
Signed-off-by: Jan Musial <[email protected]>
1 parent 466fcbc commit f3ee6a7

File tree

1 file changed

+40
-171
lines changed

1 file changed

+40
-171
lines changed

test/functional/tests/conftest.py

Lines changed: 40 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,9 @@
1818
from api.cas import installer
1919
from api.cas import casadm
2020
from api.cas import git
21-
from storage_devices.raid import Raid
22-
from storage_devices.ramdisk import RamDisk
23-
from test_utils.os_utils import Udev, kill_all_io
24-
from test_utils.disk_finder import get_disk_serial_number
25-
from test_tools.disk_utils import PartitionTable, create_partition_table
26-
from test_tools.device_mapper import DeviceMapper
27-
from test_tools.mdadm import Mdadm
28-
from log.logger import create_log, Log
21+
from api.cas.init_config import InitConfig
2922
from test_utils.singleton import Singleton
23+
from test_utils.output import CmdException
3024

3125

3226
class Opencas(metaclass=Singleton):
@@ -46,70 +40,56 @@ def pytest_collection_modifyitems(config, items):
4640
sys.stdout.flush()
4741

4842

49-
def pytest_runtest_setup(item):
50-
# There should be dut config file added to config package and
51-
# pytest should be executed with option --dut-config=conf_name'.
43+
def pytest_runtest_setup(item): # There should be dut config file added to config package and # pytest should be executed with option --dut-config=conf_name'.
5244
#
5345
# 'ip' field should be filled with valid IP string to use remote ssh executor
5446
# or it should be commented out when user want to execute tests on local machine
5547
#
5648
# User can also have own test wrapper, which runs test prepare, cleanup, etc.
5749
# Then it should be placed in plugins package
50+
TestRun.start(item)
51+
for dut in TestRun.use_all_duts():
52+
if not installer.check_if_installed():
53+
continue
54+
TestRun.LOGGER.info(f"CAS cleanup on {dut.ip}")
55+
try:
56+
InitConfig.create_default_init_config()
57+
unmount_cas_devices()
58+
try:
59+
casadm.stop_all_caches()
60+
except CmdException:
61+
TestRun.LOGGER.warning("Failed to stop all caches, will retry after generic prepare")
62+
casadm.remove_all_detached_cores()
63+
except Exception as e:
64+
raise Exception("Exception occured during CAS cleanup:\n"
65+
f"{str(e)}\n{traceback.format_exc()}")
5866

59-
test_name = item.name.split('[')[0]
60-
TestRun.LOGGER = create_log(item.config.getoption('--log-path'), test_name)
67+
TestRun.prepare()
6168

62-
duts = item.config.getoption('--dut-config')
63-
required_duts = next(item.iter_markers(name="multidut"), None)
64-
required_duts = required_duts.args[0] if required_duts is not None else 1
65-
if required_duts > len(duts):
66-
raise Exception(f"Test requires {required_duts} DUTs, only {len(duts)} DUT configs "
67-
f"provided")
68-
else:
69-
duts = duts[:required_duts]
69+
# If some generic device was set-up on top of CAS it failed to stop, try to stop it again
70+
if installer.check_if_installed():
71+
casadm.stop_all_caches()
7072

71-
TestRun.duts = []
72-
for dut in duts:
73-
try:
74-
with open(dut) as cfg:
75-
dut_config = yaml.safe_load(cfg)
76-
except Exception as ex:
77-
raise Exception(f"{ex}\n"
78-
f"You need to specify DUT config. See the example_dut_config.py file")
73+
TestRun.usr = Opencas(
74+
repo_dir=os.path.join(os.path.dirname(__file__), "../../.."),
75+
working_dir=TestRun.working_dir)
7976

80-
dut_config['plugins_dir'] = os.path.join(os.path.dirname(__file__), "../lib")
81-
dut_config['opt_plugins'] = {"test_wrapper": {}, "serial_log": {}, "power_control": {}}
82-
dut_config['extra_logs'] = {"cas": "/var/log/opencas.log"}
77+
for i, dut in enumerate(TestRun.use_all_duts()):
78+
if get_force_param(item) and not TestRun.usr.already_updated:
79+
installer.rsync_opencas_sources()
80+
installer.reinstall_opencas()
81+
elif not installer.check_if_installed():
82+
installer.rsync_opencas_sources()
83+
installer.set_up_opencas()
84+
TestRun.LOGGER.info(f"DUT-{i} info: {dut}")
8385

84-
try:
85-
TestRun.prepare(item, dut_config)
86+
TestRun.usr.already_updated = True
87+
88+
TestRun.LOGGER.add_build_info(f'Commit hash:')
89+
TestRun.LOGGER.add_build_info(f"{git.get_current_commit_hash()}")
90+
TestRun.LOGGER.add_build_info(f'Commit message:')
91+
TestRun.LOGGER.add_build_info(f'{git.get_current_commit_message()}')
8692

87-
TestRun.presetup()
88-
try:
89-
TestRun.executor.wait_for_connection(timedelta(seconds=20))
90-
except paramiko.AuthenticationException:
91-
raise
92-
except Exception:
93-
try:
94-
TestRun.plugin_manager.get_plugin('power_control').power_cycle()
95-
TestRun.executor.wait_for_connection()
96-
except Exception:
97-
raise Exception("Failed to connect to DUT.")
98-
TestRun.setup()
99-
except Exception as ex:
100-
raise Exception(f"Exception occurred during test setup:\n"
101-
f"{str(ex)}\n{traceback.format_exc()}")
102-
103-
TestRun.usr = Opencas(
104-
repo_dir=os.path.join(os.path.dirname(__file__), "../../.."),
105-
working_dir=dut_config['working_dir'])
106-
107-
TestRun.LOGGER.info(f"DUT info: {TestRun.dut}")
108-
TestRun.dut.plugin_manager = TestRun.plugin_manager
109-
TestRun.dut.executor = TestRun.executor
110-
TestRun.duts.append(TestRun.dut)
111-
112-
base_prepare(item)
11393
TestRun.LOGGER.write_to_command_log("Test body")
11494
TestRun.LOGGER.start_group("Test body")
11595

@@ -125,46 +105,6 @@ def pytest_runtest_teardown():
125105
This method is executed always in the end of each test, even if it fails or raises exception in
126106
prepare stage.
127107
"""
128-
TestRun.LOGGER.end_all_groups()
129-
130-
with TestRun.LOGGER.step("Cleanup after test"):
131-
try:
132-
if TestRun.executor:
133-
if not TestRun.executor.is_active():
134-
TestRun.executor.wait_for_connection()
135-
Udev.enable()
136-
kill_all_io()
137-
unmount_cas_devices()
138-
139-
if installer.check_if_installed():
140-
casadm.remove_all_detached_cores()
141-
casadm.stop_all_caches()
142-
from api.cas.init_config import InitConfig
143-
InitConfig.create_default_init_config()
144-
145-
from storage_devices.drbd import Drbd
146-
if installer.check_if_installed() and Drbd.is_installed():
147-
try:
148-
casadm.stop_all_caches()
149-
finally:
150-
__drbd_cleanup()
151-
elif Drbd.is_installed():
152-
Drbd.down_all()
153-
154-
DeviceMapper.remove_all()
155-
RamDisk.remove_all()
156-
except Exception as ex:
157-
TestRun.LOGGER.warning(f"Exception occurred during platform cleanup.\n"
158-
f"{str(ex)}\n{traceback.format_exc()}")
159-
160-
TestRun.LOGGER.end()
161-
for dut in TestRun.duts:
162-
with TestRun.use_dut(dut):
163-
if TestRun.executor:
164-
os.makedirs(os.path.join(TestRun.LOGGER.base_dir, "dut_info", dut.ip),
165-
exist_ok=True)
166-
TestRun.LOGGER.get_additional_logs()
167-
Log.destroy()
168108
TestRun.teardown()
169109

170110

@@ -208,74 +148,3 @@ def unmount_cas_devices():
208148

209149
def get_force_param(item):
210150
return item.config.getoption("--force-reinstall")
211-
212-
213-
def __drbd_cleanup():
214-
from storage_devices.drbd import Drbd
215-
Drbd.down_all()
216-
# If drbd instance had been configured on top of the CAS, the previos attempt to stop
217-
# failed. As drbd has been stopped try to stop CAS one more time.
218-
if installer.check_if_installed():
219-
casadm.stop_all_caches()
220-
221-
222-
def base_prepare(item):
223-
with TestRun.LOGGER.step("Cleanup before test"):
224-
TestRun.executor.run("pkill --signal=SIGKILL fsck")
225-
Udev.enable()
226-
kill_all_io()
227-
DeviceMapper.remove_all()
228-
229-
if installer.check_if_installed():
230-
try:
231-
from api.cas.init_config import InitConfig
232-
InitConfig.create_default_init_config()
233-
unmount_cas_devices()
234-
casadm.stop_all_caches()
235-
casadm.remove_all_detached_cores()
236-
except Exception:
237-
pass # TODO: Reboot DUT if test is executed remotely
238-
239-
from storage_devices.drbd import Drbd
240-
if Drbd.is_installed():
241-
__drbd_cleanup()
242-
243-
raids = Raid.discover()
244-
for raid in raids:
245-
# stop only those RAIDs, which are comprised of test disks
246-
if all(map(lambda device:
247-
any(map(lambda disk_path:
248-
disk_path in device.get_device_id(),
249-
[bd.get_device_id() for bd in TestRun.dut.disks])),
250-
raid.array_devices)):
251-
raid.umount_all_partitions()
252-
raid.remove_partitions()
253-
raid.stop()
254-
for device in raid.array_devices:
255-
Mdadm.zero_superblock(os.path.join('/dev', device.get_device_id()))
256-
257-
RamDisk.remove_all()
258-
259-
for disk in TestRun.disks.values():
260-
disk_serial = get_disk_serial_number(disk.path)
261-
if disk.serial_number != disk_serial:
262-
disk.serial_number = disk_serial
263-
264-
disk.umount_all_partitions()
265-
Mdadm.zero_superblock(os.path.join('/dev', disk.device_id))
266-
disk.remove_partitions()
267-
create_partition_table(disk, PartitionTable.gpt)
268-
269-
Udev.settle()
270-
271-
if get_force_param(item) and not TestRun.usr.already_updated:
272-
installer.rsync_opencas_sources()
273-
installer.reinstall_opencas()
274-
elif not installer.check_if_installed():
275-
installer.rsync_opencas_sources()
276-
installer.set_up_opencas()
277-
TestRun.usr.already_updated = True
278-
TestRun.LOGGER.add_build_info(f'Commit hash:')
279-
TestRun.LOGGER.add_build_info(f"{git.get_current_commit_hash()}")
280-
TestRun.LOGGER.add_build_info(f'Commit message:')
281-
TestRun.LOGGER.add_build_info(f'{git.get_current_commit_message()}')

0 commit comments

Comments
 (0)