-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwif.pl
executable file
·1555 lines (1206 loc) · 53.4 KB
/
wif.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# $Id$
# $Revision$
# $Date$
# -*- coding: utf-8 -*-
# perl
use v5.16;
use strict;
use warnings;
use vars qw/ $VERSION /;
$VERSION = '1.13.2';
# WebImblaze-Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WebImblaze-Framework is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. See the
# GNU General Public License for more details.
# Example:
# perl wif.pl ../WebImblaze/examples/demo.xml --target skynet --batch demonstration
# perl wif.pl ../WebImblaze/examples/command.xml --target skynet --batch allgood
# perl wif.pl ../WebImblaze/examples/abort.xml --target skynet --batch veryverybad
# perl wif.pl ../WebImblaze/examples/errormessage.xml --target skynet --batch notgood
# perl wif.pl ../WebImblaze/examples/corrupt.xml --target skynet --batch worstpossible
# perl wif.pl ../WebImblaze/examples/sleep.xml --target skynet --batch tired
# perl wif.pl ../WebImblaze/examples/selenium.xml --target skynet --batch gui
use Getopt::Long;
use File::Basename;
use File::Spec;
use File::Slurp; ## no critic (DiscouragedModules)
use File::Copy qw(copy), qw(move);
use File::Path qw(make_path remove_tree);
use Cwd;
use Time::HiRes 'time', 'sleep';
use Config::Tiny;
use XML::Simple; ## no critic (DiscouragedModules)
use XML::Twig;
require Data::Dumper;
my $start_folder_full = getcwd;
my ( undef, $this_script_folder_full, undef ) = fileparse( File::Spec->rel2abs(__FILE__) );
chdir $this_script_folder_full;
use lib q{.}; # current folder is not @INC from Perl 5.26
require lib::BatchSummary;
local $| = 1; # don't buffer output to STDOUT
my $is_windows = $^O eq 'MSWin32' ? 1 : 0;
my $DEFAULT_OUTPUT_LOCATION = './temp/';
my $DEFAULT_WEBIMBLAZE_LOCATION = '../WebImblaze';
my $LEGACY_DEFAULT_DRIVER = 'chrome';
my $DEFAULT_DRIVER = 'chromedriver';
# start globally read/write variables declaration - only variables declared here will be read/written directly from subs
my (
$opt_version, $opt_target, $opt_batch, $opt_environment, $opt_selenium_host,
$opt_selenium_port, $opt_headless, $opt_no_retry, $opt_help, $opt_keep,
$opt_keep_session, $opt_resume_session, $opt_capture_stdout, $opt_no_update_config, $opt_show_batch_url,
);
my ( $testfile_full, $testfile_name, $testfile_path, $testfile_parent_folder_name );
my ($config_is_automation_controller);
my ( $web_server_location_full, $web_server_address, $selenium_location_full, $driver, $chromedriver_location_full,
$webimblaze_location, $output_location );
my ($temp_folder_name);
my $config = Config::Tiny->new;
my $target_config = Config::Tiny->new;
my $global_config = Config::Tiny->new;
my $environment_config = Config::Tiny->new;
my ($std_fh);
# end globally read/write variables
# start globally read variables - will only be written to from the main script
my ( $yyyy, $mm, $dd, $hour, $minute, $second, $seconds ) =
get_date(0);
# get date for yesterday - needs to be calculated at script start, not at script end where it is used since it may run past midnight
my ( $yesterday_yyyy, $yesterday_mm, $yesterday_dd ) = get_date(-86_400);
my $today_home;
my $results_content;
# end globally read variables
get_options_and_config(); # get command line options
$today_home = "$web_server_location_full/$opt_environment/$yyyy/$mm/$dd";
show_batch_url();
# generate a random folder for the temporary files
$temp_folder_name = create_temp_folder();
# find out what run number we are up to today for this test file
my ( $run_number, $this_run_home ) = create_run_number();
capture_stdout($this_run_home);
# generate the config file, and find out where it is
my ( $config_file_full, $config_file_name, $config_file_path ) = create_webimblaze_config_file($run_number);
# indicate that WebImblaze is running the test file
write_pending_result($run_number);
# there is now a new item in the batch, so the overall summary of everything has to be rebuilt
build_summary_of_batches();
if ($is_windows) {
display_title_info( $testfile_name, $run_number, $config_file_name );
}
my $status = call_webimblaze_with_testfile( $config_file_full, $this_run_home );
write_final_result($run_number);
# the pending item in the batch is now final, so the overall summary of everything has to be rebuilt
build_summary_of_batches();
# ensure the style sheets, assets and manual files are on the web server
publish_static_files($run_number);
# tear down
remove_temp_folder( $temp_folder_name, $opt_keep );
restore_stdout();
if ( not $opt_capture_stdout ) {
print
"Result at: http://$web_server_address/$opt_environment/$yyyy/$mm/$dd/$testfile_parent_folder_name/$testfile_name/results_$run_number/results_$run_number.xml\n";
}
if ($status) { exit 1; }
else { exit 0; }
#------------------------------------------------------------------
sub call_webimblaze_with_testfile {
my ( $_config_file_full, $_this_run_home ) = @_;
my $_temp_folder_name = slash_me( $output_location . $temp_folder_name );
#print {*STDOUT} "config_file_full: [$_config_file_full]\n";
my $_abs_testfile_full = File::Spec->rel2abs($testfile_full);
my $_abs_config_file_full = File::Spec->rel2abs($_config_file_full);
my $_abs_temp_folder = slash_me( File::Spec->rel2abs($_temp_folder_name) . q{/} );
#print {*STDOUT} "\n_abs_testfile_full: [$_abs_testfile_full]\n";
#print {*STDOUT} "_abs_config_file_full: [$_abs_config_file_full]\n";
#print {*STDOUT} "_abs_temp_folder: [$_abs_temp_folder]\n";
my @_args;
push @_args, $_abs_testfile_full;
push @_args, '--publish-to';
push @_args, $_this_run_home;
if ($_abs_config_file_full) {
push @_args, '--config';
push @_args, $_abs_config_file_full;
}
if ($_abs_temp_folder) {
push @_args, '--output';
push @_args, $_abs_temp_folder;
}
if ( $config_is_automation_controller eq 'true' ) {
push @_args, '--autocontroller';
}
if ($opt_no_retry) {
push @_args, '--ignoreretry';
}
if ( defined $opt_capture_stdout || defined $opt_no_update_config ) {
push @_args, '--no-colour';
}
if ($selenium_location_full) {
push @_args, '--selenium-binary';
push @_args, $selenium_location_full;
}
if ($chromedriver_location_full) {
push @_args, '--chromedriver-binary';
push @_args, $chromedriver_location_full;
}
if ($opt_selenium_host) {
push @_args, '--selenium-host';
push @_args, $opt_selenium_host;
}
if ($opt_selenium_port) {
push @_args, '--selenium-port';
push @_args, $opt_selenium_port;
}
if ($opt_headless) {
push @_args, '--headless';
}
if ($opt_keep_session) {
push @_args, '--keep-session';
}
if ($opt_resume_session) {
push @_args, '--resume-session';
}
push @_args, '--driver';
push @_args, $driver;
# WebImblaze test cases expect the current working directory to be where wi.pl is
my $_orig_cwd = cwd;
chdir $webimblaze_location;
my $_status;
my $_wi_stdout_file_full = $_this_run_home . 'webimblaze_stdout.txt';
if ( defined $opt_capture_stdout ) {
print {*STDOUT} "\nLaunching wi.pl, STDOUT redirected to $_wi_stdout_file_full\n";
print {*STDOUT} 'perl wi.pl ' . "@_args\n";
$_status = system 'perl wi.pl ' . "@_args > $_wi_stdout_file_full 2>&1";
print {*STDOUT} "\nwi.pl execution all done.\n";
}
else {
# we run it like this so you can see test case execution progress "as it happens"
my $_message = 'Start wif.pl with --capture-stdout flag to capture wi.pl standard out' . "\n\n";
$_message .= 'WebImblaze started with args: ' . "@_args";
write_file( $_wi_stdout_file_full, $_message );
$_status = system 'perl wi.pl ' . "@_args";
}
chdir $_orig_cwd;
return $_status;
}
#------------------------------------------------------------------
sub capture_stdout {
my ($_output_location) = @_;
*OLD_STDOUT = *STDOUT;
*OLD_STDERR = *STDERR;
if ( defined $opt_capture_stdout ) {
open $std_fh, '>>', $_output_location . 'wif_stdout.txt'
or warn "Could not create a file for WIF STDOUT\n";
*STDOUT = $std_fh; ## no critic(Variables::RequireLocalizedPunctuationVars)
*STDERR = $std_fh; ## no critic(Variables::RequireLocalizedPunctuationVars)
print {*STDOUT} "\nWebImblaze Framework Config:\n";
print {*STDOUT} Data::Dumper::Dumper($config);
}
else {
write_file( $_output_location . 'wif_stdout.txt',
'Start wif.pl with --capture-stdout flag to capture wif.pl standard out' );
}
return;
}
#------------------------------------------------------------------
sub restore_stdout {
if ( defined $opt_capture_stdout ) {
*STDOUT = *OLD_STDOUT; ## no critic(Variables::RequireLocalizedPunctuationVars)
*STDERR = *OLD_STDERR; ## no critic(Variables::RequireLocalizedPunctuationVars)
}
return;
}
#------------------------------------------------------------------
sub get_date {
my ($_time_offset) = @_;
## put the specified date and time into variables - startdatetime - for recording the start time in a format an xsl stylesheet can process
my @_MONTHS = qw(01 02 03 04 05 06 07 08 09 10 11 12);
#my @_WEEKDAYS = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ( $_SECOND, $_MINUTE, $_HOUR, $_DAYOFMONTH, $_MONTH, $_YEAROFFSET, $_DAYOFWEEK, $_DAYOFYEAR, $_DAYLIGHTSAVINGS )
= localtime( time + $_time_offset );
my $_YEAR = 1900 + $_YEAROFFSET;
#my $_YY = substr $_YEAR, 2; #year as 2 digits
$_DAYOFMONTH = sprintf '%02d', $_DAYOFMONTH;
#my $_WEEKOFMONTH = int(($_DAYOFMONTH-1)/7)+1;
$_MINUTE = sprintf '%02d', $_MINUTE; #put in up to 2 leading zeros
$_SECOND = sprintf '%02d', $_SECOND;
$_HOUR = sprintf '%02d', $_HOUR;
my $_TIMESECONDS = ( $_HOUR * 60 * 60 ) + ( $_MINUTE * 60 ) + $_SECOND;
return $_YEAR, $_MONTHS[$_MONTH], $_DAYOFMONTH, $_HOUR, $_MINUTE, $_SECOND, $_TIMESECONDS;
}
#------------------------------------------------------------------
sub display_title_info {
my ( $_testfile_name, $_run_number, $_config_file_name ) = @_;
my $_result = `title temp\\$temp_folder_name $_config_file_name $_run_number:$_testfile_name`;
return;
}
#------------------------------------------------------------------
sub _start_windows_process {
my ($_command) = @_;
my $_wmic = "wmic process call create '$_command'"; #
my $_result = `$_wmic`;
#print "_wmic:$_wmic\n";
#print "$_result\n";
my $_pid;
if ( $_result =~ m/ProcessId = (\d+)/ ) {
$_pid = $1;
}
return $_pid;
}
#------------------------------------------------------------------
sub slash_me {
my ($_string) = @_;
if ($is_windows) {
$_string =~ s{/}{\\}g;
}
else {
$_string =~ s{\\}{/}g;
}
return $_string;
}
#------------------------------------------------------------------
sub __port_available {
my ($_port) = @_;
require Socket;
my $_family = Socket::PF_INET();
my $_type = Socket::SOCK_STREAM();
my $_proto = getprotobyname 'tcp' or die "getprotobyname: $!\n";
my $_host = Socket::INADDR_ANY(); # Use inet_aton for a specific interface
socket my $_sock, $_family, $_type, $_proto or die "socket: $!\n";
my $_name = Socket::sockaddr_in( $_port, $_host ) or die "sockaddr_in: $!\n";
if ( bind $_sock, $_name ) {
return 'available';
}
return 'in use';
}
sub _find_available_port {
my ($_start_port) = @_;
my $_max_attempts = 20;
foreach my $i ( 0 .. $_max_attempts ) {
if ( __port_available( $_start_port + $i ) eq 'available' ) {
return $_start_port + $i;
}
}
return 'none';
}
#------------------------------------------------------------------
sub _http_put {
my ( $_url, $_body ) = @_;
require HTTP::Request;
my $_agent = LWP::UserAgent->new;
my $_request = HTTP::Request->new( 'PUT', $_url );
$_request->content_type('application/x-www-form-urlencoded');
$_request->content($_body);
my $_response;
#print '_request:' . Data::Dumper::Dumper($_request). "\n\n";
my $_max = 50;
my $_try = 0;
ATTEMPT:
{
$_response = Data::Dumper::Dumper( $_agent->request($_request) );
if ( ( $_response =~ m/Can[\\]\'t connect to/ ) and $_try++ < $_max ) {
#print {*STDOUT} "WARN: cannot connect to $_url\n";
sleep 0.1;
redo ATTEMPT;
}
}
if ( not $_response =~ m/'_rc' => '200'/ ) {
print {*STDOUT} "\nERROR: Did not get http response 200 for PUT request\n";
print {*STDOUT} " url:$_url\n";
print {*STDOUT} " body:$_body\n\n";
}
#print "_response:$_response\n";
return;
}
#------------------------------------------------------------------
sub _http_post {
my ( $_url, @_body ) = @_;
my $_response;
my $_max = 50;
my $_try = 0;
ATTEMPT:
{
$_response = Data::Dumper::Dumper( LWP::UserAgent->new->post( $_url, \@_body ) );
if ( ( $_response =~ m/Can[\\]\'t connect to/ ) and $_try++ < $_max ) {
#print {*STDOUT} "WARN: cannot connect to $_url\n";
sleep 0.1;
redo ATTEMPT;
}
}
#print "_response:$_response\n";
return;
}
#------------------------------------------------------------------
sub publish_static_files {
my ($_run_number) = @_;
# favourite icon
_copy( 'content/root/*', $web_server_location_full );
# xsl and css stylesheets plus images
_make_path( $web_server_location_full . '/content/' );
_copy( 'content/*.css', $web_server_location_full . '/content/' );
_copy( 'content/*.xsl', $web_server_location_full . '/content/' );
_copy( 'content/*.jpg', $web_server_location_full . '/content/' );
# javascripts
_make_path( $web_server_location_full . '/scripts/' );
_copy( 'scripts/*.js', $web_server_location_full . '/scripts/' );
return;
}
#------------------------------------------------------------------
sub _copy {
my ( $_source, $_dest ) = @_;
my @_source_files = glob $_source;
foreach my $_source_file (@_source_files) {
if ( defined $opt_capture_stdout ) {
print "copy $_source_file, $_dest\n";
}
copy $_source_file, $_dest;
}
return;
}
#------------------------------------------------------------------
sub write_final_result {
my ($_run_number) = @_;
_write_final_record(
"$today_home/All_Batches/$opt_batch/$testfile_parent_folder_name" . '_'
. "$testfile_name" . '_'
. "$_run_number" . '.txt',
$_run_number,
);
_build_batch_summary();
return;
}
#------------------------------------------------------------------
sub _write_final_record {
my ( $_file_full, $_run_number ) = @_;
$results_content = read_file(
"$today_home/$testfile_parent_folder_name/$testfile_name/results_$_run_number/results_$_run_number.xml");
if ( $results_content =~ m{</test-summary>}i ) {
# WebImblaze ran to completion - all ok
}
else {
# WebImblaze did not start, or crashed part way through
_write_corrupt_record( $_file_full, $_run_number, 'WebImblaze abnormal end - run manually to diagnose' );
return;
}
# here we parse the xml file in an eval, and capture any error returned (in $@)
my $_message;
my $_result = eval { XMLin($results_content) };
if ($@) {
$_message = $@;
$_message =~ s{ at C:.*}{}g; # remove misleading reference Parser.pm
$_message =~ s{\n}{}g; # remove line feeds
#$_message =~ s/[&<>]//g;
$_message =~ s/[&]/{AMPERSAND}/g;
$_message =~ s/[<]/{LT}/g;
$_message =~ s/[>]/{GT}/g;
_write_corrupt_record( $_file_full, $_run_number, "$_message in results.xml" );
print {*STDOUT} "WebImblaze results.xml could not be parsed - CORRUPT\n";
return;
}
my $_start_time = $_result->{'test-summary'}->{'start-time'};
my $_start_seconds = $_result->{'test-summary'}->{'start-seconds'};
my $_start_date_time = $_result->{'test-summary'}->{'start-date-time'};
my $_total_run_time = $_result->{'test-summary'}->{'total-run-time'};
my $_max_response_time = $_result->{'test-summary'}->{'max-response-time'};
$_max_response_time = sprintf '%.1f', $_max_response_time;
my $_test_cases_run = $_result->{'test-summary'}->{'test-cases-run'};
#my $_test_cases_passed = $_result->{'test-summary'}->{'test-cases-passed'};
my $_test_cases_failed = $_result->{'test-summary'}->{'test-cases-failed'};
my $_assertion_skips = $_result->{'test-summary'}->{'assertion-skips'};
#my $_verifications_passed = $_result->{'test-summary'}->{'verifications-passed'};
#my $_verifications_failed = $_result->{'test-summary'}->{'verifications-failed'};
my $_execution_aborted = $_result->{'test-summary'}->{'execution-aborted'};
my ( $_yyyy, $_mm, $_dd, $_hour, $_minute, $_second, $_seconds ) = get_date(0);
my $_end_date_time = "$_yyyy-$_mm-$_dd" . 'T' . "$_hour:$_minute:$_second";
my $_record;
$_record .= qq| <run id="$opt_batch">\n|;
$_record .= qq| <batch_name>$opt_batch</batch_name>\n|;
$_record .= qq| <environment>$opt_environment</environment>\n|;
$_record .= qq| <test_parent_folder>$testfile_parent_folder_name</test_parent_folder>\n|;
$_record .= qq| <test_name>$testfile_name</test_name>\n|;
$_record .= qq| <total_run_time>$_total_run_time</total_run_time>\n|;
$_record .= qq| <test_steps_failed>$_test_cases_failed</test_steps_failed>\n|;
$_record .= qq| <test_steps_run>$_test_cases_run</test_steps_run>\n|;
$_record .= qq| <assertion_skips>$_assertion_skips</assertion_skips>\n|;
$_record .= qq| <execution_aborted>$_execution_aborted</execution_aborted>\n|;
$_record .= qq| <max_response_time>$_max_response_time</max_response_time>\n|;
$_record .= qq| <target>$opt_target</target>\n|;
$_record .= qq| <yyyy>$yyyy</yyyy>\n|;
$_record .= qq| <mm>$mm</mm>\n|;
$_record .= qq| <dd>$dd</dd>\n|;
$_record .= qq| <run_number>$_run_number</run_number>\n|;
$_record .= qq| <start_time>$_start_time</start_time>\n|;
$_record .= qq| <start_date_time>$_start_date_time</start_date_time>\n|;
$_record .= qq| <end_time>$_end_date_time</end_time>\n|;
$_record .= qq| <start_seconds>$_start_seconds</start_seconds>\n|;
$_record .= qq| <end_seconds>$_seconds</end_seconds>\n|;
$_record .= qq| <status>NORMAL</status>\n|;
$_record .= qq| </run>\n|;
_write_file( $_file_full, $_record );
return;
}
#------------------------------------------------------------------
sub _write_corrupt_record {
my ( $_file_full, $_run_number, $_message ) = @_;
my $_record;
my $_start_date_time = "$yyyy-$mm-$dd" . 'T' . "$hour:$minute:$second";
my ( $_yyyy, $_mm, $_dd, $_hour, $_minute, $_second, $_seconds ) = get_date(0);
my $_end_date_time = "$_yyyy-$_mm-$_dd" . 'T' . "$_hour:$_minute:$_second";
$_record .= qq| <run id="$opt_batch">\n|;
$_record .= qq| <batch_name>$opt_batch</batch_name>\n|;
$_record .= qq| <environment>$opt_environment</environment>\n|;
$_record .= qq| <test_parent_folder>$testfile_parent_folder_name</test_parent_folder>\n|;
$_record .= qq| <test_name>$testfile_name</test_name>\n|;
$_record .= qq| <total_run_time>0</total_run_time>\n|;
$_record .= qq| <test_steps_failed>0</test_steps_failed>\n|;
$_record .= qq| <test_steps_run>0</test_steps_run>\n|;
$_record .= qq| <assertion_skips></assertion_skips>\n|;
$_record .= qq| <execution_aborted>false</execution_aborted>\n|;
$_record .= qq| <max_response_time>0.0</max_response_time>\n|;
$_record .= qq| <target>$opt_target</target>\n|;
$_record .= qq| <yyyy>$yyyy</yyyy>\n|;
$_record .= qq| <mm>$mm</mm>\n|;
$_record .= qq| <dd>$dd</dd>\n|;
$_record .= qq| <run_number>$_run_number</run_number>\n|;
$_record .= qq| <start_time>unused</start_time>\n|;
$_record .= qq| <start_date_time>$_start_date_time</start_date_time>\n|;
$_record .= qq| <end_time>$_end_date_time</end_time>\n|;
$_record .= qq| <start_seconds>$seconds</start_seconds>\n|;
$_record .= qq| <end_seconds>$_seconds</end_seconds>\n|;
$_record .= qq| <status>CORRUPT</status>\n|;
$_record .=
qq| <status_message>[$testfile_parent_folder_name/$testfile_name results.xml] $_message</status_message>\n|;
$_record .= qq| </run>\n|;
_write_file( $_file_full, $_record );
return;
}
#------------------------------------------------------------------
sub build_summary_of_batches {
# multiple batches could be running at the same time
my $_overall_summary_full = "$today_home/All_Batches/Summary.xml";
_lock_file($_overall_summary_full);
my $_batch_summary_record_full = "$today_home/All_Batches/$opt_batch" . '_summary.record';
_lock_file($_batch_summary_record_full)
; # possibly unecessary to do this since a higher level file already has a lock
_write_summary_record($_batch_summary_record_full);
# create an array containing all files representing summary records for all of todays batches
my @_summary_records = glob( "$today_home/All_Batches/" . '*.record' );
# sort by date modified, ascending (-C would be date created)
my @_sorted_summary = sort { -M $a <=> -M $b } @_summary_records;
# write the header
my $_summary = qq|<?xml version="1.0" encoding="ISO-8859-1"?>\n|;
# $_summary .= qq|<?xml-stylesheet type="text/xsl" href="http://$web_server_address/content/Summary.xsl"?>\n|;
$_summary .=
qq|<?xml-stylesheet type="text/xsl" href="/content/Summary.xsl"?>\n|; ##trial domain relative url for Summary.xsl
$_summary .= qq|<summary version="2.0">\n|;
$_summary .= qq| <channel>\n|;
# write all the batch records
foreach (@_sorted_summary) {
$_summary .= read_file($_);
}
# write the footer
$_summary .= qq| </channel>\n|;
$_summary .= qq|</summary>\n|;
# save the file to todays area, and a copy to environment root
_write_file( $_overall_summary_full, $_summary );
_write_file( $web_server_location_full . "/$opt_environment/Summary.xml", $_summary );
# unlock the locked files
_unlock_file($_batch_summary_record_full);
_unlock_file($_overall_summary_full);
return;
}
#------------------------------------------------------------------
sub _write_summary_record {
my ($_file_full) = @_;
my $_twig;
my $_max = 5;
my $_try = 0;
ATTEMPT:
{
$_twig = XML::Twig->new();
eval { $_twig->parsefile( "$today_home/All_Batches/$opt_batch" . '.xml' ); }; # eval needs a semicolon
if ( $@ and $_try++ < $_max ) {
print {*STDOUT} "WARN: $@ Failed try $_try to parse" . "$today_home/All_Batches/$opt_batch" . ".xml\n";
sleep rand $_try;
redo ATTEMPT;
}
}
BatchSummary::_calculate_stats($_twig);
my $_record;
$_record .= qq| <title>$opt_environment Summary</title>\n|;
$_record .=
qq| <link>http://$web_server_address/$opt_environment/$yesterday_yyyy/$yesterday_mm/$yesterday_dd/All_Batches/Summary.xml</link>\n|;
$_record .= qq| <description>WebImblaze Framework Batch Summary</description>\n|;
$_record .= qq| <item>\n|;
$_record .= q| <title>|;
$_record .= BatchSummary::_build_overall_summary_text( $opt_batch, $opt_target, $dd, $mm );
$_record .= qq|</title>\n|;
$_record .=
qq| <link>http://$web_server_address/$opt_environment/$yyyy/$mm/$dd/All_Batches/$opt_batch.xml</link>\n|;
$_record .= qq| <description></description>\n|;
$_record .= qq| <pubDate>$dd/$mm/$yyyy $hour:$minute</pubDate>\n|;
$_record .= qq| </item>\n|;
_write_file( $_file_full, $_record );
return;
}
#------------------------------------------------------------------
sub write_pending_result {
my ($_run_number) = @_;
_make_path("$today_home/All_Batches/$opt_batch");
_write_pending_record(
"$today_home/All_Batches/$opt_batch/$testfile_parent_folder_name" . '_'
. "$testfile_name" . '_'
. "$_run_number" . '.txt',
$_run_number,
);
_build_batch_summary();
return;
}
#------------------------------------------------------------------
sub _build_batch_summary {
# lock batch xml file so parallel instances of wif.pl cannot update it
my $_batch_full = "$today_home/All_Batches/$opt_batch" . '.xml';
_lock_file($_batch_full);
# create an array containing all files representent runs in the batch
my @_runs = glob( "$today_home/All_Batches/$opt_batch/" . '*.txt' );
# sort by date created, ascending
my @_sorted_runs = reverse sort { -C $a <=> -C $b } @_runs;
# write the header
my $_batch = qq|<?xml version="1.0" encoding="ISO-8859-1"?>\n|;
$_batch .= qq|<?xml-stylesheet type="text/xsl" href="/content/Batch.xsl"?>\n|;
$_batch .= qq|<batch>\n|;
# write all the run records
foreach (@_sorted_runs) {
$_batch .= read_file($_);
}
# write the footer
$_batch .= qq|</batch>\n|;
# dump batch xml file from memory into file system
_write_file( "$today_home/All_Batches/$opt_batch" . '.xml', $_batch );
# unlock batch xml file
_unlock_file($_batch_full);
return;
}
#------------------------------------------------------------------
sub _write_file {
my ( $_file_full, $_file_content ) = @_;
my $_max = 10;
my $_try = 0;
ATTEMPT:
{
eval {
open my $_FILE, '>', "$_file_full" or die "\nWARN: Failed to open $_file_full for writing\n\n";
print {$_FILE} $_file_content;
close $_FILE or die "\nWARN: Failed to close $_file_full\n\n";
}; # eval needs a semicolon
if ( $@ and $_try++ < $_max ) {
print {*STDOUT} "WARN: $@ Failed try $_try to write file\n";
sleep rand $_try;
redo ATTEMPT;
}
if ($@) {
print {*STDOUT} "ERROR: Failed $_max attempts to write file $_file_full, gave up.\n";
}
}
print {*STDOUT} "...(try $_try) wrote file $_file_full \n" if defined $opt_capture_stdout;
return;
}
#------------------------------------------------------------------
sub _write_pending_record {
my ( $_file_full, $_run_number ) = @_;
my $_record;
my $_start_date_time = "$yyyy-$mm-$dd" . 'T' . "$hour:$minute:$second";
$_record .= qq| <run id="$opt_batch">\n|;
$_record .= qq| <batch_name>$opt_batch</batch_name>\n|;
$_record .= qq| <environment>$opt_environment</environment>\n|;
$_record .= qq| <test_parent_folder>$testfile_parent_folder_name</test_parent_folder>\n|;
$_record .= qq| <test_name>$testfile_name</test_name>\n|;
$_record .= qq| <total_run_time>0</total_run_time>\n|;
$_record .= qq| <test_steps_failed>0</test_steps_failed>\n|;
$_record .= qq| <test_steps_run>0</test_steps_run>\n|;
$_record .= qq| <assertion_skips></assertion_skips>\n|;
$_record .= qq| <execution_aborted>false</execution_aborted>\n|;
$_record .= qq| <max_response_time>0.0</max_response_time>\n|;
$_record .= qq| <target>$opt_target</target>\n|;
$_record .= qq| <yyyy>$yyyy</yyyy>\n|;
$_record .= qq| <mm>$mm</mm>\n|;
$_record .= qq| <dd>$dd</dd>\n|;
$_record .= qq| <run_number>$_run_number</run_number>\n|;
$_record .= qq| <start_time>unused</start_time>\n|;
$_record .= qq| <start_date_time>$_start_date_time</start_date_time>\n|;
$_record .= qq| <end_time>PENDING</end_time>\n|;
$_record .= qq| <start_seconds>$seconds</start_seconds>\n|;
$_record .= qq| <end_seconds>$seconds</end_seconds>\n|;
$_record .= qq| <status>NORMAL</status>\n|;
$_record .= qq| </run>\n|;
_write_file( $_file_full, $_record );
return;
}
#------------------------------------------------------------------
sub deprecated_check_testfile_xml_parses_ok {
my $_xml = read_file($testfile_full);
# for convenience, WebImblaze allows ampersand and less than to appear in xml data, so this needs to be masked
$_xml =~ s/&/{AMPERSAND}/g;
while ( $_xml =~ s/\w\s*=\s*"[^"]*\K<(?!case)([^"]*")/{LESSTHAN}$1/sg ) { }
while ( $_xml =~ s/\w\s*=\s*'[^']*\K<(?!case)([^']*')/{LESSTHAN}$1/sg ) { }
# here we parse the xml file in an eval, and capture any error returned (in $@)
my $_message;
my $_result = eval { XMLin($_xml) };
if ($@) {
$_message = $@;
$_message =~ s{XML::Simple.*\n}{}g; # remove misleading line number reference
die "\n" . $_message . " in $testfile_full\n";
}
return;
}
#------------------------------------------------------------------
sub _make_path {
my ($_path) = @_;
make_path( "$_path", { error => \my $err } );
if (@$err) { die "\nThis user account needs permission to create $_path\n" }
return;
}
#------------------------------------------------------------------
sub create_run_number {
if ( not -e "$web_server_location_full" ) {
die "\nWeb server location of $web_server_location_full does not exist\n";
}
# if they do not exist already, folders are created for this test file for todays date
_make_path("$web_server_location_full/$opt_environment/$yyyy/$mm");
_make_path("$today_home/$testfile_parent_folder_name/$testfile_name");
my $_run_number_full = "$today_home/$testfile_parent_folder_name/$testfile_name/Run_Number.txt";
_lock_file($_run_number_full);
my $_run_number = _increment_run_number($_run_number_full);
_unlock_file($_run_number_full);
# create a folder for this run number
my $_this_run_home = "$today_home/$testfile_parent_folder_name/$testfile_name/results_$_run_number/";
_make_path($_this_run_home);
return $_run_number, $_this_run_home;
}
#------------------------------------------------------------------
sub _increment_run_number {
my ($_run_number_full) = @_;
my $_run_number;
if ( -e $_run_number_full ) {
my $_run_number_string = read_file($_run_number_full);
if ( $_run_number_string =~ m/([\d]*)/ ) {
$_run_number = $1;
}
}
if ( !$_run_number ) {
$_run_number = 1000;
}
$_run_number++;
open my $RUNFILE, '>', "$_run_number_full" or die "\nERROR: Failed to create $_run_number_full\n\n";
print {$RUNFILE} "$_run_number";
close $RUNFILE or die "\nERROR: Failed to close $_run_number_full\n\n";
return $_run_number;
}
#------------------------------------------------------------------
sub _unlock_file {
my ($_file_to_unlock_full) = @_;
my $_unlocked_file_indicator = $_file_to_unlock_full . '_Unlocked';
my $_locked_file_indicator = _prepend_to_filename( 'Locked_', $_file_to_unlock_full );
#if (defined $opt_capture_stdout) {
# print " move\n $_locked_file_indicator".'_'."$temp_folder_name\n ".$_unlocked_file_indicator."\n\n";
#}
move $_locked_file_indicator. "_$temp_folder_name", $_unlocked_file_indicator;
return;
}
#------------------------------------------------------------------
sub _lock_file {
my ($_file_to_lock_full) = @_;
# first the file we want to lock may not even exist, in which case we create it
my $_unlocked_file_indicator = $_file_to_lock_full . '_Unlocked';
my $_locked_file_indicator = _prepend_to_filename( 'Locked_', $_file_to_lock_full );
if ( not -e "$_unlocked_file_indicator" ) {
#print "file is not unlocked: $_unlocked_file_indicator\n";
# file is not unlocked
if ( not glob( "$_locked_file_indicator" . '_*' ) ) {
#print "file is not locked either: $_locked_file_indicator".'_*'."\n";
# file is not locked either, so it must not exist
# so we can create a file indicating that the file is unlocked
_touch($_unlocked_file_indicator);
}
}
# now to actually lock the file
# we do this by renaming the Unlocked indicator file to the name Locked_filename.filesufix_tempfoldername
# the temp folder name is unique to this process
my $_max = 6;
my $_try = 0;
ATTEMPT:
{
eval {
#print "move\n$_unlocked_file_indicator\n"."$_locked_file_indicator".'_'."$temp_folder_name\n\n";
move $_unlocked_file_indicator, $_locked_file_indicator . '_' . $temp_folder_name
or die "Cannot lock file\n";
}; # eval needs a semicolon
# if we failed to lock the file but there are attempts remaining
if ( $@ and $_try++ < $_max ) {
print {*STDOUT}
"WARN: $@ Failed try $_try to lock $_file_to_lock_full\n Want lock :$_locked_file_indicator" . q{_}
. "$temp_folder_name\n";
my @_locked = glob $_locked_file_indicator . '_*';
foreach (@_locked) {
print ' found lock:' . $_ . "\n";
}
if ( not -e $_unlocked_file_indicator ) {
print ' no unlock :' . $_unlocked_file_indicator . "\n";
}
else {
print ' unlocked!!:' . $_unlocked_file_indicator . "\n";
}
sleep rand $_try;
redo ATTEMPT;
}
}
if ($@) {
# we can get here if a parallel process aborts at exactly the wrong spot
# so we forcibly lock the file
print {*STDOUT} "\nError: $@ Failed to lock $_file_to_lock_full after $_max tries\n\n";
print {*STDOUT} "Executing fail safe - deleting lock and creating our own lock\n\n";
unlink glob( $_locked_file_indicator . '_*' );
unlink $_unlocked_file_indicator;
_touch( $_locked_file_indicator . '_' . $temp_folder_name );
# this might screw things up a little, but it prevents things being screwed up a lot (i.e. no more test results for the rest of the day)
# people just have to rerun their most recent tests and all should be fine
}
else {