Skip to contentSkip to conversation

qga: add local tests on linux

PATCH New [email protected] 1 participant 6 messages Oct 2, 2015

lore
Share & tools
lore
#0 lore raw

v3 cover qga: add local tests on linux

From: Marc-André Lureau <[email protected]>

v2->v3:
- add patch to introduce g_assert_* from recent glib versions
- check QGA_TEST_SIDE_EFFECTING environment variable instead of VM run
- use base64 decoder/encoder functions instead of hardcoded strings
- fix some copy/paste comment

v1->v2:
- do not loop forever when connection to qga (Michael Roth)
- add some config file tests
- fix config verbosity
- swallow virt-what stderr
- only compile and check on Linux

Marc-André Lureau (5):
qga: add QGA_CONF environment variable
qga: do not override configuration verbosity
qtest: add a few fd-level qmp helpers
glib-compat: add 2.38/2.40/2.46 asserts
tests: add a local test for guest agent

 include/glib-compat.h |  61 ++++
 qga/main.c            |   7 +-
 tests/Makefile        |   3 +
 tests/libqtest.c      |  45 ++-
 tests/libqtest.h      |   7 +
 tests/test-qga.c      | 779 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 895 insertions(+), 7 deletions(-)
 create mode 100644 tests/test-qga.c
Signature · 1 line
2.4.3
#1 lore raw

PATCH v3 1/5 qga: add QGA_CONF environment variable

From: Marc-André Lureau <[email protected]>

Having a environment variable allows to override default configuration path, useful for testing. Note that this can't easily be an argument, since loading config is done before parsing the arguments.

---
 qga/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qga/main.c +2 -1
index d8e063a..18e1e1d 100644
@@ -945,10 +945,11 @@ static void config_load(GAConfig *config)
 {
     GError *gerr = NULL;
     GKeyFile *keyfile;
+    const char *conf = g_getenv("QGA_CONF") ?: QGA_CONF_DEFAULT;
 
     /* read system config */
     keyfile = g_key_file_new();
-    if (!g_key_file_load_from_file(keyfile, QGA_CONF_DEFAULT, 0, &gerr)) {
+    if (!g_key_file_load_from_file(keyfile, conf, 0, &gerr)) {
         goto end;
     }
     if (g_key_file_has_key(keyfile, "general", "daemon", NULL)) {
Signature · 1 line
2.4.3
#2 lore raw

PATCH v3 2/5 qga: do not override configuration verbosity

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Move the default verbosity settings before loading the configuration file, or it will overwrite it. Found thanks to writing qga tests :)

---
 qga/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qga/main.c +2 -2
index 18e1e1d..aa6a063 100644
@@ -1083,8 +1083,6 @@ static void config_parse(GAConfig *config, int argc, char **argv)
         { NULL, 0, NULL, 0 }
     };
 
-    config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
-
     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
         switch (ch) {
         case 'm':
@@ -1332,6 +1330,8 @@ int main(int argc, char **argv)
     GAState *s = g_new0(GAState, 1);
     GAConfig *config = g_new0(GAConfig, 1);
 
+    config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
+
     module_call_init(MODULE_INIT_QAPI);
 
     init_dfl_pathnames();
Signature · 1 line
2.4.3
#3 lore raw

PATCH v3 3/5 qtest: add a few fd-level qmp helpers

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add a few functions to interact with qmp via a simple fd.

---
 tests/libqtest.c | 45 +++++++++++++++++++++++++++++++++++++++++----
 tests/libqtest.h |  7 +++++++
 2 files changed, 48 insertions(+), 4 deletions(-)
tests/libqtest.c +41 -4
index e5188e0..11e541d 100644
@@ -357,7 +357,7 @@ static void qmp_response(JSONMessageParser *parser, QList *tokens)
     qmp->response = (QDict *)obj;
 }
 
-QDict *qtest_qmp_receive(QTestState *s)
+QDict *qmp_fd_receive(int fd)
 {
     QMPResponseParser qmp;
     bool log = getenv("QTEST_LOG") != NULL;
@@ -368,7 +368,7 @@ QDict *qtest_qmp_receive(QTestState *s)
         ssize_t len;
         char c;
 
-        len = read(s->qmp_fd, &c, 1);
+        len = read(fd, &c, 1);
         if (len == -1 && errno == EINTR) {
             continue;
         }
@@ -388,12 +388,17 @@ QDict *qtest_qmp_receive(QTestState *s)
     return qmp.response;
 }
 
+QDict *qtest_qmp_receive(QTestState *s)
+{
+    return qmp_fd_receive(s->qmp_fd);
+}
+
 /**
  * Allow users to send a message without waiting for the reply,
  * in the case that they choose to discard all replies up until
  * a particular EVENT is received.
  */
-void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
+void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
 {
     va_list ap_copy;
     QObject *qobj;
@@ -417,13 +422,25 @@ void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
             fprintf(stderr, "%s", str);
         }
         /* Send QMP request */
-        socket_send(s->qmp_fd, str, size);
+        socket_send(fd, str, size);
 
         QDECREF(qstr);
         qobject_decref(qobj);
     }
 }
 
+void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
+{
+    qmp_fd_sendv(s->qmp_fd, fmt, ap);
+}
+
+QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
+{
+    qmp_fd_sendv(fd, fmt, ap);
+
+    return qmp_fd_receive(fd);
+}
+
 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 {
     qtest_async_qmpv(s, fmt, ap);
@@ -432,6 +449,26 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
     return qtest_qmp_receive(s);
 }
 
+QDict *qmp_fd(int fd, const char *fmt, ...)
+{
+    va_list ap;
+    QDict *response;
+
+    va_start(ap, fmt);
+    response = qmp_fdv(fd, fmt, ap);
+    va_end(ap);
+    return response;
+}
+
+void qmp_fd_send(int fd, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    qmp_fd_sendv(fd, fmt, ap);
+    va_end(ap);
+}
+
 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
 {
     va_list ap;
tests/libqtest.h +7 -0
index ec42031..ecd9872 100644
@@ -818,4 +818,11 @@ static inline int64_t clock_set(int64_t val)
  */
 bool qtest_big_endian(void);
 
+
+QDict *qmp_fd_receive(int fd);
+void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
+void qmp_fd_send(int fd, const char *fmt, ...);
+QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
+QDict *qmp_fd(int fd, const char *fmt, ...);
+
 #endif
Signature · 1 line
2.4.3
#4 lore raw

PATCH v3 4/5 glib-compat: add 2.38/2.40/2.46 asserts

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Those are mostly useful for writing tests.

---
 include/glib-compat.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
include/glib-compat.h +61 -0
index 318e000..fb25f43 100644
@@ -165,4 +165,65 @@ static inline GThread *g_thread_new(const char *name,
 #define CompatGCond GCond
 #endif /* glib 2.31 */
 
+#ifndef g_assert_true
+#define g_assert_true(expr)                                                    \
+    do {                                                                       \
+        if (G_LIKELY(expr)) {                                                  \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be TRUE");                 \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_false
+#define g_assert_false(expr)                                                   \
+    do {                                                                       \
+        if (G_LIKELY(!(expr))) {                                               \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be FALSE");                \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_null
+#define g_assert_null(expr)                                                    \
+    do {                                                                       \
+        if (G_LIKELY((expr) == NULL)) {                                        \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be NULL");                 \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_nonnull
+#define g_assert_nonnull(expr)                                                 \
+    do {                                                                       \
+        if (G_LIKELY((expr) != NULL)) {                                        \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should not be NULL");             \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_cmpmem
+#define g_assert_cmpmem(m1, l1, m2, l2)                                        \
+    do {                                                                       \
+        gconstpointer __m1 = m1, __m2 = m2;                                    \
+        int __l1 = l1, __l2 = l2;                                              \
+        if (__l1 != __l2) {                                                    \
+            g_assertion_message_cmpnum(                                        \
+                G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,                   \
+                #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==",   \
+                __l2, 'i');                                                    \
+        } else if (memcmp(__m1, __m2, __l1) != 0) {                            \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "assertion failed (" #m1 " == " #m2 ")");      \
+        }                                                                      \
+    } while (0)
+#endif
+
 #endif
Signature · 1 line
2.4.3
#5 lore raw

PATCH v3 5/5 tests: add a local test for guest agent

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add some local guest agent tests, as it is better than nothing, only when CONFIG_POSIX (using unix sockets).

With the QGA_TEST_SIDE_EFFECTING environment variable, it will include tests with side effects, such as freezing/thawing the FS or changing the time.

(a better test would involve a managed VM (or container), but it might be better to leave that off to autotest/avocado)

---
 tests/Makefile   |   3 +
 tests/test-qga.c | 779 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 782 insertions(+)
 create mode 100644 tests/test-qga.c
tests/Makefile +3 -0
index 4063639..03ab9db 100644
@@ -80,6 +80,7 @@ check-unit-$(CONFIG_GNUTLS_HASH) += tests/test-crypto-hash$(EXESUF)
 check-unit-y += tests/test-crypto-cipher$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF)
+check-unit-$(CONFIG_POSIX) += tests/test-qga$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -453,6 +454,8 @@ endif
 qtest-obj-y = tests/libqtest.o $(test-util-obj-y)
 $(check-qtest-y): $(qtest-obj-y)
 
+tests/test-qga: tests/test-qga.o $(qtest-obj-y)
+
 .PHONY: check-help
 check-help:
 	@echo "Regression testing targets:"
tests/test-qga.c new file +779 -0

779 lines not on this page · raw message

Signature · 1 line
2.4.3