1use Test::More; # qw/no_plan/; 2use strict; 3 4use POE qw/Component::SNMP/; 5 6use lib qw(t); 7use TestPCS; 8 9my $CONF = do "config.cache"; 10 11if ( $CONF->{skip_all_tests} ) { 12 POE::Kernel->run(); 13 plan skip_all => 'No SNMP data specified.'; 14} else { 15 if (1) { 16 plan tests => 60; 17 } else { 18 $poe_kernel->run(); # quiets POE::Kernel warning 19 plan skip_all => 'not done yet'; 20 } 21} 22 23my %system = ( # sysUptime => '.1.3.6.1.2.1.1.3.0', 24 sysName => '.1.3.6.1.2.1.1.5.0', 25 # sysLocation => '.1.3.6.1.2.1.1.6.0', 26 ); 27 28my @oids = values %system; 29my $base_oid = '.1.3.6.1.2.1.1'; # system.* 30 31my $DEBUG_FLAG = 0x00; # none 32# my $DEBUG_FLAG = 0x08; # dispatcher 33# my $DEBUG_FLAG = 0x0B; # transport+dispatcher 34# my $DEBUG_FLAG = 0x1B; # transport+dispatcher+message processing 35# my $DEBUG_FLAG = 0xFF; # everything 36 37my $session2 = 1; 38 39sub snmp_run_tests { 40 my ($kernel, $heap) = @_[KERNEL, HEAP]; 41 42 # no warnings; 43 POE::Component::SNMP->create( 44 alias => 'snmp', 45 hostname => $CONF->{'hostname'}, 46 community => $CONF->{'community'}, 47 version => 'snmpv2c', 48 debug => $CONF->{debug}, 49 50 # timeout => 5, 51 52 ); 53 ok $kernel->alias_resolve( 'snmp' ), "1st session created"; 54 # use warnings; 55 56# # Failed test in t/70_multi.t at line 246. 57# Failed to bind UDP/IPv4 socket: Address already in use at t/70_multi.t line 60 58# # Looks like you planned 60 tests but only ran 32. 59# # Looks like you failed 24 tests of 32 run. 60# # Looks like your test died just after 32. 61# t/70_multi..............dubious 62# Test returned status 255 (wstat 65280, 0xff00) 63# DIED. FAILED tests 3-26, 33-60 64# Failed 52/60 tests, 13.33% okay 65 66 67 SKIP: { 68 skip "only testing with one for now", 1 unless $session2; 69 70 POE::Component::SNMP->create( 71 alias => 'snmp_2', 72 hostname => $CONF->{'hostname'}, 73 community => $CONF->{'community'}, 74 version => 'snmpv2c', 75 debug => $CONF->{debug}, 76 77 # timeout => 5, 78 ); 79 80 81 # ok $@, '-hostname parameter required'; 82 # this asserts that the alias does *not* exist 83 ok $kernel->alias_resolve( 'snmp_2' ), "2st session created"; 84 85 } 86 87 # this next batch of tests sends a certain number of requests from 88 # one session to one callback, to another session to the same 89 # callback, and then a mix. success is when the counts come out right. 90 91 # 'walk' takes longer to return than 'get'. So we do it first to 92 # arrange that the response to the second request, 'get', comes 93 # BEFORE the first request, 'walk'. 94 # $kernel->post( snmp => walk => walk_cb => -baseoid => $base_oid ); $heap->{pending}++; 95 if ($session2) { 96 $kernel->post( snmp_2 => get => get_cb2 => -varbindlist => \@oids ); $heap->{pending}{snmp_2}++; 97 get_sent($heap); 98 $kernel->post( snmp_2 => get => get_cb => -varbindlist => \@oids ); $heap->{pending}{snmp_2}++; 99 get_sent($heap); 100 101 $kernel->post( snmp_2 => getbulk => walk_cb => 102 -varbindlist => [ $base_oid ], -maxrepetitions => 8 ); $heap->{pending}{snmp_2}++; 103 set_sent($heap); 104 105 $kernel->post( snmp_2 => getbulk => walk_cb2 => 106 -varbindlist => [ $base_oid ], -maxrepetitions => 8 ); $heap->{pending}{snmp_2}++; 107 set_sent($heap); 108 109 } 110 111 112 $kernel->post( snmp => get => get_cb => -varbindlist => \@oids ); $heap->{pending}{snmp}++; 113 get_sent($heap); 114 $kernel->post( snmp => get => get_cb2 => -varbindlist => \@oids ); $heap->{pending}{snmp}++; 115 get_sent($heap); 116 117 $kernel->post( snmp => getbulk => walk_cb => 118 -varbindlist => [ $base_oid ], -maxrepetitions => 8 ); $heap->{pending}{snmp}++; 119 set_sent($heap); 120 121 $kernel->post( snmp => getbulk => walk_cb2 => 122 -varbindlist => [ $base_oid ], -maxrepetitions => 8 ); $heap->{pending}{snmp}++; 123 set_sent($heap); 124} 125 126sub get_cb { 127 my ($kernel, $heap, $request, $response) = @_[KERNEL, HEAP, ARG0, ARG1]; 128 my ($alias, $host, $cmd, @args) = @$request; 129 my ($results) = @$response; 130 131 ok get_seen($heap); 132 133 $heap->{$alias}{$cmd}++; 134 push @{$heap->{$alias}{log}}, $cmd; 135 136 ok $cmd eq 'get', "callback destination is preserved (get)"; 137 138 if (1) { 139 if (ref $results) { 140 ok ref $results eq 'HASH'; # no error 141 142 if (0) { 143 print "$host SNMP config ($cmd):\n"; 144 print "sysName: $results->{$system{sysName}}\n"; 145 print "sysUptime: $results->{$system{sysUptime}}\n"; 146 print "sysLocation: $results->{$system{sysLocation}}\n"; 147 } 148 } else { 149 print STDERR "$host SNMP error ($cmd => @args):\n$results\n"; 150 } 151 } 152 153 if (check_done_multi($heap, $alias)) { 154 $kernel->post( $alias => 'finish' ); 155 ok check_done_multi($heap, $alias); 156 } 157 158} 159 160sub get_cb2 { 161 my ($kernel, $heap, $request, $response) = @_[KERNEL, HEAP, ARG0, ARG1]; 162 my ($alias, $host, $cmd, @args) = @$request; 163 my ($results) = @$response; 164 165 ok get_seen($heap); 166 ok ref $results eq 'HASH'; # no error 167 168 $heap->{$alias}{$cmd}++; 169 push @{$heap->{$alias}{log}}, $cmd; 170 171 ok $cmd eq 'get', "callback destination is preserved (get)"; 172 173 if (check_done_multi($heap, $alias)) { 174 $kernel->post( $alias => 'finish' ); 175 ok check_done_multi($heap, $alias); 176 } 177 178} 179 180sub walk_cb { 181 my ($kernel, $heap, $request, $response) = @_[KERNEL, HEAP, ARG0, ARG1]; 182 my ($alias, $host, $cmd, @args) = @$request; 183 my ($results) = @$response; 184 185 ok set_seen($heap); 186 ok ref $results eq 'HASH'; # no error 187 188 $heap->{$alias}{$cmd}++; 189 push @{$heap->{$alias}{log}}, $cmd; 190 191 ok $cmd eq 'getbulk', "callback destination is preserved (getbulk)"; 192 193 # this is for testing aborts in mid-request 194 0 and $heap->{pending}{snmp_2} = 0 if $alias eq 'snmp_2'; 195 196 if (check_done_multi($heap, $alias)) { 197 $kernel->post( $alias => 'finish' ); 198 ok check_done_multi($heap, $alias); 199 } 200 201} 202 203sub walk_cb2 { 204 my ($kernel, $heap, $request, $response) = @_[KERNEL, HEAP, ARG0, ARG1]; 205 my ($alias, $host, $cmd, @args) = @$request; 206 my ($results) = @$response; 207 208 ok set_seen($heap); 209 ok ref $results eq 'HASH'; # no error 210 211 $heap->{$alias}{$cmd}++; 212 push @{$heap->{$alias}{log}}, $cmd; 213 214 ok $cmd eq 'getbulk', "callback destination is preserved (getbulk)"; 215 216 if (check_done_multi($heap, $alias)) { 217 $kernel->post( $alias => 'finish' ); 218 ok check_done_multi($heap, $alias); 219 } 220} 221 222sub stop_session { 223 my ($heap) = $_[HEAP]; 224 ok 1; # got here! 225 226 ok exists $heap->{pending}; 227 ok ref $heap->{pending} eq 'HASH'; 228 229 ok exists $heap->{pending}{snmp}; 230 ok exists $heap->{pending}{snmp_2}; 231 ok defined $heap->{pending}{snmp}; 232 ok defined $heap->{pending}{snmp_2}; 233 ok $heap->{pending}{snmp} == 4; 234 ok $heap->{pending}{snmp_2} == 4; 235 236 237 ok exists $heap->{snmp}; 238 ok ref $heap->{snmp} eq 'HASH'; 239 ok exists $heap->{snmp}{get}; 240 ok exists $heap->{snmp}{getbulk}; 241 ok defined $heap->{snmp}{get}; 242 ok defined $heap->{snmp}{getbulk}; 243 ok $heap->{snmp}{get} == 2; 244 ok $heap->{snmp}{getbulk} == 2; 245 246 ok exists $heap->{snmp_2}; 247 ok ref $heap->{snmp_2} eq 'HASH'; 248 ok exists $heap->{snmp_2}{get}; 249 ok exists $heap->{snmp_2}{getbulk}; 250 ok defined $heap->{snmp_2}{get}; 251 ok defined $heap->{snmp_2}{getbulk}; 252 ok $heap->{snmp_2}{get} == 2; 253 ok $heap->{snmp_2}{getbulk} == 2; 254 255 ok $heap->{snmp}{get} + $heap->{snmp}{getbulk} == $heap->{pending}{snmp}; 256 ok $heap->{snmp_2}{get} + $heap->{snmp_2}{getbulk} == $heap->{pending}{snmp}; 257 258 ok $heap->{snmp}{get} + $heap->{snmp_2}{get} == $heap->{get_seen}; 259 ok $heap->{snmp}{getbulk} + $heap->{snmp_2}{getbulk} == $heap->{set_seen}; 260 261 ok $heap->{snmp}{get} + $heap->{snmp_2}{get} == $heap->{get_sent}; 262 ok $heap->{snmp}{getbulk} + $heap->{snmp_2}{getbulk} == $heap->{set_sent}; 263 264} 265 266### declarations done. let's run it! 267 268POE::Session->create 269( inline_states => 270 { _start => \&snmp_run_tests, 271 _stop => \&stop_session, 272 get_cb => \&get_cb, 273 get_cb2 => \&get_cb2, 274 walk_cb => \&walk_cb, 275 walk_cb2 => \&walk_cb2, 276 }, 277); 278 279$poe_kernel->run; 280 281ok 1; # clean exit 282exit 0; 283 284sub check_done_multi { 285 my ($heap, $alias) = @_; 286 no warnings; 287 return $alias if $heap->{$alias}{get} + $heap->{$alias}{getbulk} == $heap->{pending}{$alias}; 288} 289