| 1 | # | ||
| 2 | # Test::System | ||
| 3 | # | ||
| 4 | # Author(s): Pablo Fischer (pfischer@cpan.org) | ||
| 5 | # Created: 11/07/2009 17:36:17 PST 17:36:17 | ||
| 6 | package Test::System; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Test::System - Test suite oriented for testing system administration tasks | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use Test::System; | ||
| 15 | |||
| 16 | my $suite = Test::System->new( | ||
| 17 | format => 'consoletable', | ||
| 18 | nodes => 'example.com', | ||
| 19 | test_groups => '~/code/my/system/tests.yaml' | ||
| 20 | ); | ||
| 21 | $suite->runtests; | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Loads and runs a set of tests for verifying a system administration task | ||
| 26 | before a possible incident happens or after a task is executed. | ||
| 27 | |||
| 28 | Tests can be run as perl scripts and actually in any format L<TAP::Harness> | ||
| 29 | supports. | ||
| 30 | |||
| 31 | Some examples of how L<Test::System> can help you: | ||
| 32 | |||
| 33 | =over 4 | ||
| 34 | |||
| 35 | =item * | ||
| 36 | |||
| 37 | Before a deployment probably you want to make sure your systems and | ||
| 38 | configuration files are ready to go. Also you probably want to make sure | ||
| 39 | that if you needed to apply patches to your configuration files, they | ||
| 40 | really got applied (even in a development server or verify directly on | ||
| 41 | production servers). So here L<Test::System> can help you verifying this | ||
| 42 | in the form of a test case. Of course, you write the test case :-) | ||
| 43 | |||
| 44 | =item * | ||
| 45 | |||
| 46 | What about if you have many servers and although you have nagios to verify | ||
| 47 | the speed of the CPU sometimes you need to verify it "on-demand" (perhaps when | ||
| 48 | you get new servers in your datacenter). Here L<Test::System> can help you to | ||
| 49 | verify that these servers are ready to go before even they get connected to nagios | ||
| 50 | or your favorite monitoring tool. | ||
| 51 | |||
| 52 | =back | ||
| 53 | |||
| 54 | One of the things you need to keep in mind is that L<Test::System> is B<NOT> a | ||
| 55 | monitoring system. It will let you verify things on demand, things that are | ||
| 56 | repetitive and perhaps you want them to be automated. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | use 5.006; | ||
| 60 | |||
| 61 | use Moose; # it turns strict and warnings | ||
| 62 | use File::Basename qw(dirname); | ||
| 63 | use YAML::Syck; | ||
| 64 | use Test::System::Output::Factory; | ||
| 65 | use TAP::Harness; | ||
| 66 | |||
| 67 | our $VERSION = '0.17'; | ||
| 68 | |||
| 69 | =head1 Attributes | ||
| 70 | |||
| 71 | The module offers a list of attributes, some of them are read-only. | ||
| 72 | |||
| 73 | =over 6 | ||
| 74 | |||
| 75 | =item B<test_groups> | ||
| 76 | |||
| 77 | YAML filename that has a list of available tests. This is not required but | ||
| 78 | can be useful if you want to group tests (like I<hardware.yaml>, I<net.yaml>, | ||
| 79 | etc). Comes also handy when user does not provide a list of tests to execute, | ||
| 80 | so all the tests listed in this file are executed. | ||
| 81 | |||
| 82 | An example of a YAML file: | ||
| 83 | |||
| 84 | ping: | ||
| 85 | description: Test the ping and do foo and make bar | ||
| 86 | code: test/foo.pl | ||
| 87 | cpu: | ||
| 88 | description: Test the CPU of nodes | ||
| 89 | code: test/cpu.pl | ||
| 90 | |||
| 91 | =cut | ||
| 92 | has 'test_groups' => ( | ||
| 93 | is => 'rw', | ||
| 94 | isa => 'Str', | ||
| 95 | trigger => \&_read_test_groups_file | ||
| 96 | ); | ||
| 97 | |||
| 98 | =item B<available_tests> | ||
| 99 | |||
| 100 | Is a read only attribute that contains a list of all available tests found in | ||
| 101 | the YAML file provided by C<test_groups>. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | has 'available_tests' => ( | ||
| 105 | is => 'ro', | ||
| 106 | isa => 'ArrayRef[Str]', | ||
| 107 | ); | ||
| 108 | |||
| 109 | =item B<nodes> | ||
| 110 | |||
| 111 | Is an attribute that can be represented as a string (like a hostname) or as a | ||
| 112 | list (where each item will be a node/hostname). This attribute has write access | ||
| 113 | and is where the tests are going to be executed to. | ||
| 114 | |||
| 115 | Another way of setting this value is when C<runtests()> is called, a test plan | ||
| 116 | (in the form of a YAML file) can be provided and it can contain the list of | ||
| 117 | nodes to use. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | has 'nodes' => ( | ||
| 121 | is => 'rw', | ||
| 122 | isa => 'Any', | ||
| 123 | trigger => \&_verify_nodes_datatype | ||
| 124 | ); | ||
| 125 | |||
| 126 | =item B<format> | ||
| 127 | |||
| 128 | A write access string that has the format of how the tests should be presented, | ||
| 129 | please refer to the modules available under L<Test::System::Output> or in your | ||
| 130 | custom factory (C<format_factory_class> attribute) class if this is the case. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | has 'format' => ( | ||
| 134 | is => 'rw', | ||
| 135 | isa => 'Str', | ||
| 136 | default => 'console', | ||
| 137 | trigger => \&_verify_format | ||
| 138 | ); | ||
| 139 | |||
| 140 | =item B<available_formats> | ||
| 141 | |||
| 142 | A list of available formats, read only. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | has 'available_formats' => ( | ||
| 146 | is => 'ro', | ||
| 147 | isa => => 'HashRef', | ||
| 148 | default => \&_fill_available_formats_hash | ||
| 149 | ); | ||
| 150 | |||
| 151 | =item B<format_factory_class> | ||
| 152 | |||
| 153 | If you want to use your own Factory for creating your output you can set this | ||
| 154 | to your class name (B<NOT the object>). | ||
| 155 | |||
| 156 | Please note that L<Test::System> will continue to use | ||
| 157 | L<Test::System::Output::Factory>, the reason of this is that any factory | ||
| 158 | subclasses should add/register their classes via their parent class. So in this | ||
| 159 | case all your new format classes should be added to L<Test::System::Output::Factory>. | ||
| 160 | For more information please take a look to L<Class::Factory>. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | has 'format_factory_class' => ( | ||
| 164 | is => 'rw', | ||
| 165 | isa => 'Str', | ||
| 166 | trigger => \&_fill_available_formats_hash | ||
| 167 | ); | ||
| 168 | |||
| 169 | =item B<harness> | ||
| 170 | |||
| 171 | Is (or will be) the harness instance once C<runtests> is executed. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | has 'harness' => ( | ||
| 175 | is => 'rw', | ||
| 176 | isa => 'TAP::Harness'); | ||
| 177 | |||
| 178 | =item B<parameters> | ||
| 179 | |||
| 180 | An attribute with write access permission. This attribute will transform all | ||
| 181 | the items of this hash to environment variables. | ||
| 182 | |||
| 183 | The use of this attribute is very handy if you want to provide some additional | ||
| 184 | data for your tests and since the tests are run in separate forks with | ||
| 185 | L<Test::Harness> then the only possible way to keep them is to make them | ||
| 186 | available through the environment (C<%ENV> hash). | ||
| 187 | |||
| 188 | Please be warned that only scalars are stored in environment variables, those | ||
| 189 | that are an array will be converted to CSV values while the rest of the data | ||
| 190 | types will be lost. | ||
| 191 | |||
| 192 | In your tests if you want to use any of these parameters they will be available | ||
| 193 | through the environment variables with a prefix of: C<TEST_SYSTEM_> or you | ||
| 194 | can use L<Test::System::Helper> to get their values. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | has 'parameters' => ( | ||
| 198 | is => 'rw', | ||
| 199 | isa => 'HashRef[Str]', | ||
| 200 | ); | ||
| 201 | |||
| 202 | |||
| 203 | =item B<results> | ||
| 204 | |||
| 205 | A hash reference that contains the results of the tests. This information is | ||
| 206 | generated by the L<TAP::Parser::Aggregator>. | ||
| 207 | |||
| 208 | =over 4 | ||
| 209 | |||
| 210 | =item * passed: Total of passed tests | ||
| 211 | |||
| 212 | =item * total: Total number of tests (passed or not) | ||
| 213 | |||
| 214 | =item * skipped: Total of skipped tests | ||
| 215 | |||
| 216 | =item * failed: Total of failed tests. | ||
| 217 | |||
| 218 | =back | ||
| 219 | |||
| 220 | =cut | ||
| 221 | has 'results' => ( | ||
| 222 | is => 'ro', | ||
| 223 | isa => 'HashRef[Str]', | ||
| 224 | ); | ||
| 225 | |||
| 226 | =item B<status> | ||
| 227 | |||
| 228 | A string (read-only) that contains a word that describes the status of all | ||
| 229 | the tests. This value is also generated by L<TAP::Parser::Aggregator>. | ||
| 230 | |||
| 231 | =cut | ||
| 232 | has 'status' => ( | ||
| 233 | is => 'ro', | ||
| 234 | isa => 'Str' | ||
| 235 | ); | ||
| 236 | |||
| 237 | =item B<show_warnings> | ||
| 238 | |||
| 239 | By default we show warnings of all things that can make your tests to run | ||
| 240 | in a different way than expected. | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =cut | ||
| 245 | has 'show_warnings' => ( | ||
| 246 | is => 'rw', | ||
| 247 | isa => 'Bool', | ||
| 248 | default => 1 | ||
| 249 | ); | ||
| 250 | |||
| 251 | =head1 Methods | ||
| 252 | |||
| 253 | =head2 B<runtests(@tests,%options)> | ||
| 254 | |||
| 255 | It will run the given test cases (C<@tests>). The C<@tests> can be: | ||
| 256 | |||
| 257 | =over 4 | ||
| 258 | |||
| 259 | =item * An array of test files to execute. | ||
| 260 | |||
| 261 | =item * Or, a string pointing to a test plan file (YAML). | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | If no C<@tests> are given but we have a C<test_groups> then B<ALL> the tests | ||
| 266 | listed inside this file will be executed. | ||
| 267 | |||
| 268 | The C<%options> is a hash of options that will be passed to the L<TAP::Harness> | ||
| 269 | object, some useful parameters are: | ||
| 270 | |||
| 271 | =over 4 | ||
| 272 | |||
| 273 | =item * verbosity | ||
| 274 | |||
| 275 | By default we mute everything with C<-9>. | ||
| 276 | |||
| 277 | =item * color | ||
| 278 | |||
| 279 | If you want the output (in console) to have color | ||
| 280 | |||
| 281 | =item * formatter | ||
| 282 | |||
| 283 | Although we use L<Test::System::Output::Factory> to offer a set of formatters | ||
| 284 | you can provide your own formatter object. See C<available_formats>. | ||
| 285 | |||
| 286 | =item * jobs | ||
| 287 | |||
| 288 | If you have many tests you probably want to increment this value (that defaults | ||
| 289 | to C<1>) so other tests can be run at the same time. | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =cut | ||
| 294 | sub runtests { | ||
| 295 | my ($self, $tests, $options) = @_; | ||
| 296 | |||
| 297 | if ($options and ref($options) ne 'HASH') { | ||
| 298 | confess "Options should be a hash reference"; | ||
| 299 | } | ||
| 300 | if ($tests and ref($tests) ne 'ARRAY') { | ||
| 301 | confess "Tests parameter is not an array (" . ref($tests) . ")"; | ||
| 302 | } | ||
| 303 | |||
| 304 | my @tests_to_run; | ||
| 305 | if (!$tests and $self->test_groups) { | ||
| 306 | @tests_to_run = keys(%{$self->available_tests}); | ||
| 307 | } else { | ||
| 308 | @tests_to_run = @$tests; | ||
| 309 | } | ||
| 310 | |||
| 311 | $self->pretests_verification(); | ||
| 312 | # No duplicate tests and build the module name | ||
| 313 | my (%seen, @test_files); | ||
| 314 | foreach my $test (@tests_to_run) { | ||
| 315 | my ($code, $description); | ||
| 316 | # What if the item is an array? of code, description? | ||
| 317 | if (ref($test) eq 'ARRAY') { | ||
| 318 | ($code, $description) = ( | ||
| 319 | $test->[0], | ||
| 320 | $test->[1]); | ||
| 321 | } else { | ||
| 322 | ($code, $description) = ($test, $test); | ||
| 323 | } | ||
| 324 | if (defined $self->available_tests) { | ||
| 325 | if (defined $self->available_tests->{$test}) { | ||
| 326 | if (defined $self->available_tests->{$test}->{'code'}) { | ||
| 327 | $code = $self->available_tests->{$test}->{'code'}; | ||
| 328 | } | ||
| 329 | if (defined $self->available_tests->{$test}->{'description'}) { | ||
| 330 | $description = $self->available_tests->{$test}->{'description'}; | ||
| 331 | } | ||
| 332 | } else { | ||
| 333 | warn "$_ is not listed in the available tests list, forcing it" | ||
| 334 | if $self->show_warnings; | ||
| 335 | } | ||
| 336 | if (!-f $code) { | ||
| 337 | if ($code eq $test) { | ||
| 338 | warn "$code does not exist as a file, skipping" | ||
| 339 | if $self->show_warnings; | ||
| 340 | } else { | ||
| 341 | warn "$_ test file ($code) does not exist as a file, skipping" | ||
| 342 | if $self->show_warnings; | ||
| 343 | } | ||
| 344 | next; | ||
| 345 | } | ||
| 346 | } else { | ||
| 347 | if (!-f $code) { | ||
| 348 | warn "$code test does not exist as a file, skipping" | ||
| 349 | if $self->show_warnings; | ||
| 350 | next; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | if ($code and $description) { | ||
| 354 | push(@test_files, [ $code, $description ]); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | # Why not {format_factory_class}? because the registered types should be in the | ||
| 358 | # Test::System::Output::Factory: | ||
| 359 | # http://search.cpan.org/~phred/Class-Factory-1.06/lib/Class/Factory.pm#Gotchas | ||
| 360 | my $factory_class = 'Test::System::Output::Factory'; | ||
| 361 | my $formatter_class = "$factory_class"->get_registered_class( | ||
| 362 | $self->format | ||
| 363 | ); | ||
| 364 | if (!defined $options->{'formatter_class'} && !defined $options->{'formatter'}) { | ||
| 365 | $options->{'formatter_class'} = $formatter_class; | ||
| 366 | } | ||
| 367 | $options->{'merge'} = 1 unless defined $options->{'merge'}; | ||
| 368 | my @lib = @INC; | ||
| 369 | if (defined $options->{'lib'} && ref($options->{'lib'}) eq 'ARRAY') { | ||
| 370 | foreach (@{$options->{'lib'}}) { | ||
| 371 | push(@lib, $_); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | $options->{'lib'} = \@lib; | ||
| 375 | $self->prepare_environment(@test_files, $options); | ||
| 376 | $self->{harness} = TAP::Harness->new($options); | ||
| 377 | $self->{harness}->callback('after_runtests' => | ||
| 378 | sub { $self->set_results(shift) }); | ||
| 379 | $self->{harness}->callback('made_parser' => | ||
| 380 | sub { $self->setup_parser(shift); }); | ||
| 381 | $self->{harness}->runtests(@test_files); | ||
| 382 | $self->clean_environment(); | ||
| 383 | return 1; | ||
| 384 | } | ||
| 385 | |||
| 386 | =head2 B<run_test_plan($test_plan, %options)> | ||
| 387 | |||
| 388 | It loads the given test plan file (should be written in YAML) in order to get | ||
| 389 | a list of tests to execute. Once it has a list of tests then it calls | ||
| 390 | C<runtests> with this list and with the C<%options>. | ||
| 391 | |||
| 392 | =cut | ||
| 393 | sub run_test_plan { | ||
| 394 | my ($self, $test_plan, $options) = @_; | ||
| 395 | |||
| 396 | if ($options and ref($options) ne 'HASH') { | ||
| 397 | die "Options should be a hash reference"; | ||
| 398 | } | ||
| 399 | |||
| 400 | my @tests_to_run = $self->get_tests_from_test_plan($test_plan); | ||
| 401 | return $self->runtests(\@tests_to_run, $options); | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 B<setup_parser($parser)> | ||
| 405 | |||
| 406 | This method should B<never> be called directly since this is triggered by | ||
| 407 | L<TAP::Harness> when every L<TAP::Parser> object gets created. | ||
| 408 | |||
| 409 | This method is useful for setting the callbacks we want the parser to trigger. | ||
| 410 | |||
| 411 | C<$parser> should be a L<TAP::Parser> object. | ||
| 412 | |||
| 413 | =cut | ||
| 414 | sub setup_parser { | ||
| 415 | my ($self, $parser) = @_; | ||
| 416 | # We want to know what happens with every test. | ||
| 417 | $parser->callback('ALL' => sub { $self->what_happened($parser, shift) }); | ||
| 418 | } | ||
| 419 | |||
| 420 | =head2 B<pretests_verification(@tests, %options)> | ||
| 421 | |||
| 422 | Does some verification before the tests are executed. This method gets called | ||
| 423 | by C<runtests>. | ||
| 424 | |||
| 425 | The parameters it accepts are the same parameters passed to C<runtests> with | ||
| 426 | the main difference the tests are already filtered (eg, if a test plan in | ||
| 427 | YAML was provided we will use it or we will use all the tests listed inside | ||
| 428 | the C<test_groups>. | ||
| 429 | |||
| 430 | =cut | ||
| 431 | sub pretests_verification { | ||
| 432 | my ($self, $tests, $options) = @_; | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | =head2 B<what_happened($parser, $result_test)> | ||
| 437 | |||
| 438 | Similar to I<setup_parser>, this should never be called since it will be | ||
| 439 | triggered by L<TAP::Parser> after each (sub)tests gets executed. | ||
| 440 | |||
| 441 | By default we check the reason of why the test failed so later we provide a | ||
| 442 | simple hash to parse with all the tests and the reasons of why they failed (if | ||
| 443 | this could be the case). And because we need to know what L<TAP::Parser> | ||
| 444 | instance we are processing we need to ask for it as a first parameter. | ||
| 445 | |||
| 446 | Parameters: | ||
| 447 | |||
| 448 | =over 4 | ||
| 449 | |||
| 450 | =item * C<$parser> should be a L<TAP::Parser> | ||
| 451 | |||
| 452 | =item * C<$result_test> should be a L<TAP::Parser::Result::Test>. | ||
| 453 | |||
| 454 | =back | ||
| 455 | |||
| 456 | =cut | ||
| 457 | sub what_happened { | ||
| 458 | my ($self, $parser, $result_test) = @_; | ||
| 459 | |||
| 460 | # We are going to do a nasty thing with TAP::Parser, we are going to create | ||
| 461 | # a new attribute/item that we can use later to get detailed results in the | ||
| 462 | # format we want. | ||
| 463 | if (!defined $parser->{'test_system_details'}) { | ||
| 464 | $parser->{'test_system_details'} = []; | ||
| 465 | $parser->{'test_system_notes'} = []; | ||
| 466 | } | ||
| 467 | |||
| 468 | if ($result_test->{'type'} eq 'test') { | ||
| 469 | my $description = $result_test->{'description'}; | ||
| 470 | if (my $directive = $result_test->{'directive'}) { | ||
| 471 | $description = $result_test->{'explanation'}; | ||
| 472 | } | ||
| 473 | my %value = map { $_ => $result_test->{$_} } keys(%$result_test); | ||
| 474 | $value{'is_ok'} = $result_test->is_ok; | ||
| 475 | push(@{$parser->{'test_system_details'}}, \%value); | ||
| 476 | } elsif ($result_test->{'type'} eq 'comment') { # for notes() | ||
| 477 | push(@{$parser->{'test_system_notes'}}, { | ||
| 478 | comment => $result_test->{'comment'}, | ||
| 479 | raw => $result_test->{'raw'}, | ||
| 480 | }); | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | =head2 B<set_results($aggregator)> | ||
| 485 | |||
| 486 | This is a callback and is only called/triggered by L<TAP::Harness> when all the | ||
| 487 | tests are done. It will fill the C<results> attribute with information of | ||
| 488 | everything. | ||
| 489 | |||
| 490 | Refer to the documentation of I<results> for more information. | ||
| 491 | |||
| 492 | The paramenter C<$aggregator> is a L<TAP::Parser::Aggregator>. | ||
| 493 | |||
| 494 | =cut | ||
| 495 | sub set_results { | ||
| 496 | my ($self, $parser_aggregator) = @_; | ||
| 497 | |||
| 498 | $self->{'status'} = $parser_aggregator->get_status; | ||
| 499 | foreach my $key (qw(passed total skipped failed)) { | ||
| 500 | $self->{'results'}->{$key} = $parser_aggregator->{$key}; | ||
| 501 | } | ||
| 502 | |||
| 503 | # So we are done with all the test, now lets parse in detail what happened | ||
| 504 | # But in the hash we want to keep we only want to have the test name (if | ||
| 505 | # that's the case) or the test file, we don't care about the description. | ||
| 506 | my %tests_ids; | ||
| 507 | if (defined $self->available_tests) { | ||
| 508 | foreach my $test (keys %{$self->available_tests}) { | ||
| 509 | if (defined $self->available_tests->{$test}->{'description'}) { | ||
| 510 | $tests_ids{$self->available_tests->{$test}->{'description'}} = $test; | ||
| 511 | } elsif (defined $self->available_tests->{$test}->{'code'}) { | ||
| 512 | $tests_ids{$self->available_tests->{$test}->{'code'}} = $test; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | $self->{'results'}->{'details'} = {}; | ||
| 518 | $self->{'results'}->{'notes'} = {}; | ||
| 519 | foreach my $key (keys %{$parser_aggregator->{'parser_for'}}) { | ||
| 520 | my $parser = $parser_aggregator->{'parser_for'}->{$key}; | ||
| 521 | foreach my $want (qw(details notes)) { | ||
| 522 | my $parser_info_key = 'test_system_' . $want; | ||
| 523 | if (defined $parser->{$parser_info_key}) { | ||
| 524 | my $id_to_store = $key; | ||
| 525 | if (defined $tests_ids{$id_to_store}) { | ||
| 526 | $id_to_store = $tests_ids{$id_to_store}; | ||
| 527 | } | ||
| 528 | $self->{'results'}->{$want}->{$id_to_store} = | ||
| 529 | $parser->{$parser_info_key}; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | =head2 B<prepare_environment()> | ||
| 536 | |||
| 537 | Prepares the environment by settings the needed environment values so they can | ||
| 538 | be used later by the tests | ||
| 539 | |||
| 540 | =cut | ||
| 541 | sub prepare_environment { | ||
| 542 | my ($self) = @_; | ||
| 543 | |||
| 544 | # Nodes are stored under the TEST_SYSTEM_NODES environment key | ||
| 545 | if ($self->nodes) { | ||
| 546 | $ENV{TEST_SYSTEM_NODES} = $self->nodes; | ||
| 547 | } | ||
| 548 | if ($self->parameters) { | ||
| 549 | if (ref($self->parameters) eq 'HASH') { | ||
| 550 | foreach my $k (keys %{$self->parameters}) { | ||
| 551 | my $value = $self->parameters->{$k}; | ||
| 552 | $k = uc $k; | ||
| 553 | if (!defined $ENV{'TEST_SYSTEM_' . $k}) { | ||
| 554 | if (ref \$value eq 'SCALAR') { | ||
| 555 | $ENV{'TEST_SYSTEM_' . $k} = $value; | ||
| 556 | } elsif (ref \$value eq 'ARRAY') { | ||
| 557 | $ENV{'TEST_SYSTEM_' . $k} = join(',', $value); | ||
| 558 | } | ||
| 559 | } | ||
| 560 | } | ||
| 561 | } | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | =head2 B<clean_environment()> | ||
| 566 | |||
| 567 | Cleans/deletes all the environment variables that match C<TEST_SYSTEM_*> | ||
| 568 | |||
| 569 | =cut | ||
| 570 | sub clean_environment { | ||
| 571 | my ($self) = @_; | ||
| 572 | |||
| 573 | my %environment_vars = %ENV; | ||
| 574 | foreach my $k (keys %environment_vars) { | ||
| 575 | if ($k =~ /^TEST_SYSTEM_/) { | ||
| 576 | delete $ENV{$k}; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | } | ||
| 580 | |||
| 581 | =head2 B<get_tests_from_test_plan($yaml_file, | ||
| 582 | $do_not_fill_parameters, | ||
| 583 | $do_not_fill_nodes)> | ||
| 584 | |||
| 585 | Reads the given tests yaml file (C<$yaml_file>). This YAML file should have at | ||
| 586 | least a list of tests and optionally can also have parameters the tests | ||
| 587 | should contain. | ||
| 588 | |||
| 589 | Although this method is used mostly internally there's the option to call it | ||
| 590 | as any other method I<Test::System> offers. | ||
| 591 | |||
| 592 | By default it will fill the parameters of your I<Test::System> instance but by | ||
| 593 | passing C<$do_not_fill_parameters> (second parameter) as true or something | ||
| 594 | that Perl understands as true then it will skip the part. This should be | ||
| 595 | presented as a hash in YAML syntax. | ||
| 596 | |||
| 597 | The above apply also to C<$do_not_fill_nodes> (third parameter). This should be | ||
| 598 | presented as an array in YAML syntax. | ||
| 599 | |||
| 600 | Once the file is read an array with all the tests will be returned. | ||
| 601 | |||
| 602 | All the tests should be contained inside a hash named 'tests'. All the tests | ||
| 603 | should be represented in the form of a list, each item of the list is the | ||
| 604 | test you want to execute. At least it should have a 'code' item, this is | ||
| 605 | the filepath of the test (or the ID from a C<test_groups>). Optionally you | ||
| 606 | can also provide the description of the test (cause otherwise the filename | ||
| 607 | will be used and it can make ugly your summary report). | ||
| 608 | |||
| 609 | An example of a YAML test plan file can be found inside the C<examples> | ||
| 610 | directory or: | ||
| 611 | |||
| 612 | tests: | ||
| 613 | - | ||
| 614 | code: ping | ||
| 615 | description: Checking the ping | ||
| 616 | - | ||
| 617 | code: example/tests/cpu.pl | ||
| 618 | description: foobar | ||
| 619 | - | ||
| 620 | code: memory | ||
| 621 | description: Ehmm.. my name? | ||
| 622 | parameters: | ||
| 623 | ping_count: 10 | ||
| 624 | nodes: | ||
| 625 | - pablo.com.mx | ||
| 626 | - example.com | ||
| 627 | |||
| 628 | =cut | ||
| 629 | sub get_tests_from_test_plan { | ||
| 630 | my ($self, $yaml_file, $do_not_fill_parameters, $do_not_fill_nodes) = @_; | ||
| 631 | |||
| 632 | if (!-f $yaml_file) { | ||
| 633 | confess "YAML file ($yaml_file) does not exist"; | ||
| 634 | return; | ||
| 635 | } | ||
| 636 | |||
| 637 | my @tests; | ||
| 638 | my $data = LoadFile($yaml_file); | ||
| 639 | if (!defined $data->{'tests'}) { | ||
| 640 | warn "YAML file ($yaml_file) does not have any tests to execute, skipping" | ||
| 641 | if $self->show_warnings; | ||
| 642 | return; | ||
| 643 | } else { | ||
| 644 | foreach my $test (@{$data->{'tests'}}) { | ||
| 645 | if (!defined $test->{'code'}) { | ||
| 646 | confess "Sorry but you need to provide the 'code' ID or " . | ||
| 647 | "filepath where your test is ($yaml_file)"; | ||
| 648 | } | ||
| 649 | if (defined $test->{'description'}) { | ||
| 650 | push(@tests, [ | ||
| 651 | $test->{'code'}, | ||
| 652 | $test->{'description'} | ||
| 653 | ]); | ||
| 654 | } else { | ||
| 655 | push(@tests, $test->{'code'}); | ||
| 656 | } | ||
| 657 | } | ||
| 658 | } | ||
| 659 | if (!$do_not_fill_parameters) { | ||
| 660 | if ($data->{'parameters'}) { | ||
| 661 | $self->parameters($data->{'parameters'}); | ||
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 665 | if (!$do_not_fill_nodes) { | ||
| 666 | if ($data->{'nodes'}) { | ||
| 667 | $self->nodes($data->{'nodes'}); | ||
| 668 | } | ||
| 669 | } | ||
| 670 | return @tests; | ||
| 671 | } | ||
| 672 | |||
| 673 | ################################# Triggers ################################## | ||
| 674 | # Trigger for test_dir, when test_dir gets modified we look for all tests | ||
| 675 | # available in the given directory and so on we populate/fill the tests list | ||
| 676 | # attribute | ||
| 677 | sub _read_test_groups_file { | ||
| 678 | my ($self, $yaml_file) = @_; | ||
| 679 | |||
| 680 | if (!-f $yaml_file) { | ||
| 681 | confess "The YAML file ($yaml_file) does not exist" | ||
| 682 | if $self->show_warnings; | ||
| 683 | } | ||
| 684 | |||
| 685 | $self->{test_groups} = $yaml_file; | ||
| 686 | my $testdir = dirname($yaml_file); | ||
| 687 | |||
| 688 | my $tests = LoadFile($yaml_file); | ||
| 689 | foreach my $test (keys %$tests) { | ||
| 690 | if (defined $tests->{$test}->{'code'}) { | ||
| 691 | my $code = $tests->{$test}->{'code'}; | ||
| 692 | if (!-f $code) { | ||
| 693 | $tests->{$test}->{'code'} = $testdir . '/' . $code; | ||
| 694 | } | ||
| 695 | } | ||
| 696 | } | ||
| 697 | $self->{'available_tests'} = $tests; | ||
| 698 | } | ||
| 699 | |||
| 700 | # Trigger for nodes, when nodes gets modified we don't really know if its | ||
| 701 | # a string or a list (and this is because we accept both) so we should make sure | ||
| 702 | # of what we get and validate it. | ||
| 703 | sub _verify_nodes_datatype { | ||
| 704 | my ($self, $nodes) = @_; | ||
| 705 | |||
| 706 | if (ref $nodes eq 'ARRAY') { | ||
| 707 | $self->{'nodes'} = join(',', @$nodes); | ||
| 708 | } else { | ||
| 709 | $self->{'nodes'} = $nodes; | ||
| 710 | } | ||
| 711 | } | ||
| 712 | |||
| 713 | # Trigger for format, when format gets modified we want to make sure the format | ||
| 714 | # is valid | ||
| 715 | sub _verify_format { | ||
| 716 | my ($self, $format) = @_; | ||
| 717 | |||
| 718 | if (!defined $self->available_formats->{$format}) { | ||
| 719 | confess "The format you provided ($format) is not valid"; | ||
| 720 | } | ||
| 721 | $self->{'format'} = $format; | ||
| 722 | } | ||
| 723 | |||
| 724 | # Fills the available_formats hash with the classes registered in the | ||
| 725 | # factory | ||
| 726 | sub _fill_available_formats_hash { | ||
| 727 | my ($self, $new_factory) = @_; | ||
| 728 | |||
| 729 | if (!$new_factory) { | ||
| 730 | $new_factory = 'Test::System::Output::Factory'; | ||
| 731 | } else { | ||
| 732 | $self->{'format_factory_class'} = $new_factory; | ||
| 733 | } | ||
| 734 | my @registered_types = "Test::System::Output::Factory"->get_registered_types; | ||
| 735 | |||
| 736 | my %hash; | ||
| 737 | foreach (@registered_types) { | ||
| 738 | $hash{$_} = 1; | ||
| 739 | } | ||
| 740 | $self->{'available_formats'} = \%hash; | ||
| 741 | return \%hash; | ||
| 742 | } | ||
| 743 | |||
| 744 | =head1 SEE ALSO | ||
| 745 | |||
| 746 | Take a look to an awesome and pretty similar CPAN module: L<Test::Server>. | ||
| 747 | |||
| 748 | =head1 AUTHOR | ||
| 749 | |||
| 750 | Pablo Fischer (pablo@pablo.com.mx). | ||
| 751 | |||
| 752 | =head1 COPYRIGHT | ||
| 753 | |||
| 754 | Copyright (C) 2009 by Pablo Fischer. | ||
| 755 | |||
| 756 | This library is free software; you can redistribute it and/or modify | ||
| 757 | it under the same terms as Perl itself. | ||
| 758 | |||
| 759 | =cut | ||
| 760 | |||
| 761 | 1; |