summaryrefslogtreecommitdiffstats
path: root/regressions/test-launch-race.pl
diff options
context:
space:
mode:
authorMatthew Booth <mbooth@redhat.com>2010-10-28 15:19:14 +0100
committerRichard W.M. Jones <rjones@redhat.com>2010-10-28 16:47:06 +0100
commitf2460699ab7f972b1981d072164a04820c52b0c6 (patch)
treee91d6c4db22202aa57177a0034a74be443d92d82 /regressions/test-launch-race.pl
parent38af2eaceb4c8e9d675835bcd6e598ccb67daf0f (diff)
downloadlibguestfs-f2460699ab7f972b1981d072164a04820c52b0c6.tar.gz
libguestfs-f2460699ab7f972b1981d072164a04820c52b0c6.tar.xz
libguestfs-f2460699ab7f972b1981d072164a04820c52b0c6.zip
Ensure atomic creation of a cached appliance
Cached appliances are discovered by their predictable path. Previously we were creating a cached appliance directly in this predictable path. This had at least 2 undesirable effects: * Interrupting appliance creation would leave a corrupt appliance * 2 processes could simultaneously attempt to create the same appliance, causing corruption. This patch causes the cached appliance to be created in a temporary directory, and then renamed to the predictable path. As rename is an atomic operation, this makes the whole creation atomic. This patch also changes the predictable path to have a prefix of 'guestfs.'. This will make it simpler for system administrators to clean up old cached appliances. This patch resolves RHBZ#639405
Diffstat (limited to 'regressions/test-launch-race.pl')
-rwxr-xr-xregressions/test-launch-race.pl60
1 files changed, 60 insertions, 0 deletions
diff --git a/regressions/test-launch-race.pl b/regressions/test-launch-race.pl
new file mode 100755
index 00000000..941eff6c
--- /dev/null
+++ b/regressions/test-launch-race.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+# Copyright (C) 2010 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# Test that 2 simultaneous launches in a clean cache directory will both succeed
+
+use strict;
+use warnings;
+
+use File::Temp qw(tempdir);
+use POSIX;
+
+use Sys::Guestfs;
+
+# Use a temporary TMPDIR to ensure it's clean
+my $tmpdir = tempdir (CLEANUP => 1);
+$ENV{TMPDIR} = $tmpdir;
+
+my $testimg = $tmpdir.'/test.img';
+system ("touch $testimg");
+
+my $pid = fork();
+die ("fork failed: $!") if ($pid < 0);
+
+if ($pid == 0) {
+ my $g = Sys::Guestfs->new ();
+ $g->add_drive ($testimg);
+ $g->launch ();
+ _exit (0);
+}
+
+my $g = Sys::Guestfs->new ();
+$g->add_drive ($testimg);
+$g->launch ();
+$g = undef;
+
+waitpid ($pid, 0) or die ("waitpid: $!");
+die ("child failed") unless ($? == 0);
+
+# Check that only 1 temporary cache directory was created
+my $dh;
+opendir ($dh, $tmpdir) or die ("Failed to open $tmpdir: $!");
+my @cachedirs = grep { /^guestfs\./ } readdir ($dh);
+closedir ($dh) or die ("Failed to close $tmpdir: $!");
+
+my $ncachedirs = scalar(@cachedirs);
+die ("Expected 1 cachedir, found $ncachedirs") unless ($ncachedirs == 1);