| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test::AnsibleModule; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 49185 | use Mojo::Base -base; | 
|  | 3 |  |  |  |  | 33830 |  | 
|  | 3 |  |  |  |  | 26 |  | 
| 4 | 3 |  |  | 3 |  | 582 | use Test::More; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 26 |  | 
| 5 | 3 |  |  | 3 |  | 3001 | use Mojo::JSON qw/decode_json encode_json/; | 
|  | 3 |  |  |  |  | 241696 |  | 
|  | 3 |  |  |  |  | 264 |  | 
| 6 | 3 |  |  | 3 |  | 2755 | use Mojo::Asset::File; | 
|  | 3 |  |  |  |  | 44292 |  | 
|  | 3 |  |  |  |  | 38 |  | 
| 7 | 3 |  |  | 3 |  | 107 | use Carp qw/croak/; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 141 |  | 
| 8 | 3 |  |  | 3 |  | 19 | use Data::Dumper qw/Dumper/; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1706 |  | 
| 9 |  |  |  |  |  |  | $Data::Dumper::Sortkeys++; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has 'last_response'; | 
| 12 |  |  |  |  |  |  | has 'success'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub fail_ok { | 
| 15 | 1 |  |  | 1 | 1 | 797 | my $self = shift; | 
| 16 | 1 |  |  |  |  | 28 | my $rc   = $self->exec_module(@_); | 
| 17 | 1 |  |  |  |  | 250 | $self->_test('ok', $rc, 'Returned non-zero return code'); | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub is_response { | 
| 21 | 3 |  |  | 3 | 1 | 2017 | my $self = shift; | 
| 22 | 3 |  |  |  |  | 13 | 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 | 1573 | my $self = shift; | 
| 28 | 4 |  |  |  |  | 17 | my $rc   = $self->exec_module(@_); | 
| 29 |  |  |  |  |  |  | $self->_test('ok', !$rc, | 
| 30 | 4 |  |  |  |  | 1104 | 'Response code is success (' . $self->last_response->{msg} . ')'); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub exec_module { | 
| 34 | 5 |  |  | 5 | 1 | 11 | my $self   = shift; | 
| 35 | 5 |  |  |  |  | 12 | my $module = shift; | 
| 36 | 5 | 100 |  |  |  | 31 | my $args   = ref $_[0] ? $_[0] : {@_}; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 5 |  |  |  |  | 82 | my $file = Mojo::Asset::File->new; | 
| 39 | 5 |  |  |  |  | 51 | $file->add_chunk(encode_json($args)); | 
| 40 | 5 |  |  |  |  | 3050 | my $p; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 5 |  | 50 |  |  | 44 | open($p, "-|", join(" ", $module, $file->path)) | 
| 43 |  |  |  |  |  |  | // croak "Could not run module: $!"; | 
| 44 | 5 |  |  |  |  | 549936 | my $response = ""; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 5 |  |  |  |  | 662544 | while (my $line = <$p>) { | 
| 47 | 5 |  |  |  |  | 97 | $response .= $line; | 
| 48 |  |  |  |  |  |  | } | 
| 49 | 5 |  |  |  |  | 131 | my $res = decode_json($response); | 
| 50 | 5 |  |  |  |  | 1966 | $self->last_response($res); | 
| 51 | 5 |  |  |  |  | 522 | close $p; | 
| 52 | 5 |  |  |  |  | 206 | return $? >> 8; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _test { | 
| 56 | 8 |  |  | 8 |  | 940 | my ($self, $name, @args) = @_; | 
| 57 | 8 |  |  |  |  | 33 | local $Test::Builder::Level = $Test::Builder::Level + 2; | 
| 58 | 8 |  |  |  |  | 245 | 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 |