19
19
from api .cas import casadm
20
20
from api .cas import git
21
21
from api .cas .cas_service import opencas_drop_in_directory
22
- from storage_devices .raid import Raid
23
- from storage_devices .ramdisk import RamDisk
24
- from test_utils .os_utils import Udev , kill_all_io
25
- from test_utils .disk_finder import get_disk_serial_number
26
- from test_tools .disk_utils import PartitionTable , create_partition_table
27
- from test_tools .device_mapper import DeviceMapper
28
- from test_tools .mdadm import Mdadm
29
- from test_tools .fs_utils import remove
30
- from log .logger import create_log , Log
22
+ from api .cas .init_config import InitConfig
31
23
from test_utils .singleton import Singleton
24
+ from test_utils .output import CmdException
25
+ from test_tools .fs_utils import remove
32
26
33
27
34
28
class Opencas (metaclass = Singleton ):
@@ -57,61 +51,73 @@ def pytest_runtest_setup(item):
57
51
#
58
52
# User can also have own test wrapper, which runs test prepare, cleanup, etc.
59
53
# Then it should be placed in plugins package
60
-
61
- test_name = item .name .split ('[' )[0 ]
62
- TestRun .LOGGER = create_log (item .config .getoption ('--log-path' ), test_name )
63
-
64
- duts = item .config .getoption ('--dut-config' )
65
- required_duts = next (item .iter_markers (name = "multidut" ), None )
66
- required_duts = required_duts .args [0 ] if required_duts is not None else 1
67
- if required_duts > len (duts ):
68
- raise Exception (f"Test requires { required_duts } DUTs, only { len (duts )} DUT configs "
69
- f"provided" )
70
- else :
71
- duts = duts [:required_duts ]
72
-
73
- TestRun .duts = []
74
- for dut in duts :
54
+ duts = []
55
+ for dut_file in item .config .getoption ("--dut-config" ):
75
56
try :
76
- with open (dut ) as cfg :
77
- dut_config = yaml .safe_load (cfg )
57
+ with open (dut_file ) as df :
58
+ dut_config = yaml .safe_load (df )
78
59
except Exception as ex :
79
- raise Exception (f"{ ex } \n "
80
- f"You need to specify DUT config. See the example_dut_config.py file" )
81
-
82
- dut_config ['plugins_dir' ] = os .path .join (os .path .dirname (__file__ ), "../lib" )
83
- dut_config ['opt_plugins' ] = {"test_wrapper" : {}, "serial_log" : {}, "power_control" : {}}
84
- dut_config ['extra_logs' ] = {"cas" : "/var/log/opencas.log" }
60
+ raise Exception (
61
+ "You need to specify DUT config. See the example_dut_config.py file"
62
+ ) from ex
63
+
64
+ dut_config ["extra_logs" ] = {"cas" : "/var/log/opencas.log" }
65
+ duts .append (dut_config )
66
+
67
+ config = {
68
+ "test_name" : item .name .split ("[" )[0 ],
69
+ "log_path" : item .config .getoption ("--log-path" ),
70
+ "duts" : duts ,
71
+ "item" : item ,
72
+ }
73
+
74
+ TestRun .start (config )
75
+ for dut in TestRun .use_all_duts ():
76
+ if not installer .check_if_installed ():
77
+ continue
78
+ TestRun .LOGGER .info (f"CAS cleanup on { dut .ip } " )
79
+ remove (opencas_drop_in_directory , recursive = True , ignore_errors = True )
85
80
86
81
try :
87
- TestRun .prepare (item , dut_config )
88
-
89
- TestRun .presetup ()
82
+ InitConfig .create_default_init_config ()
83
+ unmount_cas_devices ()
90
84
try :
91
- TestRun .executor .wait_for_connection (timedelta (seconds = 20 ))
92
- except paramiko .AuthenticationException :
93
- raise
94
- except Exception :
95
- try :
96
- TestRun .plugin_manager .get_plugin ('power_control' ).power_cycle ()
97
- TestRun .executor .wait_for_connection ()
98
- except Exception :
99
- raise Exception ("Failed to connect to DUT." )
100
- TestRun .setup ()
101
- except Exception as ex :
102
- raise Exception (f"Exception occurred during test setup:\n "
103
- f"{ str (ex )} \n { traceback .format_exc ()} " )
85
+ casadm .stop_all_caches ()
86
+ except CmdException :
87
+ TestRun .LOGGER .warning (
88
+ "Failed to stop all caches, will retry after generic prepare"
89
+ )
90
+ casadm .remove_all_detached_cores ()
91
+ except Exception as e :
92
+ raise Exception ("Exception occured during CAS cleanup:\n "
93
+ f"{ str (e )} \n { traceback .format_exc ()} " )
94
+
95
+ TestRun .prepare ()
96
+
97
+ # If some generic device was set-up on top of CAS it failed to stop, try to stop it again
98
+ if installer .check_if_installed ():
99
+ casadm .stop_all_caches ()
104
100
105
- TestRun .usr = Opencas (
106
- repo_dir = os .path .join (os .path .dirname (__file__ ), "../../.." ),
107
- working_dir = dut_config [ ' working_dir' ] )
101
+ TestRun .usr = Opencas (
102
+ repo_dir = os .path .join (os .path .dirname (__file__ ), "../../.." ),
103
+ working_dir = TestRun . working_dir )
108
104
109
- TestRun .LOGGER .info (f"DUT info: { TestRun .dut } " )
110
- TestRun .dut .plugin_manager = TestRun .plugin_manager
111
- TestRun .dut .executor = TestRun .executor
112
- TestRun .duts .append (TestRun .dut )
105
+ for i , dut in enumerate (TestRun .use_all_duts ()):
106
+ if get_force_param (item ) and not TestRun .usr .already_updated :
107
+ installer .rsync_opencas_sources ()
108
+ installer .reinstall_opencas ()
109
+ elif not installer .check_if_installed ():
110
+ installer .rsync_opencas_sources ()
111
+ installer .set_up_opencas ()
112
+ TestRun .LOGGER .info (f"DUT-{ i } info: { dut } " )
113
+
114
+ TestRun .usr .already_updated = True
115
+
116
+ TestRun .LOGGER .add_build_info (f'Commit hash:' )
117
+ TestRun .LOGGER .add_build_info (f"{ git .get_current_commit_hash ()} " )
118
+ TestRun .LOGGER .add_build_info (f'Commit message:' )
119
+ TestRun .LOGGER .add_build_info (f'{ git .get_current_commit_message ()} ' )
113
120
114
- base_prepare (item )
115
121
TestRun .LOGGER .write_to_command_log ("Test body" )
116
122
TestRun .LOGGER .start_group ("Test body" )
117
123
@@ -127,46 +133,6 @@ def pytest_runtest_teardown():
127
133
This method is executed always in the end of each test, even if it fails or raises exception in
128
134
prepare stage.
129
135
"""
130
- TestRun .LOGGER .end_all_groups ()
131
-
132
- with TestRun .LOGGER .step ("Cleanup after test" ):
133
- try :
134
- if TestRun .executor :
135
- if not TestRun .executor .is_active ():
136
- TestRun .executor .wait_for_connection ()
137
- Udev .enable ()
138
- kill_all_io ()
139
- unmount_cas_devices ()
140
-
141
- if installer .check_if_installed ():
142
- casadm .remove_all_detached_cores ()
143
- casadm .stop_all_caches ()
144
- from api .cas .init_config import InitConfig
145
- InitConfig .create_default_init_config ()
146
-
147
- from storage_devices .drbd import Drbd
148
- if installer .check_if_installed () and Drbd .is_installed ():
149
- try :
150
- casadm .stop_all_caches ()
151
- finally :
152
- __drbd_cleanup ()
153
- elif Drbd .is_installed ():
154
- Drbd .down_all ()
155
-
156
- DeviceMapper .remove_all ()
157
- RamDisk .remove_all ()
158
- except Exception as ex :
159
- TestRun .LOGGER .warning (f"Exception occurred during platform cleanup.\n "
160
- f"{ str (ex )} \n { traceback .format_exc ()} " )
161
-
162
- TestRun .LOGGER .end ()
163
- for dut in TestRun .duts :
164
- with TestRun .use_dut (dut ):
165
- if TestRun .executor :
166
- os .makedirs (os .path .join (TestRun .LOGGER .base_dir , "dut_info" , dut .ip ),
167
- exist_ok = True )
168
- TestRun .LOGGER .get_additional_logs ()
169
- Log .destroy ()
170
136
TestRun .teardown ()
171
137
172
138
@@ -210,76 +176,3 @@ def unmount_cas_devices():
210
176
211
177
def get_force_param (item ):
212
178
return item .config .getoption ("--force-reinstall" )
213
-
214
-
215
- def __drbd_cleanup ():
216
- from storage_devices .drbd import Drbd
217
- Drbd .down_all ()
218
- # If drbd instance had been configured on top of the CAS, the previos attempt to stop
219
- # failed. As drbd has been stopped try to stop CAS one more time.
220
- if installer .check_if_installed ():
221
- casadm .stop_all_caches ()
222
-
223
-
224
- def base_prepare (item ):
225
- with TestRun .LOGGER .step ("Cleanup before test" ):
226
- TestRun .executor .run ("pkill --signal=SIGKILL fsck" )
227
- Udev .enable ()
228
- kill_all_io ()
229
- DeviceMapper .remove_all ()
230
-
231
- if installer .check_if_installed ():
232
- try :
233
- from api .cas .init_config import InitConfig
234
- InitConfig .create_default_init_config ()
235
- unmount_cas_devices ()
236
- casadm .stop_all_caches ()
237
- casadm .remove_all_detached_cores ()
238
- except Exception :
239
- pass # TODO: Reboot DUT if test is executed remotely
240
-
241
- remove (opencas_drop_in_directory , recursive = True , ignore_errors = True )
242
-
243
- from storage_devices .drbd import Drbd
244
- if Drbd .is_installed ():
245
- __drbd_cleanup ()
246
-
247
- raids = Raid .discover ()
248
- for raid in raids :
249
- # stop only those RAIDs, which are comprised of test disks
250
- if all (map (lambda device :
251
- any (map (lambda disk_path :
252
- disk_path in device .get_device_id (),
253
- [bd .get_device_id () for bd in TestRun .dut .disks ])),
254
- raid .array_devices )):
255
- raid .umount_all_partitions ()
256
- raid .remove_partitions ()
257
- raid .stop ()
258
- for device in raid .array_devices :
259
- Mdadm .zero_superblock (os .path .join ('/dev' , device .get_device_id ()))
260
-
261
- RamDisk .remove_all ()
262
-
263
- for disk in TestRun .disks .values ():
264
- disk_serial = get_disk_serial_number (disk .path )
265
- if disk .serial_number != disk_serial :
266
- disk .serial_number = disk_serial
267
-
268
- disk .umount_all_partitions ()
269
- Mdadm .zero_superblock (os .path .join ('/dev' , disk .device_id ))
270
- disk .remove_partitions ()
271
- create_partition_table (disk , PartitionTable .gpt )
272
-
273
- Udev .settle ()
274
-
275
- if get_force_param (item ) and not TestRun .usr .already_updated :
276
- installer .rsync_opencas_sources ()
277
- installer .reinstall_opencas ()
278
- elif not installer .check_if_installed ():
279
- installer .rsync_opencas_sources ()
280
- installer .set_up_opencas ()
281
- TestRun .usr .already_updated = True
282
- TestRun .LOGGER .add_build_info (f'Commit hash:' )
283
- TestRun .LOGGER .add_build_info (f"{ git .get_current_commit_hash ()} " )
284
- TestRun .LOGGER .add_build_info (f'Commit message:' )
285
- TestRun .LOGGER .add_build_info (f'{ git .get_current_commit_message ()} ' )
0 commit comments