summaryrefslogtreecommitdiff
path: root/runtest.pl.in
blob: 2ab60963325287c44b55661cfec807087a37d61b (plain)
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
#!@PERL@

use strict;
use warnings;

use File::Basename;
use Getopt::Std;

chdir(dirname($0));
use lib dirname($0).'/infrastructure';

use BoxPlatform;

my $prepare_only = 0;
my $verbose_build = 0;
our ($opt_n, $opt_v);
getopts('nv');

# Don't actually run the test, just prepare for it.
$prepare_only = $opt_n;
$verbose_build = $opt_v;

my ($test_name,$test_mode) = @ARGV;
$test_mode = 'debug' if not defined $test_mode or $test_mode eq '';
$test_mode = lc($test_mode);

if($test_name eq '' || ($test_mode ne 'debug' && $test_mode ne 'release'))
{
	print <<__E;
Run Test utility -- bad usage.

runtest.pl (test|ALL) [release|debug]

Mode defaults to debug.

__E
	exit(2);
}

my @results;
my $exit_code = 0;

if($test_name ne 'ALL')
{
	# run one or more specified test
	if ($test_name =~ m/,/)
	{
		foreach my $test (split m/,/, $test_name)
		{
			runtest($test);
		}
	}
	else
	{
		runtest($test_name);
	}
}
else
{
	# run all tests
	my @tests;
	open MODULES,'modules.txt' or die "Can't open modules file";
	while(<MODULES>)
	{
		# omit bits on some platforms?
		next if m/\AEND-OMIT/;
		if(m/\AOMIT:(.+)/)
		{
			if($1 eq $build_os or $1 eq $ac_target_os)
			{
				while(<MODULES>)
				{
					last if m/\AEND-OMIT/;	
				}
			}
			next;
		}
		push @tests,$1 if m~\Atest/(\w+)\s~;
	}
	close MODULES;
	
	runtest($_) for(@tests)
}

# report results
print "--------\n",join("\n",@results),"\n";

if ($exit_code != 0)
{
	print <<__E;

One or more tests have failed. Please check the following common causes:

* Check that no instances of bbstored or bbackupd are already running
  on this machine.
* Make sure there isn't a firewall blocking incoming or outgoing connections
  on port 2201.
* Check that there is sufficient space in the filesystem that the tests
  are being run from (at least 1 GB free).
* The backupdiff test fails if it takes too long, so it's sensitive to
  the speed of the host and your connection to it.

After checking all the above, if you still have problems please contact
us on the mailing list, boxbackup\@boxbackup.org. Thanks!
__E
}

exit $exit_code;

sub runtest
{
	my ($t) = @_;

	# Attempt to make this test.
	my $flag = ($test_mode eq 'release')?(BoxPlatform::make_flag('RELEASE')):'';
	my ($make_res, $test_project_exe);

	if($target_msvc)
	{
		$test_project_exe = "test_$t";
		# Assume that MSVC projects are built with CMake, so we can use
		# MSBuild to run the tests.
		my $test_src_dir = "test\\$t";
		my $test_dst_dir = "$test_mode\\test\\$t";
		my $quiet = $verbose_build ? "" : "/consoleloggerparameters:ErrorsOnly";

		my @commands = (
			"msbuild /nologo $quiet ".
			"infrastructure\\cmake\\build\\INSTALL.vcxproj",
			"xcopy /s /i /y /q $test_src_dir $test_dst_dir",
			"copy infrastructure\\cmake\\build\\$test_mode\\$test_project_exe.exe $test_dst_dir"
		);

		if(-d $test_dst_dir)
		{
			unshift @commands, "rd /s /q $test_dst_dir";
		}

		foreach my $command (@commands)
		{
			$make_res = system($command);
			if ($make_res != 0)
			{ 
				push @results, "$t: make failed: $command";
				last; 
			}
		}

		# Windows doesn't support testextra files either, so fake it.
		if ($make_res == 0 and -r "$test_src_dir/testextra")
		{
			open EXTRA, "$test_src_dir/testextra"
				or die "$test_src_dir/testextra: $!";
			foreach my $line (<EXTRA>)
			{
				chomp $line;
				if ($line =~ m/^mkdir (.*)/)
				{
					mkdir("$test_dst_dir/$1")
						or die "$test_dst_dir/$1: $!";
				}
				elsif ($line =~ m/^rm -rf (.*)/)
				{
					if(-d "$test_dst_dir\\$1")
					{
						my $cmd = "rd /s/q $test_dst_dir\\$1";
						my $status = system($cmd);
						$status == 0 or die "$cmd: failed with ".
							"status $status";
					}
				}
				elsif ($line =~ m/^cp (.*) (.*)/)
				{
					my ($src, $dst) = ($1, $2);
					$src =~ s|/|\\|g;
					$dst =~ s|/|\\|g;
					my $cmd = "xcopy /s /i /y /q ".
						"$test_dst_dir\\$src $test_dst_dir\\$dst";
					my $status = system($cmd);
					$status == 0 or die "$cmd: failed with ".
						"status $status";
				}
				else
				{
					die "Unsupported command in ".
						"$test_src_dir/testextra: $line";
				}
			}
		}
	}
	else
	{
		my $quiet = $verbose_build ? "VERBOSE=1" : "";
		$make_res = system("cd test/$t && $make_command $quiet $flag");
	}

	if($make_res != 0)
	{
		push @results,"$t: make failed";
		$exit_code = 2;
		return;
	}

	my $logfile = "test-$t.log";
	my $test_res;

	if($prepare_only)
	{
		return;
	}
	
	# run it
	if($target_msvc)
	{
		# no tee.exe, so let's do it ourselves.
		open LOG, ">$logfile" or die "$logfile: $!";
		chdir("$test_mode/test/$t");
		open TEE, "$test_project_exe.exe |"
			or die "$test_project_exe.exe: $!";
		while (my $line = <TEE>)
		{
			print $line;
			print LOG $line;
		}
		close LOG;
		close TEE;
		chdir("../../..");
	}
	else
	{
		$test_res = system("cd $test_mode/test/$t ; ./t 2>&1 " .
			"| tee ../../../$logfile");
	}

	# open test results
	if(open RESULTS, $logfile)
	{
		my $last;
		while(<RESULTS>)
		{
			$last = $_ if m/\w/;
		}
		close RESULTS;

		chomp $last;
		$last =~ s/\r//;
		push @results, "$t: $last";

		if ($last ne "PASSED") 
		{ 
			$exit_code = 1;
		}
	}
	else
	{
		push @results, 
			"$t: failed to open test log file: $logfile: $!";
	}
	
	# delete test results
	# unlink $logfile;
}