| 1 | package Test::Instance::DNS; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use IPC::System::Simple qw/ system /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Net::EmptyPort qw/ empty_port /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | our $VERSION = '0.001'; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Test::Instance::DNS - Mock DNS server for testing | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | use Test::More; | ||||||||||
| 17 | use Test::DNS; | ||||||||||
| 18 | use Test::Instance::DNS; | ||||||||||
| 19 | |||||||||||
| 20 | my $t_i_dns = Test::Instance::DNS->new( | ||||||||||
| 21 | listen_addr => '127.0.0.1', | ||||||||||
| 22 | zone_file => 't/etc/db.example.com', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | $t_i_dns->run; | ||||||||||
| 26 | |||||||||||
| 27 | my $dns = Test::DNS->new(nameservers => ['127.0.0.1']); | ||||||||||
| 28 | $dns->object->port($t_i_dns->listen_port); | ||||||||||
| 29 | |||||||||||
| 30 | $dns->is_a('example.com' => '192.0.2.1'); | ||||||||||
| 31 | |||||||||||
| 32 | done_testing; | ||||||||||
| 33 | |||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||
| 35 | |||||||||||
| 36 | Provides a local mock DNS server usable for testing. | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | has listen_port => ( | ||||||||||
| 41 | is => 'lazy', | ||||||||||
| 42 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | return empty_port; | |||||||||
| 44 | }, | ||||||||||
| 45 | 1 | ); | |||||||||
| 46 | |||||||||||
| 47 | has listen_addr => ( | ||||||||||
| 48 | is => 'lazy', | ||||||||||
| 49 | 1 | ▶ | coerce => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||
| 50 | 1 | ▶ | ref( $_[0] ) eq 'ARRAY' ? $_[0] : [ $_[0] ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | }, | ||||||||||
| 52 | 0 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 53 | 0 | return ['::1', '127.0.0.1' ], | |||||||||
| 54 | }, | ||||||||||
| 55 | 1 | ); | |||||||||
| 56 | |||||||||||
| 57 | 1 | has zone_file => ( | |||||||||
| 58 | is => 'ro', | ||||||||||
| 59 | required => 1, | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | has nameserver => ( | ||||||||||
| 63 | is => 'lazy', | ||||||||||
| 64 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||
| 65 | 1 | my $self = shift; | |||||||||
| 66 | 1 | my $module = __PACKAGE__ . '::Server'; | |||||||||
| 67 | 1 | s/::/\//g, s/$/.pm/ for $module; | |||||||||
| 68 | 1 | ▶ | if ( require $module ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | return $INC{$module}; | |||||||||
| 70 | } | ||||||||||
| 71 | 0 | die "Couldnt find $module"; | |||||||||
| 72 | }, | ||||||||||
| 73 | 1 | ); | |||||||||
| 74 | |||||||||||
| 75 | 1 | has pid => ( is => 'rwp' ); | |||||||||
| 76 | |||||||||||
| 77 | has _temp_dir => ( | ||||||||||
| 78 | is => 'lazy', | ||||||||||
| 79 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build__temp_dir: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 1 | return File::Temp->newdir; | |||||||||
| 81 | }, | ||||||||||
| 82 | 1 | ); | |||||||||
| 83 | |||||||||||
| 84 | has pid_file_path => ( | ||||||||||
| 85 | is => 'lazy', | ||||||||||
| 86 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build_pid_file_path: called CC=1 SCAR=0.0
| |||||||||||
| 87 | 1 | my $self = shift; | |||||||||
| 88 | 1 | return File::Spec->catfile( $self->_temp_dir->dirname, 'server.pid' ); | |||||||||
| 89 | }, | ||||||||||
| 90 | 1 | ); | |||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | sub _nameserver_cmd { | ||||||||
|
Subroutine
_nameserver_cmd: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 1 | my $self = shift; | |||||||||
| 94 | |||||||||||
| 95 | 1 | return join ( ' ', | |||||||||
| 96 | 'perl', $self->nameserver, | ||||||||||
| 97 | 'run', | ||||||||||
| 98 | '--listen_port', $self->listen_port, | ||||||||||
| 99 | '--zone', $self->zone_file, | ||||||||||
| 100 | '--pid', $self->pid_file_path, | ||||||||||
| 101 | '&', | ||||||||||
| 102 | ); | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 106 | 1 | my $self = shift; | |||||||||
| 107 | |||||||||||
| 108 | 1 | system( $self->_nameserver_cmd ); | |||||||||
| 109 | |||||||||||
| 110 | 1 | for (1 .. 10) { | |||||||||
| 111 | 2 | $self->_set_pid( $self->get_pid ); | |||||||||
| 112 | 2 | ▶ | last if defined $self->pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 1 | sleep 1; | |||||||||
| 114 | } | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 2 | ▶ | sub get_pid { | ||||||||
|
Subroutine
get_pid: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 118 | 2 | my $self = shift; | |||||||||
| 119 | |||||||||||
| 120 | 2 | my $pid = undef; | |||||||||
| 121 | 2 | ▶ | if ( -f $self->pid_file_path ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 1 | open( my $fh, '<', $self->pid_file_path ); | |||||||||
| 123 | 1 | $pid = <$fh>; # read first line | |||||||||
| 124 | 1 | chomp $pid; | |||||||||
| 125 | 1 | close $fh; | |||||||||
| 126 | } | ||||||||||
| 127 | 2 | return $pid; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 1 | ▶ | sub DEMOLISH { | ||||||||
|
Subroutine
DEMOLISH: called CC=6 SCAR=19.2
Pod
undocumented
| |||||||||||
| 131 | 1 | my $self = shift; | |||||||||
| 132 | |||||||||||
| 133 | 1 | ▶ | if ( my $pid = $self->pid ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | # print "Killing nameserver with pid " . $pid . "\n"; | ||||||||||
| 135 | 1 | for my $signal ( qw/ TERM TERM INT KILL / ) { | |||||||||
| 136 | 1 | $self->_kill_pid($signal); | |||||||||
| 137 | 1 | for ( 1..10 ) { | |||||||||
| 138 | 1 | ▶ | last unless $self->_kill_pid( 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | sleep 1; | |||||||||
| 140 | } | ||||||||||
| 141 | 1 | ▶ | last unless $self->_kill_pid( 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 3 | ▶ | sub _kill_pid { | ||||||||
|
Subroutine
_kill_pid: called CC=2 SCAR=7.0
| |||||||||||
| 147 | 3 | my ( $self, $signal ) = @_; | |||||||||
| 148 | |||||||||||
| 149 | #print "Signal [" . $signal . "]\n"; | ||||||||||
| 150 | #print "Pid [" . $self->pid . "]\n"; | ||||||||||
| 151 | 3 | ▶ | return unless $self->pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 3 | my $ret = kill $signal, $self->pid; | |||||||||
| 153 | #print "Kill Return code: [" . $ret . "]\n"; | ||||||||||
| 154 | 3 | return $ret; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 1 | 1; | |||||||||
| 158 | |||||||||||
| 159 | =head1 AUTHOR | ||||||||||
| 160 | |||||||||||
| 161 | Tom Bloor E<lt>t.bloor@shadowcat.co.ukE<gt> | ||||||||||
| 162 | |||||||||||
| 163 | =head1 COPYRIGHT | ||||||||||
| 164 | |||||||||||
| 165 | Copyright 2018- Tom Bloor | ||||||||||
| 166 | |||||||||||
| 167 | =head1 LICENSE | ||||||||||
| 168 | |||||||||||
| 169 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 170 | it under the same terms as Perl itself. | ||||||||||
| 171 | |||||||||||
| 172 | =head1 SEE ALSO | ||||||||||
| 173 | |||||||||||
| 174 | L<Test::DNS> L<Net::DNS> | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||