Skip to content

Commit 07964a1

Browse files
nhitaracerv
authored andcommitted
main: add configurable fault injection interval
1 parent 54229a3 commit 07964a1

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

libkirk/main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ def _finjection_config(value: str) -> int:
199199
return max(0, min(100, ret))
200200

201201

202+
def _finterval_config(value: str) -> int:
203+
"""
204+
Return interval of fault injection.
205+
"""
206+
if not value:
207+
return 1
208+
209+
try:
210+
ret = int(value)
211+
except TypeError as err:
212+
raise argparse.ArgumentTypeError("Invalid number") from err
213+
214+
return max(1, ret)
215+
216+
202217
def _get_skip_tests(skip_tests: str, skip_file: str) -> str:
203218
"""
204219
Return the skipped tests regexp.
@@ -361,6 +376,7 @@ async def session_run() -> None:
361376
randomize=args.randomize,
362377
runtime=args.runtime,
363378
fault_prob=args.fault_injection,
379+
fault_interval=args.fault_interval,
364380
)
365381
except asyncio.CancelledError:
366382
await session.stop()
@@ -518,6 +534,12 @@ def run(cmd_args: Optional[List[str]] = None) -> None:
518534
default=0,
519535
help="Probability of failure (0-100)",
520536
)
537+
exec_opts.add_argument(
538+
"--fault-interval",
539+
type=_finterval_config,
540+
default=1,
541+
help="Fault injection interval (default: 1)",
542+
)
521543
exec_opts.add_argument(
522544
"--optimize-sut",
523545
"-O",

libkirk/session.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,11 @@ async def _run_scheduler(self, suites_obj: List[Suite], runtime: float) -> None:
403403
except asyncio.TimeoutError:
404404
await self._scheduler.stop()
405405

406-
async def _apply_fault_injection(self, fault_prob: int) -> None:
406+
async def _apply_fault_injection(
407+
self,
408+
fault_prob: int,
409+
fault_interval: int = 1,
410+
) -> None:
407411
"""
408412
Check if we can apply fault injection configuration
409413
and eventually does it.
@@ -415,7 +419,7 @@ async def _apply_fault_injection(self, fault_prob: int) -> None:
415419
warn_msg = "Run as root to use kernel fault injection"
416420
else:
417421
if await self._sut.is_fault_injection_enabled():
418-
await self._sut.setup_fault_injection(fault_prob)
422+
await self._sut.setup_fault_injection(fault_prob, fault_interval)
419423
else:
420424
if fault_prob != 0:
421425
warn_msg = "Fault injection is not enabled. Running tests normally"
@@ -436,6 +440,7 @@ async def run(
436440
randomize: bool = False,
437441
runtime: float = 0,
438442
fault_prob: int = 0,
443+
fault_interval: int = 1,
439444
) -> None:
440445
"""
441446
Run a new session and store results inside a JSON file.
@@ -460,6 +465,8 @@ async def run(
460465
:type runtime: float
461466
:param fault_prob: Fault injection probability.
462467
:type fault_prob: int
468+
:param fault_interval: Fault injection interval.
469+
:type fault_interval: int
463470
"""
464471
async with self._run_lock:
465472
await libkirk.events.fire("session_started", suites, self._tmpdir.abspath)
@@ -477,7 +484,7 @@ async def run(
477484
await self._exec_command(command)
478485

479486
if fault_prob != 0:
480-
await self._apply_fault_injection(fault_prob)
487+
await self._apply_fault_injection(fault_prob, fault_interval)
481488

482489
if suites:
483490
suites_obj = await self._read_suites(

libkirk/sut.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,24 @@ async def is_fault_injection_enabled(self) -> bool:
370370

371371
return True
372372

373-
async def setup_fault_injection(self, prob: int) -> None:
373+
async def setup_fault_injection(
374+
self,
375+
prob: int,
376+
interval: int = 1,
377+
) -> None:
374378
"""
375379
Configure kernel fault injection. When prob is zero, the fault
376380
injection is set to default values.
377381
378-
:param prob: Fault probabilty in between 0-100.
382+
:param prob: Fault probability in between 0-100.
379383
:type prob: int
384+
:param interval: Fault interval.
385+
:type interval: int
380386
"""
381387
if not await self.is_running():
382388
raise SUTError("SUT is not running")
383389

384-
interval = 1 if prob == 0 else 100
390+
interval = 1 if prob == 0 else interval
385391
times = 1 if prob == 0 else -1
386392

387393
async def _set_value(value: int, path: str) -> None:

libkirk/tests/test_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def test_finjection_config_over_100(self):
110110

111111
def test_finjection_config_negative(self):
112112
assert libkirk.main._finjection_config("-5") == 0
113+
114+
def test_finterval_config_empty(self):
115+
assert libkirk.main._finterval_config("") == 1
116+
117+
def test_finterval_config_negative(self):
118+
assert libkirk.main._finterval_config("-5") == 1
119+
120+
@pytest.mark.parametrize("val", ("1", "20", "100", "2000"))
121+
def test_finterval_config(self, val):
122+
assert libkirk.main._finterval_config(val) == int(val)
113123

114124
def test_get_skip_tests_empty(self):
115125
assert libkirk.main._get_skip_tests("", "") == ""

0 commit comments

Comments
 (0)