Skip to content

Commit 30af228

Browse files
committed
test(integration): add above/below threshold command tests
- Add system stats fixture (CPU, memory, disk usage) - Add 3 integration tests for above command (OK, WARNING, CRITICAL) - Add 3 integration tests for below command (OK, WARNING, CRITICAL) All threshold tests use realistic system metrics and validate end-to-end behavior with HTTP mock server. 16 integration tests total now.
1 parent 5753657 commit 30af228

2 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"errorcode": 0,
3+
"message": "Done",
4+
"severity": "NONE",
5+
"system": {
6+
"cpuusagepcnt": 15.5,
7+
"memoryusagepcnt": 45.2,
8+
"disk0perusage": 60.8,
9+
"disk1perusage": 25.3,
10+
"numcpus": 4,
11+
"mgmtcpuusagepcnt": 10.2,
12+
"pktcpuusagepcnt": 5.3,
13+
"rescpuusagepcnt": 0.0
14+
}
15+
}

tests/test_integration_with_mock_api.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,187 @@ def test_ntp_check_with_thresholds(self, mock_nitro_server):
237237
assert "WARNING" in result.message
238238

239239

240+
class TestAboveCommandWithMockAPI:
241+
"""Test above (threshold) command against mock API"""
242+
243+
def test_above_check_ok(self, mock_nitro_server):
244+
"""Test above check when value is below threshold (OK)"""
245+
with NITROClient(
246+
hostname=mock_nitro_server.host,
247+
port=mock_nitro_server.port,
248+
username="nsroot",
249+
password="nsroot",
250+
ssl=False,
251+
) as client:
252+
# system stats have cpuusagepcnt=15.5
253+
args = Namespace(
254+
command="above",
255+
objecttype="system",
256+
objectname="cpuusagepcnt", # Field name to check
257+
warning="50",
258+
critical="80",
259+
filter=None,
260+
limit=None,
261+
)
262+
263+
from check_netscaler.commands.threshold import ThresholdCommand
264+
265+
command = ThresholdCommand(client, args)
266+
result = command.execute()
267+
268+
assert result.status == STATE_OK
269+
assert "cpuusagepcnt" in result.message
270+
assert "15.5" in result.message
271+
272+
def test_above_check_warning(self, mock_nitro_server):
273+
"""Test above check when value exceeds warning threshold"""
274+
with NITROClient(
275+
hostname=mock_nitro_server.host,
276+
port=mock_nitro_server.port,
277+
username="nsroot",
278+
password="nsroot",
279+
ssl=False,
280+
) as client:
281+
# system stats have memoryusagepcnt=45.2
282+
args = Namespace(
283+
command="above",
284+
objecttype="system",
285+
objectname="memoryusagepcnt",
286+
warning="40", # 45.2 exceeds this
287+
critical="80",
288+
filter=None,
289+
limit=None,
290+
)
291+
292+
from check_netscaler.commands.threshold import ThresholdCommand
293+
294+
command = ThresholdCommand(client, args)
295+
result = command.execute()
296+
297+
assert result.status == STATE_WARNING
298+
assert "WARNING" in result.message
299+
assert "memoryusagepcnt" in result.message
300+
301+
def test_above_check_critical(self, mock_nitro_server):
302+
"""Test above check when value exceeds critical threshold"""
303+
with NITROClient(
304+
hostname=mock_nitro_server.host,
305+
port=mock_nitro_server.port,
306+
username="nsroot",
307+
password="nsroot",
308+
ssl=False,
309+
) as client:
310+
# system stats have disk0perusage=60.8
311+
args = Namespace(
312+
command="above",
313+
objecttype="system",
314+
objectname="disk0perusage",
315+
warning="40",
316+
critical="59", # 60.8 exceeds this
317+
filter=None,
318+
limit=None,
319+
)
320+
321+
from check_netscaler.commands.threshold import ThresholdCommand
322+
323+
command = ThresholdCommand(client, args)
324+
result = command.execute()
325+
326+
assert result.status == STATE_CRITICAL
327+
assert "CRITICAL" in result.message
328+
assert "disk0perusage" in result.message
329+
330+
331+
class TestBelowCommandWithMockAPI:
332+
"""Test below (threshold) command against mock API"""
333+
334+
def test_below_check_ok(self, mock_nitro_server):
335+
"""Test below check when value is above threshold (OK)"""
336+
with NITROClient(
337+
hostname=mock_nitro_server.host,
338+
port=mock_nitro_server.port,
339+
username="nsroot",
340+
password="nsroot",
341+
ssl=False,
342+
) as client:
343+
# system stats have disk1perusage=25.3
344+
args = Namespace(
345+
command="below",
346+
objecttype="system",
347+
objectname="disk1perusage",
348+
warning="20", # 25.3 is above this - OK
349+
critical="10",
350+
filter=None,
351+
limit=None,
352+
)
353+
354+
from check_netscaler.commands.threshold import ThresholdCommand
355+
356+
command = ThresholdCommand(client, args)
357+
result = command.execute()
358+
359+
assert result.status == STATE_OK
360+
assert "disk1perusage" in result.message
361+
362+
def test_below_check_warning(self, mock_nitro_server):
363+
"""Test below check when value falls below warning threshold"""
364+
with NITROClient(
365+
hostname=mock_nitro_server.host,
366+
port=mock_nitro_server.port,
367+
username="nsroot",
368+
password="nsroot",
369+
ssl=False,
370+
) as client:
371+
# system stats have disk1perusage=25.3
372+
args = Namespace(
373+
command="below",
374+
objecttype="system",
375+
objectname="disk1perusage",
376+
warning="30", # 25.3 is below this - WARNING
377+
critical="10",
378+
filter=None,
379+
limit=None,
380+
)
381+
382+
from check_netscaler.commands.threshold import ThresholdCommand
383+
384+
command = ThresholdCommand(client, args)
385+
result = command.execute()
386+
387+
assert result.status == STATE_WARNING
388+
assert "WARNING" in result.message
389+
assert "disk1perusage" in result.message
390+
391+
def test_below_check_critical(self, mock_nitro_server):
392+
"""Test below check when value falls below critical threshold"""
393+
with NITROClient(
394+
hostname=mock_nitro_server.host,
395+
port=mock_nitro_server.port,
396+
username="nsroot",
397+
password="nsroot",
398+
ssl=False,
399+
) as client:
400+
# system stats have disk1perusage=25.3
401+
args = Namespace(
402+
command="below",
403+
objecttype="system",
404+
objectname="disk1perusage",
405+
warning="30",
406+
critical="26", # 25.3 is below this - CRITICAL
407+
filter=None,
408+
limit=None,
409+
)
410+
411+
from check_netscaler.commands.threshold import ThresholdCommand
412+
413+
command = ThresholdCommand(client, args)
414+
result = command.execute()
415+
416+
assert result.status == STATE_CRITICAL
417+
assert "CRITICAL" in result.message
418+
assert "disk1perusage" in result.message
419+
420+
240421
class TestLicenseCommandWithMockAPI:
241422
"""Test license command against mock API"""
242423

0 commit comments

Comments
 (0)