[libvirt] [TCK] Add test case for domain hook testing.

Osier Yang jyang at redhat.com
Thu Oct 21 22:05:46 UTC 2010


The test case will be valid only when the Sys::Virt::TCK connection
object is "qemu:///system" or "lxc:///", because currently libvirt
only support hooks for QEMU and LXC domain.

Also update scripts/hooks/051-daemon-hook.t, replace "$hook->foo"
with "$hook->foo()".
---
 lib/Sys/Virt/TCK/Hooks.pm       |    8 +-
 scripts/hooks/051-daemon-hook.t |   36 ++++----
 scripts/hooks/052-domain-hook.t |  183 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 205 insertions(+), 22 deletions(-)
 create mode 100644 scripts/hooks/052-domain-hook.t

diff --git a/lib/Sys/Virt/TCK/Hooks.pm b/lib/Sys/Virt/TCK/Hooks.pm
index 7d20fa4..eb5e8f9 100644
--- a/lib/Sys/Virt/TCK/Hooks.pm
+++ b/lib/Sys/Virt/TCK/Hooks.pm
@@ -35,7 +35,7 @@ sub new {
         conf_dir => $params{conf_dir} ? $params{conf_dir} : $HOOKS_CONF_DIR,
         name => $params{conf_dir}.'/'.$params{type},
         expect_result => $params{expect_result} ? $params{expect_result} : 0,
-        log_name => $params{log_name} ? $params{log_name} : "/tmp/$self->{type}.log",
+        log_name => $params{log_name} ? $params{log_name} : "/tmp/$params{type}.log",
         libvirtd_status => undef,
         domain_name => undef,
         domain_state => undef,
@@ -144,13 +144,13 @@ sub expect_log {
             }
         }
     } elsif ($self->{type} eq 'qemu' or $self->{type} eq 'lxc') {
-        if ($domain_state eq 'running') {
-            if ($action eq 'stop') {
+        if ($domain_state eq &Sys::Virt::Domain::STATE_RUNNING) {
+            if ($action eq 'destroy') {
                $expect_log = "$hook $domain_name stopped end -";
             } else {
                 die "hooks testing doesn't support $action running domain";
             }
-        } elsif ($domain_state eq 'shut off') {
+        } elsif ($domain_state eq &Sys::Virt::Domain::STATE_SHUTOFF) {
             if ($action eq 'start') {
                $expect_log = "$hook $domain_name start begin -";
             } else {
diff --git a/scripts/hooks/051-daemon-hook.t b/scripts/hooks/051-daemon-hook.t
index c378bd0..5218d24 100644
--- a/scripts/hooks/051-daemon-hook.t
+++ b/scripts/hooks/051-daemon-hook.t
@@ -50,24 +50,24 @@ SKIP: {
                                           conf_dir => '/etc/libvirt/hooks',
                                           log_name => '/tmp/daemon.log');
 
-    $hook->libvirtd_status;
+    $hook->libvirtd_status();
     BAIL_OUT "libvirtd is not running, Exit..." 
         if ($hook->{libvirtd_status} eq 'stopped');
 
-    eval { $hook->prepare; };
+    eval { $hook->prepare(); };
     BAIL_OUT "failed to setup hooks testing ENV: $@" if $@;
 
-    diag "restart libvirtd for hooks scripts taking effect";
-    $hook->action('restart');
-    $hook->service_libvirtd;
+    diag "reload libvirtd for hooks scripts taking effect";
+    $hook->action('reload');
+    $hook->service_libvirtd();
     unlink $hook->{log_name} unless -f $hook->{log_name};
 
     # stop libvirtd
     $hook->action('stop');
-    $hook->expect_log;
+    $hook->expect_log();
 
     diag "$hook->{action} libvirtd";
-    $hook->service_libvirtd;
+    $hook->service_libvirtd();
 
     my $hook_data = slurp($hook->{name});
     diag "hook script: $hook->{name} '$hook_data'";
@@ -82,17 +82,17 @@ SKIP: {
     diag "expected log:\n$hook->{expect_log}";
 
     diag "check if the actual log is same with expected log";
-    ok($hook->compare_log, "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
+    ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
 
     diag "check if libvirtd is stopped";
     ok(`service libvirtd status` =~ /stopped/, "libvirtd is stopped"); 
 
     # start libvirtd
     $hook->action('start');
-    $hook->expect_log;
+    $hook->expect_log();
 
     diag "$hook->{action} libvirtd";
-    $hook->service_libvirtd;
+    $hook->service_libvirtd();
 
     $hook_data = slurp($hook->{name});
     diag "hook script: $hook->{name} '$hook_data'";
@@ -107,17 +107,17 @@ SKIP: {
     diag "expected log: \n$hook->{expect_log}";
 
     diag "check if the actual log is same with expected log";
-    ok($hook->compare_log, "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
+    ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
 
     diag "check if libvirtd is still running";
     ok(`service libvirtd status` =~ /running/, "libvirtd is running"); 
 
     # restart libvirtd
     $hook->action('restart');
-    $hook->expect_log;
+    $hook->expect_log();
 
     diag "$hook->{action} libvirtd";
-    $hook->service_libvirtd;
+    $hook->service_libvirtd();
 
     $hook_data = slurp($hook->{name});
     diag "hook script: $hook->{name} '$hook_data'";
@@ -132,17 +132,17 @@ SKIP: {
     diag "expected log: \n$hook->{expect_log}";
 
     diag "check if the actual log is same with expected log";
-    ok($hook->compare_log, "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
+    ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
 
     diag "check if libvirtd is still running";
     ok(`service libvirtd status` =~ /running/, "libvirtd is running"); 
 
     # reload libvirtd
     $hook->action('reload');
-    $hook->expect_log;
+    $hook->expect_log();
 
     diag "$hook->{action} libvirtd";
-    $hook->service_libvirtd;
+    $hook->service_libvirtd();
 
     $hook_data = slurp($hook->{name});
     diag "hook script: $hook->{name} '$hook_data'";
@@ -157,11 +157,11 @@ SKIP: {
     diag "expected log: \n$hook->{expect_log}";
 
     diag "check if the actual log is same with expected log";
-    ok($hook->compare_log, "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
+    ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd");
 
     diag "check if libvirtd is still running";
     ok(`service libvirtd status` =~ /running/, "libvirtd is running"); 
 
-    $hook->cleanup;
+    $hook->cleanup();
 };
 
diff --git a/scripts/hooks/052-domain-hook.t b/scripts/hooks/052-domain-hook.t
new file mode 100644
index 0000000..e82aeb9
--- /dev/null
+++ b/scripts/hooks/052-domain-hook.t
@@ -0,0 +1,183 @@
+# -*- perl -*-
+#
+# Copyright (C) 2010 Red Hat, Inc.
+# Copyright (C) 2010 Osier Yang
+#
+# This program is free software; You can redistribute it and/or modify
+# it under the GNU General Public License as published by the Free
+# Software Foundation; either version 2, or (at your option) any
+# later version
+#
+# The file "LICENSE" distributed along with this file provides full
+# details of the terms and conditions
+#
+
+=pod
+
+=head1 NAME
+
+domain/052-domain-hook.t - domain hook testing
+
+=head1 DESCRIPTION
+
+This test case validates that the hook for QEMU or LXC domain is 
+invoked correctly while start/stop domain, and if the exit status 
+of testing hook script is 0, it expects domain could be started and 
+stopped successfully. Otherwise, it expects domain 'start' will be
+failed, 'stop' is fine.
+
+=cut
+
+use strict;
+use warnings;
+
+use Slurp;
+
+use Test::More tests => 12;
+
+use Sys::Virt::TCK;
+use Sys::Virt::TCK::Hooks;
+
+my $tck = Sys::Virt::TCK->new();
+my $conn = eval { $tck->setup(); };
+BAIL_OUT "failed to setup test harness: $@" if $@;
+END { $tck->cleanup if $tck; }
+
+SKIP: {
+    my $uri = $conn->get_uri();
+
+    skip 12, "Not using QEMU/LXC driver" unless
+        $uri eq "qemu:///system" or $uri eq "lxc:///";
+
+    my $xml = $tck->generic_domain("tck")->as_xml;
+
+    diag "Creating a new persistent domain";
+    my $dom;
+    ok_domain(sub { $dom = $conn->define_domain($xml) }, "created persistent domain object");
+    
+    my $hook_type = $uri eq "qemu:///system" ? 'qemu' : 'lxc';
+
+    my $hook = Sys::Virt::TCK::Hooks->new(type => $hook_type,
+                                          conf_dir => '/etc/libvirt/hooks',
+                                          expect_result => 0);
+    eval { $hook->prepare(); };
+    BAIL_OUT "failed to setup hooks testing ENV: $@" if $@;
+
+    diag "reload libvirtd for hooks scripts taking effect";
+    $hook->action('reload');
+    $hook->service_libvirtd();
+
+    # start domain
+    my $domain_state = $dom->get_info()->{state}; 
+    my $domain_name = $dom->get_name();
+
+    $hook->domain_name($domain_name);
+    $hook->domain_state($domain_state);
+    $hook->action('start');
+    $hook->expect_log();
+
+    diag "start $domain_name";
+    $dom->create();
+
+    diag "check if the domain is running";
+    $domain_state = $dom->get_info()->{state};
+    ok($domain_state eq &Sys::Virt::Domain::STATE_RUNNING, "domain is running");
+
+    my $hook_data = slurp($hook->{name});
+    diag "hook script: $hook->{name} '$hook_data'";
+
+    diag "check if $hook->{name} is invoked";
+    ok(-f "$hook->{name}", "$hook->{name} is invoked");
+
+    my $actual_log_data = slurp($hook->{log_name});
+    diag "acutal log: $hook->{log_name} '$actual_log_data'";
+
+    diag "expect log:\n $hook->{expect_log}";
+
+    diag "check if the actual log is same with expected log";
+    ok($hook->compare_log, "$hook->{name} is invoked correctly while start $domain_name");
+
+    diag "truncate $hook->{log_name}";
+    truncate $hook->{log_name}, 0 if -f $hook->{log_name};
+
+    # stop domain
+    $domain_state = $dom->get_info()->{state};
+
+    $hook->domain_state($domain_state);
+    $hook->action('destroy');
+    $hook->expect_log();
+
+    diag "destroy $domain_name";
+    $dom->destroy();
+
+    diag "check if the domain is shut off";
+    $domain_state = $dom->get_info()->{state};
+    ok($domain_state eq &Sys::Virt::Domain::STATE_SHUTOFF, "domain is shut off");
+
+    $hook_data = slurp($hook->{name});
+    diag "hook script: $hook->{name} '$hook_data'";
+
+    diag "check if $hook->{name} is invoked";
+    ok(-f "$hook->{name}", "$hook->{name} is invoked");
+
+    $actual_log_data = slurp($hook->{log_name});
+    diag "acutal log: $hook->{log_name} '$actual_log_data'";
+
+    diag "expect log:\n $hook->{expect_log}";
+
+    diag "check if the actual log is same with expected log";
+    ok($hook->compare_log(), "$hook->{name} is invoked correctly while start $domain_name");
+
+    $hook->cleanup();
+
+    # Create a new testing hook script with exit status is 1.
+    $hook = Sys::Virt::TCK::Hooks->new(type => $hook_type,
+                                          conf_dir => '/etc/libvirt/hooks',
+                                          expect_result => 1);
+    eval { $hook->prepare(); };
+    BAIL_OUT "failed to setup hooks testing ENV: $@" if $@;
+
+    diag "reload libvirtd for hooks scripts taking effect";
+    $hook->action('reload');
+    $hook->service_libvirtd();
+
+    # start domain once more after the testing hook script is changed.
+    $domain_state = $dom->get_info()->{state}; 
+    $domain_name = $dom->get_name();
+
+    $hook->domain_name($domain_name);
+    $hook->domain_state($domain_state);
+    $hook->action('start');
+    $hook->expect_log();
+
+    diag "start $domain_name";
+    eval { $dom->create(); };
+    ok($@, $@);
+
+    diag "check if the domain is running";
+    $domain_state = $dom->get_info()->{state};
+    ok($domain_state eq &Sys::Virt::Domain::STATE_SHUTOFF, "domain is not started ");
+
+    $hook_data = slurp($hook->{name});
+    diag "hook script: $hook->{name} '$hook_data'";
+
+    diag "check if $hook->{name} is invoked";
+    ok(-f "$hook->{name}", "$hook->{name} is invoked");
+
+    $actual_log_data = slurp($hook->{log_name});
+    diag "acutal log: $hook->{log_name} '$actual_log_data'";
+
+    diag "expect log:\n $hook->{expect_log}";
+
+    diag "check if the actual log is same with expected log";
+    ok($hook->compare_log, "$hook->{name} is invoked correctly while start $domain_name");
+
+    # undefine domain
+    diag "undefine $domain_name";
+    $dom->undefine();
+
+    ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
+         Sys::Virt::Error::ERR_NO_DOMAIN);
+
+    $hook->cleanup();
+};
-- 
1.7.1




More information about the libvir-list mailing list