Version 1.5.26.
[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., 675 Mass Ave, Cambridge, MA 02139, 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 # Use a temporary TMPDIR to ensure it's clean
29 my $tmpdir = tempdir (CLEANUP => 1);
30 $ENV{TMPDIR} = $tmpdir;
31
32 my $testimg = $tmpdir.'/test.img';
33 system ("touch $testimg");
34
35 my $pid = fork();
36 die ("fork failed: $!") if ($pid < 0);
37
38 if ($pid == 0) {
39   my $g = Sys::Guestfs->new ();
40   $g->add_drive ($testimg);
41   $g->launch ();
42   _exit (0);
43 }
44
45 my $g = Sys::Guestfs->new ();
46 $g->add_drive ($testimg);
47 $g->launch ();
48 $g = undef;
49
50 waitpid ($pid, 0) or die ("waitpid: $!");
51 die ("child failed") unless ($? == 0);
52
53 # Check that only 1 temporary cache directory was created
54 my $dh;
55 opendir ($dh, $tmpdir) or die ("Failed to open $tmpdir: $!");
56 my @cachedirs = grep { /^guestfs\./ } readdir ($dh);
57 closedir ($dh) or die ("Failed to close $tmpdir: $!");
58
59 my $ncachedirs = scalar(@cachedirs);
60 die ("Expected 1 cachedir, found $ncachedirs") unless ($ncachedirs == 1);