| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Test::AnsibleModule; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
46468
|
use Mojo::Base -base; |
|
|
3
|
|
|
|
|
38093
|
|
|
|
3
|
|
|
|
|
27
|
|
|
4
|
3
|
|
|
3
|
|
643
|
use Test::More; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
31
|
|
|
5
|
3
|
|
|
3
|
|
3657
|
use Mojo::JSON qw/decode_json encode_json/; |
|
|
3
|
|
|
|
|
222967
|
|
|
|
3
|
|
|
|
|
276
|
|
|
6
|
3
|
|
|
3
|
|
2812
|
use Mojo::Asset::File; |
|
|
3
|
|
|
|
|
46414
|
|
|
|
3
|
|
|
|
|
41
|
|
|
7
|
3
|
|
|
3
|
|
117
|
use Carp qw/croak/; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
156
|
|
|
8
|
3
|
|
|
3
|
|
19
|
use Data::Dumper qw/Dumper/; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
1645
|
|
|
9
|
|
|
|
|
|
|
$Data::Dumper::Sortkeys++; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'last_response'; |
|
12
|
|
|
|
|
|
|
has 'success'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub fail_ok { |
|
15
|
1
|
|
|
1
|
1
|
540
|
my $self = shift; |
|
16
|
1
|
|
|
|
|
11
|
my $rc = $self->exec_module(@_); |
|
17
|
1
|
|
|
|
|
400
|
$self->_test('ok', $rc, 'Returned non-zero return code'); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub is_response { |
|
21
|
3
|
|
|
3
|
1
|
3742
|
my $self = shift; |
|
22
|
3
|
|
|
|
|
10
|
my $res = shift; |
|
23
|
3
|
|
|
|
|
21
|
$self->_test('is', Dumper($self->last_response), Dumper($res), @_); |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub run_ok { |
|
27
|
4
|
|
|
4
|
1
|
2566
|
my $self = shift; |
|
28
|
4
|
|
|
|
|
17
|
my $rc = $self->exec_module(@_); |
|
29
|
|
|
|
|
|
|
$self->_test('ok', !$rc, |
|
30
|
4
|
|
|
|
|
1554
|
'Response code is success (' . $self->last_response->{msg} . ')'); |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub exec_module { |
|
34
|
5
|
|
|
5
|
1
|
12
|
my $self = shift; |
|
35
|
5
|
|
|
|
|
15
|
my $module = shift; |
|
36
|
5
|
100
|
|
|
|
32
|
my $args = ref $_[0] ? $_[0] : {@_}; |
|
37
|
|
|
|
|
|
|
|
|
38
|
5
|
|
|
|
|
73
|
my $file = Mojo::Asset::File->new; |
|
39
|
5
|
|
|
|
|
51
|
$file->add_chunk(encode_json($args)); |
|
40
|
5
|
|
|
|
|
3263
|
my $p; |
|
41
|
|
|
|
|
|
|
|
|
42
|
5
|
|
50
|
|
|
49
|
open($p, "-|", join(" ", $module, $file->path)) |
|
43
|
|
|
|
|
|
|
// croak "Could not run module: $!"; |
|
44
|
5
|
|
|
|
|
38461
|
my $response = ""; |
|
45
|
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
650624
|
while (my $line = <$p>) { |
|
47
|
5
|
|
|
|
|
163
|
$response .= $line; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
5
|
|
|
|
|
169
|
my $res = decode_json($response); |
|
50
|
5
|
|
|
|
|
2762
|
$self->last_response($res); |
|
51
|
5
|
|
|
|
|
581
|
close $p; |
|
52
|
5
|
|
|
|
|
278
|
return $? >> 8; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _test { |
|
56
|
8
|
|
|
8
|
|
1168
|
my ($self, $name, @args) = @_; |
|
57
|
8
|
|
|
|
|
53
|
local $Test::Builder::Level = $Test::Builder::Level + 2; |
|
58
|
8
|
|
|
|
|
394
|
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 |