Blame libvirt-gconfig/tests/test-domain-parse.c

Packit a07778
/*
Packit a07778
 * test-domain-create.c: test libvirt-gconfig domain parsing
Packit a07778
 *
Packit a07778
 * Copyright (C) 2011 Red Hat, Inc.
Packit a07778
 *
Packit a07778
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit a07778
 * copy of this software and associated documentation files (the
Packit a07778
 * "Software"), to deal in the Software without restriction, including
Packit a07778
 * without limitation the rights to use, copy, modify, merge, publish,
Packit a07778
 * distribute, sublicense, and/or sell copies of the Software, and to
Packit a07778
 * permit persons to whom the Software is furnished to do so, subject to
Packit a07778
 * the following conditions:
Packit a07778
 *
Packit a07778
 * The above copyright notice and this permission notice shall be included
Packit a07778
 * in all copies or substantial portions of the Software.
Packit a07778
 *
Packit a07778
 * The Software is provided "as is", without warranty of any kind, express
Packit a07778
 * or implied, including but not limited to the warranties of
Packit a07778
 * merchantability, fitness for a particular purpose and noninfringement.
Packit a07778
 * In no event shall the authors or copyright holders be liable for any
Packit a07778
 * claim, damages or other liability, whether in an action of contract,
Packit a07778
 * tort or otherwise, arising from, out of or in connection with the
Packit a07778
 * software or the use or other dealings in the Software.
Packit a07778
 *
Packit a07778
 * Author: Christophe Fergeau <cfergeau@redhat.com>
Packit a07778
 */
Packit a07778
Packit a07778
#include <config.h>
Packit a07778
Packit a07778
#include <string.h>
Packit a07778
#include <libvirt-gconfig/libvirt-gconfig.h>
Packit a07778
Packit a07778
Packit a07778
int main(int argc, char **argv)
Packit a07778
{
Packit a07778
    GVirConfigDomain *domain;
Packit a07778
    const char *name;
Packit a07778
    GStrv features;
Packit a07778
    char *xml;
Packit a07778
    GError *error = NULL;
Packit a07778
Packit a07778
    gvir_config_init(&argc, &argv);
Packit a07778
    if (argc != 2) {
Packit a07778
        g_print("Usage: %s filename\n", argv[0]);
Packit a07778
        g_print("Attempt to parse the libvirt XML definition from filename\n");
Packit a07778
        return 1;
Packit a07778
    }
Packit a07778
Packit a07778
    g_file_get_contents(argv[1], &xml, NULL, &error);
Packit a07778
    if (error != NULL) {
Packit a07778
        g_print("Couldn't read %s: %s\n", argv[1], error->message);
Packit a07778
        return 2;
Packit a07778
    }
Packit a07778
Packit a07778
    domain = gvir_config_domain_new_from_xml(xml, &error);
Packit a07778
    if (error != NULL) {
Packit a07778
        g_print("Couldn't parse %s: %s\n", argv[1], error->message);
Packit a07778
        return 3;
Packit a07778
    }
Packit a07778
    g_assert(domain != NULL);
Packit a07778
    gvir_config_object_validate(GVIR_CONFIG_OBJECT(domain), &error);
Packit a07778
    if (error != NULL) {
Packit a07778
        g_print("%s format is invalid: %s\n", argv[1], error->message);
Packit a07778
        g_clear_error(&error);
Packit a07778
    }
Packit a07778
Packit a07778
    name = gvir_config_domain_get_name(domain);
Packit a07778
    g_assert(name != NULL);
Packit a07778
    g_assert(strcmp(name, "foo") == 0);
Packit a07778
Packit a07778
    g_assert(gvir_config_domain_get_memory(domain) == 987654321);
Packit a07778
Packit a07778
    features = gvir_config_domain_get_features(domain);
Packit a07778
    g_assert(g_strv_length(features) == 2);
Packit a07778
    g_assert(strcmp(features[0], "f1") == 0);
Packit a07778
    g_assert(strcmp(features[1], "f2") == 0);
Packit a07778
    g_strfreev(features);
Packit a07778
Packit a07778
    g_free(xml);
Packit a07778
Packit a07778
    xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(domain));
Packit a07778
    g_print("%s\n", xml);
Packit a07778
    g_free(xml);
Packit a07778
    g_object_unref(G_OBJECT(domain));
Packit a07778
Packit a07778
    return 0;
Packit a07778
}