Permalink
Browse files

tests: Use new guestfs_add_drive_scratch API where possible in tests.

Replaces code such as:

  fd = open "test1.img"
  ftruncate fd, size
  close fd
  g.add_drive "test1.img"

with the shorter and simpler:

  g.add_drive_scratch size
  • Loading branch information...
1 parent 57064c1 commit 14fabcd88e1500368affad86b3cf221f3884a739 @rwmjones rwmjones committed Jul 19, 2013
@@ -20,7 +20,6 @@ package guestfs
import (
"testing"
- "os"
// "sort"
)
@@ -31,17 +30,7 @@ func Test100Launch (t *testing.T) {
}
defer g.Close ()
- f, ferr := os.Create ("test.img")
- if ferr != nil {
- t.Errorf ("could not create file: %s", ferr)
- }
- defer os.Remove ("test.img")
- if ferr := f.Truncate (500 * 1024 * 1024); ferr != nil {
- t.Errorf ("could not truncate file: %s", ferr)
- }
- f.Close ()
-
- err := g.Add_drive ("test.img", nil)
+ err := g.Add_drive_scratch (500 * 1024 * 1024, nil);
if err != nil {
t.Errorf ("%s", err)
}
@@ -24,6 +24,8 @@ import Control.Monad
main = do
g <- G.create
+ {- XXX replace with a call to add_drive_scratch once
+ optional arguments are supported -}
fd <- openFile "test.img" WriteMode
hSetFileSize fd (500 * 1024 * 1024)
hClose fd
@@ -25,16 +25,8 @@
public static void main (String[] argv)
{
try {
- // Delete any previous test file if one was left around.
- File old = new File ("test.img");
- old.delete ();
-
- RandomAccessFile f = new RandomAccessFile ("test.img", "rw");
- f.setLength (500 * 1024 * 1024);
- f.close ();
-
GuestFS g = new GuestFS ();
- g.add_drive ("test.img");
+ g.add_drive_scratch (500 * 1024 * 1024, null);
g.launch ();
g.pvcreate ("/dev/sda");
@@ -57,9 +49,6 @@ public static void main (String[] argv)
g.shutdown ();
g.close ();
-
- File f2 = new File ("test.img");
- f2.delete ();
}
catch (Exception exn) {
System.err.println (exn);
@@ -23,12 +23,7 @@ local G = require "guestfs"
local g = G.create ()
-file = io.open ("test.img", "w")
-file:seek ("set", 500 * 1024 * 1024)
-file:write (' ')
-file:close ()
-
-g:add_drive ("test.img")
+g:add_drive_scratch (500 * 1024 * 1024)
g:launch ()
@@ -45,5 +40,3 @@ assert (table.getn (lvs) == 2 and
g:shutdown ()
g:close ()
-
-os.remove ("test.img")
@@ -23,12 +23,7 @@ local G = require "guestfs"
local g = G.create ()
-file = io.open ("test.img", "w")
-file:seek ("set", 10 * 1024 * 1024)
-file:write (' ')
-file:close ()
-
-g:add_drive ("test.img")
+g:add_drive_scratch (10 * 1024 * 1024)
g:launch ()
@@ -60,5 +55,3 @@ assert (dirs[5]["name"] == "q", "incorrect name in slot 5")
g:shutdown ()
g:close ()
-
-os.remove ("test.img")
@@ -20,12 +20,7 @@ open Unix
let () =
let g = new Guestfs.guestfs () in
-
- let fd = openfile "test.img" [O_WRONLY;O_CREAT;O_NOCTTY;O_TRUNC] 0o666 in
- ftruncate fd (500 * 1024 * 1024);
- close fd;
-
- g#add_drive "test.img";
+ g#add_drive_scratch (Int64.of_int (500 * 1024 * 1024));
g#launch ();
g#pvcreate "/dev/sda";
@@ -58,7 +53,6 @@ let () =
failwith "Guestfs.readdir returned incorrect result";
g#shutdown ();
- g#close ();
- unlink "test.img"
+ g#close ()
let () = Gc.compact ()
View
@@ -17,18 +17,13 @@
use strict;
use warnings;
-use Test::More tests => 26;
+use Test::More tests => 25;
use Sys::Guestfs;
my $g = Sys::Guestfs->new ();
ok ($g);
-open FILE, ">test.img";
-truncate FILE, 500*1024*1024;
-close FILE;
-ok (1);
-
-$g->add_drive ("test.img");
+$g->add_drive_scratch (500*1024*1024);
ok (1);
$g->launch ();
@@ -76,6 +71,4 @@ $g->shutdown ();
ok (1);
undef $g;
-ok (1);
-
-unlink ("test.img");
+ok (1)
View
@@ -19,21 +19,15 @@
use strict;
use warnings;
-use Test::More tests => 15;
+use Test::More tests => 14;
use Errno;
use Sys::Guestfs;
my $g = Sys::Guestfs->new ();
ok ($g);
-
-open FILE, ">test.img";
-truncate FILE, 500*1024*1024;
-close FILE;
-ok (1);
-
-$g->add_drive ("test.img", format => "raw");
+$g->add_drive_scratch (500*1024*1024);
ok (1);
$g->launch ();
@@ -72,5 +66,3 @@ ok ($err != Errno::EEXIST());
undef $g;
ok (1);
-
-unlink ("test.img");
@@ -22,15 +22,7 @@ if ($g == false) {
exit;
}
-$tmp = dirname(__FILE__)."/test.img";
-$size = 100 * 1024 * 1024;
-if (! $fp = fopen ($tmp, 'w+')) {
- die ("Error: cannot create file '".$tmp."'\n");
-}
-ftruncate ($fp, $size);
-fclose ($fp);
-
-if (! guestfs_add_drive ($g, $tmp) ||
+if (! guestfs_add_drive_scratch ($g, 100 * 1024 * 1024) ||
! guestfs_launch ($g) ||
! guestfs_part_disk ($g, "/dev/sda", "mbr") ||
! guestfs_pvcreate ($g, "/dev/sda1") ||
@@ -52,8 +44,6 @@ if (!is_int ($version["major"]) ||
echo ("Error: incorrect return type from guestfs_version\n");
}
-unlink ($tmp);
-
echo ("OK\n");
?>
--EXPECT--
View
@@ -19,10 +19,7 @@
import guestfs
g = guestfs.GuestFS (python_return_dict=True)
-f = open ("test.img", "w")
-f.truncate (500 * 1024 * 1024)
-f.close ()
-g.add_drive ("test.img")
+g.add_drive_scratch (500 * 1024 * 1024)
g.launch ()
g.pvcreate ("/dev/sda")
@@ -33,5 +30,3 @@
raise "Error: g.lvs() returned incorrect result"
g.shutdown ()
g.close ()
-
-os.unlink ("test.img")
View
@@ -24,11 +24,7 @@ class TestLoad < Test::Unit::TestCase
def test_launch
g = Guestfs::create()
- File.open("test.img", "w") {
- |f| f.seek(500*1024*1024); f.write("\0")
- }
-
- g.add_drive("test.img")
+ g.add_drive_scratch(500*1024*1024)
g.launch()
g.pvcreate("/dev/sda")
@@ -42,7 +38,5 @@ def test_launch
end
g.sync()
-
- File.unlink("test.img")
end
end
@@ -23,19 +23,12 @@
class TestLoad < Test::Unit::TestCase
def test_rhbz507346
g = Guestfs::create()
-
- File.open("test.img", "w") {
- |f| f.seek(10*1024*1024); f.write("\0")
- }
-
- g.add_drive("test.img")
+ g.add_drive_scratch(10*1024*1024)
g.launch()
exception = assert_raise TypeError do
g.command(1)
end
assert_match /wrong argument type Fixnum \(expected Array\)/, exception.message
-
- File.unlink("test.img")
end
end
View
@@ -45,9 +45,7 @@
#define DEFAULT_TIMEOUT 600
static int timeout = DEFAULT_TIMEOUT;
-static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX";
-static void make_files (void);
static void set_qemu (guestfs_h *g, const char *path, int use_wrapper);
static void
@@ -192,8 +190,6 @@ main (int argc, char *argv[])
if (qemu)
set_qemu (g, qemu, qemu_use_wrapper);
- make_files ();
-
/* Print out any environment variables which may relate to this test. */
for (i = 0; environ[i] != NULL; ++i) {
if (STRPREFIX (environ[i], "LIBGUESTFS_"))
@@ -224,12 +220,9 @@ main (int argc, char *argv[])
ignore_value (system ("getenforce"));
/* Configure the handle. */
- if (guestfs_add_drive_opts (g, tmpf,
- GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
- -1) == -1) {
+ if (guestfs_add_drive_scratch (g, 100*1024*1024, -1) == -1) {
fprintf (stderr,
- _("libguestfs-test-tool: failed to add drive '%s'\n"),
- tmpf);
+ _("libguestfs-test-tool: failed to add scratch drive\n"));
exit (EXIT_FAILURE);
}
@@ -401,40 +394,3 @@ set_qemu (guestfs_h *g, const char *path, int use_wrapper)
guestfs_set_qemu (g, qemuwrapper);
atexit (cleanup_wrapper);
}
-
-static void
-cleanup_tmpfiles (void)
-{
- unlink (tmpf);
-}
-
-static void
-make_files (void)
-{
- int fd;
-
- /* Allocate the sparse file for /dev/sda. */
- fd = mkstemp (tmpf);
- if (fd == -1) {
- perror (tmpf);
- exit (EXIT_FAILURE);
- }
-
- if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
- perror ("lseek");
- close (fd);
- unlink (tmpf);
- exit (EXIT_FAILURE);
- }
-
- if (write (fd, "\0", 1) == -1) {
- perror ("write");
- close (fd);
- unlink (tmpf);
- exit (EXIT_FAILURE);
- }
-
- close (fd);
-
- atexit (cleanup_tmpfiles); /* Removes tmpf. */
-}
@@ -29,12 +29,7 @@
my $nr_files = 1000000;
my $image_size = 2*1024*1024*1024;
-unlink "test.img";
-open FILE, ">test.img" or die "test.img: $!";
-truncate FILE, $image_size or die "test.img: truncate: $!";
-close FILE or die "test.img: $!";
-
-$g->add_drive ("test.img", format => "raw");
+$g->add_drive_scratch ($image_size);
$g->launch ();
@@ -79,5 +74,3 @@
$g->shutdown ();
$g->close ();
-
-unlink "test.img"
@@ -26,16 +26,9 @@
# Allow the test to be skipped since btrfs is often broken.
exit 77 if $ENV{SKIP_TEST_BTRFS_SUBVOLUME_DEFAULT_PL};
-my $testimg = "test1.img";
-
-unlink $testimg;
-open FILE, ">$testimg" or die "$testimg: $!";
-truncate FILE, 1024*1024*1024 or die "$testimg: truncate: $!";
-close FILE or die "$testimg: $!";
-
my $g = Sys::Guestfs->new ();
-$g->add_drive ($testimg, format => "raw");
+$g->add_drive_scratch (1024*1024*1024);
$g->launch ();
# If btrfs is not available, bail.
@@ -94,5 +87,3 @@
$g->shutdown ();
$g->close ();
-
-unlink $testimg or die "$testimg: unlink: $!";
Oops, something went wrong.

0 comments on commit 14fabcd

Please sign in to comment.