Skip to content

Commit

Permalink
tests: Use new guestfs_add_drive_scratch API where possible in tests.
Browse files Browse the repository at this point in the history
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
rwmjones committed Jul 20, 2013
1 parent 57064c1 commit 14fabcd
Show file tree
Hide file tree
Showing 25 changed files with 35 additions and 352 deletions.
13 changes: 1 addition & 12 deletions golang/src/libguestfs.org/guestfs/guestfs_100_launch_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package guestfs


import ( import (
"testing" "testing"
"os"
// "sort" // "sort"
) )


Expand All @@ -31,17 +30,7 @@ func Test100Launch (t *testing.T) {
} }
defer g.Close () defer g.Close ()


f, ferr := os.Create ("test.img") err := g.Add_drive_scratch (500 * 1024 * 1024, nil);
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)
if err != nil { if err != nil {
t.Errorf ("%s", err) t.Errorf ("%s", err)
} }
Expand Down
2 changes: 2 additions & 0 deletions haskell/Guestfs050LVCreate.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import Control.Monad


main = do main = do
g <- G.create g <- G.create
{- XXX replace with a call to add_drive_scratch once
optional arguments are supported -}
fd <- openFile "test.img" WriteMode fd <- openFile "test.img" WriteMode
hSetFileSize fd (500 * 1024 * 1024) hSetFileSize fd (500 * 1024 * 1024)
hClose fd hClose fd
Expand Down
13 changes: 1 addition & 12 deletions java/t/GuestFS100Launch.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,16 +25,8 @@ public class GuestFS100Launch
public static void main (String[] argv) public static void main (String[] argv)
{ {
try { 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 (); GuestFS g = new GuestFS ();
g.add_drive ("test.img"); g.add_drive_scratch (500 * 1024 * 1024, null);
g.launch (); g.launch ();


g.pvcreate ("/dev/sda"); g.pvcreate ("/dev/sda");
Expand All @@ -57,9 +49,6 @@ public static void main (String[] argv)


g.shutdown (); g.shutdown ();
g.close (); g.close ();

File f2 = new File ("test.img");
f2.delete ();
} }
catch (Exception exn) { catch (Exception exn) {
System.err.println (exn); System.err.println (exn);
Expand Down
9 changes: 1 addition & 8 deletions lua/tests/050-lvcreate.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ local G = require "guestfs"


local g = G.create () local g = G.create ()


file = io.open ("test.img", "w") g:add_drive_scratch (500 * 1024 * 1024)
file:seek ("set", 500 * 1024 * 1024)
file:write (' ')
file:close ()

g:add_drive ("test.img")


g:launch () g:launch ()


Expand All @@ -45,5 +40,3 @@ assert (table.getn (lvs) == 2 and
g:shutdown () g:shutdown ()


g:close () g:close ()

os.remove ("test.img")
9 changes: 1 addition & 8 deletions lua/tests/060-readdir.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ local G = require "guestfs"


local g = G.create () local g = G.create ()


file = io.open ("test.img", "w") g:add_drive_scratch (10 * 1024 * 1024)
file:seek ("set", 10 * 1024 * 1024)
file:write (' ')
file:close ()

g:add_drive ("test.img")


g:launch () g:launch ()


Expand Down Expand Up @@ -60,5 +55,3 @@ assert (dirs[5]["name"] == "q", "incorrect name in slot 5")
g:shutdown () g:shutdown ()


g:close () g:close ()

os.remove ("test.img")
10 changes: 2 additions & 8 deletions ocaml/t/guestfs_100_launch.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ open Unix


let () = let () =
let g = new Guestfs.guestfs () in let g = new Guestfs.guestfs () in

g#add_drive_scratch (Int64.of_int (500 * 1024 * 1024));
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#launch (); g#launch ();


g#pvcreate "/dev/sda"; g#pvcreate "/dev/sda";
Expand Down Expand Up @@ -58,7 +53,6 @@ let () =
failwith "Guestfs.readdir returned incorrect result"; failwith "Guestfs.readdir returned incorrect result";


g#shutdown (); g#shutdown ();
g#close (); g#close ()
unlink "test.img"


let () = Gc.compact () let () = Gc.compact ()
13 changes: 3 additions & 10 deletions perl/t/100-launch.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@


use strict; use strict;
use warnings; use warnings;
use Test::More tests => 26; use Test::More tests => 25;


use Sys::Guestfs; use Sys::Guestfs;


my $g = Sys::Guestfs->new (); my $g = Sys::Guestfs->new ();
ok ($g); ok ($g);
open FILE, ">test.img"; $g->add_drive_scratch (500*1024*1024);
truncate FILE, 500*1024*1024;
close FILE;
ok (1);

$g->add_drive ("test.img");
ok (1); ok (1);


$g->launch (); $g->launch ();
Expand Down Expand Up @@ -76,6 +71,4 @@ $g->shutdown ();
ok (1); ok (1);


undef $g; undef $g;
ok (1); ok (1)

unlink ("test.img");
12 changes: 2 additions & 10 deletions perl/t/810-mkdir-eexist.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@


use strict; use strict;
use warnings; use warnings;
use Test::More tests => 15; use Test::More tests => 14;


use Errno; use Errno;


use Sys::Guestfs; use Sys::Guestfs;


my $g = Sys::Guestfs->new (); my $g = Sys::Guestfs->new ();
ok ($g); ok ($g);

$g->add_drive_scratch (500*1024*1024);
open FILE, ">test.img";
truncate FILE, 500*1024*1024;
close FILE;
ok (1);

$g->add_drive ("test.img", format => "raw");
ok (1); ok (1);


$g->launch (); $g->launch ();
Expand Down Expand Up @@ -72,5 +66,3 @@ ok ($err != Errno::EEXIST());


undef $g; undef $g;
ok (1); ok (1);

unlink ("test.img");
12 changes: 1 addition & 11 deletions php/extension/guestfs_php_002.phpt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ if ($g == false) {
exit; exit;
} }


$tmp = dirname(__FILE__)."/test.img"; if (! guestfs_add_drive_scratch ($g, 100 * 1024 * 1024) ||
$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) ||
! guestfs_launch ($g) || ! guestfs_launch ($g) ||
! guestfs_part_disk ($g, "/dev/sda", "mbr") || ! guestfs_part_disk ($g, "/dev/sda", "mbr") ||
! guestfs_pvcreate ($g, "/dev/sda1") || ! guestfs_pvcreate ($g, "/dev/sda1") ||
Expand All @@ -52,8 +44,6 @@ if (!is_int ($version["major"]) ||
echo ("Error: incorrect return type from guestfs_version\n"); echo ("Error: incorrect return type from guestfs_version\n");
} }


unlink ($tmp);

echo ("OK\n"); echo ("OK\n");
?> ?>
--EXPECT-- --EXPECT--
Expand Down
7 changes: 1 addition & 6 deletions python/t/100-launch.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import guestfs import guestfs


g = guestfs.GuestFS (python_return_dict=True) g = guestfs.GuestFS (python_return_dict=True)
f = open ("test.img", "w") g.add_drive_scratch (500 * 1024 * 1024)
f.truncate (500 * 1024 * 1024)
f.close ()
g.add_drive ("test.img")
g.launch () g.launch ()


g.pvcreate ("/dev/sda") g.pvcreate ("/dev/sda")
Expand All @@ -33,5 +30,3 @@
raise "Error: g.lvs() returned incorrect result" raise "Error: g.lvs() returned incorrect result"
g.shutdown () g.shutdown ()
g.close () g.close ()

os.unlink ("test.img")
8 changes: 1 addition & 7 deletions ruby/t/tc_100_launch.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ class TestLoad < Test::Unit::TestCase
def test_launch def test_launch
g = Guestfs::create() g = Guestfs::create()


File.open("test.img", "w") { g.add_drive_scratch(500*1024*1024)
|f| f.seek(500*1024*1024); f.write("\0")
}

g.add_drive("test.img")
g.launch() g.launch()


g.pvcreate("/dev/sda") g.pvcreate("/dev/sda")
Expand All @@ -42,7 +38,5 @@ def test_launch
end end


g.sync() g.sync()

File.unlink("test.img")
end end
end end
9 changes: 1 addition & 8 deletions ruby/t/tc_800_rhbz507346.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,19 +23,12 @@
class TestLoad < Test::Unit::TestCase class TestLoad < Test::Unit::TestCase
def test_rhbz507346 def test_rhbz507346
g = Guestfs::create() g = Guestfs::create()

g.add_drive_scratch(10*1024*1024)
File.open("test.img", "w") {
|f| f.seek(10*1024*1024); f.write("\0")
}

g.add_drive("test.img")
g.launch() g.launch()


exception = assert_raise TypeError do exception = assert_raise TypeError do
g.command(1) g.command(1)
end end
assert_match /wrong argument type Fixnum \(expected Array\)/, exception.message assert_match /wrong argument type Fixnum \(expected Array\)/, exception.message

File.unlink("test.img")
end end
end end
48 changes: 2 additions & 46 deletions test-tool/test-tool.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
#define DEFAULT_TIMEOUT 600 #define DEFAULT_TIMEOUT 600


static int timeout = DEFAULT_TIMEOUT; 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 set_qemu (guestfs_h *g, const char *path, int use_wrapper);


static void static void
Expand Down Expand Up @@ -192,8 +190,6 @@ main (int argc, char *argv[])
if (qemu) if (qemu)
set_qemu (g, qemu, qemu_use_wrapper); set_qemu (g, qemu, qemu_use_wrapper);


make_files ();

/* Print out any environment variables which may relate to this test. */ /* Print out any environment variables which may relate to this test. */
for (i = 0; environ[i] != NULL; ++i) { for (i = 0; environ[i] != NULL; ++i) {
if (STRPREFIX (environ[i], "LIBGUESTFS_")) if (STRPREFIX (environ[i], "LIBGUESTFS_"))
Expand Down Expand Up @@ -224,12 +220,9 @@ main (int argc, char *argv[])
ignore_value (system ("getenforce")); ignore_value (system ("getenforce"));


/* Configure the handle. */ /* Configure the handle. */
if (guestfs_add_drive_opts (g, tmpf, if (guestfs_add_drive_scratch (g, 100*1024*1024, -1) == -1) {
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
-1) == -1) {
fprintf (stderr, fprintf (stderr,
_("libguestfs-test-tool: failed to add drive '%s'\n"), _("libguestfs-test-tool: failed to add scratch drive\n"));
tmpf);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }


Expand Down Expand Up @@ -401,40 +394,3 @@ set_qemu (guestfs_h *g, const char *path, int use_wrapper)
guestfs_set_qemu (g, qemuwrapper); guestfs_set_qemu (g, qemuwrapper);
atexit (cleanup_wrapper); 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. */
}
9 changes: 1 addition & 8 deletions tests/bigdirs/test-big-dirs.pl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
my $nr_files = 1000000; my $nr_files = 1000000;
my $image_size = 2*1024*1024*1024; my $image_size = 2*1024*1024*1024;


unlink "test.img"; $g->add_drive_scratch ($image_size);
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->launch (); $g->launch ();


Expand Down Expand Up @@ -79,5 +74,3 @@


$g->shutdown (); $g->shutdown ();
$g->close (); $g->close ();

unlink "test.img"
11 changes: 1 addition & 10 deletions tests/btrfs/test-btrfs-subvolume-default.pl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@
# Allow the test to be skipped since btrfs is often broken. # Allow the test to be skipped since btrfs is often broken.
exit 77 if $ENV{SKIP_TEST_BTRFS_SUBVOLUME_DEFAULT_PL}; 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 (); my $g = Sys::Guestfs->new ();


$g->add_drive ($testimg, format => "raw"); $g->add_drive_scratch (1024*1024*1024);
$g->launch (); $g->launch ();


# If btrfs is not available, bail. # If btrfs is not available, bail.
Expand Down Expand Up @@ -94,5 +87,3 @@


$g->shutdown (); $g->shutdown ();
$g->close (); $g->close ();

unlink $testimg or die "$testimg: unlink: $!";
Loading

0 comments on commit 14fabcd

Please sign in to comment.