Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/apps/nmap/cli/discovery.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
*** Settings ***
Documentation Test the Podman container-usage mode
Library Collections

Resource ${CURDIR}${/}..${/}..${/}..${/}resources/import.resource

Test Timeout 120s

*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}podman.json

${cmd} ${CENTREON_PLUGINS}
... --plugin=apps::nmap::cli::plugin
... --mode=discovery

*** Test Cases ***
Container usage ${tc}
[Documentation] Check nmap discovery
[Tags] apps nmap
Log To Console \n
${command} Catenate
... ${cmd}
... --subnet='127.0.0.1/32'
... ${extraoptions}

Ctn Run Command And Check Result As Json ${command} ${expected_result}

Examples: tc extraoptions expected_result --
... 1 ${EMPTY} {"end_time":1747232859,"discovered_items":1,"results":[{"hostname":"localhost","ip":"127.0.0.1","hostnames":[{"type":"PTR","name":"localhost"}],"vendor":null,"status":"up","addresses":[{"address":"127.0.0.1","type":"ipv4"}],"os_accuracy":null,"os":null,"services":null,"type":"unknown"}],"duration":0,"start_time":1747232859}
... 2 --nmap-options='-sS -sU -R -O --osscan-limit --osscan-guess -p U:161,162,T:21-25,80,139,443,3306,5985,5986,8080,8443 -oX - ' {"end_time":1747232859,"discovered_items":1,"results":[{"hostname":"localhost","ip":"127.0.0.1","hostnames":[{"type":"PTR","name":"localhost"}],"vendor":null,"status":"up","addresses":[{"address":"127.0.0.1","type":"ipv4"}],"os_accuracy":null,"os":null,"services":null,"type":"unknown"}],"duration":1,"start_time":1747232859}
... 3 --nmap-options='-sS -oX - ' {"results":[{"ip":"127.0.0.1","type":"unknown","os_accuracy":null,"status":"up","services":null,"hostnames":[{"type":"PTR","name":"localhost"}],"os":null,"hostname":"localhost","vendor":null,"addresses":[{"address":"127.0.0.1","type":"ipv4"}]}],"duration":0,"discovered_items":1,"start_time":1747234100,"end_time":1747234100}

*** Keywords ***
96 changes: 96 additions & 0 deletions tests/centreon/plugins/misc.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use strict;
use warnings;
use Test2::V0;
use Test2::Tools::Compare qw{is like match};
use FindBin;
use lib "$FindBin::RealBin/../../../src";
use centreon::plugins::misc;
use centreon::plugins::output;
use centreon::plugins::options;
# in real world one should use execute(), but as many options are not supported by windows_execute,
# the signature is not coherent, and we want to test everything on this unix_execute()
sub test_unix_execute {
my $mock_output = mock 'centreon::plugins::output'; # this is from Test2::Tools::Mock, included by Test2::V0

my $option = centreon::plugins::options->new();
my $output = centreon::plugins::output->new(options => $option);

my @tests = (
{
expect => '"string to" output "$( echo noworking)"',
msg => 'double quote stay when no interpretation',
args => {
command_path => "/bin",
command => 'echo',
command_options => '"string to" output "$( echo noworking)"',
no_shell_interpretation => 1,
}
},
{
expect => 'string to output noworking',
msg => 'double quote diseapear when interpretation is enabled',
args => {
command_path => "/bin",
command => 'echo',
command_options => '"string to" output "$( echo noworking)"',
}
},
{
expect => 'stringToOutput adding',
msg => 'interpretation by default active',
args => {
command => 'echo stringToOutput $(echo adding)',
}
},
{
expect => 'stringToOutput $(echo adding)',
msg => 'interpretation by default active',
args => {
command => 'echo stringToOutput $(echo adding)',
no_shell_interpretation => 1,
}
},
{
expect => '',
msg => "no error when no argument given to command without interpolation",
args => {
command => 'echo',
no_shell_interpretation => 1,
}
}

);
for my $test (@tests) {
my ($stdout, $exit_code) = centreon::plugins::misc::unix_execute(
output => $output,
options => { timeout => 10 },
no_quit => 1,
%{$test->{args}},
);
is($stdout, $test->{expect}, $test->{msg});
}

my ($stdout, $exit_code) = centreon::plugins::misc::unix_execute(
output => $output,
options => { timeout => 10 },
command => 'NoBinary',
command_output => '"string to" output "$( echo noworking)"',
no_shell_interpretation => 1,
no_quit => 1
);
like($stdout, qr/Can't exec "NoBinary": No such file or directory at.*/, 'no_quit option always return');
($stdout, $exit_code) = centreon::plugins::misc::unix_execute(
output => $output,
options => { timeout => 10 },
sudo => 1,
command => 'NoBinary',
command_output => '"string to" output "$( echo noworking)"',
no_shell_interpretation => 1,
no_quit => 1
);
like($stdout, qr/Can't exec "sudo": No such file or directory at.*/, 'sudo option add sudo binary before command');

}

test_unix_execute();
done_testing();
10 changes: 10 additions & 0 deletions tests/resources/resources.resource
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ Ctn Verify Command Output
... Wrong output result for command:\n${command}\n\nObtained:\n${output}\n\nExpected:\n${expected_result}\n
... values=False
... collapse_spaces=True

Ctn Run Command And Check Result As Json
[Arguments] ${command} ${expected}
Log To Console ${command}
${output} Run ${command}
${output} Strip String ${output}
${json_output}= evaluate json.loads('''${output}''') json
${json_expected}= evaluate json.loads('''${expected}''') json
Dictionaries Should Be Equal ${json_output} ${json_expected} ignore_keys=['end_time', 'start_time', 'duration']
Log Dictionary ${json_output}
Loading