--- tests/Makefile.am | 10 +- tests/qemustartuppolicytest.c | 218 +++++++++++++++++++++ .../domain-input-cdrom-present-policy-optional.xml | 28 +++ ...domain-output-cdrom-present-policy-optional.xml | 29 +++ 4 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 tests/qemustartuppolicytest.c create mode 100644 tests/qemustartuppolicytestdata/domain-input-cdrom-present-policy-optional.xml create mode 100644 tests/qemustartuppolicytestdata/domain-output-cdrom-present-policy-optional.xml diff --git a/tests/Makefile.am b/tests/Makefile.am index a9bcf4c..e54532c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -84,6 +84,7 @@ EXTRA_DIST = \ qemuxml2argvdata \ qemuxml2xmloutdata \ qemuxmlnsdata \ + qemustartuppolicytestdata \ securityselinuxlabeldata \ schematestutils.sh \ sexpr2xmldata \ @@ -162,7 +163,7 @@ if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest \ qemuargv2xmltest qemuhelptest domainsnapshotxml2xmltest \ qemumonitortest qemumonitorjsontest qemuhotplugtest \ - qemuagenttest + qemuagenttest qemustartuppolicytest endif if WITH_LXC @@ -439,12 +440,17 @@ domainsnapshotxml2xmltest_SOURCES = \ domainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ testutils.c testutils.h domainsnapshotxml2xmltest_LDADD = $(qemu_LDADDS) + +qemustartuppolicytest_SOURCES = \ + qemustartuppolicytest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h $(NULL) +qemustartuppolicytest_LDADD = $(qemu_LDADDS) else EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \ qemuxmlnstest.c qemuhelptest.c domainsnapshotxml2xmltest.c \ qemumonitortest.c testutilsqemu.c testutilsqemu.h \ qemumonitorjsontest.c qemuhotplugtest.c \ - qemuagenttest.c \ + qemuagenttest.c qemustartuppolicytest.c \ $(QEMUMONITORTESTUTILS_SOURCES) endif diff --git a/tests/qemustartuppolicytest.c b/tests/qemustartuppolicytest.c new file mode 100644 index 0000000..0f0d4b8 --- /dev/null +++ b/tests/qemustartuppolicytest.c @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2013 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.1 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, see + * <http://www.gnu.org/licenses/>. + * + */ + +#include <config.h> + +#include <stdlib.h> +#include <stdbool.h> + +#include "testutils.h" + +#ifdef WITH_QEMU + +# include "internal.h" +# include "conf/domain_event.h" +# include "qemu/qemu_conf.h" +# include "qemu/qemu_process.h" +# include "testutilsqemu.h" + +# define VIR_FROM_THIS VIR_FROM_NONE + +static virQEMUDriver driver; + +enum { + MANDATORY, + REQUISITE, + OPTIONAL +}; + +typedef struct _qemuStartupPolicyTestData qemuStartupPolicyTestData; +struct _qemuStartupPolicyTestData { + const char *domain_filename; + int policy; + bool disk_present; + bool fail; + bool cold_boot; +}; + +static int +qemuNewDomainObjects(virDomainXMLOptionPtr xmlopt, + virDomainObjPtr *vm, + const char *filename) +{ + int ret = -1; + + if (!(*vm = virDomainObjNew(xmlopt))) + goto cleanup; + + if (!((*vm)->def = virDomainDefParseFile(filename, + driver.caps, + driver.xmlopt, + QEMU_EXPECTED_VIRT_TYPES, + 0))) + goto cleanup; + + ret = 0; + +cleanup: + return ret; +} + +static int +testQemuStartupPolicy(const void *data) +{ + int ret = -1, check_ret = -1; + qemuStartupPolicyTestData *test = (qemuStartupPolicyTestData *)data; + const char *domain_filename = test->domain_filename; + bool cold_boot = test->cold_boot; + char *inxml = NULL; + char *outxml = NULL; + char *outXmlData = NULL; + char *actual = NULL; + virDomainObjPtr vm = NULL; + + if (virAsprintf(&inxml, "%s/qemustartuppolicytestdata/domain-input-%s.xml", + abs_srcdir, domain_filename) < 0) + goto cleanup; + + if (qemuNewDomainObjects(driver.xmlopt, &vm, inxml) < 0) + goto cleanup; + + check_ret = qemuDomainCheckDiskPresence(&driver, vm, cold_boot); + + if (check_ret < 0) { + if (test->disk_present) { + fprintf(stderr, "Disk is present, presence checking failed\n"); + } else { + if (test->policy == MANDATORY) { + if (!test->fail) + fprintf(stderr, "Disk is not present, " + "failed with mandatory policy\n"); + else + ret = 0; + } else if (test->policy == REQUISITE) { + if (cold_boot) { + if (!test->fail) + fprintf(stderr, "Disk is not present, " + "failed with requisite policy\n"); + else + ret = 0; + } else { + fprintf(stderr, "It is not cold_boot, " + "should not fail with requisite policy\n"); + } + } else if (test->policy == OPTIONAL && !test->fail) { + fprintf(stderr, "Optional policy should not break disk check\n"); + } else { + fprintf(stderr, "presence checking failed without any policy\n"); + } + } + goto cleanup; + } + + if (!(actual = virDomainDefFormat(vm->def, VIR_DOMAIN_XML_INACTIVE))) + goto cleanup; + + if (virAsprintf(&outxml, "%s/qemustartuppolicytestdata/domain-output-%s.xml", + abs_srcdir, domain_filename) < 0) + goto cleanup; + + if (virtTestLoadFile(outxml, &outXmlData) < 0) + goto cleanup; + + if (STRNEQ(outXmlData, actual)) { + virtTestDifference(stderr, outXmlData, actual); + goto cleanup; + } + + ret = 0; + +cleanup: + VIR_FREE(inxml); + VIR_FREE(outxml); + VIR_FREE(actual); + VIR_FREE(outXmlData); + virObjectUnref(vm); + return ret; +} + +static int +mymain(void) +{ + int ret = 0; + qemuStartupPolicyTestData data = {NULL}; + + driver.config = virQEMUDriverConfigNew(false); + if ((driver.caps = testQemuCapsInit()) == NULL) + return EXIT_FAILURE; + + if (!(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver))) + return EXIT_FAILURE; + + if (!(driver.domainEventState = virDomainEventStateNew())) + return EXIT_FAILURE; + +# define DO_TEST(file, pol, present, fial) \ + data.domain_filename = file; \ + data.policy = pol; \ + data.disk_present = present; \ + data.fail = fial; \ + \ + if (virtTestRun(#file, 1, testQemuStartupPolicy, &data) < 0) \ + ret = -1; \ + +# define DO_TEST_MANDATORY(file, disk_present, fail) \ + do { \ + data.cold_boot = true; \ + DO_TEST(file, MANDATORY, disk_present, fail) \ + } while (0) + +# define DO_TEST_REQUISITE(file, disk_present, boot, fail) \ + do { \ + data.cold_boot = boot; \ + DO_TEST(file, REQUISITE, disk_present, fail) \ + } while (0) + +# define DO_TEST_OPTIONAL(file, disk_present) \ + do { \ + data.cold_boot = true; \ + DO_TEST(file, OPTIONAL, disk_present, false) \ + } while (0) + + DO_TEST_OPTIONAL("cdrom-present-policy-optional", false); + + virObjectUnref(driver.caps); + virObjectUnref(driver.xmlopt); + virObjectUnref(driver.config); + virDomainEventStateFree(driver.domainEventState); + + return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIRT_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_QEMU */ diff --git a/tests/qemustartuppolicytestdata/domain-input-cdrom-present-policy-optional.xml b/tests/qemustartuppolicytestdata/domain-input-cdrom-present-policy-optional.xml new file mode 100644 index 0000000..7087a21 --- /dev/null +++ b/tests/qemustartuppolicytestdata/domain-input-cdrom-present-policy-optional.xml @@ -0,0 +1,28 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219100</memory> + <currentMemory unit='KiB'>219100</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <disk type='file' device='cdrom'> + <target dev='hdc' bus='ide'/> + <driver name='qemu' type='raw'/> + <source file='/dev/null' startupPolicy='optional'/> + <readonly/> + <address type='drive' controller='0' bus='1' target='0' unit='0'/> + </disk> + <controller type='usb' index='0'/> + <controller type='ide' index='0'/> + <memballoon model='virtio'/> + </devices> +</domain> diff --git a/tests/qemustartuppolicytestdata/domain-output-cdrom-present-policy-optional.xml b/tests/qemustartuppolicytestdata/domain-output-cdrom-present-policy-optional.xml new file mode 100644 index 0000000..f5316f9 --- /dev/null +++ b/tests/qemustartuppolicytestdata/domain-output-cdrom-present-policy-optional.xml @@ -0,0 +1,29 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219100</memory> + <currentMemory unit='KiB'>219100</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <disk type='file' device='cdrom'> + <driver name='qemu' type='raw'/> + <source file='/dev/null' startupPolicy='optional'/> + <target dev='hdc' bus='ide'/> + <readonly/> + <address type='drive' controller='0' bus='1' target='0' unit='0'/> + </disk> + <controller type='usb' index='0'/> + <controller type='ide' index='0'/> + <controller type='pci' index='0' model='pci-root'/> + <memballoon model='virtio'/> + </devices> +</domain> -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list