X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=src%2Fgenerator.ml;h=c4f285119308fdb4ecd6911e77ac85086474f173;hp=2cf96ee01143ca18f0a4f3352c918eef1fd4e734;hb=875dc84cc01dacb1e254e294a66e179b96fbdbc6;hpb=babc0846cc911b01a58a7385d30ad25889b7175a diff --git a/src/generator.ml b/src/generator.ml index 2cf96ee..c4f2851 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -130,9 +130,13 @@ can easily destroy all your data>." (* 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 500MB, - * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc). + * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and + * a fourth squashfs block device with some known files on it (/dev/sdd). + * * Note for partitioning purposes, the 500MB device has 63 cylinders. * + * The squashfs block device (/dev/sdd) comes from images/test.sqsh. + * * To be able 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 @@ -173,6 +177,12 @@ and test = *) | TestOutputList of seq * string list (* Run the command sequence and expect the output of the final + * command to be the list of block devices (could be either + * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th + * character of each string). + *) + | TestOutputListOfDevices of seq * string list + (* Run the command sequence and expect the output of the final * command to be the integer. *) | TestOutputInt of seq * int @@ -370,7 +380,12 @@ for whatever operations you want to perform (ie. read access if you just want to read the image or write access if you want to modify the image). -This is equivalent to the qemu parameter C<-drive file=filename>."); +This is equivalent to the qemu parameter C<-drive file=filename>. + +Note that this call checks for the existence of C. This +stops you from specifying other types of drive which are supported +by qemu such as C and C URLs. To specify those, use +the general C call instead."); ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"], [], @@ -378,7 +393,33 @@ This is equivalent to the qemu parameter C<-drive file=filename>."); "\ This function adds a virtual CD-ROM disk image to the guest. -This is equivalent to the qemu parameter C<-cdrom filename>."); +This is equivalent to the qemu parameter C<-cdrom filename>. + +Note that this call checks for the existence of C. This +stops you from specifying other types of drive which are supported +by qemu such as C and C URLs. To specify those, use +the general C call instead."); + + ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"], + [], + "add a drive in snapshot mode (read-only)", + "\ +This adds a drive in snapshot mode, making it effectively +read-only. + +Note that writes to the device are allowed, and will be seen for +the duration of the guestfs handle, but they are written +to a temporary file which is discarded as soon as the guestfs +handle is closed. We don't currently have any method to enable +changes to be committed, although qemu can support this. + +This is equivalent to the qemu parameter +C<-drive file=filename,snapshot=on>. + +Note that this call checks for the existence of C. This +stops you from specifying other types of drive which are supported +by qemu such as C and C URLs. To specify those, use +the general C call instead."); ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [], [], @@ -658,8 +699,8 @@ This command is mostly useful for interactive sessions. Programs should probably use C instead."); ("list_devices", (RStringList "devices", []), 7, [], - [InitEmpty, Always, TestOutputList ( - [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"])], + [InitEmpty, Always, TestOutputListOfDevices ( + [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])], "list the block devices", "\ List all the block devices. @@ -667,9 +708,9 @@ List all the block devices. The full block device names are returned, eg. C"); ("list_partitions", (RStringList "partitions", []), 8, [], - [InitBasicFS, Always, TestOutputList ( + [InitBasicFS, Always, TestOutputListOfDevices ( [["list_partitions"]], ["/dev/sda1"]); - InitEmpty, Always, TestOutputList ( + InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"]; ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])], "list the partitions", @@ -682,9 +723,9 @@ This does not return logical volumes. For that you will need to call C."); ("pvs", (RStringList "physvols", []), 9, [], - [InitBasicFSonLVM, Always, TestOutputList ( + [InitBasicFSonLVM, Always, TestOutputListOfDevices ( [["pvs"]], ["/dev/sda1"]); - InitEmpty, Always, TestOutputList ( + InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"]; ["pvcreate"; "/dev/sda1"]; ["pvcreate"; "/dev/sda2"]; @@ -1002,7 +1043,14 @@ Create a directory named C."); ["is_dir"; "/new/foo"]]; InitBasicFS, Always, TestOutputTrue [["mkdir_p"; "/new/foo/bar"]; - ["is_dir"; "/new"]]], + ["is_dir"; "/new"]]; + (* Regression tests for RHBZ#503133: *) + InitBasicFS, Always, TestRun + [["mkdir"; "/new"]; + ["mkdir_p"; "/new"]]; + InitBasicFS, Always, TestLastFail + [["touch"; "/new"]; + ["mkdir_p"; "/new"]]], "create a directory and parents", "\ Create a directory named C, creating any parent directories @@ -1070,7 +1118,7 @@ other objects like files. See also C."); ("pvcreate", (RErr, [String "device"]), 39, [], - [InitEmpty, Always, TestOutputList ( + [InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"]; ["pvcreate"; "/dev/sda1"]; ["pvcreate"; "/dev/sda2"]; @@ -1193,7 +1241,7 @@ We hope to resolve this bug in a future version. In the meantime use C."); ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"], - [InitEmpty, Always, TestOutputList ( + [InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; ["mkfs"; "ext2"; "/dev/sda1"]; ["mount"; "/dev/sda1"; "/"]; @@ -1211,7 +1259,7 @@ specified either by its mountpoint (path) or the device which contains the filesystem."); ("mounts", (RStringList "devices", []), 46, [], - [InitBasicFS, Always, TestOutputList ( + [InitBasicFS, Always, TestOutputListOfDevices ( [["mounts"]], ["/dev/sda1"])], "show mounted filesystems", "\ @@ -1562,7 +1610,7 @@ This uses the L command."); ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [], [InitBasicFS, Always, TestOutput ( (* Pick a file from cwd which isn't likely to change. *) - [["upload"; "COPYING.LIB"; "/COPYING.LIB"]; + [["upload"; "../COPYING.LIB"; "/COPYING.LIB"]; ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")], "upload a file from the local machine", "\ @@ -1576,7 +1624,7 @@ See also C."); ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [], [InitBasicFS, Always, TestOutput ( (* Pick a file from cwd which isn't likely to change. *) - [["upload"; "COPYING.LIB"; "/COPYING.LIB"]; + [["upload"; "../COPYING.LIB"; "/COPYING.LIB"]; ["download"; "/COPYING.LIB"; "testdownload.tmp"]; ["upload"; "testdownload.tmp"; "/upload"]; ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")], @@ -1612,7 +1660,10 @@ See also C, C."); ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d"); InitBasicFS, Always, TestOutput ( [["write_file"; "/new"; "test\n"; "0"]; - ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123")], + ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123"); + InitBasicFS, Always, TestOutput ( + [["mount"; "/dev/sdd"; "/"]; + ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")], "compute MD5, SHAx or CRC checksum of file", "\ This call computes the MD5, SHAx or CRC checksum of the @@ -1797,7 +1848,7 @@ This also forcibly removes all logical volumes in the volume group (if any)."); ("pvremove", (RErr, [String "device"]), 79, [], - [InitEmpty, Always, TestOutputList ( + [InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; ["pvcreate"; "/dev/sda1"]; ["vgcreate"; "VG"; "/dev/sda1"]; @@ -1806,7 +1857,7 @@ group (if any)."); ["vgremove"; "VG"]; ["pvremove"; "/dev/sda1"]; ["lvs"]], []); - InitEmpty, Always, TestOutputList ( + InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; ["pvcreate"; "/dev/sda1"]; ["vgcreate"; "VG"; "/dev/sda1"]; @@ -1815,7 +1866,7 @@ group (if any)."); ["vgremove"; "VG"]; ["pvremove"; "/dev/sda1"]; ["vgs"]], []); - InitEmpty, Always, TestOutputList ( + InitEmpty, Always, TestOutputListOfDevices ( [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; ["pvcreate"; "/dev/sda1"]; ["vgcreate"; "VG"; "/dev/sda1"]; @@ -2266,6 +2317,36 @@ even if the filesystem appears to be clean (C<-f>). This command is only needed because of C (q.v.). Normally you should use C."); + ("sleep", (RErr, [Int "secs"]), 109, [], + [InitNone, Always, TestRun ( + [["sleep"; "1"]])], + "sleep for some seconds", + "\ +Sleep for C seconds."); + + ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [], + [InitNone, Always, TestOutputInt ( + [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; + ["mkfs"; "ntfs"; "/dev/sda1"]; + ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0); + InitNone, Always, TestOutputInt ( + [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; + ["mkfs"; "ext2"; "/dev/sda1"]; + ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)], + "probe NTFS volume", + "\ +This command runs the L command which probes +an NTFS C for mountability. (Not all NTFS volumes can +be mounted read-write, and some cannot be mounted at all). + +C is a boolean flag. Set it to true if you want to test +if the volume can be mounted read-write. Set it to false if +you want to test if the volume can be mounted read-only. + +The return value is an integer which C<0> if the operation +would succeed, or some non-zero value documented in the +L manual page."); + ] let all_functions = non_daemon_functions @ daemon_functions @@ -2498,6 +2579,7 @@ let name_of_argt = function let seq_of_test = function | TestRun s | TestOutput (s, _) | TestOutputList (s, _) + | TestOutputListOfDevices (s, _) | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s | TestOutputLength (s, _) | TestOutputStruct (s, _) | TestLastFail s -> s @@ -3421,8 +3503,12 @@ and generate_daemon_actions () = pr " struct guestfs_%s_args args;\n" name; List.iter ( function + (* Note we allow the string to be writable, in order to + * allow device name translation. This is safe because + * we can modify the string (passed from RPC). + *) | String n - | OptString n -> pr " const char *%s;\n" n + | OptString n -> pr " char *%s;\n" n | StringList n -> pr " char **%s;\n" n | Bool n -> pr " int %s;\n" n | Int n -> pr " int %s;\n" n @@ -3733,11 +3819,6 @@ and generate_tests () = static guestfs_h *g; static int suppress_error = 0; -/* This will be 's' or 'h' depending on whether the guest kernel - * names IDE devices /dev/sd* or /dev/hd*. - */ -static char devchar = 's'; - static void print_error (guestfs_h *g, void *data, const char *msg) { if (!suppress_error) @@ -3794,11 +3875,9 @@ int main (int argc, char *argv[]) { char c = 0; int failed = 0; - const char *srcdir; const char *filename; - int fd, i; + int fd; int nr_tests, test_num = 0; - char **devs; no_test_warnings (); @@ -3896,6 +3975,11 @@ int main (int argc, char *argv[]) exit (1); } + if (guestfs_add_drive_ro (g, \"../images/test.sqsh\") == -1) { + printf (\"guestfs_add_drive_ro ../images/test.sqsh FAILED\\n\"); + exit (1); + } + if (guestfs_launch (g) == -1) { printf (\"guestfs_launch FAILED\\n\"); exit (1); @@ -3905,28 +3989,6 @@ int main (int argc, char *argv[]) exit (1); } - /* Detect if the appliance uses /dev/sd* or /dev/hd* in device - * names. This changed between RHEL 5 and RHEL 6 so we have to - * support both. - */ - devs = guestfs_list_devices (g); - if (devs == NULL || devs[0] == NULL) { - printf (\"guestfs_list_devices FAILED\\n\"); - exit (1); - } - if (strncmp (devs[0], \"/dev/sd\", 7) == 0) - devchar = 's'; - else if (strncmp (devs[0], \"/dev/hd\", 7) == 0) - devchar = 'h'; - else { - printf (\"guestfs_list_devices returned unexpected string '%%s'\\n\", - devs[0]); - exit (1); - } - for (i = 0; devs[i] != NULL; ++i) - free (devs[i]); - free (devs); - nr_tests = %d; " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests; @@ -4069,9 +4131,6 @@ and generate_one_test_body name i test_name init test = | TestOutput (seq, expected) -> pr " /* TestOutput for %s (%d) */\n" name i; pr " char expected[] = \"%s\";\n" (c_quote expected); - if String.length expected > 7 && - String.sub expected 0 7 = "/dev/sd" then - pr " expected[5] = devchar;\n"; let seq, last = get_seq_last seq in let test () = pr " if (strcmp (r, expected) != 0) {\n"; @@ -4094,8 +4153,35 @@ and generate_one_test_body name i test_name init test = pr " }\n"; pr " {\n"; pr " char expected[] = \"%s\";\n" (c_quote str); - if String.length str > 7 && String.sub str 0 7 = "/dev/sd" then - pr " expected[5] = devchar;\n"; + pr " if (strcmp (r[%d], expected) != 0) {\n" i; + pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i; + pr " return -1;\n"; + pr " }\n"; + pr " }\n" + ) expected; + pr " if (r[%d] != NULL) {\n" (List.length expected); + pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n" + test_name; + pr " print_strings (r);\n"; + pr " return -1;\n"; + pr " }\n" + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last + | TestOutputListOfDevices (seq, expected) -> + pr " /* TestOutputListOfDevices for %s (%d) */\n" name i; + let seq, last = get_seq_last seq in + let test () = + iteri ( + fun i str -> + pr " if (!r[%d]) {\n" i; + pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name; + pr " print_strings (r);\n"; + pr " return -1;\n"; + pr " }\n"; + pr " {\n"; + pr " char expected[] = \"%s\";\n" (c_quote str); + pr " r[%d][5] = 's';\n" i; pr " if (strcmp (r[%d], expected) != 0) {\n" i; pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i; pr " return -1;\n"; @@ -4241,8 +4327,6 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = | String n, arg | OptString n, arg -> pr " char %s[] = \"%s\";\n" n (c_quote arg); - if String.length arg > 7 && String.sub arg 0 7 = "/dev/sd" then - pr " %s[5] = devchar;\n" n | Int _, _ | Bool _, _ | FileIn _, _ | FileOut _, _ -> () @@ -4251,8 +4335,6 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = iteri ( fun i str -> pr " char %s_%d[] = \"%s\";\n" n i (c_quote str); - if String.length str > 7 && String.sub str 0 7 = "/dev/sd" then - pr " %s_%d[5] = devchar;\n" n i ) strs; pr " char *%s[] = {\n" n; iteri ( @@ -4850,8 +4932,14 @@ and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true) List.iter ( function | String n - | OptString n -> next (); pr "const char *%s" n - | StringList n -> next (); pr "char * const* const %s" n + | OptString n -> + next (); + if not in_daemon then pr "const char *%s" n + else pr "char *%s" n + | StringList n -> + next (); + if not in_daemon then pr "char * const* const %s" n + else pr "char **%s" n | Bool n -> next (); pr "int %s" n | Int n -> next (); pr "int %s" n | FileIn n @@ -7629,6 +7717,8 @@ and generate_lang_bindtests call = CallStringList ["1"]; CallBool false; CallInt 0; CallString ""; CallString ""] + (* XXX Add here tests of the return and error functions. *) + let output_to filename = let filename_new = filename ^ ".new" in chan := open_out filename_new;