* Added the AN/test.conf file needed for Tools.t and added testing for the new Get, Log and Validate methods (save for Get->switches or Log->entry as I am not sure how to test them yet).

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent 0ee84cd31e
commit e49d5285c0
  1. 123
      AN/Tools.t
  2. 2
      AN/Tools/Storage.pm
  3. 7
      AN/test.conf
  4. 3
      cgi-bin/home

@ -10,7 +10,7 @@ use utf8;
our $VERSION="3.0.0"; our $VERSION="3.0.0";
# Call in the test module, telling it how many tests to expect to run. # Call in the test module, telling it how many tests to expect to run.
use Test::More tests => 39; use Test::More tests => 62;
# Load my module via 'use_ok' test. # Load my module via 'use_ok' test.
BEGIN BEGIN
@ -22,49 +22,49 @@ BEGIN
### Core tests ### Core tests
my $an = AN::Tools->new(); my $an = AN::Tools->new();
like($an, qr/^AN::Tools=HASH\(0x\w+\)$/, "Verifying that AN::Tools object is valid."); like($an, qr/^AN::Tools=HASH\(0x\w+\)$/, "Verifying that AN::Tools object is valid.");
like($an->data, qr/^HASH\(0x\w+\)$/, "Verifying that '\$an->data' is a hash reference."); like($an->data, qr/^HASH\(0x\w+\)$/, "Verifying that 'data' is a hash reference.");
is($an->environment, "cli", "Verifying that \$an->environment initially reports 'cli'."); is($an->environment, "cli", "Verifying that environment initially reports 'cli'.");
$an->environment('html'); $an->environment('html');
is($an->environment, "html", "Verifying that \$an->environment was properly set to 'html'."); is($an->environment, "html", "Verifying that environment was properly set to 'html'.");
$an->environment('cli'); $an->environment('cli');
is($an->environment, "cli", "Verifying that \$an->environment was properly reset back to 'cli'."); is($an->environment, "cli", "Verifying that environment was properly reset back to 'cli'.");
# Test handles to child modules. # Test handles to child modules.
like($an->Alert, qr/^AN::Tools::Alert=HASH\(0x\w+\)$/, "Verifying that '\$an->Alert' is a handle to AN::Tools::Alert."); like($an->Alert, qr/^AN::Tools::Alert=HASH\(0x\w+\)$/, "Verifying that 'Alert' is a handle to AN::Tools::Alert.");
like($an->Storage, qr/^AN::Tools::Storage=HASH\(0x\w+\)$/, "Verifying that '\$an->Alert' is a handle to AN::Tools::Storage."); like($an->Storage, qr/^AN::Tools::Storage=HASH\(0x\w+\)$/, "Verifying that 'Alert' is a handle to AN::Tools::Storage.");
like($an->Words, qr/^AN::Tools::Words=HASH\(0x\w+\)$/, "Verifying that '\$an->Alert' is a handle to AN::Tools::Words."); like($an->Words, qr/^AN::Tools::Words=HASH\(0x\w+\)$/, "Verifying that 'Alert' is a handle to AN::Tools::Words.");
### AN::Tools::Storage methods ### AN::Tools::Storage methods
# Search directory tests # Search directory tests
my $array1 = $an->Storage->search_directories; my $array1 = $an->Storage->search_directories;
my $a1_count = @{$array1}; my $a1_count = @{$array1};
cmp_ok($a1_count, '>', 0, "Verifying that \$an->Storage->search_directories has at least one entry. Found: [$a1_count] directories."); cmp_ok($a1_count, '>', 0, "Verifying that Storage->search_directories has at least one entry. Found: [$a1_count] directories.");
$an->Storage->search_directories({directories => "/root,/usr/bin,/some/fake/directory"}); $an->Storage->search_directories({directories => "/root,/usr/bin,/some/fake/directory"});
my $array2 = $an->Storage->search_directories; my $array2 = $an->Storage->search_directories;
my $a2_count = @{$array2}; my $a2_count = @{$array2};
cmp_ok($a2_count, '==', 2, "Verifying that \$an->Storage->search_directories now has 2 entries from a passed in CSV, testing that the list changed and a fake directory was dropped."); cmp_ok($a2_count, '==', 2, "Verifying that Storage->search_directories now has 2 entries from a passed in CSV, testing that the list changed and a fake directory was dropped.");
$an->Storage->search_directories({directories => ["/usr/bin", "/tmp", "/home"] }); $an->Storage->search_directories({directories => ["/usr/bin", "/tmp", "/home"] });
my $array3 = $an->Storage->search_directories; my $array3 = $an->Storage->search_directories;
my $a3_count = @{$array3}; my $a3_count = @{$array3};
cmp_ok($a3_count, '==', 3, "Verifying that \$an->Storage->search_directories now has 3 entries from a passed in array reference, verifying that the list changed again."); cmp_ok($a3_count, '==', 3, "Verifying that Storage->search_directories now has 3 entries from a passed in array reference, verifying that the list changed again.");
$an->Storage->search_directories({directories => "invalid" }); $an->Storage->search_directories({directories => "invalid" });
my $array4 = $an->Storage->search_directories; my $array4 = $an->Storage->search_directories;
my $a4_count = @{$array4}; my $a4_count = @{$array4};
cmp_ok($a4_count, '==', $a1_count, "Verifying that \$an->Storage->search_directories has the original number of directories: [$a4_count] after being called with an invalid 'directories' parameter, showing that it reset properly."); cmp_ok($a4_count, '==', $a1_count, "Verifying that Storage->search_directories has the original number of directories: [$a4_count] after being called with an invalid 'directories' parameter, showing that it reset properly.");
my $test_path = $an->Storage->find({ file => "AN/Tools.t" }); my $test_path = $an->Storage->find({ file => "AN/Tools.t" });
is($test_path, "/usr/share/perl5/AN/Tools.t", "Verifying that \$an->Storage->find successfully found 'AN/Tools.t'."); is($test_path, "/usr/share/perl5/AN/Tools.t", "Verifying that Storage->find successfully found 'AN/Tools.t'.");
my $bad_path = $an->Storage->find({ file => "AN/wa.t" }); my $bad_path = $an->Storage->find({ file => "AN/wa.t" });
is($bad_path, "#!not_found!#", "Verifying that \$an->Storage->find properly returned '#!not_found!#' for a non-existed file."); is($bad_path, "#!not_found!#", "Verifying that Storage->find properly returned '#!not_found!#' for a non-existed file.");
# Config file read tests. # Config file read tests.
$an->data->{foo}{bar}{a} = "test"; $an->data->{foo}{bar}{a} = "test";
is($an->Storage->read_config({ file => "AN/test.conf" }), 0, "Verifying that '\$an->Storage->read_config' successfully found 'AN/test.conf'."); is($an->Storage->read_config({ file => "AN/test.conf" }), 0, "Verifying that 'Storage->read_config' successfully found 'AN/test.conf'.");
is($an->Storage->read_config({ file => "" }), 1, "Verifying that '\$an->Storage->read_config' returns '1' when called without a 'file' parameter being set."); is($an->Storage->read_config({ file => "" }), 1, "Verifying that 'Storage->read_config' returns '1' when called without a 'file' parameter being set.");
is($an->Storage->read_config({ file => "AN/moo.conf" }), 2, "Verifying that '\$an->Storage->read_config' returns '2' when the non-existent 'AN/moo.conf' is passed."); is($an->Storage->read_config({ file => "AN/moo.conf" }), 2, "Verifying that 'Storage->read_config' returns '2' when the non-existent 'AN/moo.conf' is passed.");
cmp_ok($an->data->{foo}{bar}{a}, 'eq', 'I am "a"', "Verifying that 'AN/test.conf's 'foo::bar::a' overwrote an earlier set value."); cmp_ok($an->data->{foo}{bar}{a}, 'eq', 'I am "a"', "Verifying that 'AN/test.conf's 'foo::bar::a' overwrote an earlier set value.");
cmp_ok($an->data->{foo}{bar}{b}, 'eq', 'I am "b", split with tabs and having trailing spaces.', "Verifying that 'AN/test.conf's 'foo::bar::b' has whitespaces removed as expected."); cmp_ok($an->data->{foo}{bar}{b}, 'eq', 'I am "b", split with tabs and having trailing spaces.', "Verifying that 'AN/test.conf's 'foo::bar::b' has whitespaces removed as expected.");
cmp_ok($an->data->{foo}{baz}{1}, 'eq', 'This is \'1\' with no spaces', "Verifying that 'AN/test.conf's 'foo::baz::1' parsed without spaces around '='."); cmp_ok($an->data->{foo}{baz}{1}, 'eq', 'This is \'1\' with no spaces', "Verifying that 'AN/test.conf's 'foo::baz::1' parsed without spaces around '='.");
@ -72,15 +72,15 @@ cmp_ok($an->data->{foo}{baz}{2}, 'eq', 'I had a $dollar = sign and split with ta
### AN::Tools::Words methods ### AN::Tools::Words methods
# Make sure we can read words files # Make sure we can read words files
is($an->Words->read({file => $an->data->{path}{words}{'an-tools.xml'}}), 0, "Verifying that '\$an->Words->read' properly returned '0' when asked to read the AN::Tools's words file."); is($an->Words->read({file => $an->data->{path}{words}{'an-tools.xml'}}), 0, "Verifying that 'Words->read' properly returned '0' when asked to read the AN::Tools's words file.");
is($an->Words->read({file => ''}), 1, "Verifying that '\$an->Words->read' properly returned '1' when asked to read a works file without a file being passed."); is($an->Words->read({file => ''}), 1, "Verifying that 'Words->read' properly returned '1' when asked to read a works file without a file being passed.");
is($an->Words->read({file => '/tmp/dummy.xml'}), 2, "Verifying that '\$an->Words->read' properly returned '2' when asked to read a non-existent file."); is($an->Words->read({file => '/tmp/dummy.xml'}), 2, "Verifying that 'Words->read' properly returned '2' when asked to read a non-existent file.");
### NOTE: At this time, we don't test for unreadable files (rc = 3) or general read faults as set by XML::Simple (rc = 4). ### NOTE: At this time, we don't test for unreadable files (rc = 3) or general read faults as set by XML::Simple (rc = 4).
# Make sure we can read raw strings. # Make sure we can read raw strings.
is($an->Words->key({key => 't_0001'}), "Test replace: [#!variable!test!#].", "Verifying that '\$an->Words->key' returned the Canadian English 't_0001' string."); is($an->Words->key({key => 't_0001'}), "Test replace: [#!variable!test!#].", "Verifying that 'Words->key' returned the Canadian English 't_0001' string.");
is($an->Words->key({key => 't_0001', language => 'jp'}), "テスト いれかえる: [#!variable!test!#]。", "Verifying that '\$an->Words->read' returned the Japanese 't_0001' string."); is($an->Words->key({key => 't_0001', language => 'jp'}), "テスト いれかえる: [#!variable!test!#]。", "Verifying that 'Words->read' returned the Japanese 't_0001' string.");
is($an->Words->key({key => 't_0003', language => 'jp'}), "#!not_found!#", "Verifying that '\$an->Words->read' returned '#!not_found!#' for the missing 't_0003' key."); is($an->Words->key({key => 't_0003', language => 'jp'}), "#!not_found!#", "Verifying that 'Words->read' returned '#!not_found!#' for the missing 't_0003' key.");
# Make sure we can read processed strings. # Make sure we can read processed strings.
is($an->Words->string({ is($an->Words->string({
@ -130,14 +130,69 @@ like($an->Get->date_and_time({file_name => 1, time_only => 1}), qr/^\d\d-\d\d-\d
like($an->Get->date_and_time({use_time => 1234567890}), qr/2009\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime returned the expected date."); like($an->Get->date_and_time({use_time => 1234567890}), qr/2009\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime returned the expected date.");
like($an->Get->date_and_time({use_time => 1234567890, offset => 31536000}), qr/2010\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime with a one year in the future offset returned the expected date."); like($an->Get->date_and_time({use_time => 1234567890, offset => 31536000}), qr/2010\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime with a one year in the future offset returned the expected date.");
like($an->Get->date_and_time({use_time => 1234567890, offset => -31536000}), qr/2008\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime with a one year in the past offset returned the expected date."); like($an->Get->date_and_time({use_time => 1234567890, offset => -31536000}), qr/2008\/02\/1[34] \d\d:\d\d:\d\d$/, "Verified that a specific unixtime with a one year in the past offset returned the expected date.");
### TODO: How to test Get->switches?
# my $time = time; like($an->Get->host_uuid, qr/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/, "Verifying ability to read host uuid.");
# print "Current time: .............. [".$an->Get->date_and_time({})."]\n";
# print "Current time only: ......... [".$an->Get->date_and_time({time_only => 1})."]\n"; ### Logging
# print "Current date only: ......... [".$an->Get->date_and_time({date_only => 1})."]\n"; # TODO: How to check logs in journalctl?
# print "Current file time: ......... [".$an->Get->date_and_time({file_name => 1})."]\n"; is($an->Log->level, "1", "Verifying the default log level is '1'.");
# print "Current file time only: .... [".$an->Get->date_and_time({file_name => 1, time_only => 1})."]\n"; $an->Log->level(0);
# print "Current file date only: .... [".$an->Get->date_and_time({file_name => 1, date_only => 1})."]\n"; is($an->Log->level, "0", "Verifying the log level changed to '0'.");
# print "One hour ago: .............. [".$an->Get->date_and_time({offset => -3600})."]\n"; $an->Log->level(1);
# print "Ten hours from now: ........ [".$an->Get->date_and_time({offset => 36000})."]\n"; is($an->Log->level, "1", "Verifying the log level changed to '1'.");
# print "Ten hours ago: ............. [".$an->Get->date_and_time({offset => -36000})."]\n"; $an->Log->level(2);
is($an->Log->level, "2", "Verifying the log level changed to '2'.");
$an->Log->level(3);
is($an->Log->level, "3", "Verifying the log level changed to '3'.");
$an->Log->level(4);
is($an->Log->level, "4", "Verifying the log level changed to '4'.");
$an->Log->level("foo");
is($an->Log->level, "4", "Verifying the log level stayed at '4' with bad input.");
$an->Log->level(1);
is($an->Log->level, "1", "Verifying the log level changed back to '1'.");
# We're simulating switches to test Log->_adjust_log_level
$an->data->{switches}{V} = "#!set!#";
$an->data->{switches}{v} = "";
$an->data->{switches}{vv} = "";
$an->data->{switches}{vvv} = "";
$an->data->{switches}{vvvv} = "";
$an->Log->_adjust_log_level;
is($an->Log->level, "0", "Verifying the log level was set to '0' with Log->_adjust_log_leve() with 'V' switch set.");
$an->data->{switches}{V} = "";
$an->data->{switches}{v} = "#!set!#";
$an->Log->_adjust_log_level;
is($an->Log->level, "1", "Verifying the log level was set to '1' with Log->_adjust_log_leve() with 'v' switch set.");
$an->data->{switches}{v} = "";
$an->data->{switches}{vv} = "#!set!#";
$an->Log->_adjust_log_level;
is($an->Log->level, "2", "Verifying the log level was set to '2' with Log->_adjust_log_leve() with 'vv' switch set.");
$an->data->{switches}{vv} = "";
$an->data->{switches}{vvv} = "#!set!#";
$an->Log->_adjust_log_level;
is($an->Log->level, "3", "Verifying the log level was set to '3' with Log->_adjust_log_leve() with 'vvv' switch set.");
$an->data->{switches}{vvv} = "";
$an->data->{switches}{vvvv} = "#!set!#";
$an->Log->_adjust_log_level;
is($an->Log->level, "4", "Verifying the log level was set to '4' with Log->_adjust_log_leve() with 'vvvv' switch set.");
$an->data->{switches}{vvvv} = "";
$an->data->{switches}{v} = "#!set!#";
$an->Log->_adjust_log_level;
is($an->Log->level, "1", "Verifying the log level was set back to '1' with Log->_adjust_log_leve() with 'v' switch set.");
# Test Log->secure
is($an->Log->secure, "0", "Verifying that logging secure messages is disabled by default.");
$an->Log->secure("foo");
is($an->Log->secure, "0", "Verifying that logging secure messages stayed disabled on bad input.");
$an->Log->secure(1);
is($an->Log->secure, "1", "Verifying that logging secure messages was enabled.");
$an->Log->secure(0);
is($an->Log->secure, "0", "Verifying that logging secure messages was disabled again.");
### Template
my $active_skin = $an->Template->skin;
### Validate
is($an->Validate->is_uuid({uuid => $an->Get->host_uuid}), "1", "Verifying that Validate->is_uuid() validates the host UUID.");
is($an->Validate->is_uuid({uuid => uc($an->Get->host_uuid)}), "0", "Verifying that Validate->is_uuid() fails to validates the host UUID in upper case.");
is($an->Validate->is_uuid({uuid => "cfdab37c-5b1a-433d-9285-978e7caa134"}), "0", "Verifying that Validate->is_uuid() fails to validates a UUID missing a letter.");
is($an->Validate->is_uuid({uuid => "cfdab37c5b1a433d9285-978e7caa1342"}), "0", "Verifying that Validate->is_uuid() fails to validate a UUID without hyphens.");

@ -347,6 +347,8 @@ sub search_directories
my $seen_directories = {}; my $seen_directories = {};
foreach my $directory (sort {$a cmp $b} @{$array}) foreach my $directory (sort {$a cmp $b} @{$array})
{ {
next if not defined $directory;
# Convert '.' to $ENV{PWD} # Convert '.' to $ENV{PWD}
if ($directory eq ".") if ($directory eq ".")
{ {

@ -0,0 +1,7 @@
### DO NOT EDIT THIS FILE!
# This is a test configuration file used by AN::Tools to validate AN::Tools::Storage->read_config()'. If you
# change any of the variable = value pairs below, you MUST also update 'AN/Tools.t'!
foo::bar::a = I am "a"
foo::bar::b = I am "b", split with tabs and having trailing spaces.
foo::baz::1=This is '1' with no spaces
foo::baz::2 = I had a $dollar = sign and split with tabs.

@ -14,5 +14,8 @@ $| = 1;
print "Content-type: text/html; charset=utf-8\n\n"; print "Content-type: text/html; charset=utf-8\n\n";
print "hello.\n"; print "hello.\n";
my $template = $an->Template->get({file => "main.html", name => "master"});
print $template;
exit(0); exit(0);

Loading…
Cancel
Save