From: "Daniel P. Berrange" <berrange(a)redhat.com>
If systemd machine does not exist, return -2 instead of -1,
so that applications don't need to repeat the tedious error
checking code
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/util/virsystemd.c | 13 ++++++++++-
tests/virsystemdmock.c | 14 +++++++----
tests/virsystemdtest.c | 63 +++++++++++++++++++++++++++++++++++---------------
3 files changed, 66 insertions(+), 24 deletions(-)
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index 8477cd3..11d1153 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -26,6 +26,7 @@
#include "virstring.h"
#include "viralloc.h"
#include "virutil.h"
+#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_SYSTEMD
@@ -38,6 +39,8 @@
* @rootdir: root directory of machine filesystem
* @pidleader: PID of the leader process
* @slice: name of the slice to place the machine in
+ *
+ * Returns 0 on success, -1 on fatal error, or -2 if systemd-machine is not available
*/
int virSystemdCreateMachine(const char *name,
const char *drivername,
@@ -117,6 +120,7 @@ int virSystemdCreateMachine(const char *name,
* allow further API calls to be made against the object.
*/
+ VIR_DEBUG("Attempting to create machine via systemd");
if (virDBusCallMethod(conn,
NULL,
"org.freedesktop.machine1",
@@ -135,8 +139,15 @@ int virSystemdCreateMachine(const char *name,
(unsigned int)pidleader,
rootdir ? rootdir : "",
1, "Slice", "s",
- slicename) < 0)
+ slicename) < 0) {
+ virErrorPtr err = virGetLastError();
+ if (err->code == VIR_ERR_DBUS_SERVICE &&
+ STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown"))
{
+ virResetLastError();
+ ret = -2;
+ }
goto cleanup;
+ }
ret = 0;
diff --git a/tests/virsystemdmock.c b/tests/virsystemdmock.c
index 5f9cce6..1f4413c 100644
--- a/tests/virsystemdmock.c
+++ b/tests/virsystemdmock.c
@@ -60,16 +60,20 @@ dbus_bool_t dbus_connection_set_watch_functions(DBusConnection
*connection ATTRI
DBusMessage *dbus_connection_send_with_reply_and_block(DBusConnection *connection
ATTRIBUTE_UNUSED,
DBusMessage *message,
int timeout_milliseconds
ATTRIBUTE_UNUSED,
- DBusError *error
ATTRIBUTE_UNUSED)
+ DBusError *error)
{
- DBusMessage *reply;
+ DBusMessage *reply = NULL;
dbus_message_set_serial(message, 7);
- if (getenv("FAIL_NO_SERVICE"))
+ if (getenv("FAIL_BAD_SERVICE"))
reply = dbus_message_new_error(message,
-
"org.freedesktop.DBus.Error.ServiceUnknown",
- "The name org.freedesktop.machine1 was not
provided by any .service files");
+ "org.freedesktop.systemd.badthing",
+ "Something went wrong creating the
machine");
+ else if (getenv("FAIL_NO_SERVICE"))
+ dbus_set_error(error,
+ "org.freedesktop.DBus.Error.ServiceUnknown",
+ "%s", "The name org.freedesktop.machine1 was not
provided by any .service files");
else
reply = dbus_message_new_method_return(message);
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index 3992722..bcf3ad3 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -82,35 +82,60 @@ static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED)
3, 3, 3, 3,
4, 4, 4, 4
};
+ int rv;
setenv("FAIL_NO_SERVICE", "1", 1);
- if (virSystemdCreateMachine("demo",
- "qemu",
- true,
- uuid,
- NULL,
- 123,
- false,
- NULL) == 0) {
+ if ((rv = virSystemdCreateMachine("demo",
+ "qemu",
+ true,
+ uuid,
+ NULL,
+ 123,
+ false,
+ NULL)) == 0) {
fprintf(stderr, "%s", "Unexpected create machine
success\n");
return -1;
}
- virErrorPtr err = virGetLastError();
+ if (rv != -2) {
+ fprintf(stderr, "%s", "Unexpected create machine error\n");
+ return -1;
+ }
+
+ return 0;
+}
- if (!err) {
- fprintf(stderr, "No error raised");
+static int testCreateBadSystemd(const void *opaque ATTRIBUTE_UNUSED)
+{
+ unsigned char uuid[VIR_UUID_BUFLEN] = {
+ 1, 1, 1, 1,
+ 2, 2, 2, 2,
+ 3, 3, 3, 3,
+ 4, 4, 4, 4
+ };
+ int rv;
+
+ setenv("FAIL_BAD_SERVICE", "1", 1);
+
+ if ((rv = virSystemdCreateMachine("demo",
+ "qemu",
+ true,
+ uuid,
+ NULL,
+ 123,
+ false,
+ NULL)) == 0) {
+ fprintf(stderr, "%s", "Unexpected create machine
success\n");
return -1;
}
- if (err->code == VIR_ERR_DBUS_SERVICE &&
- STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown"))
- return 0;
+ if (rv != -1) {
+ fprintf(stderr, "%s", "Unexpected create machine error\n");
+ return -1;
+ }
- fprintf(stderr, "Unexpected error code %d / message %s\n",
- err->code, err->str2);
- return -1;
+ return 0;
}
static int
@@ -122,7 +147,9 @@ mymain(void)
ret = -1;
if (virtTestRun("Test create machine ", 1, testCreateMachine, NULL) <
0)
ret = -1;
- if (virtTestRun("Test create nosystemd ", 1, testCreateNoSystemd, NULL)
< 0)
+ if (virtTestRun("Test create no systemd ", 1, testCreateNoSystemd, NULL)
< 0)
+ ret = -1;
+ if (virtTestRun("Test create bad systemd ", 1, testCreateBadSystemd, NULL)
< 0)
ret = -1;
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
--
1.8.1.4