[libgovirt] tests: Add test for new cluster/host OvirtVm API



commit 4672d00bc9a6dc13bf56f2bff16373ed256a6213
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Tue Jul 25 16:22:48 2017 +0200

    tests: Add test for new cluster/host OvirtVm API

 tests/Makefile.am                                  |    1 +
 tests/mock-xml-data/test-parse-vm-host-cluster.xml |   86 +++++++++++++++
 tests/test-govirt.c                                |  113 ++++++++++++++++++++
 3 files changed, 200 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 24f0c6b..b8def14 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,6 +7,7 @@ EXTRA_DIST =                            \
        https-cert/server-cert.pem      \
        https-cert/server-key.csr       \
        https-cert/server-key.pem       \
+       mock-xml-data/                  \
        $(NULL)
 
 AM_LDFLAGS =                                   \
diff --git a/tests/mock-xml-data/test-parse-vm-host-cluster.xml 
b/tests/mock-xml-data/test-parse-vm-host-cluster.xml
new file mode 100644
index 0000000..e7edfa7
--- /dev/null
+++ b/tests/mock-xml-data/test-parse-vm-host-cluster.xml
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<vm href="/ovirt-engine/api/vms/96f4b45c-a795-4de9-89cb-d009e4bceedc" 
id="96f4b45c-a795-4de9-89cb-d009e4bceedc">
+    <bios>
+        <boot_menu>
+            <enabled>false</enabled>
+        </boot_menu>
+    </bios>
+    <cpu>
+        <architecture>x86_64</architecture>
+        <topology>
+            <cores>1</cores>
+            <sockets>1</sockets>
+            <threads>1</threads>
+        </topology>
+    </cpu>
+    <cpu_shares>0</cpu_shares>
+    <creation_time>2017-01-01T00:00:00.000+00:00</creation_time>
+    <delete_protected>false</delete_protected>
+    <display>
+        <address>10.0.0.123</address>
+        <allow_override>true</allow_override>
+        <certificate>
+            <subject>O=org.gnome.libgovirt,CN=hyper01.govirt.gnome.org</subject>
+        </certificate>
+        <copy_paste_enabled>true</copy_paste_enabled>
+        <file_transfer_enabled>true</file_transfer_enabled>
+        <monitors>1</monitors>
+        <proxy>10.0.0.10</proxy>
+        <secure_port>5900</secure_port>
+        <single_qxl_pci>true</single_qxl_pci>
+        <smartcard_enabled>false</smartcard_enabled>
+        <type>spice</type>
+    </display>
+    <high_availability>
+        <enabled>false</enabled>
+        <priority>1</priority>
+    </high_availability>
+    <io>
+        <threads>0</threads>
+    </io>
+    <memory>2097152000</memory>
+    <memory_policy>
+        <ballooning>true</ballooning>
+        <guaranteed>2097152000</guaranteed>
+        <max>8388608000</max>
+    </memory_policy>
+    <migration>
+        <auto_converge>inherit</auto_converge>
+        <compressed>inherit</compressed>
+    </migration>
+    <migration_downtime>-1</migration_downtime>
+    <origin>ovirt</origin>
+    <os>
+        <boot>
+            <devices>
+                <device>network</device>
+                <device>hd</device>
+            </devices>
+        </boot>
+        <type>rhel_7x64</type>
+    </os>
+    <sso>
+        <methods>
+            <method id="guest_agent"/>
+        </methods>
+    </sso>
+    <start_paused>false</start_paused>
+    <stateless>false</stateless>
+    <time_zone>
+        <name>Etc/GMT</name>
+    </time_zone>
+    <type>desktop</type>
+    <usb>
+        <enabled>true</enabled>
+        <type>native</type>
+    </usb>
+    <cluster href="/ovirt-engine/api/clusters/00000000-0000-0000-0001-000000000000" 
id="00000000-0000-0000-0001-000000000000"/>
+    <next_run_configuration_exists>false</next_run_configuration_exists>
+    <numa_tune_mode>interleave</numa_tune_mode>
+    <placement_policy>
+        <affinity>migratable</affinity>
+    </placement_policy>
+    <run_once>true</run_once>
+    <status>up</status>
+    <host href="/ovirt-engine/api/hosts/00000000-0000-0000-0002-000000000000" 
id="00000000-0000-0000-0002-000000000000"/>
+</vm>
diff --git a/tests/test-govirt.c b/tests/test-govirt.c
index d37d924..c09ac94 100644
--- a/tests/test-govirt.c
+++ b/tests/test-govirt.c
@@ -219,6 +219,118 @@ static void test_govirt_list_vms(void)
 }
 
 
+typedef struct {
+    const char *uuid;
+    const char *name;
+    const char *xml;
+    const char *filename;
+} MockOvirtVm;
+
+static void govirt_mock_httpd_add_vms(GovirtMockHttpd *httpd, MockOvirtVm vms[], size_t count)
+{
+    GString *vms_xml;
+    unsigned int i;
+
+    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api",
+                                  "<api><link href=\"/ovirt-engine/api/vms\" rel=\"vms\"/></api>");
+
+    /* Create /api/vms */
+    vms_xml = g_string_new("<vms>");
+    for (i = 0; i < count; i++) {
+        MockOvirtVm *vm = &vms[i];
+
+        g_string_append_printf(vms_xml, "<vm href=\"/ovirt-engine/api/vms/%s\" id=\"%s\">", vm->uuid, 
vm->uuid);
+        g_string_append_printf(vms_xml, "<name>%s</name>", vm->name);
+        g_string_append_printf(vms_xml, "</vm>");
+    }
+    g_string_append(vms_xml, "</vms>");
+
+    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api/vms", vms_xml->str);
+    g_string_free(vms_xml, TRUE);
+
+    /* Create individual /api/vms/$uuid entry points */
+    for (i = 0; i < count; i++) {
+        MockOvirtVm *vm = &vms[i];
+        char *href;
+
+        href = g_strdup_printf("/ovirt-engine/api/vms/%s", vm->uuid);
+        if (vm->xml != NULL) {
+            govirt_mock_httpd_add_request(httpd, "GET", href, vm->xml);
+        } else if (vm->filename != NULL) {
+            char *body;
+
+            if (!g_file_get_contents(g_test_get_filename(G_TEST_DIST, "mock-xml-data", vm->filename, NULL),
+                                     &body, NULL, NULL)) {
+                g_warn_if_reached();
+                continue;
+            }
+
+            govirt_mock_httpd_add_request(httpd, "GET", href, body);
+            g_free(body);
+        }
+        g_free(href);
+    }
+}
+
+
+static void test_govirt_parse_vm_host_cluster(void)
+{
+    OvirtProxy *proxy;
+    OvirtApi *api;
+    OvirtCollection *vms;
+    OvirtResource *vm;
+    GError *error = NULL;
+    GovirtMockHttpd *httpd;
+    OvirtHost *host;
+    OvirtCluster *cluster;
+    char *guid;
+    MockOvirtVm mock_vm = { .uuid = "00000000-0000-0000-0000-000000000000",
+                            .name = "vm1",
+                            .filename="test-parse-vm-host-cluster.xml" };
+
+    httpd = govirt_mock_httpd_new(GOVIRT_HTTPS_PORT);
+    govirt_mock_httpd_add_vms(httpd, &mock_vm, 1);
+    govirt_mock_httpd_start(httpd);
+
+    proxy = ovirt_proxy_new("localhost:" G_STRINGIFY(GOVIRT_HTTPS_PORT));
+    ovirt_proxy_set_mock_ca(proxy);
+    api = ovirt_proxy_fetch_api(proxy, &error);
+    g_test_assert_expected_messages();
+    g_assert_nonnull(api);
+    g_assert_no_error(error);
+
+    vms = ovirt_api_get_vms(api);
+    ovirt_collection_fetch(vms, proxy, &error);
+    g_assert_no_error(error);
+
+    vm = ovirt_collection_lookup_resource(vms, "vm1");
+    g_assert_nonnull(vm);
+    ovirt_resource_refresh(vm, proxy, &error);
+    g_assert_no_error(error);
+
+    check_vm_display(OVIRT_VM(vm));
+
+    host = ovirt_vm_get_host(OVIRT_VM(vm));
+    g_assert_nonnull(host);
+    g_object_get(G_OBJECT(host), "guid", &guid, NULL);
+    g_assert_cmpstr(guid, ==, "00000000-0000-0000-0002-000000000000");
+    g_free(guid);
+    g_object_unref(host);
+
+    cluster = ovirt_vm_get_cluster(OVIRT_VM(vm));
+    g_assert_nonnull(cluster);
+    g_object_get(G_OBJECT(cluster), "guid", &guid, NULL);
+    g_assert_cmpstr(guid, ==, "00000000-0000-0000-0001-000000000000");
+    g_free(guid);
+    g_object_unref(cluster);
+
+    g_object_unref(vm);
+    g_object_unref(proxy);
+
+    govirt_mock_httpd_stop(httpd);
+}
+
+
 static void test_govirt_list_duplicate_vms(void)
 {
     OvirtProxy *proxy;
@@ -287,6 +399,7 @@ main(int argc, char **argv)
     g_test_add_func("/govirt/test-http", test_govirt_http);
     g_test_add_func("/govirt/test-list-vms", test_govirt_list_vms);
     g_test_add_func("/govirt/test-list-duplicate-vms", test_govirt_list_duplicate_vms);
+    g_test_add_func("/govirt/test-parse-vm-host-cluster", test_govirt_parse_vm_host_cluster);
 
     return g_test_run();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]