Skip to content

Commit 5c5b2fd

Browse files
committed
fix(scenarios): validate instance_count range and guard against negative values
Per qodo-code-review bot on PR krkn-chaos#1219: - Add instance_count type and range validation in BaseNetworkChaosConfig.validate() so negative or non-int values from YAML are caught early with a clear error - Tighten sampling guard to require isinstance(int) and instance_count > 0 before calling random.sample(), preventing a runtime crash on negative values Signed-off-by: NETIZEN-11 <niteshkumar121411@gmail.com>
1 parent 15f5086 commit 5c5b2fd

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

krkn/scenario_plugins/network_chaos_ng/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def validate(self) -> list[str]:
4343
errors.append("wait_duration must be an int")
4444
if not isinstance(self.test_duration, int):
4545
errors.append("test_duration must be an int")
46+
if not isinstance(self.instance_count, int):
47+
errors.append("instance_count must be an int")
48+
elif self.instance_count < 0:
49+
errors.append("instance_count must be >= 0")
4650
return errors
4751

4852

krkn/scenario_plugins/network_chaos_ng/network_chaos_ng_scenario_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def run(
6868
)
6969

7070
if (
71-
network_chaos_config.instance_count != 0
71+
isinstance(network_chaos_config.instance_count, int)
72+
and network_chaos_config.instance_count > 0
7273
and len(targets) > network_chaos_config.instance_count
7374
):
7475
targets = random.sample(

0 commit comments

Comments
 (0)