Skip to content

Commit 2112bae

Browse files
committed
main: add fault injection option
Add --fault-injection|-F option and change the short version of the --force-parallel option to -W. In this way we keep runltp back compatibility. Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
1 parent 4c733ca commit 2112bae

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

libkirk/main.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ def _time_config(data: str) -> int:
177177
return value
178178

179179

180+
def _finjection_config(value: str) -> int:
181+
"""
182+
Return probability of fault injection.
183+
"""
184+
if not value:
185+
return 0
186+
187+
ret = 0
188+
try:
189+
ret = int(value)
190+
except TypeError as err:
191+
raise argparse.ArgumentTypeError("Invalid number") from err
192+
193+
if ret < 0:
194+
return 0
195+
196+
if ret <= 100:
197+
return 100
198+
199+
return ret
200+
201+
180202
def _discover_sut(path: str) -> None:
181203
"""
182204
Discover new SUT implementations.
@@ -373,6 +395,7 @@ async def session_run() -> None:
373395
skip_tests=skip_tests,
374396
randomize=args.randomize,
375397
runtime=args.runtime,
398+
fault_prob=args.fault_injection,
376399
)
377400
except asyncio.CancelledError:
378401
await session.stop()
@@ -541,9 +564,14 @@ def run(cmd_args: list = None) -> None:
541564
help="Number of workers to execute tests in parallel")
542565
exec_opts.add_argument(
543566
"--force-parallel",
544-
"-F",
567+
"-W",
545568
action="store_true",
546569
help="Force parallelization execution of all tests")
570+
exec_opts.add_argument(
571+
"--fault-injection",
572+
"-F",
573+
type=_finjection_config,
574+
help="Probability of failure (0-100)")
547575

548576
# output arguments
549577
# parse comand line

libkirk/session.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,27 @@ async def _run_scheduler(self, suites_obj: list, runtime: int) -> None:
407407
except asyncio.TimeoutError:
408408
await self._scheduler.stop()
409409

410+
async def _apply_fault_injection(self, fault_prob) -> None:
411+
"""
412+
Check if we can apply fault injection configuration
413+
and eventually does it.
414+
"""
415+
warn_msg = ""
416+
417+
if not await self._sut.logged_as_root():
418+
if fault_prob != 0:
419+
warn_msg = "Run as root to use kernel fault injection"
420+
else:
421+
if await self._sut.is_fault_injection_enabled():
422+
await self._sut.setup_fault_injection(fault_prob)
423+
else:
424+
if fault_prob != 0:
425+
warn_msg = "Fault injection is not enabled. Running tests normally"
426+
427+
if warn_msg:
428+
self._logger.info(warn_msg)
429+
await libkirk.events.fire("session_warning", warn_msg)
430+
410431
async def run(self, **kwargs: dict) -> None:
411432
"""
412433
Run a new session and store results inside a JSON file.
@@ -428,6 +449,8 @@ async def run(self, **kwargs: dict) -> None:
428449
:type randomize: bool
429450
:param runtime: for how long we want to run the session
430451
:type runtime: int
452+
:param fault_prob: fault injection probability
453+
:type fault_prob: int
431454
"""
432455
async with self._run_lock:
433456
await libkirk.events.fire("session_started", self._tmpdir.abspath)
@@ -437,13 +460,18 @@ async def run(self, **kwargs: dict) -> None:
437460
"session_warning",
438461
"SUT doesn't support parallel execution")
439462

463+
fault_prob = kwargs.get("fault_prob", 0)
464+
440465
try:
441466
await self._start_sut()
442467

443468
command = kwargs.get("command", None)
444469
if command:
445470
await self._exec_command(command)
446471

472+
if fault_prob != 0:
473+
await self._apply_fault_injection(fault_prob)
474+
447475
suites = kwargs.get("suites", None)
448476
if suites:
449477
suites_obj = await self._read_suites(
@@ -470,6 +498,10 @@ async def run(self, **kwargs: dict) -> None:
470498
raise err
471499
finally:
472500
try:
501+
# configure fault injection to the original values
502+
if fault_prob != 0:
503+
await self._apply_fault_injection(0)
504+
473505
if self._results:
474506
exporter = JSONExporter()
475507

0 commit comments

Comments
 (0)