X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=src%2Fgenerator.ml;h=f8e3934f13e29db044e8e9b77e91c671e4e04d4a;hp=bb3744ed016b55d43bad20995b6c7c3231a59b68;hb=675411d34f807420d1b714436fc5a58fc0022dae;hpb=bfdc03be234d6d95f18450846433bce4f97e184c diff --git a/src/generator.ml b/src/generator.ml index bb3744e..f8e3934 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -143,13 +143,6 @@ can easily destroy all your data>." * * Between each test we blockdev-setrw, umount-all, lvm-remove-all. * - * If the appliance is running an older Linux kernel (eg. RHEL 5) then - * devices are named /dev/hda etc. To cope with this, the test suite - * adds some hairly logic to detect this case, and then automagically - * replaces all strings which match "/dev/sd.*" with "/dev/hd.*". - * When writing test cases you shouldn't have to worry about this - * difference. - * * Don't assume anything about the previous contents of the block * devices. Use 'Init*' to create some initial scenarios. * @@ -177,6 +170,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 @@ -374,7 +373,7 @@ 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,cache=off>. Note that this call checks for the existence of C. This stops you from specifying other types of drive which are supported @@ -693,7 +692,7 @@ This command is mostly useful for interactive sessions. Programs should probably use C instead."); ("list_devices", (RStringList "devices", []), 7, [], - [InitEmpty, Always, TestOutputList ( + [InitEmpty, Always, TestOutputListOfDevices ( [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])], "list the block devices", "\ @@ -702,9 +701,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", @@ -717,9 +716,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"]; @@ -1112,7 +1111,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"]; @@ -1235,7 +1234,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"; "/"]; @@ -1253,7 +1252,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", "\ @@ -1371,7 +1370,9 @@ or compatible processor architecture). The single parameter is an argv-style list of arguments. The first element is the name of the program to run. Subsequent elements are parameters. The list must be -non-empty (ie. must contain a program name). +non-empty (ie. must contain a program name). Note that +the command runs directly, and is I invoked via +the shell (see C). The return value is anything printed to I by the command. @@ -1439,7 +1440,9 @@ locations."); "run a command, returning lines", "\ This is the same as C, but splits the -result into a list of lines."); +result into a list of lines. + +See also: C"); ("stat", (RStat "statbuf", [String "path"]), 52, [], [InitBasicFS, Always, TestOutputStruct ( @@ -1656,7 +1659,10 @@ See also C, C."); [["write_file"; "/new"; "test\n"; "0"]; ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123"); InitBasicFS, Always, TestOutput ( - [["mount"; "/dev/sdd"; "/"]; + (* RHEL 5 thinks this is an HFS+ filesystem unless we give + * the type explicitly. + *) + [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"]; ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")], "compute MD5, SHAx or CRC checksum of file", "\ @@ -1842,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"]; @@ -1851,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"]; @@ -1860,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"]; @@ -2311,6 +2317,91 @@ 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."); + + ("sh", (RString "output", [String "command"]), 111, [], + [], (* XXX needs tests *) + "run a command via the shell", + "\ +This call runs a command from the guest filesystem via the +guest's C. + +This is like C, but passes the command to: + + /bin/sh -c \"command\" + +Depending on the guest's shell, this usually results in +wildcards being expanded, shell expressions being interpolated +and so on. + +All the provisos about C apply to this call."); + + ("sh_lines", (RStringList "lines", [String "command"]), 112, [], + [], (* XXX needs tests *) + "run a command via the shell returning lines", + "\ +This is the same as C, but splits the result +into a list of lines. + +See also: C"); + + ("glob_expand", (RStringList "paths", [String "pattern"]), 113, [], + [InitBasicFS, Always, TestOutputList ( + [["mkdir_p"; "/a/b/c"]; + ["touch"; "/a/b/c/d"]; + ["touch"; "/a/b/c/e"]; + ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]); + InitBasicFS, Always, TestOutputList ( + [["mkdir_p"; "/a/b/c"]; + ["touch"; "/a/b/c/d"]; + ["touch"; "/a/b/c/e"]; + ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]); + InitBasicFS, Always, TestOutputList ( + [["mkdir_p"; "/a/b/c"]; + ["touch"; "/a/b/c/d"]; + ["touch"; "/a/b/c/e"]; + ["glob_expand"; "/a/*/x/*"]], [])], + "expand a wildcard path", + "\ +This command searches for all the pathnames matching +C according to the wildcard expansion rules +used by the shell. + +If no paths match, then this returns an empty list +(note: not an error). + +It is just a wrapper around the C L function +with flags C. +See that manual page for more details."); + ] let all_functions = non_daemon_functions @ daemon_functions @@ -2543,6 +2634,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 @@ -3466,8 +3558,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 @@ -3590,7 +3686,7 @@ and generate_daemon_actions () = ) daemon_functions; pr " default:\n"; - pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d\", proc_nr);\n"; + pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory\", proc_nr);\n"; pr " }\n"; pr "}\n"; pr "\n"; @@ -3778,11 +3874,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) @@ -3840,9 +3931,8 @@ int main (int argc, char *argv[]) char c = 0; int failed = 0; const char *filename; - int fd, i; + int fd; int nr_tests, test_num = 0; - char **devs; no_test_warnings (); @@ -3949,32 +4039,17 @@ int main (int argc, char *argv[]) printf (\"guestfs_launch FAILED\\n\"); exit (1); } + + /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */ + alarm (600); + if (guestfs_wait_ready (g) == -1) { printf (\"guestfs_wait_ready FAILED\\n\"); 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); + /* Cancel previous alarm. */ + alarm (0); nr_tests = %d; @@ -4118,9 +4193,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"; @@ -4143,8 +4215,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"; @@ -4290,8 +4389,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 _, _ -> () @@ -4300,8 +4397,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 ( @@ -4767,6 +4862,8 @@ generator (const char *text, int state) len = strlen (text); } + rl_attempted_completion_over = 1; + while ((name = commands[index]) != NULL) { index++; if (strncasecmp (name, text, len) == 0) @@ -4783,8 +4880,12 @@ char **do_completion (const char *text, int start, int end) char **matches = NULL; #ifdef HAVE_LIBREADLINE + rl_completion_append_character = ' '; + if (start == 0) matches = rl_completion_matches (text, generator); + else if (complete_dest_paths) + matches = rl_completion_matches (text, complete_dest_paths_generator); #endif return matches; @@ -4899,8 +5000,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 @@ -6393,6 +6500,7 @@ static VALUE ruby_guestfs_close (VALUE gv) List.iter ( function | String n | FileIn n | FileOut n -> + pr " Check_Type (%sv, T_STRING);\n" n; pr " const char *%s = StringValueCStr (%sv);\n" n n; pr " if (!%s)\n" n; pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n"; @@ -6400,7 +6508,8 @@ static VALUE ruby_guestfs_close (VALUE gv) | OptString n -> pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n | StringList n -> - pr " char **%s;" n; + pr " char **%s;\n" n; + pr " Check_Type (%sv, T_ARRAY);\n" n; pr " {\n"; pr " int i, len;\n"; pr " len = RARRAY_LEN (%sv);\n" n;