diff options
| author | Andreas Guldstrand <andreas.guldstrand@gmail.com> | 2016-06-19 02:15:47 +0200 | 
|---|---|---|
| committer | Andreas Guldstrand <andreas.guldstrand@gmail.com> | 2016-06-19 02:15:47 +0200 | 
| commit | 0720fc4a35284b99c2f7f619b0c0ffb4b3202449 (patch) | |
| tree | cf360d0dfbf479c3cb7395f788d07f04535136a8 /t | |
| parent | 7d94163380d43fa609681f6081e2d603e56f8e71 (diff) | |
| download | sbotools2-0720fc4a35284b99c2f7f619b0c0ffb4b3202449.tar.xz | |
Add race tests for sboconfig::config_write()
Diffstat (limited to 't')
| -rwxr-xr-x | t/29-race-sboconfig.t | 71 | 
1 files changed, 71 insertions, 0 deletions
diff --git a/t/29-race-sboconfig.t b/t/29-race-sboconfig.t new file mode 100755 index 0000000..d0f9d3d --- /dev/null +++ b/t/29-race-sboconfig.t @@ -0,0 +1,71 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use Test::Exit; +use FindBin '$RealBin'; +use lib "$RealBin/../SBO-Lib/lib"; +use Capture::Tiny qw/ capture_merged /; +use File::Temp 'tempdir'; +use Cwd; + +plan tests => 6; + +sub load { +	my ($script, %opts) = @_; + +	local @ARGV = exists $opts{argv} ? @{ $opts{argv} } : '-h'; +	my ($ret, $exit, $out, $do_err); +	my $eval = eval { +		$out = capture_merged { $exit = exit_code { +			$ret = do "$RealBin/../$script"; +			$do_err = $@; +		}; }; +		1; +	}; +	my $err = $@; + +	note explain { ret => $ret, exit => $exit, out => $out, eval => $eval, err => $err, do_err => $do_err } if $opts{explain}; +} + +# 1-4: sboconfig race test... +{ +	load('sboconfig'); + +	my $conffile = '/etc/sbotools/sbotools.conf'; +	rename $conffile, "$conffile.bak"; +	main::config_write('foo', 'bar'); +	no warnings 'redefine'; +	local *_race::cond = sub { unlink $conffile; }; + +	my $exit; +	my $out = capture_merged { $exit = exit_code { main::config_write('foo', 'bar'); }; }; + +	is ($out, "Unable to open /etc/sbotools/sbotools.conf.\n", "sboconfig's config_write() gave correct output"); +	is ($exit, 6, "sboconfig's config_write exited with 6"); + +	local *_race::cond = sub { mkdir $conffile; }; + +	undef $exit; +	$out = capture_merged { $exit = exit_code { main::config_write('foo', 'bar'); }; }; + +	is ($out, "Unable to open /etc/sbotools/sbotools.conf.\n", "sboconfig's config_write() gave correct output"); +	is ($exit, 6, "sboconfig's config_write exited with 6"); + +	rmdir $conffile; +	local *_race::cond = sub { 1; }; +	main::config_write('foo', 'bar'); +	my $cnt = 0; +	local *_race::cond = sub { do { unlink $conffile; mkdir $conffile } if $cnt++; }; + +	undef $exit; +	$out = capture_merged { $exit = exit_code { main::config_write('foo', 'baz'); }; }; + +	is ($out, "Unable to open /etc/sbotools/sbotools.conf.\n", "sboconfig's config_write() gave correct output"); +	is ($exit, 6, "sboconfig's config_write exited with 6"); + +	unlink $conffile; +	rmdir $conffile; +	rename "$conffile.bak", $conffile; +}  | 
