regressions: Allow test-launch-race.pl to be skipped.
[libguestfs.git] / regressions / test-launch-race.pl
1 #!/usr/bin/perl
2 # Copyright (C) 2010 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 # Test that 2 simultaneous launches in a clean cache directory will both succeed
19
20 use strict;
21 use warnings;
22
23 use File::Temp qw(tempdir);
24 use POSIX;
25
26 use Sys::Guestfs;
27
28 # Allow this test to be skipped.
29 exit 0 if $ENV{SKIP_TEST_LAUNCH_RACE_PL};
30
31 # Use a temporary TMPDIR to ensure it's clean
32 my $tmpdir = tempdir (CLEANUP => 1);
33 $ENV{TMPDIR} = $tmpdir;
34
35 my $testimg = $tmpdir.'/test.img';
36 system ("touch $testimg");
37
38 my $pid = fork();
39 die ("fork failed: $!") if ($pid < 0);
40
41 if ($pid == 0) {
42   my $g = Sys::Guestfs->new ();
43   $g->add_drive ($testimg);
44   $g->launch ();
45   _exit (0);
46 }
47
48 my $g = Sys::Guestfs->new ();
49 $g->add_drive ($testimg);
50 $g->launch ();
51 $g = undef;
52
53 waitpid ($pid, 0) or die ("waitpid: $!");
54 die ("child failed") unless ($? == 0);
55
56 # Check that only 1 temporary cache directory was created.
57 #
58 # No cache directory is OK too (as long as the appliance launched w/o
59 # failure) because it indicates we're not using supermin.
60 my $dh;
61 opendir ($dh, $tmpdir) or die ("Failed to open $tmpdir: $!");
62 my @cachedirs = grep { /^guestfs\./ } readdir ($dh);
63 closedir ($dh) or die ("Failed to close $tmpdir: $!");
64
65 my $ncachedirs = scalar(@cachedirs);
66 die "Expected 0 or 1 cachedir, found $ncachedirs"
67     unless $ncachedirs == 0 || $ncachedirs == 1;