-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathUtil.pm
3057 lines (2426 loc) · 81.4 KB
/
Util.pm
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
package Test::Nginx::Util;
use strict;
use warnings;
our $VERSION = '0.30';
use base 'Exporter';
use POSIX qw( SIGQUIT SIGKILL SIGTERM SIGHUP );
use File::Spec ();
use HTTP::Response;
use Cwd qw( cwd );
use List::Util qw( shuffle );
use Time::HiRes qw( sleep );
use File::Path qw(make_path remove_tree);
use File::Find qw(find);
use File::Temp qw( tempfile :POSIX );
use Scalar::Util qw( looks_like_number );
use IO::Socket::INET;
use IO::Socket::UNIX;
use Test::LongString;
use POSIX ":sys_wait_h";
use Carp qw( croak );
our $ConfigVersion;
our $FilterHttpConfig;
our $RandPorts;
my $LoadedIPCRun;
our $NoLongString = undef;
our $FirstTime = 1;
our $ReusePort = $ENV{TEST_NGINX_REUSE_PORT};
our $UseHttp3 = $ENV{TEST_NGINX_USE_HTTP3};
our $UseHttp2 = $ENV{TEST_NGINX_USE_HTTP2};
our $UseHup = $ENV{TEST_NGINX_USE_HUP};
our $UseRr = $ENV{TEST_NGINX_USE_RR};
our $Verbose = $ENV{TEST_NGINX_VERBOSE};
our $LatestNginxVersion = 0.008039;
our $NoNginxManager = $ENV{TEST_NGINX_NO_NGINX_MANAGER} || 0;
our $Profiling = 0;
sub expand_env_in_text ($$$);
sub use_http2 ($);
sub use_http3 ($);
our $InSubprocess;
our $RepeatEach = 1;
our $MAX_PROCESSES = 10;
our $LoadModules = $ENV{TEST_NGINX_LOAD_MODULES};
our $NoShuffle = $ENV{TEST_NGINX_NO_SHUFFLE} || 0;
our $UseValgrind = $ENV{TEST_NGINX_USE_VALGRIND};
our $UseStap = $ENV{TEST_NGINX_USE_STAP};
our $StapOutFile = $ENV{TEST_NGINX_STAP_OUT};
our $EventType = $ENV{TEST_NGINX_EVENT_TYPE};
our $PostponeOutput = $ENV{TEST_NGINX_POSTPONE_OUTPUT};
our $Timeout = $ENV{TEST_NGINX_TIMEOUT} || 3;
our $QuicIdleTimeout = $ENV{TEST_NGINX_QUIC_IDLE_TIMEOUT} || 0.6;
our $CheckLeak = $ENV{TEST_NGINX_CHECK_LEAK} || 0;
our $Benchmark = $ENV{TEST_NGINX_BENCHMARK} || 0;
our $BenchmarkWarmup = $ENV{TEST_NGINX_BENCHMARK_WARMUP} || 0;
our $CheckAccumErrLog = $ENV{TEST_NGINX_CHECK_ACCUM_ERR_LOG};
our $ServerAddr = '127.0.0.1';
our $ServerName = 'localhost';
our $Http3SSLCrt = $ENV{TEST_NGINX_HTTP3_CRT};
our $Http3SSLCrtKey = $ENV{TEST_NGINX_HTTP3_KEY};;
our $ValgrindExtraTimeout = 0.3;
if ($UseHttp2 && $UseHttp3) {
die "Ambiguous: both TEST_NGINX_USE_HTTP3 and TEST_NGINX_USE_HTTP2 are set.\n";
}
sub bail_out (@);
if ($UseHttp3 && (!defined $Http3SSLCrt || !defined $Http3SSLCrtKey)) {
warn <<_EOC_;
Please generate the certificate/key pair using the following command,
and set TEST_NGINX_HTTP3_CRT and TEST_NGINX_HTTP3_KEY enviroment variables:
openssl req -x509 -newkey rsa:4096 -keyout http3.key -out http3.crt -days \\
7000 -nodes -subj '/CN=localhost' 2> /dev/null
_EOC_
die "certificate/key pair not found";
}
our $ServerConfigHttp3 = '';
our $WorkerUser = $ENV{TEST_NGINX_WORKER_USER};
if (defined $WorkerUser && $WorkerUser !~ /^\w[-\w]+(?:\s+\w[-\w]+)?$/) {
die "Bad value in the env TEST_NGINX_WORKER_USER: $WorkerUser\n";
}
our $StapOutFileHandle;
our @RandStrAlphabet = ('A' .. 'Z', 'a' .. 'z', '0' .. '9',
'#', '@', '-', '_', '^');
our $PrevBlock;
our $ErrLogFilePos;
if ($Benchmark) {
if ($UseStap) {
warn "WARNING: TEST_NGINX_BENCHMARK and TEST_NGINX_USE_STAP "
."are both set and the former wins.\n";
undef $UseStap;
}
if ($UseValgrind) {
warn "WARNING: TEST_NGINX_BENCHMARK and TEST_NGINX_USE_VALGRIND "
."are both set and the former wins.\n";
undef $UseValgrind;
}
if ($UseHup) {
warn "WARNING: TEST_NGINX_BENCHMARK and TEST_NGINX_USE_HUP "
."are both set and the former wins.\n";
undef $UseHup;
}
if ($CheckLeak) {
warn "WARNING: TEST_NGINX_BENCHMARK and TEST_NGINX_CHECK_LEAK "
."are both set and the former wins.\n";
undef $CheckLeak;
}
}
if ($CheckLeak) {
if ($UseStap) {
warn "WARNING: TEST_NGINX_CHECK_LEAK and TEST_NGINX_USE_STAP "
."are both set and the former wins.\n";
undef $UseStap;
}
if ($UseValgrind) {
warn "WARNING: TEST_NGINX_CHECK_LEAK and TEST_NGINX_USE_VALGRIND "
."are both set and the former wins.\n";
undef $UseValgrind;
}
if ($UseHup) {
warn "WARNING: TEST_NGINX_CHECK_LEAK and TEST_NGINX_USE_HUP "
."are both set and the former wins.\n";
undef $UseHup;
}
}
if ($UseHup) {
if ($UseStap) {
warn "WARNING: TEST_NGINX_USE_HUP and TEST_NGINX_USE_STAP "
."are both set and the former wins.\n";
undef $UseStap;
}
}
if ($UseValgrind) {
if ($UseStap) {
warn "WARNING: TEST_NGINX_USE_VALGRIND and TEST_NGINX_USE_STAP "
."are both set and the former wins.\n";
undef $UseStap;
}
}
#$SIG{CHLD} = 'IGNORE';
sub is_running ($) {
my $pid = shift;
return kill 0, $pid;
}
sub gen_rand_str {
my $len = shift;
my $s = '';
for (my $i = 0; $i < $len; $i++) {
my $j = int rand scalar @RandStrAlphabet;
my $c = $RandStrAlphabet[$j];
$s .= $c;
}
return $s;
}
sub gen_rand_port (;$$) {
my ($tries, $used_ports) = @_;
$tries //= 1000;
$used_ports //= {};
my $rand_port;
for (my $i = 0; $i < $tries; $i++) {
# NB: reserved ports for stream_server_config* (1..3)
# 1984 + 3 + 1 = 1988
my $port = int(rand 63547) + 1988;
next if $used_ports->{$port};
my $sock = IO::Socket::INET->new(
LocalAddr => $ServerAddr,
LocalPort => $port,
Proto => 'tcp',
Timeout => 0.1,
);
if (defined $sock) {
$sock->close();
$rand_port = $port;
last;
}
if ($Verbose) {
warn "Try again, port $port is already in use: $@\n";
}
}
return $rand_port;
}
sub is_udp_port_used($) {
my $port = shift;
my $filename = "/proc/net/udp";
open my $fh, $filename or die "Could not open $filename. $!";
while (<$fh>) {
my $line = $_;
if ($line =~ /^ *\d+: [0-9A-F]+:([0-9A-F]+) /) {
if ($port == hex($1)) {
close $fh;
return 1;
}
}
}
close $fh;
return 0;
}
sub is_tcp_port_used($) {
my $port = shift;
my $filename = "/proc/net/tcp";
open my $fh, $filename or die "Could not open $filename. $!";
while (<$fh>) {
my $line = $_;
if ($line =~ /^ *\d+: [0-9A-F]+:([0-9A-F]+) /) {
if ($port == hex($1)) {
close $fh;
return 1;
}
}
}
close $fh;
return 0;
}
sub no_long_string () {
$NoLongString = 1;
}
sub is_str (@) {
my ($got, $expected, $desc) = @_;
if (ref $expected && ref $expected eq 'Regexp') {
return Test::More::like($got, $expected, $desc);
}
if ($NoLongString) {
return Test::More::is($got, $expected, $desc);
}
return is_string($got, $expected, $desc);
}
sub server_addr (@) {
if (@_) {
#warn "setting server addr to $_[0]\n";
$ServerAddr = shift;
}
else {
return $ServerAddr;
}
}
sub server_name (@) {
if (@_) {
$ServerName = shift;
} else {
return $ServerName;
}
}
sub stap_out_fh {
return $StapOutFileHandle;
}
sub stap_out_fname {
return $StapOutFile;
}
sub timeout (@) {
if (@_) {
$Timeout = shift;
}
else {
$Timeout;
}
}
sub no_shuffle () {
$NoShuffle = 1;
}
sub no_nginx_manager () {
$NoNginxManager = 1;
}
sub use_hup() {
$UseHup = 1;
}
our @CleanupHandlers;
our @TestCleanupHandlers;
our @BlockPreprocessors;
our $Randomize = $ENV{TEST_NGINX_RANDOMIZE};
our $NginxBinary = $ENV{TEST_NGINX_BINARY} || 'nginx';
our $Workers = 1;
our $WorkerConnections = $ENV{TEST_NGINX_USE_HTTP3} ? 1024 : 64;
our $LogLevel = $ENV{TEST_NGINX_LOG_LEVEL} || 'debug';
our $MasterProcessEnabled = $ENV{TEST_NGINX_MASTER_PROCESS} || 'off';
our $DaemonEnabled = 'on';
our $ServerPort = $ENV{TEST_NGINX_SERVER_PORT} || $ENV{TEST_NGINX_PORT} || 1984;
our $ServerPortForClient = $ENV{TEST_NGINX_CLIENT_PORT} || $ServerPort || 1984;
our $NoRootLocation = 0;
our $TestNginxSleep = $ENV{TEST_NGINX_SLEEP} || 0.015;
our $BuildSlaveName = $ENV{TEST_NGINX_BUILDSLAVE};
our $ForceRestartOnTest = (defined $ENV{TEST_NGINX_FORCE_RESTART_ON_TEST})
? $ENV{TEST_NGINX_FORCE_RESTART_ON_TEST} : 1;
srand $$;
if ($Randomize) {
my $tries = 1000;
$ServerPort = gen_rand_port $tries;
if (!defined $ServerPort) {
bail_out "Cannot find an available listening port number after $tries attempts.\n";
}
$ServerPortForClient = $ServerPort;
}
our $ChildPid;
our $UdpServerPid;
our $TcpServerPid;
our @EnvToNginx;
sub env_to_nginx (@) {
if (!@_) {
croak "env_to_nginx: no arguments specified";
}
for my $v (@_) {
if ($v !~ /^[A-Za-z_]/ || $v =~ /\n/) {
croak "env_to_nginx: bad argument value: $v\n";
}
push @EnvToNginx, $v;
}
}
sub sleep_time {
return $TestNginxSleep;
}
sub verbose {
return $Verbose;
}
sub server_port (@) {
if (@_) {
$ServerPort = shift;
} else {
$ServerPort;
}
}
sub server_port_for_client (@) {
if (@_) {
$ServerPortForClient = shift;
} else {
$ServerPortForClient;
}
}
sub repeat_each (@) {
if (@_) {
if ($CheckLeak || $Benchmark) {
return;
}
$RepeatEach = shift;
}
return $RepeatEach;
}
sub worker_connections (@) {
if (@_) {
$WorkerConnections = shift;
} else {
return $WorkerConnections;
}
}
sub no_root_location () {
$NoRootLocation = 1;
}
sub workers (@) {
if (@_) {
#warn "setting workers to $_[0]";
$Workers = shift;
} else {
return $Workers;
}
}
sub log_level (@) {
if (@_) {
$LogLevel = shift;
} else {
return $LogLevel;
}
}
sub check_accum_error_log () {
$CheckAccumErrLog = 1;
}
sub master_on () {
if ($CheckLeak) {
return;
}
$MasterProcessEnabled = 'on';
}
sub master_off () {
$MasterProcessEnabled = 'off';
}
sub master_process_enabled (@) {
if ($CheckLeak) {
return;
}
if (@_) {
$MasterProcessEnabled = shift() ? 'on' : 'off';
} else {
return $MasterProcessEnabled;
}
}
our @EXPORT = qw(
expand_env_in_text
use_http2
use_http3
env_to_nginx
is_str
check_accum_error_log
is_running
$NoLongString
no_long_string
$ServerAddr
server_addr
$ServerName
server_name
parse_time
$UseStap
verbose
sleep_time
stap_out_fh
stap_out_fname
bail_out
add_cleanup_handler
add_test_cleanup_handler
access_log_data
error_log_data
setup_server_root
write_config_file
get_canon_version
get_nginx_version
trim
show_all_chars
parse_headers
run_tests
get_pid_from_pidfile
$ServerPortForClient
$ServerPort
$NginxVersion
$PcreVersion
$PidFile
$ServRoot
$ConfFile
$RunTestHelper
$CheckErrorLog
$CheckShutdownErrorLog
$FilterHttpConfig
$NoNginxManager
$RepeatEach
$CheckLeak
$Benchmark
$BenchmarkWarmup
add_block_preprocessor
timeout
worker_connections
workers
master_on
master_off
config_preamble
repeat_each
master_process_enabled
log_level
no_shuffle
no_root_location
html_dir
server_root
server_port
server_port_for_client
no_nginx_manager
use_hup
is_udp_port_used
is_tcp_port_used
);
if ($Profiling || $UseValgrind || $UseStap) {
$DaemonEnabled = 'off';
if (!$UseValgrind) {
$MasterProcessEnabled = 'off';
}
}
our $ConfigPreamble = '';
sub config_preamble ($) {
$ConfigPreamble = shift;
}
our $RunTestHelper;
our $CheckErrorLog;
our $CheckShutdownErrorLog;
our $NginxVersion;
our $PcreVersion;
our $NginxRawVersion;
our $OpenSSLVersion;
sub add_block_preprocessor(&) {
unshift @BlockPreprocessors, shift;
}
#our ($PrevRequest)
our $PrevConfig;
our $ServRoot;
if ($Randomize) {
$ServRoot = File::Spec->rel2abs("t/servroot_" . $ServerPort);
} else {
$ServRoot = $ENV{TEST_NGINX_SERVROOT} || File::Spec->rel2abs('t/servroot');
}
$ENV{TEST_NGINX_SERVER_ROOT} = $ServRoot;
our $LogDir = File::Spec->catfile($ServRoot, 'logs');
our $ErrLogFile = File::Spec->catfile($LogDir, 'error.log');
our $AccLogFile = File::Spec->catfile($LogDir, 'access.log');
our $HtmlDir = File::Spec->catfile($ServRoot, 'html');
our $ConfDir = File::Spec->catfile($ServRoot, 'conf');
our $ConfFile = File::Spec->catfile($ConfDir, 'nginx.conf');
our $PidFile = File::Spec->catfile($LogDir, 'nginx.pid');
our $CacheDir = File::Spec->catfile($ServRoot, 'cache');
sub parse_time ($) {
my $tm = shift;
if (defined $tm) {
if ($tm =~ s/([^_a-zA-Z])ms$/$1/) {
$tm = $tm / 1000;
} elsif ($tm =~ s/([^_a-zA-Z])s$/$1/) {
# do nothing
} else {
# do nothing
}
}
return $tm;
}
sub html_dir () {
return $HtmlDir;
}
sub server_root () {
return $ServRoot;
}
sub add_cleanup_handler ($) {
unshift @CleanupHandlers, shift;
}
sub add_test_cleanup_handler ($) {
unshift @TestCleanupHandlers, shift;
}
sub bail_out (@) {
cleanup();
Test::More::BAIL_OUT(@_);
}
sub kill_process ($$$) {
my ($pid, $wait, $name) = @_;
if ($wait) {
if (defined $pid) {
if ($ENV{TEST_NGINX_FAST_SHUTDOWN}) {
if ($Verbose) {
warn "sending TERM signal to $pid";
}
kill(SIGTERM, $pid);
} else {
if ($Verbose) {
warn "sending QUIT signal to $pid";
}
kill(SIGQUIT, $pid);
}
}
if ($Verbose) {
warn "waitpid timeout: ", timeout();
}
my $timeout_val = timeout();
while ($timeout_val > 0 && is_running($pid)) {
waitpid($pid, WNOHANG);
sleep 0.05;
$timeout_val -= 0.05;
}
if (is_running($pid)) {
warn "$name - timeout when waiting for the process $pid to exit";
if (getpgrp($pid) == $pid) {
kill(SIGKILL, -$pid);
sleep 0.05;
} else {
kill(SIGKILL, $pid);
waitpid($pid, 0);
}
}
}
my $i = 1;
my $step = $TestNginxSleep;
while ($i <= 20) {
#warn "ps returns: ", system("ps -p $pid > /dev/stderr"), "\n";
#warn "$pid is running? ", is_running($pid) ? "Y" : "N", "\n";
if (!is_running($pid)) {
return;
}
if ($Verbose) {
warn "WARNING: killing the child process $pid.\n";
}
if (kill(SIGTERM, $pid) == 0) { # send term signal
warn "WARNING: failed to send term signal to the child process with PID $pid.\n";
}
$step *= 1.2;
$step = 0.5 if $step > 0.5;
sleep $step;
} continue {
$i++;
}
#system("ps aux|grep $pid > /dev/stderr");
warn "$name - WARNING: killing the child process $pid with force...";
if (getpgrp($pid) == $pid) {
kill(SIGKILL, -$pid);
sleep 0.05;
} else {
kill(SIGKILL, $pid);
waitpid($pid, 0);
}
if (is_running($pid)) {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm timeout();
waitpid($pid, 0);
alarm 0;
}
}
sub cleanup_test ($) {
my $block = shift;
if ($Verbose) {
warn "cleaning up test ", $block->name;
}
for my $hdl (@TestCleanupHandlers) {
$hdl->($block);
}
}
sub cleanup () {
if ($Verbose) {
warn "cleaning up everything";
}
for my $hdl (@CleanupHandlers) {
$hdl->();
}
if (defined $UdpServerPid) {
kill_process($UdpServerPid, 1, "cleanup");
undef $UdpServerPid;
}
if (defined $TcpServerPid) {
kill_process($TcpServerPid, 1, "cleanup");
undef $TcpServerPid;
}
if (defined $ChildPid) {
kill_process($ChildPid, 1, "cleanup");
undef $ChildPid;
}
}
sub access_log_data () {
sleep $TestNginxSleep * 3;
open my $in, $AccLogFile or do {
if ($AccLogFile ne "off") {
die "open $AccLogFile failed: $!";
}
return undef;
};
my @lines = <$in>;
close $in;
return \@lines;
}
sub error_log_data () {
# this is for logging in the log-phase which is after the server closes the connection:
sleep $TestNginxSleep * 3;
open my $in, $ErrLogFile or
return undef;
if (!$CheckAccumErrLog && $ErrLogFilePos > 0) {
seek $in, $ErrLogFilePos, 0;
}
my @lines = <$in>;
if (!$CheckAccumErrLog) {
$ErrLogFilePos = tell($in);
}
close $in;
return \@lines;
}
sub check_prev_block_shutdown_error_log () {
my $block = $PrevBlock;
if (!defined $block) {
return;
}
my $name = $block->name;
my $dry_run;
if (defined $block->shutdown_error_log) {
if ($UseHup) {
$dry_run = 1;
}
if ($ENV{TEST_NGINX_NO_CLEAN}) {
$dry_run = 1;
}
$CheckShutdownErrorLog->($block, $dry_run);
}
}
sub run_tests () {
$NginxVersion = get_nginx_version();
if (defined $NginxVersion) {
#warn "[INFO] Using nginx version $NginxVersion ($NginxRawVersion)\n";
}
$PcreVersion = get_pcre_version();
if (!defined $ENV{TEST_NGINX_SERVER_PORT}) {
$ENV{TEST_NGINX_SERVER_PORT} = $ServerPort;
}
for my $block ($NoShuffle ? Test::Base::blocks() : shuffle Test::Base::blocks()) {
for my $hdl (@BlockPreprocessors) {
$hdl->($block);
}
$block->set_value("name", $0 . " " . $block->name);
run_test($block);
cleanup_test($block);
$PrevBlock = $block;
}
cleanup();
}
sub setup_server_root ($) {
my $first_time = shift;
if (-d $ServRoot) {
# Take special care, so we won't accidentally remove
# real user data when TEST_NGINX_SERVROOT is mis-used.
remove_tree($ConfDir, glob("$ServRoot/*_cache"),
glob("$ServRoot/*_temp"));
if ($UseHup) {
find({ bydepth => 1, no_chdir => 1, wanted => sub {
if (! -d $_) {
if ($_ =~ /(?:\bnginx\.pid|\.sock|\.crt|\.key)$/) {
return unless $first_time;
}
#warn "removing file $_";
system("rm $_") == 0 or warn "Failed to remove $_\n";
}
}}, $ServRoot);
} else {
remove_tree($CacheDir, $HtmlDir, $LogDir);
rmdir $ServRoot or
bail_out "Can't remove $ServRoot (not empty?): $!";
}
}
if (!-d $ServRoot) {
mkdir $ServRoot or
bail_out "Failed to do mkdir $ServRoot\n";
}
if (!-d $LogDir) {
mkdir $LogDir or
bail_out "Failed to do mkdir $LogDir\n";
}
if (!-d $HtmlDir) {
mkdir $HtmlDir or
bail_out "Failed to do mkdir $HtmlDir\n";
}
my $index_file = "$HtmlDir/index.html";
open my $out, ">$index_file" or
bail_out "Can't open $index_file for writing: $!\n";
print $out '<html><head><title>It works!</title></head><body>It works!</body></html>';
close $out;
mkdir $ConfDir or
bail_out "Failed to do mkdir $ConfDir\n";
}
sub write_user_files ($$) {
my ($block, $rand_ports) = @_;
my $name = $block->name;
my $files = $block->user_files;
if ($files) {
if (!ref $files) {
my $raw = $files;
open my $in, '<', \$raw;
$files = [];
my ($fname, $body, $date);
while (<$in>) {
if (/>>> (\S+)(?:\s+(.+))?/) {
if ($fname) {
push @$files, [$fname, $body, $date];
}
$fname = $1;
$date = $2;
undef $body;
} else {
$body .= $_;
}
}
if ($fname) {
push @$files, [$fname, $body, $date];
}
} elsif (ref $files ne 'ARRAY') {
bail_out "$name - wrong value type: ", ref $files,
", only scalar or ARRAY are accepted";
}
for my $file (@$files) {
my ($fname, $body, $date) = @$file;
#warn "write file $fname with content [$body]\n";
if (!defined $body) {
$body = '';
}
my $path;
if ($fname !~ m{^/}) {
$path = "$HtmlDir/$fname";
} else {
$path = $fname;
}
if ($path =~ /(.*)\//) {
my $dir = $1;
if (! -d $dir) {
make_path($dir) or bail_out "$name - Cannot create directory ", $dir;
}
}
$body = expand_env_in_text $body, $name, $rand_ports;
open my $out, ">$path" or
bail_out "$name - Cannot open $path for writing: $!\n";
binmode $out;
#warn "write file $path with data len ", length $body;
print $out $body;
close $out;
if ($date) {
my $cmd = "TZ=GMT touch -t '$date' $HtmlDir/$fname";
system($cmd) == 0 or
bail_out "Failed to run shell command: $cmd\n";
}
}
}
}
sub write_config_file ($$$) {
my ($block, $config, $rand_ports) = @_;
my $name = $block->name;
my $http_config = $block->http_config;