sbotools2

Maintenance fork of the original sbotools version 2
git clone git://git.server.ky/slackcoder/sbotools2
Log | Files | Refs | README

22-race.t (3251B)


      1 #!/usr/bin/env perl
      2 
      3 use strict;
      4 use warnings;
      5 use Test::More;
      6 use Test::Exit;
      7 use FindBin '$RealBin';
      8 use lib "$RealBin/../SBO-Lib/lib";
      9 use SBO::Lib qw/ open_fh %config /;
     10 use Capture::Tiny qw/ capture_merged /;
     11 use File::Temp 'tempdir';
     12 use Cwd;
     13 
     14 plan tests => 8;
     15 
     16 sub emulate_race {
     17 	my ($file, $caller) = @_;
     18 	$caller = "SBO::Lib::$caller";
     19 
     20 	no warnings 'redefine';
     21 	*_race::cond = sub { unlink $file if $caller eq (caller(1))[3]; };
     22 }
     23 
     24 # 1: emulate race condition for open_fh
     25 {
     26 	my $tempdir = tempdir(CLEANUP => 1);
     27 	my $file = "$tempdir/foo";
     28 	system('touch', $file);
     29 
     30 	emulate_race($file, 'Util::open_fh');
     31 
     32 	my ($fh, $exit) = open_fh $file, '<';
     33 	is ($exit, 6, 'open_fh returned exit value 6');
     34 }
     35 
     36 # 2-3: emulate race in open_fh by get_slack_version
     37 {
     38 	my $sv_file = '/etc/slackware-version';
     39 	capture_merged {
     40 		system('mkdir', '-p', '/etc');
     41 		system('mv', $sv_file, "$sv_file.bak");
     42 		system('touch', $sv_file);
     43 	};
     44 
     45 	my $exit;
     46 	emulate_race($sv_file, 'Util::open_fh');
     47 	local $config{SLACKWARE_VERSION} = 'FALSE';
     48 	my $out = capture_merged { $exit = exit_code { SBO::Lib::get_slack_version(); }; };
     49 
     50 	is ($exit, 6, 'get_slackware_version() exited with correct exitcode');
     51 	is ($out, "Unable to open $sv_file.\n", 'get_slackware_version output correct');
     52 
     53 	system('mv', "$sv_file.bak", $sv_file) if -e "$sv_file.bak";
     54 }
     55 
     56 # 4-7: emulate races in git_sbo_tree
     57 SKIP: {
     58 	my $tempdir = tempdir(CLEANUP => 1);
     59 
     60 	my $repo = '/usr/sbo/repo';
     61 	system('mkdir', '-p', $repo);
     62 	system('mv', $repo, "$repo.bak");
     63 
     64 	capture_merged { system <<"GIT"; };
     65 		cd $tempdir
     66 		git init
     67 		mkdir -p test
     68 		cp -R "$RealBin/LO/nonexistentslackbuild" test
     69 		git add test
     70 		git commit -m 'added test/nonexistentslackbuild'
     71 
     72 		cd /usr/sbo
     73 		git clone file://$tempdir repo
     74 GIT
     75 
     76 	no warnings 'redefine';
     77 	*_race::cond = sub { system('rm', '-rf', $repo) if $_[0] eq '$repo_path can be deleted after -d check' };
     78 
     79 	my $res;
     80 	my $out = capture_merged { $res = SBO::Lib::git_sbo_tree("file://$tempdir", ''); };
     81         note($out);
     82 
     83 	is ($out, '', 'git_sbo_tree() no output');
     84 	is ($res, 0, 'git_sbo_tree() returned 0');
     85 
     86 	capture_merged { system('git', 'clone', "file://$tempdir", $repo); };
     87 	my $cwd = getcwd();
     88 	mkdir "$tempdir/bar";
     89 	chdir "$tempdir/bar";
     90 	*_race::cond = sub {
     91 		system('rm', '-rf', "$repo/.git") if $_[0] eq 'git repo could be changed or deleted here';
     92 		system('rmdir', "$tempdir/bar") if $_[0] eq '$cwd could be deleted here';
     93 	};
     94 
     95 	undef $res;
     96 	$out = capture_merged { $res = SBO::Lib::git_sbo_tree("file://$tempdir", ''); };
     97 
     98 	is ($out, "fatal: not a git repository (or any of the parent directories): .git\n", 'git_sbo_tree() gave correct output');
     99 	is ($res, 0, 'git_sbo_tree() returned 0');
    100 
    101 	chdir $cwd;
    102 	system('rm', '-rf', $repo);
    103 	system('mv', "$repo.bak", $repo);
    104 }
    105 
    106 # 8: emulate race in read_config
    107 {
    108   my $conf_file = "/etc/sbotools/sbotools.conf";
    109 
    110   mkdir "/etc/sbotools";
    111   rename $conf_file, "$conf_file.bak";
    112   system touch => $conf_file;
    113 
    114   no warnings 'redefine';
    115 
    116   local *SBO::Lib::Util::open_read = sub { return undef, 1 };
    117 
    118   my $out = capture_merged { SBO::Lib::Util::read_config(); };
    119 
    120   is ($out, "Unable to open $conf_file.\n", "read_config() output correct");
    121 
    122   unlink $conf_file;
    123   rename "$conf_file.bak", $conf_file;
    124 }