| 1 |
|
| #!/usr/bin/perl -w |
| 2 |
|
| #----------------------------------------------------------------- |
| 3 |
|
| # service_unit_tester.pl |
| 4 |
|
| # Author: Edward Kawas <edward.kawas@gmail.com>, |
| 5 |
|
| # For copyright and disclaimer see below. |
| 6 |
|
| # |
| 7 |
|
| # $Id: service_unit_tester.pl,v 1.2 2009/02/12 19:29:47 kawas Exp $ |
| 8 |
|
| # |
| 9 |
|
| # BETA |
| 10 |
|
| # |
| 11 |
|
| # This script goes ahead an asks a registry what services exist |
| 12 |
|
| # and then goes ahead and extracts their signature urls. |
| 13 |
|
| # |
| 14 |
|
| # The signature urls are then parsed into services and any service |
| 15 |
|
| # with a valid unit test is tested and the data stored in an XML file. |
| 16 |
|
| # |
| 17 |
|
| # Configurable options: |
| 18 |
|
| # TIMEOUT - the timeout in seconds to wait for each service |
| 19 |
|
| # CATEGORIES - the moby service categories to test |
| 20 |
|
| # URL - the registry endpoint <optional> |
| 21 |
|
| # URI - the registry namespace <optional> |
| 22 |
|
| # DIRECTORY - the place to store details between jobs <optional> |
| 23 |
|
| # |
| 24 |
|
| # IMPORTANT NOTES: |
| 25 |
|
| # This script doesn't fork yet, so as more and more services provide |
| 26 |
|
| # unit tests, this script will take longer and longer to complete. |
| 27 |
|
| # |
| 28 |
|
| # This script works on windows/unix/linux. Other machines have not been |
| 29 |
|
| # tested, but they may well work just fine since we don't do any black |
| 30 |
|
| # magic. |
| 31 |
|
| #----------------------------------------------------------------- |
| 32 |
|
| |
| 33 |
|
| use strict; |
| 34 |
|
| use warnings; |
| 35 |
|
| |
| 36 |
|
| use MOBY::Config; |
| 37 |
|
| use MOBY::Client::Central; |
| 38 |
|
| use MOBY::RDF::Parsers::ServiceParser; |
| 39 |
|
| use SOAP::Lite; |
| 40 |
|
| use XML::LibXML; |
| 41 |
|
| use HTTP::Request::Common qw(POST); |
| 42 |
|
| use LWP::UserAgent; |
| 43 |
|
| use MOBY::Async::Service; |
| 44 |
|
| |
| 45 |
|
| use Data::Dumper; |
| 46 |
|
| |
| 47 |
|
| ######-------USER CONFIGURABLE PARAMETERS-------###### |
| 48 |
|
| # how long in seconds to wait for a service to respond |
| 49 |
|
| my $TIMEOUT = 20; |
| 50 |
|
| |
| 51 |
|
| # the categories of services to unit test |
| 52 |
|
| my @CATEGORIES = qw / moby cgi moby-async /; |
| 53 |
|
| |
| 54 |
|
| ######-------------------------------------------###### |
| 55 |
|
| |
| 56 |
|
| # the registry to query |
| 57 |
|
| my $URL = $ENV{MOBY_SERVER} || 'http://moby.ucalgary.ca/moby/MOBY-Central.pl'; |
| 58 |
|
| my $URI = $ENV{MOBY_URI} || 'http://moby.ucalgary.ca/MOBY/Central'; |
| 59 |
|
| |
| 60 |
|
| # The directory to store the job details |
| 61 |
|
| my $CONF = MOBY::Config->new; |
| 62 |
|
| my $DIRECTORY = $CONF->{mobycentral}->{service_tester_path} || '/tmp/'; |
| 63 |
|
| |
| 64 |
|
| # hashes whose key is the service provider and the value is an array of service names |
| 65 |
|
| my %RESULTS = (); |
| 66 |
|
| my $FILENAME = 'unitTestStats.xml'; |
| 67 |
|
| |
| 68 |
|
| print "saving results to $DIRECTORY/$FILENAME (if possible)\n"; |
| 69 |
|
| |
| 70 |
|
| # a hash of signature urls that we have found and will process |
| 71 |
|
| my %signature_urls = (); |
| 72 |
|
| |
| 73 |
|
| # this is just to test if I'm going to have permissions |
| 74 |
|
| # right now, rather than an hour from now... Arghghg! |
| 75 |
|
| open( OUT, ">>$DIRECTORY/$FILENAME" ) |
| 76 |
|
| || die("Cannot Open File '$DIRECTORY/$FILENAME' $!"); |
| 77 |
|
| close OUT; |
| 78 |
|
| |
| 79 |
|
| # create the central client and get all service providers once |
| 80 |
|
| my $central = |
| 81 |
|
| MOBY::Client::Central->new( |
| 82 |
|
| Registries => { |
| 83 |
|
| mobycentral => { |
| 84 |
|
| URL => $URL, |
| 85 |
|
| URI => $URI |
| 86 |
|
| } |
| 87 |
|
| } |
| 88 |
|
| ); |
| 89 |
|
| my @providers = $central->retrieveServiceProviders(); |
| 90 |
|
| |
| 91 |
|
| # get the signature urls for all services that we are interested in |
| 92 |
|
| foreach my $cat (@CATEGORIES) { |
| 93 |
|
| foreach my $authURI (@providers) { |
| 94 |
|
| my ( $second, $minute, $hour, @whatever ) = localtime(); |
| 95 |
|
| $hour = "0$hour" if $hour <= 9; |
| 96 |
|
| $second = "0$second" if $second <= 9; |
| 97 |
|
| $minute = "0$minute" if $minute <= 9; |
| 98 |
|
| |
| 99 |
|
| print |
| 100 |
|
| "Retrieving signature urls for services registered by '$authURI' as '$cat' @ $hour:$minute:$second\n"; |
| 101 |
|
| my ( $services, $reg ) = |
| 102 |
|
| $central->findService( |
| 103 |
|
| Registry => "mobycentral", |
| 104 |
|
| category => $cat, |
| 105 |
|
| authURI => $authURI |
| 106 |
|
| ); |
| 107 |
|
| ( $second, $minute, $hour, @whatever ) = localtime(); |
| 108 |
|
| $hour = "0$hour" if $hour <= 9; |
| 109 |
|
| $second = "0$second" if $second <= 9; |
| 110 |
|
| $minute = "0$minute" if $minute <= 9; |
| 111 |
|
| print "Services found " |
| 112 |
|
| . scalar @$services |
| 113 |
|
| . "... processing @ $hour:$minute:$second \n"; |
| 114 |
|
| |
| 115 |
|
| my $count = 0; |
| 116 |
|
| print "\tservice count: " . scalar(@$services) . "\n"; |
| 117 |
|
| foreach (@$services) { |
| 118 |
|
| |
| 119 |
|
| # no sig url, then nothing to test |
| 120 |
|
| next unless $_->signatureURL; |
| 121 |
|
| |
| 122 |
|
| # ignore test services or sig urls we have already processed |
| 123 |
|
| next |
| 124 |
|
| if $_->authority eq 'samples.jmoby.net' |
| 125 |
|
| or $signature_urls{ $_->signatureURL }; |
| 126 |
|
| |
| 127 |
|
| # add the sig url to the hash |
| 128 |
|
| $signature_urls{ $_->signatureURL } = 1; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| ( $second, $minute, $hour, @whatever ) = localtime(); |
| 132 |
|
| $hour = "0$hour" if $hour <= 9; |
| 133 |
|
| $second = "0$second" if $second <= 9; |
| 134 |
|
| $minute = "0$minute" if $minute <= 9; |
| 135 |
|
| print |
| 136 |
|
| "Retrieved signature urls for '$cat' services from '$authURI' ... completed @ $hour:$minute:$second \n"; |
| 137 |
|
| } |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| # now get the services from the signature URL and test them |
| 141 |
|
| my $parser = MOBY::RDF::Parsers::ServiceParser->new(); |
| 142 |
|
| foreach my $sigurl ( keys %signature_urls ) { |
| 143 |
|
| |
| 144 |
|
| # get the services from the URL |
| 145 |
|
| my $service_arrayref = $parser->getServices($sigurl); |
| 146 |
|
| # call their unit tests |
| 147 |
|
| foreach my $serv ( @{$service_arrayref} ) { |
| 148 |
|
| my $tests = $serv->unitTests; |
| 149 |
|
| next unless $tests; |
| 150 |
|
| foreach my $test (@$tests) { |
| 151 |
|
| if ( $test->xpath or $test->regex or $test->example_input ) { |
| 152 |
|
| my $name = $serv->name; |
| 153 |
|
| my $auth = $serv->authority; |
| 154 |
|
| my $url = $serv->URL; |
| 155 |
|
| my $cat = $serv->category; |
| 156 |
|
| my $out = undef; |
| 157 |
|
| my $input = $test->example_input || _empty_input(); |
| 158 |
|
| print "\tTesting the service $name from $auth ($cat)...\n"; |
| 159 |
|
| |
| 160 |
|
| # add the authority if not yet in the hash |
| 161 |
|
| $RESULTS{$auth} = () |
| 162 |
|
| if not exists $RESULTS{$auth}; |
| 163 |
|
| do { |
| 164 |
|
| my $soap = |
| 165 |
|
| SOAP::Lite->uri("http://biomoby.org/") |
| 166 |
|
| ->proxy( $url, timeout => $TIMEOUT )->on_fault( |
| 167 |
|
| sub { |
| 168 |
|
| my $soap = shift; |
| 169 |
|
| my $res = shift; |
| 170 |
|
| |
| 171 |
|
| # record that the service failed the test ... |
| 172 |
|
| push @{ $RESULTS{$auth}{$name} }, |
| 173 |
|
| { |
| 174 |
|
| name => $name, |
| 175 |
|
| xpath => 'false', |
| 176 |
|
| regex => 'false', |
| 177 |
|
| output => 'false' |
| 178 |
|
| }; |
| 179 |
|
| exit(0); |
| 180 |
|
| } |
| 181 |
|
| ); |
| 182 |
|
| |
| 183 |
|
| $out = |
| 184 |
|
| $soap->$name( SOAP::Data->type( 'string' => "$input" ) ) |
| 185 |
|
| ->result; |
| 186 |
|
| } if $cat eq 'moby'; |
| 187 |
|
| |
| 188 |
|
| # test cgi services |
| 189 |
|
| do { |
| 190 |
|
| my $ua = LWP::UserAgent->new; |
| 191 |
|
| $ua->timeout($TIMEOUT); |
| 192 |
|
| my $req = POST $url, [ data => $input ]; |
| 193 |
|
| $req = $ua->request($req); |
| 194 |
|
| $out = $req->content if $req->is_success; |
| 195 |
|
| do { |
| 196 |
|
| |
| 197 |
|
| # record that the service failed the test ... |
| 198 |
|
| push @{ $RESULTS{$auth}{$name} }, |
| 199 |
|
| { |
| 200 |
|
| name => $name, |
| 201 |
|
| xpath => 'false', |
| 202 |
|
| regex => 'false', |
| 203 |
|
| output => 'false' |
| 204 |
|
| }; |
| 205 |
|
| } unless $req->is_success; |
| 206 |
|
| } if $cat eq 'cgi'; |
| 207 |
|
| |
| 208 |
|
| # test async services |
| 209 |
|
| do { |
| 210 |
|
| |
| 211 |
|
| my $WSDL = $central->retrieveService($serv); |
| 212 |
|
| my $async = MOBY::Async::Service->new(service => $WSDL); |
| 213 |
|
| eval {$out = $async->raw_execute($input);}; |
| 214 |
|
| # record that we have an error if we failed the test |
| 215 |
|
| push @{ $RESULTS{$auth}{$name} }, |
| 216 |
|
| { |
| 217 |
|
| name => $name, |
| 218 |
|
| xpath => 'false', |
| 219 |
|
| regex => 'false', |
| 220 |
|
| output => 'false' |
| 221 |
|
| } if $@; |
| 222 |
|
| } if $cat eq 'moby-async'; |
| 223 |
|
| |
| 224 |
|
| # nothing to test unless we have some output |
| 225 |
|
| next unless $out; |
| 226 |
|
| |
| 227 |
|
| # perform the unit tests |
| 228 |
|
| my %unit; |
| 229 |
|
| $unit{name} = $name; |
| 230 |
|
| |
| 231 |
|
| # test regex |
| 232 |
|
| if ( $test->regex ) { |
| 233 |
|
| $unit{regex} = $test->test_regex($out) ? 'true' : 'false'; |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
|
| # test xpath |
| 237 |
|
| if ( $test->xpath ) { |
| 238 |
|
| $unit{xpath} = $test->test_xpath($out) ? 'true' : 'false'; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
|
| # semantically compare XML |
| 242 |
|
| if ( $test->expected_output ) { |
| 243 |
|
| $unit{output} = $test->test_output_xml($out) ? 'true' : 'false'; |
| 244 |
|
| } |
| 245 |
|
| push @{ $RESULTS{$auth}{$name} }, \%unit; |
| 246 |
|
| } |
| 247 |
|
| } |
| 248 |
|
| } |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| # create the XML file that contains our information |
| 252 |
|
| my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); |
| 253 |
|
| my $root = $doc->createElement('Services'); |
| 254 |
|
| $doc->setDocumentElement($root); |
| 255 |
|
| |
| 256 |
|
| # authorities |
| 257 |
|
| for my $auth ( sort keys %RESULTS ) { |
| 258 |
|
| my $element = $doc->createElement('authority'); |
| 259 |
|
| $element->setAttribute( 'id', $auth ); |
| 260 |
|
| my %services = %{ $RESULTS{$auth} }; |
| 261 |
|
| next unless %services; |
| 262 |
|
| # service names |
| 263 |
|
| foreach my $service (sort keys %services) { |
| 264 |
|
| my @tests = @{ $services{$service} }; |
| 265 |
|
| next unless @tests; |
| 266 |
|
| my $child = $doc->createElement('service'); |
| 267 |
|
| $child->setAttribute( 'id', $service ); |
| 268 |
|
| my $unitTests = $doc->createElement('UnitTests'); |
| 269 |
|
| #array of unit tests |
| 270 |
|
| foreach my $s (@tests) { |
| 271 |
|
| next unless $s; |
| 272 |
|
| my $test = $doc->createElement('unitTest'); |
| 273 |
|
| # sets values to true/false |
| 274 |
|
| $test->setAttribute( 'xpath', $s->{xpath} ) if $s->{xpath}; |
| 275 |
|
| $test->setAttribute( 'regex', $s->{regex} ) if $s->{regex}; |
| 276 |
|
| $test->setAttribute( 'output', $s->{output} ) if $s->{output}; |
| 277 |
|
| # add the test to unittests |
| 278 |
|
| $unitTests->appendChild($test); |
| 279 |
|
| } |
| 280 |
|
| # add the unittests to $child |
| 281 |
|
| $child->appendChild($unitTests); |
| 282 |
|
| # append this child |
| 283 |
|
| $element->appendChild($child); |
| 284 |
|
| } |
| 285 |
|
| $root->appendChild($element); |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
|
| # save our XML file |
| 289 |
|
| open( OUT, ">$DIRECTORY/$FILENAME" ) |
| 290 |
|
| || die("Cannot Open File $DIRECTORY/$FILENAME $!"); |
| 291 |
|
| print OUT $doc->toString(1); |
| 292 |
|
| close OUT; |
| 293 |
|
| |
| 294 |
|
| # empty input for those services that dont consume anything |
| 295 |
|
| sub _empty_input { |
| 296 |
|
| return <<'END_OF_XML'; |
| 297 |
|
| <?xml version="1.0" encoding="UTF-8"?> |
| 298 |
|
| <moby:MOBY xmlns:moby="http://www.biomoby.org/moby"> |
| 299 |
|
| <moby:mobyContent> |
| 300 |
|
| <moby:mobyData/> |
| 301 |
|
| </moby:mobyContent> |
| 302 |
|
| </moby:MOBY> |
| 303 |
|
| END_OF_XML |
| 304 |
|
| } |
| 305 |
|
| |