Skip to content

Commit e9dadd7

Browse files
committed
Merge branch 'main' into fix-autoliniting--precommit
2 parents af8d4e1 + aaa18b3 commit e9dadd7

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
app = FastAPI(
55
title="Stressed API",
6-
description="The API simulates controlled workloads and failures to stress-test a system, assessing its resilience, performance, and recovery capabilities.",
6+
description="simulates controlled workloads and failures to stress-test a system",
77
version="1.0.0",
88
)
99

app/managers/load_manager.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def add_cpu_load(self, value: float):
7171
if value <= 0:
7272
raise ValueError("CPU load must be greater than 0")
7373
if value > self.system_cpus:
74-
raise ValueError(f"CPU load cannot exceed system CPU count ({self.system_cpus})")
74+
raise ValueError(
75+
f"CPU load cannot exceed system CPU count ({self.system_cpus})"
76+
)
7577

7678
if not os.path.exists(self.cpu_script_path):
7779
raise RuntimeError("CPU stress script is missing")
@@ -103,7 +105,9 @@ def add_memory_load(self, value: int):
103105
if value <= 0:
104106
raise ValueError("Memory load must be greater than 0")
105107
if value > self.system_memory:
106-
raise ValueError(f"Memory load cannot exceed system memory ({self.system_memory}MB)")
108+
raise ValueError(
109+
f"Memory load cannot exceed system memory ({self.system_memory}MB)"
110+
)
107111

108112
if not os.path.exists(self.memory_script_path):
109113
raise RuntimeError("Memory stress script is missing")
@@ -121,7 +125,9 @@ def dynamic_memory_load(
121125
):
122126
"""Dynamic memory load with validation"""
123127
if duration <= 0 or duration > self.max_duration:
124-
raise ValueError(f"Duration must be between 1 and {self.max_duration} seconds")
128+
raise ValueError(
129+
f"Duration must be between 1 and {self.max_duration} seconds"
130+
)
125131
if end_value < start_value:
126132
raise ValueError("End value must be greater than start value")
127133

@@ -159,11 +165,17 @@ def apply_dynamic_memory_load(interval_num):
159165
apply_dynamic_memory_load(0)
160166

161167
def dynamic_cpu_load(
162-
self, start_value: float, end_value: float, duration: int, stop_at_end: bool = False
168+
self,
169+
start_value: float,
170+
end_value: float,
171+
duration: int,
172+
stop_at_end: bool = False,
163173
):
164174
"""Dynamic CPU load with validation"""
165175
if duration <= 0 or duration > self.max_duration:
166-
raise ValueError(f"Duration must be between 1 and {self.max_duration} seconds")
176+
raise ValueError(
177+
f"Duration must be between 1 and {self.max_duration} seconds"
178+
)
167179
if end_value < start_value:
168180
raise ValueError("End value must be greater than start value")
169181

app/managers/log_manager.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,20 @@ def _start_automatic_logs(self):
8585
service=os.getenv("LOG_SERVICE", "auto-logger"),
8686
format=LogFormat(os.getenv("LOG_FORMAT", "json").lower()),
8787
interval=int(os.getenv("LOG_INTERVAL", "5")),
88-
duration=int(os.getenv("LOG_DURATION", "60"))
88+
duration=int(os.getenv("LOG_DURATION", "60")),
8989
)
90-
90+
9191
# Create single log immediately
92-
asyncio.create_task(self._create_single_log(
93-
log_data,
94-
getattr(logging, log_data.level.upper())
95-
))
92+
asyncio.create_task(
93+
self._create_single_log(log_data, getattr(logging, log_data.level.upper()))
94+
)
9695

9796
# Setup recurring logs if needed
9897
if log_data.interval and log_data.duration:
9998
asyncio.create_task(
100-
self._create_recurring_logs(log_data, log_data.interval, log_data.duration)
99+
self._create_recurring_logs(
100+
log_data, log_data.interval, log_data.duration
101+
)
101102
)
102103

103104
async def create_log(self, log_data: LogRequest) -> dict | str:
@@ -169,4 +170,4 @@ async def log_creator(self, log_data: LogRequest, interval: int, duration: int):
169170
end_time = time.time() + duration
170171
while time.time() < end_time:
171172
await self.create_log(log_data)
172-
await asyncio.sleep(interval)
173+
await asyncio.sleep(interval)

app/managers/system_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import psutil
22
from datetime import datetime, timedelta
33
import os
4-
import asyncio
54

65

76
class SystemManager:
87
def __init__(self):
98
if os.getenv("ENABLE_AUTO_TERMINATION", "false").lower() == "true":
109
delay = int(os.getenv("AUTO_TERMINATION_DELAY", "300"))
1110
self.terminate(delay)
12-
11+
1312
def get_system_info(self):
1413
"""Get system information"""
1514
return {

app/models/schemas.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class DynamicMemoryLoadRequest(BaseModel):
2222

2323

2424
class LoadRequest(BaseModel):
25-
value: float = Field(..., ge=0, description="Load value (CPU cores or MB of memory)")
25+
value: float = Field(
26+
..., ge=0, description="Load value (CPU cores or MB of memory)"
27+
)
2628

2729

2830
class ProbeRequest(BaseModel):

tests/unit/test_load_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def test_stop_all_loads(self, load_manager):
162162

163163
def test_add_cpu_load_script_missing(self, load_manager, monkeypatch):
164164
"""Test failure when cpu_stress.py is missing"""
165+
165166
def mock_exists(path):
166167
return False
167168

@@ -233,4 +234,6 @@ async def test_edge_cases_dynamic_load(self, load_manager):
233234

234235
# Test with maximum duration
235236
with pytest.raises(ValueError, match="Duration must be between 1 and"):
236-
load_manager.dynamic_memory_load(100, 200, 3601, True) # Duration exceeds max_duration
237+
load_manager.dynamic_memory_load(
238+
100, 200, 3601, True
239+
) # Duration exceeds max_duration

tests/unit/test_log_manager.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from datetime import datetime, UTC
44
from app.managers.log_manager import LogManager, JsonFormatter, PlainTextFormatter
55
from app.models.schemas import LogRequest, LogFormat
6-
import os
76
import asyncio
87

98

@@ -202,14 +201,18 @@ async def test_automatic_logs_initialization(self, monkeypatch, caplog):
202201
manager = LogManager()
203202
# Wait a bit for the log to be created
204203
await asyncio.sleep(0.1)
205-
204+
206205
# Verify logs were created with correct configuration
207-
assert any("Test auto message" in record.message for record in caplog.records), \
208-
"Expected log message not found in records"
209-
assert all(record.levelname == "INFO" for record in caplog.records), \
210-
"Not all logs are at INFO level"
211-
assert all(hasattr(record, "service") and record.service == "test-service"
212-
for record in caplog.records), \
213-
"Service attribute missing or incorrect"
214-
assert isinstance(manager.logger.handlers[0].formatter, JsonFormatter), \
215-
"Incorrect formatter type"
206+
assert any(
207+
"Test auto message" in record.message for record in caplog.records
208+
), "Expected log message not found in records"
209+
assert all(
210+
record.levelname == "INFO" for record in caplog.records
211+
), "Not all logs are at INFO level"
212+
assert all(
213+
hasattr(record, "service") and record.service == "test-service"
214+
for record in caplog.records
215+
), "Service attribute missing or incorrect"
216+
assert isinstance(
217+
manager.logger.handlers[0].formatter, JsonFormatter
218+
), "Incorrect formatter type"

0 commit comments

Comments
 (0)