BGPmon-core-1

 view release on metacpan or  search on metacpan

t/08-bgpmon-fetchdata-file.t  view on Meta::CPAN


#Read the remainder of the messages
while( defined($msg) ){
    $msg = read_xml_message();
}
#There are 8 messages in this file, the connection should be closed, and
#I shouldn't be able to re-close the connection
is(messages_read(),8,"read_xml_message - read all messages");
ok(!is_connected(),"is_connected - not connected after EOF");
is(close_connection(),1,"close_connection - close after closed");

#Try to read from no connection
$msg = read_xml_message();
is(get_error_code("read_xml_message"),302,"read_xml_message - read without connection");

# read from a non-existant file
$ret = connect_file($location."updates.20120101.0000.xml.bz2");
is($ret,1,"connect_file - nonexistent file");
is(get_error_code("connect_file"),304,"connect_file - Nonexistent file");

=comment
# read from a file we don't have permissions for
$ret = connect_file("/etc/sudoers");
is($ret,1,"connect_file - no permission on file");
is(get_error_code("connect_file"),307,"connect_file - bad permissions");
=cut

# read from a wrong format file
$ret = connect_file($location."bgpmon-fetch-file-bgpdump-001");
is($ret,1,"connect_file - bgpdump format");
ok(!is_connected(),"connect_file - wrong format");

# start reading, delete file, continue reading
$ret = connect_file($location."bgpmon-fetch-file-valid.xml.gz");
ok( is_connected(),"confirm file connection");
$msg = read_xml_message();
$msg = read_xml_message();
`rm -f /tmp/BGP.File.$$/extract_bgp.$$`;
$msg = read_xml_message();
is($msg,undef,"read_xml_message - delete a file mid-read");
is(get_error_code("read_xml_message"),304,"read_xml_message - check error code on deleted file mid-read");

if( !(-e $location."/compress_test.gz") ){
	my $te = $location."compress_test.gz";
    `echo "This is an uncompressed file" > $te`;
}
# decompress an uncompressed file and try to connect
#This will fail because the scratch directory has not been initialized
$ret = BGPmon::Fetch::File::decompress_file($location."compress_test.gz");
isnt($ret,undef,"decompress_file - corrupt compressed file");
#Try actually connecting to the file this time
$ret = connect_file($location."compress_test.gz");
is($ret,1,"connect_file - connect to binary file");
ok(!is_connected(),"connect_file - not connected to binary file");

#Connect to a file, read a message, change the permissions, try to read again
#This should work because permissions only get checked once, not per-read
$ret = connect_file($location."bgpmon-fetch-file-valid.xml.gz");
ok( is_connected(),"confirm file connection");
$msg = read_xml_message();
`chmod a-r "/tmp/BGP.File.$$/extract_bgp.$$"`;
$msg = read_xml_message();
ok(defined($msg),"read_xml_message - chmod mid-read");
close_connection();

#Connect to a file, read a message, move the file, try to read again
$ret = connect_file($location."bgpmon-fetch-file-valid.xml.gz");
ok( is_connected(),"confirm file connection");
$msg = read_xml_message();
ok(defined($msg),"read_xml_message - read before move");
`mv "/tmp/BGP.File.$$/extract_bgp.$$" "/tmp/BGP.File.$$/extract_bgp.$$.bak"`;
#Now that we've moved the file that we were reading, the next read will fail
$msg = read_xml_message();
is($msg,undef,"read_xml_message - move mid-read");

#Connect to a file with a broken XML message
$ret = connect_file($location."bad_xml_test.bz2");
ok(is_connected(),"confirm file connection");
$msg = read_xml_message();
ok(!defined($msg),"read_xml_message - detect bad XML");
close_connection();

#Connect to a file with an extra ARCHIVER/START message in the middle
$ret = connect_file($location."bgpmon-translator-dup-archiver-msg.xml");
ok(is_connected(),"confirm file connection");
$msg = read_xml_message();
$msg = read_xml_message();
$msg = read_xml_message();
is(get_error_code("read_xml_message"),314,"read_xml_message - duplicate ARCHIVER/OPENED message");

#We are going to close this connection, reinitialize with flags set to ignore
#data errors, then test reading on previously-wrong files
$ret = close_connection();
$ret = init_bgpdata('ignore_data_errors' => 1,'ignore_incomplete_data' => 1);
is($ret,1,'init_bgpdata - set the flags w/ default directory');
$ret = connect_file($location."bgpmon-fetch-file-no-archiver.xml.bz2");
ok( is_connected() ,"connect_file - valid XML archive (compressed)");

#Try to read the first message out of the file
#This test should pass because we're ignoring the missing ARCHIVER messages
$msg = read_xml_message();
ok(defined($msg),"read_xml_message - ignore missing ARCHIVER tags");
ok(is_connected(), "read_xml_message - ignore missing ARCHIVER tags");
is(get_error_code("read_xml_message"),0,"read_xml_message - error code");
close_connection();

done_testing();
1;



( run in 0.875 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )