summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-11-08 11:36:35 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-11-09 13:11:53 +0000
commit1efed122c07792f4c66a4083159cfacfb1893212 (patch)
treec751f26ec9d3c152905fdf34d48a2b79a0af5216 /tests
parente8d937bd739a70394ec72482a8cb1df3b0601dcc (diff)
downloadlibguestfs-1efed122c07792f4c66a4083159cfacfb1893212.tar.gz
libguestfs-1efed122c07792f4c66a4083159cfacfb1893212.tar.xz
libguestfs-1efed122c07792f4c66a4083159cfacfb1893212.zip
lib: Rework temporary and cache directory code.
New APIs: set-tmpdir, get-tmpdir, set-cachedir, get-cachedir. The current code has evolved over time and has a number of problems: (a) A single environment variable ($TMPDIR) controls the location of several directories. (b) It's hard for the library user to predict which directory libguestfs will use, unless the user simulates the same internal steps that libguestfs performs. This commit fixes these issues. (a) Now three environment variables control the location of all small temporary files, and the appliance cache: For temporary files: $LIBGUESTFS_TMPDIR or $TMPDIR or /tmp. For the appliance cache: $LIBGUESTFS_CACHEDIR or $TMPDIR or /var/tmp. The user can also set these directories explicitly through API calls (guestfs_set_tmpdir and guestfs_set_cachedir). (b) The user can also retrieve the actual directories that libguestfs will use, by calling guestfs_get_tmpdir and guestfs_get_cachedir. These functions are also used internally. This commit also: - reworks the internal tmpdir code - removes the internal (undocumented) guestfs_tmpdir call (replacing it with calls to the documented guestfs_get_tmpdir API instead) - changes the ./run script to set LIBGUESTFS_TMPDIR and LIBGUESTFS_CACHEDIR - adds a test - fixes a few places like libguestfs-make-fixed-appliance which depended on $TMPDIR
Diffstat (limited to 'tests')
-rw-r--r--tests/tmpdirs/Makefile.am26
-rwxr-xr-xtests/tmpdirs/test-tmpdirs.pl61
2 files changed, 87 insertions, 0 deletions
diff --git a/tests/tmpdirs/Makefile.am b/tests/tmpdirs/Makefile.am
new file mode 100644
index 00000000..32fd6326
--- /dev/null
+++ b/tests/tmpdirs/Makefile.am
@@ -0,0 +1,26 @@
+# libguestfs
+# Copyright (C) 2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+include $(top_srcdir)/subdir-rules.mk
+
+TESTS = \
+ test-tmpdirs.pl
+
+TESTS_ENVIRONMENT = $(top_builddir)/run --test
+
+EXTRA_DIST = \
+ $(TESTS)
diff --git a/tests/tmpdirs/test-tmpdirs.pl b/tests/tmpdirs/test-tmpdirs.pl
new file mode 100755
index 00000000..1d68c640
--- /dev/null
+++ b/tests/tmpdirs/test-tmpdirs.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+# Copyright (C) 2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test logic for setting location of tmpdir and cachedir.
+
+use strict;
+use warnings;
+
+use Sys::Guestfs;
+
+# Remove any environment variables that may have been set by the
+# user or the ./run script which could affect this test.
+delete $ENV{LIBGUESTFS_TMPDIR};
+delete $ENV{LIBGUESTFS_CACHEDIR};
+delete $ENV{TMPDIR};
+
+my $g;
+
+# Defaults with no environment variables set.
+$g = Sys::Guestfs->new ();
+die unless $g->get_tmpdir () eq "/tmp";
+die unless $g->get_cachedir () eq "/var/tmp";
+
+# Setting environment variables.
+$ENV{LIBGUESTFS_TMPDIR} = "a";
+$ENV{LIBGUESTFS_CACHEDIR} = "b";
+$ENV{TMPDIR} = "c";
+
+$g = Sys::Guestfs->new ();
+die unless $g->get_tmpdir () eq "a";
+die unless $g->get_cachedir () eq "b";
+
+# Creating a handle which isn't affected by environment variables.
+$g = Sys::Guestfs->new (environment => 0);
+die unless $g->get_tmpdir () eq "/tmp";
+die unless $g->get_cachedir () eq "/var/tmp";
+
+# Uses TMPDIR if the others are not set.
+delete $ENV{LIBGUESTFS_TMPDIR};
+$g = Sys::Guestfs->new ();
+die unless $g->get_tmpdir () eq "c";
+die unless $g->get_cachedir () eq "b";
+
+delete $ENV{LIBGUESTFS_CACHEDIR};
+$g = Sys::Guestfs->new ();
+die unless $g->get_tmpdir () eq "c";
+die unless $g->get_cachedir () eq "c";