From: Richard Jones Date: Fri, 10 Apr 2009 14:10:29 +0000 (+0100) Subject: Added test framework, no tests implemented yet. X-Git-Tag: 0.8~3 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=99f68f259f92eee884c6c7396f61b9c16e2bf354;p=libguestfs.git Added test framework, no tests implemented yet. --- diff --git a/.gitignore b/.gitignore index 1b1a0bd..eedd8a8 100644 --- a/.gitignore +++ b/.gitignore @@ -65,5 +65,6 @@ perl/pm_to_blib pod2htm?.tmp stamp-h1 test*.img +tests update-initramfs.sh vmlinuz.* diff --git a/Makefile.am b/Makefile.am index 55e5dde..47954a5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -61,6 +61,17 @@ $(INITRAMFSIMG): initramfs/fakeroot.log daemon/guestfsd bash $(builddir)/update-initramfs.sh touch $@ +# Tests. These are auto-generated from the test descriptions. + +check_PROGRAMS = tests + +tests_SOURCES = tests.c +tests_CFLAGS = \ + -I$(top_builddir)/src -Wall +tests_LDADD = $(top_builddir)/src/libguestfs.la + +TESTS = $(check_PROGRAMS) + # Manual pages. # guestfs-actions.pod and guestfs-structs are autogenerated. There is # no include mechanism for POD, so we have to do it by hand. diff --git a/src/generator.ml b/src/generator.ml index 253a1e0..06a638e 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -18,8 +18,10 @@ *) (* This script generates a large amount of code and documentation for - * all the daemon actions. To add a new action there are only two - * files you need to change, this one to describe the interface, and + * all the daemon actions. + * + * To add a new action there are only two files you need to change, + * this one to describe the interface (see the big table below), and * daemon/.c to write the implementation. * * After editing this file, run it (./src/generator.ml) to regenerate @@ -84,6 +86,73 @@ type flags = | FishAction of string (* call this function in guestfish *) | NotInFish (* do not export via guestfish *) +(* You can supply zero or as many tests as you want per API call. + * + * Note that the test environment has 3 block devices, of size 10M, 20M + * and 30M (respectively /dev/sda, /dev/sdb, /dev/sdc). To run the + * tests in a reasonable amount of time, the virtual machine and + * block devices are reused between tests. So don't try testing + * kill_subprocess :-x + * + * Don't assume anything about the previous contents of the block + * devices. Use 'Init*' to create some initial scenarios. + *) +type tests = test list +and test = + (* Run the command sequence and just expect nothing to fail. *) + | TestRun of test_init * seq + (* Run the command sequence and expect the output of the final + * command to be the string. + *) + | TestOutput of test_init * seq * string + (* Run the command sequence and expect the output of the final + * command to be the list of strings. + *) + | TestOutputList of test_init * seq * string list + (* Run the command sequence and expect the output of the final + * command to be the integer. + *) + | TestOutputInt of test_init * seq * int + (* Run the command sequence and expect the output of the final + * command to be a true value (!= 0 or != NULL). + *) + | TestOutputTrue of test_init * seq + (* Run the command sequence and expect the output of the final + * command to be a false value (== 0 or == NULL, but not an error). + *) + | TestOutputFalse of test_init * seq + (* Run the command sequence and expect the output of the final + * command to be a list of the given length (but don't care about + * content). + *) + | TestOutputLength of test_init * seq * int + (* Run the command sequence and expect the final command (only) + * to fail. + *) + | TestLastFail of test_init * seq + +(* Some initial scenarios for testing. *) +and test_init = + (* Do nothing, block devices could contain random stuff. *) + | InitNone + (* /dev/sda contains a single partition /dev/sda1, which is formatted + * as ext2, empty [except for lost+found] and mounted on /. + * /dev/sdb and /dev/sdc may have random content. + * No LVM. + *) + | InitEmpty + (* /dev/sda: + * /dev/sda1 (is a PV): + * /dev/VG/LV: + * formatted as ext2, empty [except for lost+found], mounted on / + * /dev/sdb and /dev/sdc may have random content. + *) + | InitEmptyLVM + +(* Sequence of commands for testing. *) +and seq = cmd list +and cmd = string list + (* Note about long descriptions: When referring to another * action, use the format C (ie. the full name of * the C function). This will be replaced as appropriate in other @@ -94,6 +163,7 @@ type flags = let non_daemon_functions = [ ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"], + [], "launch the qemu subprocess", "\ Internally libguestfs is implemented by running a virtual machine @@ -103,6 +173,7 @@ You should call this after configuring the handle (eg. adding drives) but before performing any actions."); ("wait_ready", (RErr, []), -1, [NotInFish], + [], "wait until the qemu subprocess launches", "\ Internally libguestfs is implemented by running a virtual machine @@ -112,11 +183,13 @@ You should call this after C to wait for the launch to complete."); ("kill_subprocess", (RErr, []), -1, [], + [], "kill the qemu subprocess", "\ This kills the qemu subprocess. You should never need to call this."); ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"], + [], "add an image to examine or modify", "\ This function adds a virtual machine disk image C to the @@ -133,6 +206,7 @@ image). This is equivalent to the qemu parameter C<-drive file=filename>."); ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"], + [], "add a CD-ROM disk image to examine", "\ This function adds a virtual CD-ROM disk image to the guest. @@ -140,6 +214,7 @@ This function adds a virtual CD-ROM disk image to the guest. This is equivalent to the qemu parameter C<-cdrom filename>."); ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [], + [], "add qemu parameters", "\ This can be used to add arbitrary qemu command line parameters @@ -152,6 +227,7 @@ The first character of C string must be a C<-> (dash). C can be NULL."); ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"], + [], "set the search path", "\ Set the path that libguestfs searches for kernel and initrd.img. @@ -165,6 +241,7 @@ must make sure it remains valid for the lifetime of the handle. Setting C to C restores the default path."); ("get_path", (RConstString "path", []), -1, [], + [], "get the search path", "\ Return the current search path. @@ -173,6 +250,7 @@ This is always non-NULL. If it wasn't set already, then this will return the default path."); ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"], + [], "set autosync mode", "\ If C is true, this enables autosync. Libguestfs will make a @@ -180,11 +258,13 @@ best effort attempt to run C when the handle is closed (also if the program exits without closing handles)."); ("get_autosync", (RBool "autosync", []), -1, [], + [], "get autosync mode", "\ Get the autosync flag."); ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"], + [], "set verbose mode", "\ If C is true, this turns on verbose messages (to C). @@ -193,6 +273,7 @@ Verbose messages are disabled unless the environment variable C is defined and set to C<1>."); ("get_verbose", (RBool "verbose", []), -1, [], + [], "get verbose mode", "\ This returns the verbose messages flag.") @@ -200,6 +281,13 @@ This returns the verbose messages flag.") let daemon_functions = [ ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [], + [TestOutput ( + InitNone, + [["sfdisk"]; + ["mkfs"; "ext2"; "/dev/sda1"]; + ["mount"; "/dev/sda1"; "/"]; + ["write_file"; "/new"; "new file contents"; "0"]; + ["cat"; "/new"]], "new file contents")], "mount a guest disk at a position in the filesystem", "\ Mount a guest disk at a position in the filesystem. Block devices @@ -220,6 +308,7 @@ The filesystem options C and C are set with this call, in order to improve reliability."); ("sync", (RErr, []), 2, [], + [ TestRun (InitNone, [["sync"]])], "sync disks, writes are flushed through to the disk image", "\ This syncs the disk, so that any writes are flushed through to the @@ -229,6 +318,10 @@ You should always call this if you have modified a disk image, before closing the handle."); ("touch", (RErr, [String "path"]), 3, [], + [TestOutputTrue ( + InitEmpty, + [["touch"; "/new"]; + ["exists"; "/new"]])], "update file timestamps or create a new file", "\ Touch acts like the L command. It can be used to @@ -236,6 +329,10 @@ update the timestamps on a file, or, if the file does not exist, to create a new zero-length file."); ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning], + [TestOutput ( + InitEmpty, + [["write_file"; "/new"; "new file contents"; "0"]; + ["cat"; "/new"]], "new file contents")], "list the contents of a file", "\ Return the contents of the file named C. @@ -246,6 +343,9 @@ as end of string). For those you need to use the C function which has a more complex interface."); ("ll", (RString "listing", [String "directory"]), 5, [], + [], (* XXX Tricky to test because it depends on the exact format + * of the 'ls -l' command, which changes between F10 and F11. + *) "list the files in a directory (long format)", "\ List the files in C (relative to the root directory, @@ -255,6 +355,12 @@ This command is mostly useful for interactive sessions. It is I intended that you try to parse the output string."); ("ls", (RStringList "listing", [String "directory"]), 6, [], + [TestOutputList ( + InitEmpty, + [["touch"; "/new"]; + ["touch"; "/newer"]; + ["touch"; "/newest"]; + ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])], "list the files in a directory", "\ List the files in C (relative to the root directory, @@ -265,6 +371,9 @@ This command is mostly useful for interactive sessions. Programs should probably use C instead."); ("list_devices", (RStringList "devices", []), 7, [], + [TestOutputList ( + InitNone, + [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"])], "list the block devices", "\ List all the block devices. @@ -272,6 +381,13 @@ List all the block devices. The full block device names are returned, eg. C"); ("list_partitions", (RStringList "partitions", []), 8, [], + [TestOutputList ( + InitEmpty, + [["list_partitions"]], ["/dev/sda1"]); + TestOutputList ( + InitEmpty, + [["sfdisk"]; + ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])], "list the partitions", "\ List all the partitions detected on all block devices. @@ -282,6 +398,16 @@ This does not return logical volumes. For that you will need to call C."); ("pvs", (RStringList "physvols", []), 9, [], + [TestOutputList ( + InitEmptyLVM, + [["pvs"]], ["/dev/sda1"]); + TestOutputList ( + InitNone, + [["sfdisk"]; + ["pvcreate"; "/dev/sda1"]; + ["pvcreate"; "/dev/sda2"]; + ["pvcreate"; "/dev/sda3"]; + ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])], "list the LVM physical volumes (PVs)", "\ List all the physical volumes detected. This is the equivalent @@ -293,6 +419,18 @@ PVs (eg. C). See also C."); ("vgs", (RStringList "volgroups", []), 10, [], + [TestOutputList ( + InitEmptyLVM, + [["vgs"]], ["VG"]); + TestOutputList ( + InitNone, + [["sfdisk"]; + ["pvcreate"; "/dev/sda1"]; + ["pvcreate"; "/dev/sda2"]; + ["pvcreate"; "/dev/sda3"]; + ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"]; + ["vgcreate"; "VG2"; "/dev/sda3"]; + ["vgs"]], ["VG1"; "VG2"])], "list the LVM volume groups (VGs)", "\ List all the volumes groups detected. This is the equivalent @@ -304,6 +442,21 @@ detected (eg. C). See also C."); ("lvs", (RStringList "logvols", []), 11, [], + [TestOutputList ( + InitEmptyLVM, + [["lvs"]], ["/dev/VG/LV"]); + TestOutputList ( + InitNone, + [["sfdisk"]; + ["pvcreate"; "/dev/sda1"]; + ["pvcreate"; "/dev/sda2"]; + ["pvcreate"; "/dev/sda3"]; + ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"]; + ["vgcreate"; "VG2"; "/dev/sda3"]; + ["lvcreate"; "LV1"; "VG1"; "5000"]; + ["lvcreate"; "LV2"; "VG1"; "5000"]; + ["lvcreate"; "LV3"; "VG2"; "5000"]; + ["lvs"]], ["LV1"; "LV2"; "LV3"])], "list the LVM logical volumes (LVs)", "\ List all the logical volumes detected. This is the equivalent @@ -315,24 +468,41 @@ This returns a list of the logical volume device names See also C."); ("pvs_full", (RPVList "physvols", []), 12, [], + [TestOutputLength ( + InitEmptyLVM, + [["pvs"]], 1)], "list the LVM physical volumes (PVs)", "\ List all the physical volumes detected. This is the equivalent of the L command. The \"full\" version includes all fields."); ("vgs_full", (RVGList "volgroups", []), 13, [], + [TestOutputLength ( + InitEmptyLVM, + [["pvs"]], 1)], "list the LVM volume groups (VGs)", "\ List all the volumes groups detected. This is the equivalent of the L command. The \"full\" version includes all fields."); ("lvs_full", (RLVList "logvols", []), 14, [], + [TestOutputLength ( + InitEmptyLVM, + [["pvs"]], 1)], "list the LVM logical volumes (LVs)", "\ List all the logical volumes detected. This is the equivalent of the L command. The \"full\" version includes all fields."); ("read_lines", (RStringList "lines", [String "path"]), 15, [], + [TestOutputList ( + InitEmpty, + [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"]; + ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]); + TestOutputList ( + InitEmpty, + [["write_file"; "/new"; ""; "0"]; + ["read_lines"; "/new"]], [])], "read file as lines", "\ Return the contents of the file named C. @@ -346,6 +516,7 @@ as end of line). For those you need to use the C function which has a more complex interface."); ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [], + [], (* XXX Augeas code needs tests. *) "create a new Augeas handle", "\ Create a new Augeas handle for editing configuration files. @@ -396,6 +567,7 @@ To close the handle, you can call C. To find out more about Augeas, see L."); ("aug_close", (RErr, []), 26, [], + [], (* XXX Augeas code needs tests. *) "close the current Augeas handle", "\ Close the current Augeas handle and free up any resources @@ -404,6 +576,7 @@ C again before you can use any other Augeas functions."); ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [], + [], (* XXX Augeas code needs tests. *) "define an Augeas variable", "\ Defines an Augeas variable C whose value is the result @@ -414,6 +587,7 @@ On success this returns the number of nodes in C, or C<0> if C evaluates to something which is not a nodeset."); ("aug_defnode", (RIntBool ("nrnodes", "created"), [String "name"; String "expr"; String "val"]), 18, [], + [], (* XXX Augeas code needs tests. *) "define an Augeas node", "\ Defines a variable C whose value is the result of @@ -428,17 +602,20 @@ number of nodes in the nodeset, and a boolean flag if a node was created."); ("aug_get", (RString "val", [String "path"]), 19, [], + [], (* XXX Augeas code needs tests. *) "look up the value of an Augeas path", "\ Look up the value associated with C. If C matches exactly one node, the C is returned."); ("aug_set", (RErr, [String "path"; String "val"]), 20, [], + [], (* XXX Augeas code needs tests. *) "set Augeas path to value", "\ Set the value associated with C to C."); ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [], + [], (* XXX Augeas code needs tests. *) "insert a sibling Augeas node", "\ Create a new sibling C