aboutsummaryrefslogtreecommitdiff
path: root/t/29-race-sboconfig.t
blob: d0f9d3dee8517691e774b58c4b3ecac63318b818 (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
#!/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;
}