Add trivial test connecting via systemd socket activation.

Signed-off-by: Nir Soffer <[email protected]>
---

Changes in v2:
- Skip test if nbdkit or nbdkit memory plugin are not available (Richard)
- Use single connection, supporting multiple connections needs more
  work.

v1 was here:
https://listman.redhat.com/archives/libguestfs/2021-November/msg00049.html

 tests/Makefile.am                         |  9 ++++
 tests/connect-systemd-socket-activation.c | 65 +++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 tests/connect-systemd-socket-activation.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7f00f6f..0074acf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -192,6 +192,7 @@ check_PROGRAMS += \
        opt-list \
        opt-info \
        opt-list-meta \
+       connect-systemd-socket-activation \
        connect-unix \
        connect-tcp \
        connect-tcp6 \
@@ -235,6 +236,7 @@ TESTS += \
        opt-list \
        opt-info \
        opt-list-meta \
+       connect-systemd-socket-activation \
        connect-unix \
        connect-tcp \
        connect-tcp6 \
@@ -424,6 +426,13 @@ opt_info_LDADD = $(top_builddir)/lib/libnbd.la
 opt_list_meta_SOURCES = opt-list-meta.c
 opt_list_meta_LDADD = $(top_builddir)/lib/libnbd.la
 
+connect_systemd_socket_activation_SOURCES = \
+       connect-systemd-socket-activation.c \
+       requires.c \
+       requires.h \
+       $(NULL)
+connect_systemd_socket_activation_LDADD = $(top_builddir)/lib/libnbd.la
+
 connect_unix_SOURCES = connect-unix.c
 connect_unix_LDADD = $(top_builddir)/lib/libnbd.la
 
diff --git a/tests/connect-systemd-socket-activation.c 
b/tests/connect-systemd-socket-activation.c
new file mode 100644
index 0000000..64da2b6
--- /dev/null
+++ b/tests/connect-systemd-socket-activation.c
@@ -0,0 +1,65 @@
+/* NBD client library in userspace
+ * Copyright (C) 2021 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/* Test connecting using systemd socket activation. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include <libnbd.h>
+
+#include "requires.h"
+
+int
+main (int argc, char *argv[])
+{
+  char *args[] = {"nbdkit", "-f", "memory", "size=1m", NULL};
+  struct nbd_handle *nbd;
+  char *uri = NULL;
+  int result = EXIT_FAILURE;
+
+  requires ("nbdkit --version");
+  requires ("nbdkit memory --version");
+
+  printf("Connecting via systemd socket activation...\n");
+
+  nbd = nbd_create ();
+  if (nbd == NULL)
+    goto out;
+
+  if (nbd_connect_systemd_socket_activation (nbd, args) == -1)
+    goto out;
+
+  /* Libnbd creates unix socket internally, but this is not documented yet. */
+  uri = nbd_get_uri (nbd);
+
+  printf ("Connected to %s\n", uri);
+  result = EXIT_SUCCESS;
+
+out:
+  if (result == EXIT_FAILURE)
+    fprintf (stderr, "%s\n", nbd_get_error ());
+
+  free (uri);
+  nbd_close (nbd);
+
+  exit (result);
+}
-- 
2.31.1

_______________________________________________
Libguestfs mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/libguestfs

Reply via email to