From: Frederic Mora <fmora@xxxxxxxxx> --- src/tests/qemu-no-disk-and-media.xml | 41 +++++++++ src/tests/test-connect-get-capabilities.phpt | 27 ++++++ src/tests/test-connect-get-info.phpt | 110 +++++++++++++++++++++++++ src/tests/test-connect-get-machine-types.phpt | 30 +++++++ src/tests/test-domain-create-and-shutdown.phpt | 38 +++++++++ src/tests/test-domain-define.phpt | 38 +++++++++ src/tests/test-domain-disk.phpt | 71 ++++++++++++++++ src/tests/test-domain-get.phpt | 91 ++++++++++++++++++++ src/tests/test-domain-lookup-by-name.phpt | 35 ++++++++ src/tests/test-domain-nic.phpt | 52 ++++++++++++ src/tests/test-logfile.phpt | 43 ++++++++++ 11 files changed, 576 insertions(+) create mode 100644 src/tests/qemu-no-disk-and-media.xml create mode 100644 src/tests/test-connect-get-capabilities.phpt create mode 100644 src/tests/test-connect-get-info.phpt create mode 100644 src/tests/test-connect-get-machine-types.phpt create mode 100644 src/tests/test-domain-create-and-shutdown.phpt create mode 100644 src/tests/test-domain-define.phpt create mode 100644 src/tests/test-domain-disk.phpt create mode 100644 src/tests/test-domain-get.phpt create mode 100644 src/tests/test-domain-lookup-by-name.phpt create mode 100644 src/tests/test-domain-nic.phpt create mode 100644 src/tests/test-logfile.phpt diff --git a/src/tests/qemu-no-disk-and-media.xml b/src/tests/qemu-no-disk-and-media.xml new file mode 100644 index 0000000..bad09f9 --- /dev/null +++ b/src/tests/qemu-no-disk-and-media.xml @@ -0,0 +1,41 @@ +<domain type='qemu'> + <name>test-guest-no-disk-and-media</name> + <uuid>28a15e00-9c40-ddc1-b6df-9a08921b93f0</uuid> + <memory>65535</memory> + <currentMemory>65535</currentMemory> + <vcpu>1</vcpu> + <os> + <type arch='i686'>hvm</type> + <boot dev='cdrom'/> + </os> + <features> + <acpi/> + <apic/> + <pae/> + </features> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>restart</on_crash> + <devices> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='scsi' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> + </controller> + <serial type='pty'> + <target port='0'/> + </serial> + <console type='pty'> + <target type='serial' port='0'/> + </console> + <input type='mouse' bus='ps2'/> + <graphics type='vnc' port='-1' autoport='yes'/> + <video> + <model type='cirrus' vram='9216' heads='1'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> + </video> + </devices> +</domain> + diff --git a/src/tests/test-connect-get-capabilities.phpt b/src/tests/test-connect-get-capabilities.phpt new file mode 100644 index 0000000..3c3d50f --- /dev/null +++ b/src/tests/test-connect-get-capabilities.phpt @@ -0,0 +1,27 @@ +--TEST-- +libvirt_connect_get_capabilities +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + + echo "# libvirt_connect\n"; + var_dump($conn = libvirt_connect('test:///default', false)); + if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + + echo "# libvirt_connect_get_capabilities\n"; + var_dump($res = libvirt_connect_get_emulator($conn)); + if (!$res) { + die('Connect get capabilities failed with error: '.libvirt_get_last_error()); + } + + unset($conn); +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_connect_get_capabilities +string(%d) "%s" +Done diff --git a/src/tests/test-connect-get-info.phpt b/src/tests/test-connect-get-info.phpt new file mode 100644 index 0000000..1cfa527 --- /dev/null +++ b/src/tests/test-connect-get-info.phpt @@ -0,0 +1,110 @@ +--TEST-- +libvirt_connect_get_information +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + +echo "# libvirt_connect\n"; +var_dump($conn = libvirt_connect('test:///default', false)); +if (!is_resource($conn)) { + die('Connection to default hypervisor failed'); +} + +echo "# libvirt_node_get_info\n"; +$res = libvirt_node_get_info($conn); + +if (!is_array($res)) { + die("libvirt_node_get_info returned unexpected value: " . print_r($res, true) . + "\nFailed with error: ".libvirt_get_last_error()); +} + +foreach (array('memory', 'cpus') as $k) { + if (!array_key_exists($k, $res) || !is_numeric($res[$k])) { + die("libvirt_node_get_info: Absent or incorrect key \'$k\'. Value: " . print_r($res, true) . + "\nFailed with error: ".libvirt_get_last_error()); + } +} + +unset($res); + +echo "# libvirt_connect_get_uri\n"; +var_dump(libvirt_connect_get_uri($conn)); + +echo "# libvirt_connect_get_hostname\n"; +var_dump(libvirt_connect_get_hostname($conn)); + +echo "# libvirt_domain_get_counts\n"; +$counts = libvirt_domain_get_counts($conn); + +if (!is_array($counts)) { + die("libvirt_domain_get_counts returned unexpected value: " . print_r($counts, true) . + "\nFailed with error: ".libvirt_get_last_error()); +} + +foreach (array("total", "active", "inactive") as $k) { + if (!array_key_exists($k, $counts) || !is_numeric($counts[$k])) { + die("libvirt_domain_get_counts: Absent or incorrect key \'$k\'. Value: " . print_r($counts, true) . + "\nFailed with error: ".libvirt_get_last_error()); + } +} + +echo "# libvirt_list_active_domains\n"; +var_dump(count(libvirt_list_active_domains($conn)) == $counts['active']); + +echo "# libvirt_list_inactive_domains\n"; +var_dump(count(libvirt_list_inactive_domains($conn)) == $counts['inactive']); + +unset($count); + +echo "# libvirt_connect_get_hypervisor\n"; +$res = libvirt_connect_get_hypervisor($conn); +if (!is_array($res)) { + die("libvirt_connect_get_hypervisor returned unexpected value: " . print_r($res, true) . + "\nFailed with error: ".libvirt_get_last_error()); +} +unset($res); + +echo "# libvirt_connect_get_maxvcpus\n"; +var_dump(is_numeric(libvirt_connect_get_maxvcpus($conn))); + +echo "# libvirt_connect_get_information\n"; +$res = @libvirt_connect_get_information($conn); // Need to suppress driver-dependent warning messages +if (!is_array($res)) { + die("libvirt_connect_get_information returned unexpected value: " . print_r($res, true) . + "\nFailed with error: ".libvirt_get_last_error()); +} +unset($res); + +echo "# libvirt_connect_get_encrypted\n"; +var_dump(libvirt_connect_get_encrypted($conn) != -1); + +echo "# libvirt_connect_get_secure\n"; +var_dump(libvirt_connect_get_secure($conn) != -1); + +unset($conn); + +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_node_get_info +# libvirt_connect_get_uri +string(%d) "%s" +# libvirt_connect_get_hostname +string(%d) "%s" +# libvirt_domain_get_counts +# libvirt_list_active_domains +bool(true) +# libvirt_list_inactive_domains +bool(true) +# libvirt_connect_get_hypervisor +# libvirt_connect_get_maxvcpus +bool(true) +# libvirt_connect_get_information +# libvirt_connect_get_encrypted +bool(true) +# libvirt_connect_get_secure +bool(true) +Done diff --git a/src/tests/test-connect-get-machine-types.phpt b/src/tests/test-connect-get-machine-types.phpt new file mode 100644 index 0000000..c9b0cd8 --- /dev/null +++ b/src/tests/test-connect-get-machine-types.phpt @@ -0,0 +1,30 @@ +--TEST-- +libvirt_connect_get_machine_types +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + + echo "# libvirt_connect\n"; + var_dump($conn = libvirt_connect('test:///default', false)); + if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + + echo "# libvirt_connect_get_machine_types\n"; + $res = libvirt_connect_get_machine_types($conn); + if (!$res) { + die('Get machine types failed with error: '.libvirt_get_last_error()); + } + if (!is_array($res)) { + die("libvirt_connect_get_machine_types returned unexpected value: " . print_r($res, true) . + "\nFailed with error: ".libvirt_get_last_error()); + } + unset($res); + unset($conn); +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_connect_get_machine_types +Done diff --git a/src/tests/test-domain-create-and-shutdown.phpt b/src/tests/test-domain-create-and-shutdown.phpt new file mode 100644 index 0000000..2bed1ab --- /dev/null +++ b/src/tests/test-domain-create-and-shutdown.phpt @@ -0,0 +1,38 @@ +--TEST-- +libvirt_domain_shutdown +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + echo "# libvirt_connect\n"; + var_dump($conn = libvirt_connect('test:///default', false)); + if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + + $xml = file_get_contents(__DIR__.'/example-no-disk-and-media.xml'); + + echo "# libvirt_domain_create_xml\n"; + var_dump($res = libvirt_domain_create_xml($conn, $xml)); + if (!is_resource($res)) + die('Domain definition failed with error: '.libvirt_get_last_error()); + + echo "# libvirt_domain_shutdown\n"; + var_dump($ret = libvirt_domain_shutdown($res)); + if (!$ret) { + die('Domain shutdown failed with error: '.libvirt_get_last_error()); + } + + unset($res); + unset($conn); + +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_create_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_shutdown +bool(true) +Done + diff --git a/src/tests/test-domain-define.phpt b/src/tests/test-domain-define.phpt new file mode 100644 index 0000000..114a127 --- /dev/null +++ b/src/tests/test-domain-define.phpt @@ -0,0 +1,38 @@ +--TEST-- +libvirt_domain_define_xml +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + + echo "# libvirt_connect\n"; + var_dump($conn = libvirt_connect('test:///default', false)); + if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + + $xml = file_get_contents(__DIR__.'/example-no-disk-and-media.xml'); + + echo "# libvirt_domain_define_xml\n"; + var_dump($dom = libvirt_domain_define_xml($conn, $xml)); + if (!is_resource($dom)) + die('Domain definition failed with error: '.libvirt_get_last_error()); + + echo "# libvirt_domain_undefine\n"; + var_dump($ret = libvirt_domain_undefine($dom)); + if (!$ret) { + unlink($name); + die('Domain undefine failed with error: '.libvirt_get_last_error()); + } + + unset($dom); + unset($conn); +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_define_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_undefine +bool(true) +Done diff --git a/src/tests/test-domain-disk.phpt b/src/tests/test-domain-disk.phpt new file mode 100644 index 0000000..c2b7cb5 --- /dev/null +++ b/src/tests/test-domain-disk.phpt @@ -0,0 +1,71 @@ +--TEST-- +libvirt_domain_disk +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + +$disk_devname = 'hda'; // Disk that will be added to existing guest config +$disk_devtype = 'ide'; +$disk_iodriver = 'raw'; +$disk_image = '/tmp/test-libvirt-php.img'; +touch($disk_image); + +$logfile = __DIR__ . '/test.log'; +@unlink($logfile); +echo "# libvirt_logfile_set\n"; +if (!libvirt_logfile_set($logfile, 100)) { // Log file will be 100 KB max + die('Logfile set failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_connect\n"; +var_dump($conn = libvirt_connect('qemu:///session', false)); +if (!is_resource($conn)) + die('Connection to default hypervisor failed. Is libvirt started? What about the QEMU driver?'); + +// libvirt_domain_disk_add needs a persistent guest loaded from a file +$xml = file_get_contents(__DIR__.'/qemu-no-disk-and-media.xml'); +echo "# libvirt_domain_define_xml\n"; +var_dump($dom = libvirt_domain_define_xml($conn, $xml)); +if (!is_resource($dom)) { + die('Domain definition failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_domain_disk_add\n"; +// The doc is a wrong. It says the function returns a domain resource. +// It actually returns a boolean! +var_dump($ret = libvirt_domain_disk_add($dom, $disk_image, $disk_devname, $disk_devtype, $disk_iodriver, 0)); +if (!$ret) { + die('Domain disk add failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_domain_get_disk_devices\n"; +$info = libvirt_domain_get_disk_devices($dom); +var_dump(is_array($info)); +if (!array_key_exists('num', $info)) + die('libvirt_domain_get_disk_devices: Num element is missing in the output array'); + +echo "# libvirt_domain_undefine\n"; +var_dump($ret = libvirt_domain_undefine($dom)); +if (!$ret) { + die('Domain undefine failed with error: '.libvirt_get_last_error()); +} + +unset($dom); +unset($conn); + +?> +Done +--EXPECTF-- +# libvirt_logfile_set +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_define_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_disk_add +bool(true) +# libvirt_domain_get_disk_devices +bool(true) +# libvirt_domain_undefine +bool(true) +Done \ No newline at end of file diff --git a/src/tests/test-domain-get.phpt b/src/tests/test-domain-get.phpt new file mode 100644 index 0000000..c723210 --- /dev/null +++ b/src/tests/test-domain-get.phpt @@ -0,0 +1,91 @@ +--TEST-- +libvirt_domain_get_xml +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + +echo "# libvirt_connect\n"; +var_dump($conn = libvirt_connect('test:///default', false)); +if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + +// We need a running domain to be able to pull the id +$xml = file_get_contents(__DIR__.'/example-no-disk-and-media.xml'); +echo "# libvirt_domain_create_xml\n"; +var_dump($dom = libvirt_domain_create_xml($conn, $xml)); +if (!is_resource($dom)) { + die('Domain definition failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_domain_get_name\n"; +var_dump($dom_name = libvirt_domain_get_name($dom)); + +echo "# libvirt_domain_get_uuid_string\n"; +var_dump($dom_uuid_str = libvirt_domain_get_uuid_string($dom)); + +echo "# libvirt_domain_get_uuid\n"; +var_dump($dom_uuid = libvirt_domain_get_uuid($dom)); + +echo "# libvirt_domain_get_id\n"; +var_dump($dom_id = libvirt_domain_get_id($dom)); + +echo "# libvirt_domain_lookup_by_uuid_string\n"; +var_dump($dom2 = libvirt_domain_lookup_by_uuid_string($conn, $dom_uuid_str)); +if (!is_resource($dom2)) { + die('Domain lookup by UUID string failed with error: '.libvirt_get_last_error()); +} +unset($dom2); + +echo "# libvirt_domain_lookup_by_id\n"; +var_dump($dom2 = libvirt_domain_lookup_by_id($conn, $dom_id)); +if (!is_resource($dom2)) { + die('Domain lookup by ID failed with error: '.libvirt_get_last_error()); +} +unset($dom2); + +echo "# libvirt_domain_is_persistent\n"; +var_dump(libvirt_domain_is_persistent($dom) !== -1); + +echo "# libvirt_domain_get_xml_desc\n"; +$xmlstr = libvirt_domain_get_xml_desc($dom, NULL); + +$xmlok = (strpos($xmlstr, "<name>{$dom_name}</name>") && + strpos($xmlstr, "<uuid>{$dom_uuid_str}</uuid>")); +var_dump($xmlok); + +echo "# libvirt_domain_shutdown\n"; +var_dump($ret = libvirt_domain_shutdown($dom)); +if (!$ret) { + die('Domain shutdown failed with error: '.libvirt_get_last_error()); +} + +unset($res); +unset($dom); +unset($conn); +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_create_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_get_name +string(%d) "test-guest-no-disk-and-media" +# libvirt_domain_get_uuid_string +string(36) "%s" +# libvirt_domain_get_uuid +string(%d) "%s" +# libvirt_domain_get_id +int(%d) +# libvirt_domain_lookup_by_uuid_string +resource(%d) of type (Libvirt domain) +# libvirt_domain_lookup_by_id +resource(%d) of type (Libvirt domain) +# libvirt_domain_is_persistent +bool(true) +# libvirt_domain_get_xml_desc +bool(true) +# libvirt_domain_shutdown +bool(true) +Done diff --git a/src/tests/test-domain-lookup-by-name.phpt b/src/tests/test-domain-lookup-by-name.phpt new file mode 100644 index 0000000..8beb43c --- /dev/null +++ b/src/tests/test-domain-lookup-by-name.phpt @@ -0,0 +1,35 @@ +--TEST-- +libvirt_domain_lookup_by_name +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + + echo "# libvirt_connect\n"; + var_dump($conn = libvirt_connect('test:///default', false)); + if (!is_resource($conn)) + die('Connection to default hypervisor failed'); + // Loads domain of type 'test', name 'test-guest-no-disk-and-media' + $xml = file_get_contents(__DIR__.'/example-no-disk-and-media.xml'); + + echo "# libvirt_domain_create_xml\n"; + var_dump($res = libvirt_domain_create_xml($conn, $xml)); + if (!is_resource($res)) + die('Domain definition failed with error: '.libvirt_get_last_error()); + + echo "# libvirt_domain_lookup_by_name\n"; + var_dump($dom = libvirt_domain_lookup_by_name($conn, "test")); + + unset($dom); + unset($res); + unset($conn); +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_create_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_lookup_by_name +resource(%d) of type (Libvirt domain) +Done diff --git a/src/tests/test-domain-nic.phpt b/src/tests/test-domain-nic.phpt new file mode 100644 index 0000000..ac72c64 --- /dev/null +++ b/src/tests/test-domain-nic.phpt @@ -0,0 +1,52 @@ +--TEST-- +libvirt_domain_nic +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + +$mac = '00:11:22:33:44:55'; // Parms for defining a NIC +$network = 'default'; +$model = 'e1000'; + +echo "# libvirt_connect\n"; +var_dump($conn = libvirt_connect('qemu:///session', false)); +if (!is_resource($conn)) + die('Connection to default hypervisor failed. Is libvirt started? What about the QEMU driver?'); + +// libvirt_domain_nic_add needs a persistent guest loaded from a file +$xml = file_get_contents(__DIR__.'/qemu-no-disk-and-media.xml'); +echo "# libvirt_domain_define_xml\n"; +var_dump($dom = libvirt_domain_define_xml($conn, $xml)); +if (!is_resource($dom)) { + die('Domain definition failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_domain_nic_add\n"; +// The doc is wrong. It says the function returns a domain resource. +// It actually returns a boolean! +var_dump($ret = libvirt_domain_nic_add($dom, $mac, $network, $model, 0)); +if (!$ret) { + die('Domain nic add failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_domain_get_interface_devices\n"; +$info = libvirt_domain_get_interface_devices($dom); +var_dump(is_array($info)); +if (!array_key_exists('num', $info)) + die('libvirt_domain_get_interface_devices: Num element is missing in the output array'); + +unset($conn); + +?> +Done +--EXPECTF-- +# libvirt_connect +resource(%d) of type (Libvirt connection) +# libvirt_domain_define_xml +resource(%d) of type (Libvirt domain) +# libvirt_domain_nic_add +bool(true) +# libvirt_domain_get_interface_devices +bool(true) +Done \ No newline at end of file diff --git a/src/tests/test-logfile.phpt b/src/tests/test-logfile.phpt new file mode 100644 index 0000000..4cfae15 --- /dev/null +++ b/src/tests/test-logfile.phpt @@ -0,0 +1,43 @@ +--TEST-- +libvirt_logfile_set +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php + +$logfile = 'test.log'; +@unlink($logfile); + +echo "# libvirt_logfile_set\n"; +if (!libvirt_logfile_set($logfile, 1)) { + die('Logfile set failed with error: '.libvirt_get_last_error()); +} + +echo "# libvirt_connect\n"; +var_dump($conn = libvirt_connect('test:///default', false)); +if (!is_resource($conn)) { + die('Connection to default hypervisor failed'); +} + +unset($res); +unset($conn); + +$fp = fopen($logfile, 'r'); +$log = fread($fp, filesize($logfile)); +fclose($fp); + +$logok = (strpos($log, 'libvirt_connect: Connection') && + strpos($log, 'libvirt_connection_dtor: virConnectClose')); + +unlink($logfile); + +var_dump($logok); + +?> +Done +--EXPECTF-- +# libvirt_logfile_set +# libvirt_connect +resource(%d) of type (Libvirt connection) +bool(true) +Done -- 2.5.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list