tests.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/perl
  2. =pod
  3. Tests to verify that the provided modifications to timezone formats produce
  4. the correct results. The first set of tests verify the fixTzstr subroutine
  5. converts the quoted values to something that won't make FreeSWITCH default to
  6. UTC.
  7. The second set of tests confirms that those timezone changes actually produce
  8. the correct timestamps.
  9. Make sure FreeSWITCH already has already loaded the timezones.conf.xml that you
  10. want to test.
  11. To run tests:
  12. TIMEZONES_XML_PATH=path/to/timezones.conf.xml prove tests.pl
  13. =cut
  14. use strict;
  15. use warnings;
  16. use Test::More;
  17. use ESL;
  18. use XML::LibXML::Reader;
  19. require "./fix-tzstr.pl";
  20. use Env qw(TIMEZONES_XML_PATH);
  21. die "The TIMEZONES_XML_PATH environment variable must be set to test timezones." unless ( defined($TIMEZONES_XML_PATH) );
  22. ok( fixTzstr("<-02>2", "doesntmatterhere") eq "STD2" );
  23. ok( fixTzstr("EST5EDT,M3.2.0,M11.1.0", "US/Eastern") eq "EST5EDT,M3.2.0,M11.1.0" );
  24. ok( fixTzstr("<+11>-11", "GMT-11") eq "GMT-11" );
  25. ok( fixTzstr("<-02>2<-01>,M3.5.0/-1,M10.5.0/0", "America/Godthab") eq "STD2DST,M3.5.0/-1,M10.5.0/0" );
  26. my $test_count = 4;
  27. my $tz_fmt = "%Y-%m-%d %H:%M:%S";
  28. my $c = new ESL::ESLconnection("127.0.0.1", "8021", "ClueCon");
  29. $c->api("reloadxml")->getBody();
  30. my $epoch = $c->api("strepoch")->getBody();
  31. run_tests($epoch);
  32. run_tests("1699613236"); # testing DST, add more epochs as needed
  33. sub run_tests {
  34. my $epoch = shift;
  35. my $reader = XML::LibXML::Reader->new(location => $TIMEZONES_XML_PATH);
  36. while ($reader->read) {
  37. my $tag = $reader;
  38. if ( $tag->name eq "zone" && $tag->hasAttributes() ) {
  39. my $zn = $tag->getAttribute("name");
  40. my $cmd = `TZ='$zn' date +'$tz_fmt' --date='\@$epoch'`;
  41. my $sys_time = $cmd =~ s/\n//r;
  42. my $fs_time = $c->api("strftime_tz $zn $epoch|$tz_fmt")->getBody();
  43. ok ( $sys_time eq $fs_time, $zn ) or diag(
  44. " (sys) $sys_time\t(fs) $fs_time"
  45. );
  46. $test_count++;
  47. }
  48. }
  49. }
  50. done_testing($test_count);