Update FSF address.
[libguestfs.git] / perl / t / 400-events.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 use strict;
19 use warnings;
20 use Test::More tests => 7;
21
22 use Sys::Guestfs;
23
24 my $h = Sys::Guestfs->new ();
25 ok ($h);
26
27 sub log_callback {
28     my $ev = shift;
29     my $eh = shift;
30     my $buf = shift;
31     my $array = shift;
32
33     chomp $buf if $ev == $Sys::Guestfs::EVENT_APPLIANCE;
34
35     # We don't get to see this output because it is eaten up by the
36     # test harness, but generate it anyway.
37     printf("perl event logged: event=0x%x eh=%d buf='%s' array=[%s]\n",
38            $ev, $eh, $buf, join (", ", @$array));
39 }
40
41 my $close_invoked = 0;
42
43 sub close_callback {
44     $close_invoked++;
45     log_callback (@_);
46 }
47
48 # Register an event callback for all log messages.
49 my $events = $Sys::Guestfs::EVENT_APPLIANCE | $Sys::Guestfs::EVENT_LIBRARY |
50     $Sys::Guestfs::EVENT_TRACE;
51 my $eh;
52 $eh = $h->set_event_callback (\&log_callback, $events);
53 ok ($eh >= 0);
54
55 # Check that the close event is invoked.
56 $h->set_event_callback (\&close_callback, $Sys::Guestfs::EVENT_CLOSE);
57 ok ($eh >= 0);
58
59 # Now make sure we see some messages.
60 $h->set_trace (1);
61 $h->set_verbose (1);
62 ok (1);
63
64 # Do some stuff.
65 $h->add_drive_ro ("/dev/null");
66 $h->set_autosync (1);
67 ok (1);
68
69 # Close the handle.  The close callback should be invoked.
70 ok ($close_invoked == 0);
71 undef $h;
72 ok ($close_invoked == 1);