1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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();
};
|