summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-11-19 13:00:52 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-11-19 14:01:40 +0000
commitf77ddb9e114e560724d6548499047ae6894ab59c (patch)
tree6341c27ca32fb5e1262f1263974991a1d6357b22 /lua
parentd14557d434de5cb1c9754ae0f3e8e820e0a46694 (diff)
downloadlibguestfs-f77ddb9e114e560724d6548499047ae6894ab59c.tar.gz
libguestfs-f77ddb9e114e560724d6548499047ae6894ab59c.tar.xz
libguestfs-f77ddb9e114e560724d6548499047ae6894ab59c.zip
lua: Various fixes and enhancements:
- add support for events (with test) - test progress messages - update documentation to describe events - refactor handle closing code - refactor error code - use 'assert' in test code instead of 'if ... then error end'
Diffstat (limited to 'lua')
-rw-r--r--lua/Makefile.am10
-rw-r--r--lua/examples/guestfs-lua.pod27
-rwxr-xr-xlua/tests/027-create-multiple.lua15
-rwxr-xr-xlua/tests/030-config.lua4
-rwxr-xr-xlua/tests/050-lvcreate.lua7
-rwxr-xr-xlua/tests/060-readdir.lua8
-rwxr-xr-xlua/tests/400-events.lua49
-rwxr-xr-xlua/tests/400-progress.lua44
8 files changed, 136 insertions, 28 deletions
diff --git a/lua/Makefile.am b/lua/Makefile.am
index e2daceee..7fff50b8 100644
--- a/lua/Makefile.am
+++ b/lua/Makefile.am
@@ -55,12 +55,14 @@ TESTS = \
tests/025-create-flags.lua \
tests/027-create-multiple.lua \
tests/030-config.lua \
- tests/070-optargs.lua
+ tests/070-optargs.lua \
+ tests/400-events.lua
if ENABLE_APPLIANCE
TESTS += \
tests/050-lvcreate.lua \
- tests/060-readdir.lua
+ tests/060-readdir.lua \
+ tests/400-progress.lua
endif
EXTRA_DIST += \
@@ -71,7 +73,9 @@ EXTRA_DIST += \
tests/030-config.lua \
tests/050-lvcreate.lua \
tests/060-readdir.lua \
- tests/070-optargs.lua
+ tests/070-optargs.lua \
+ tests/400-events.lua \
+ tests/400-progress.lua
# Custom install rule.
install-data-hook:
diff --git a/lua/examples/guestfs-lua.pod b/lua/examples/guestfs-lua.pod
index c8afb887..83900cae 100644
--- a/lua/examples/guestfs-lua.pod
+++ b/lua/examples/guestfs-lua.pod
@@ -83,6 +83,33 @@ The C<errno> (corresponding to L<guestfs(3)/guestfs_last_errno>).
Note that some errors can also be thrown as plain strings. You
need to check the type.
+=head2 EVENTS
+
+Events can be registered by calling C<set_event_callback>:
+
+ eh = g:set_event_callback (cb, "close")
+
+or to register a single callback for multiple events make the
+second argument a list:
+
+ eh = g:set_event_callback (cb, { "appliance", "library", "trace" })
+
+The callback (C<cb>) is called with the following parameters:
+
+ function cb (g, event, eh, flags, buf, array)
+ -- g is the guestfs handle
+ -- event is a string which is the name of the event that fired
+ -- flags is always zero
+ -- buf is the data buffer (eg. log message etc)
+ -- array is the array of 64 bit ints (eg. progress bar status etc)
+ ...
+ end
+
+You can also remove a callback using the event handle (C<eh>) that was
+returned when you registered the callback:
+
+ g:delete_event_callback (eh)
+
=head1 EXAMPLE 1: CREATE A DISK IMAGE
@EXAMPLE1@
diff --git a/lua/tests/027-create-multiple.lua b/lua/tests/027-create-multiple.lua
index 30ce6155..bd6cae56 100755
--- a/lua/tests/027-create-multiple.lua
+++ b/lua/tests/027-create-multiple.lua
@@ -27,15 +27,6 @@ g1:set_path ("1")
g2:set_path ("2")
g3:set_path ("3")
-if g1:get_path () ~= "1" then
- error (string.format ("incorrect path in g1, expected '1', got '%s'",
- g1:get_path ()))
-end
-if g2:get_path () ~= "2" then
- error (string.format ("incorrect path in g2, expected '2', got '%s'",
- g2:get_path ()))
-end
-if g3:get_path () ~= "3" then
- error (string.format ("incorrect path in g3, expected '3', got '%s'",
- g3:get_path ()))
-end
+assert (g1:get_path () == "1", "incorrect path in g1, expected '1'")
+assert (g2:get_path () == "2", "incorrect path in g2, expected '2'")
+assert (g3:get_path () == "3", "incorrect path in g3, expected '3'")
diff --git a/lua/tests/030-config.lua b/lua/tests/030-config.lua
index a1325584..53e47fc8 100755
--- a/lua/tests/030-config.lua
+++ b/lua/tests/030-config.lua
@@ -28,9 +28,7 @@ g:set_autosync (false)
g:set_autosync (true)
g:set_path (".")
-if g:get_path () ~= "." then
- error ()
-end
+assert (g:get_path () == ".")
g:add_drive ("/dev/null")
diff --git a/lua/tests/050-lvcreate.lua b/lua/tests/050-lvcreate.lua
index a9d9920e..3bd95c23 100755
--- a/lua/tests/050-lvcreate.lua
+++ b/lua/tests/050-lvcreate.lua
@@ -35,10 +35,9 @@ g:lvcreate ("LV1", "VG", 200)
g:lvcreate ("LV2", "VG", 200)
local lvs = g:lvs ()
-if table.getn (lvs) ~= 2 or lvs[1] ~= "/dev/VG/LV1" or lvs[2] ~= "/dev/VG/LV2"
-then
- error ("g:lvs returned incorrect result")
-end
+assert (table.getn (lvs) == 2 and
+ lvs[1] == "/dev/VG/LV1" and lvs[2] == "/dev/VG/LV2",
+ "g:lvs returned incorrect result")
g:shutdown ()
diff --git a/lua/tests/060-readdir.lua b/lua/tests/060-readdir.lua
index dd060840..07e8e3ba 100755
--- a/lua/tests/060-readdir.lua
+++ b/lua/tests/060-readdir.lua
@@ -51,12 +51,8 @@ print_dirs (dirs)
-- Slots 1, 2, 3 contain "." and ".." and "lost+found" respectively.
-if (dirs[4]["name"] ~= "p") then
- error "incorrect name in slot 4"
-end
-if (dirs[5]["name"] ~= "q") then
- error "incorrect name in slot 5"
-end
+assert (dirs[4]["name"] == "p", "incorrect name in slot 4")
+assert (dirs[5]["name"] == "q", "incorrect name in slot 5")
g:shutdown ()
diff --git a/lua/tests/400-events.lua b/lua/tests/400-events.lua
new file mode 100755
index 00000000..c29cc62d
--- /dev/null
+++ b/lua/tests/400-events.lua
@@ -0,0 +1,49 @@
+#!/usr/bin/lua
+-- libguestfs Lua bindings -*- lua -*-
+-- 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.
+
+require "guestfs"
+
+g = Guestfs.create ()
+
+function log_callback (g, event, eh, flags, buf, array)
+ io.write (string.format ("lua event logged: event=%s eh=%d buf='%s'\n",
+ event, eh, buf))
+end
+
+close_invoked = 0
+function close_callback (g, event, eh, flags, buf, array)
+ close_invoked = close_invoked+1
+ log_callback (g, event, eh, flags, buf, array)
+end
+
+-- Register an event callback for all log messages.
+g:set_event_callback (log_callback, { "appliance", "library", "trace" })
+
+-- Register an event callback for the close event.
+g:set_event_callback (close_callback, "close")
+
+-- Make sure we see some messages.
+g:set_trace (true)
+g:set_verbose (true)
+
+-- Do some stuff.
+g:add_drive_ro ("/dev/null")
+
+-- Close the handle. The close callback should be invoked.
+g:close ()
+assert (close_invoked == 1, "close callback was not invoked")
diff --git a/lua/tests/400-progress.lua b/lua/tests/400-progress.lua
new file mode 100755
index 00000000..e0e17ac8
--- /dev/null
+++ b/lua/tests/400-progress.lua
@@ -0,0 +1,44 @@
+#!/usr/bin/lua
+-- libguestfs Lua bindings -*- lua -*-
+-- 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.
+
+require "guestfs"
+
+g = Guestfs.create ()
+
+g:add_drive ("/dev/null")
+g:launch ()
+
+calls = 0
+function cb ()
+ calls = calls+1
+end
+
+eh = g:set_event_callback (cb, "progress")
+assert (g:debug ("progress", {"5"}) == "ok", "debug progress command failed")
+assert (calls > 0, "progress callback was not invoked")
+
+calls = 0
+g:delete_event_callback (eh)
+assert (g:debug ("progress", {"5"}) == "ok", "debug progress command failed")
+assert (calls == 0, "progress callback was invoked when deleted")
+
+g:set_event_callback (cb, "progress")
+assert (g:debug ("progress", {"5"}) == "ok", "debug progress command failed")
+assert (calls > 0, "progress callback was not invoked")
+
+g:close ()