daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / perl / t / 810-mkdir-eexist.t
1 # libguestfs Perl bindings -*- perl -*-
2 # Copyright (C) 2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 # Test $g->last_errno (RHBZ#672491).
19
20 use strict;
21 use warnings;
22 use Test::More tests => 15;
23
24 use Errno;
25
26 use Sys::Guestfs;
27
28 my $g = Sys::Guestfs->new ();
29 ok ($g);
30
31 open FILE, ">test.img";
32 truncate FILE, 500*1024*1024;
33 close FILE;
34 ok (1);
35
36 $g->add_drive_opts ("test.img", format => "raw");
37 ok (1);
38
39 $g->launch ();
40 ok (1);
41
42 $g->part_disk ("/dev/sda", "mbr");
43 ok (1);
44 $g->mkfs ("ext2", "/dev/sda1");
45 ok (1);
46
47 $g->mount_options ("", "/dev/sda1", "/");
48 ok (1);
49
50 # Directory doesn't exist, so this mkdir should succeed.
51 $g->mkdir ("/foo");
52 ok (1);
53
54 # Directory exists, we should be able to recover gracefully.
55 eval {
56     $g->mkdir ("/foo");
57 };
58 ok ($@);
59 my $err = $g->last_errno ();
60 ok ($err > 0);
61 ok ($err == Errno::EEXIST());
62
63 # Can't create subdirectories with missing parents; this should
64 # be a different errno.
65 eval {
66     $g->mkdir ("/bar/baz");
67 };
68 ok ($@);
69 $err = $g->last_errno ();
70 ok ($err > 0);
71 ok ($err != Errno::EEXIST());
72
73 undef $g;
74 ok (1);
75
76 unlink ("test.img");