summaryrefslogtreecommitdiffstats
path: root/plugins/t/check_swap.t
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-19 12:07:01 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-19 12:07:01 +0100
commitec18b80cdbdc2c4c1e7eb587e251177e8cc7ca11 (patch)
tree4ee0acfbdec6452879a3c01fcb50da194bdab887 /plugins/t/check_swap.t
parentd27e0862a9e514149d81622a82622d31ab3bee9b (diff)
downloadmonitoring-plugins-ec18b80cdbdc2c4c1e7eb587e251177e8cc7ca11.tar.gz
Add tests for check_swap with JSON output
Diffstat (limited to 'plugins/t/check_swap.t')
-rw-r--r--plugins/t/check_swap.t50
1 files changed, 49 insertions, 1 deletions
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t
index 6ef2323d..93e481c3 100644
--- a/plugins/t/check_swap.t
+++ b/plugins/t/check_swap.t
@@ -6,8 +6,9 @@
6 6
7use strict; 7use strict;
8use warnings; 8use warnings;
9use Test::More tests => 14; 9use Test::More tests => 35;
10use NPTest; 10use NPTest;
11use JSON;
11 12
12my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; 13my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/';
13my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; 14my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/';
@@ -43,3 +44,50 @@ like( $result->output, $warnOutput, "Right output" );
43$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) 44$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical)
44cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); 45cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' );
45like( $result->output, $failureOutput, "Right output" ); 46like( $result->output, $failureOutput, "Right output" );
47
48
49$outputFormat = '--output-format mp-test-json';
50my $output;
51my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/';
52
53$result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK
54cmp_ok( $result->return_code, "==", 0, "Always OK" );
55$output = decode_json($result->output);
56is($output->{'state'}, "OK", "State was correct");
57like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
58
59$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free
60cmp_ok( $result->return_code, "==", 0, "Always OK" );
61$output = decode_json($result->output);
62is($output->{'state'}, "OK", "State was correct");
63like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
64
65$result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free
66cmp_ok( $result->return_code, "==", 0, "Always OK" );
67$output = decode_json($result->output);
68is($output->{'state'}, "OK", "State was correct");
69like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
70
71$result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical)
72cmp_ok( $result->return_code, "==", 0, "Always OK" );
73$output = decode_json($result->output);
74is($output->{'state'}, "CRITICAL", "State was correct");
75like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
76
77$result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn)
78cmp_ok( $result->return_code, "==", 0, "Always OK" );
79$output = decode_json($result->output);
80is($output->{'state'}, "WARNING", "State was correct");
81like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
82
83$result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn)
84cmp_ok( $result->return_code, "==", 0, "Always OK" );
85$output = decode_json($result->output);
86is($output->{'state'}, "WARNING", "State was correct");
87like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");
88
89$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical)
90cmp_ok( $result->return_code, "==", 0, "Always OK" );
91$output = decode_json($result->output);
92is($output->{'state'}, "CRITICAL", "State was correct");
93like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct");