| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Test::AnsibleModule; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
94796
|
use Mojo::Base -base; |
|
|
3
|
|
|
|
|
30103
|
|
|
|
3
|
|
|
|
|
16
|
|
|
4
|
3
|
|
|
3
|
|
496
|
use Test::More; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
27
|
|
|
5
|
3
|
|
|
3
|
|
1457
|
use Mojo::JSON qw/decode_json encode_json/; |
|
|
3
|
|
|
|
|
301647
|
|
|
|
3
|
|
|
|
|
213
|
|
|
6
|
3
|
|
|
3
|
|
1007
|
use Mojo::Asset::File; |
|
|
3
|
|
|
|
|
61217
|
|
|
|
3
|
|
|
|
|
36
|
|
|
7
|
3
|
|
|
3
|
|
109
|
use Carp qw/croak/; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
237
|
|
|
8
|
3
|
|
|
3
|
|
19
|
use Data::Dumper qw/Dumper/; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
1290
|
|
|
9
|
|
|
|
|
|
|
$Data::Dumper::Sortkeys++; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'last_response'; |
|
12
|
|
|
|
|
|
|
has 'success'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub fail_ok { |
|
15
|
1
|
|
|
1
|
1
|
684
|
my $self = shift; |
|
16
|
1
|
|
|
|
|
15
|
my $rc = $self->exec_module(@_); |
|
17
|
1
|
|
|
|
|
198
|
$self->_test('ok', $rc, 'Returned non-zero return code'); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub is_response { |
|
21
|
3
|
|
|
3
|
1
|
2402
|
my $self = shift; |
|
22
|
3
|
|
|
|
|
10
|
my $res = shift; |
|
23
|
3
|
|
|
|
|
15
|
$self->_test('is', Dumper($self->last_response), Dumper($res), @_); |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub run_ok { |
|
27
|
4
|
|
|
4
|
1
|
2129
|
my $self = shift; |
|
28
|
4
|
|
|
|
|
18
|
my $rc = $self->exec_module(@_); |
|
29
|
|
|
|
|
|
|
$self->_test('ok', !$rc, |
|
30
|
4
|
|
|
|
|
722
|
'Response code is success (' . $self->last_response->{msg} . ')'); |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub exec_module { |
|
34
|
5
|
|
|
5
|
1
|
12
|
my $self = shift; |
|
35
|
5
|
|
|
|
|
10
|
my $module = shift; |
|
36
|
5
|
100
|
|
|
|
19
|
my $args = ref $_[0] ? $_[0] : {@_}; |
|
37
|
|
|
|
|
|
|
|
|
38
|
5
|
|
|
|
|
61
|
my $file = Mojo::Asset::File->new; |
|
39
|
5
|
|
|
|
|
47
|
$file->add_chunk(encode_json($args)); |
|
40
|
5
|
|
|
|
|
5437
|
my $p; |
|
41
|
|
|
|
|
|
|
|
|
42
|
5
|
|
50
|
|
|
64
|
open($p, "-|", join(" ", $module, $file->path)) |
|
43
|
|
|
|
|
|
|
// croak "Could not run module: $!"; |
|
44
|
5
|
|
|
|
|
12141
|
my $response = ""; |
|
45
|
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
807287
|
while (my $line = <$p>) { |
|
47
|
5
|
|
|
|
|
131
|
$response .= $line; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
5
|
|
|
|
|
111
|
my $res = decode_json($response); |
|
50
|
5
|
|
|
|
|
2020
|
$self->last_response($res); |
|
51
|
5
|
|
|
|
|
384
|
close $p; |
|
52
|
5
|
|
|
|
|
164
|
return $? >> 8; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _test { |
|
56
|
8
|
|
|
8
|
|
810
|
my ($self, $name, @args) = @_; |
|
57
|
8
|
|
|
|
|
48
|
local $Test::Builder::Level = $Test::Builder::Level + 2; |
|
58
|
8
|
|
|
|
|
218
|
return $self->success(!!Test::More->can($name)->(@args)); |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Test::AnsibleModule - Test your ansible modules. |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
use Test::AnsibleModule; |
|
71
|
|
|
|
|
|
|
my $t=Test::AnsibleModule->new(); |
|
72
|
|
|
|
|
|
|
$t->run_ok('modules/foobar'); |
|
73
|
|
|
|
|
|
|
is_deeploy($t->last_response,{ changed => 0 }); |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Test an Ansible module by running it and passing it input as JSON, and decoding the response. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 last_response |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
The deserialized response from the last module run. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 METHODS |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 run_ok [] |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Test that the job runs, and returns a 0 error code (succeeds). |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 fail_ok [] |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Test that the jobs runs, and returns a non-zero error code (fails). |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 is_response , [] |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Compare the last response to the provided struct. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 exec_module [] |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Run a module, return it's exit code |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
L |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |