File: Hooks.pm

package info (click to toggle)
libvirt-tck 0.1.0~2.git890d1c-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,128 kB
  • sloc: perl: 2,885; sh: 1,180; xml: 992; makefile: 6
file content (257 lines) | stat: -rw-r--r-- 6,081 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#
# Copyright (C) 2010 Red Hat, Inc.
# Copyright (C) 2010 Osier Yang <jyang@redhat.com>
#
# 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
#

package Sys::Virt::TCK::Hooks;

use strict;
use warnings;

use Fcntl ':mode';
use POSIX qw(strftime);
use Slurp;

my $HOOKS_CONF_DIR="/etc/libvirt/hooks";

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my %params = @_;
    my $self = {};

    my $type = $params{type} ? $params{type} : die "type parameter is required";

    $self = {
        type => $type,
        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/$params{type}.log",
        libvirtd_status => undef,
        domain_name => undef,
        domain_state => undef,
        expect_log => undef,
        action => undef,
    };

    bless $self, $class;

    return $self;
}

sub log_name {
    my $self = shift;
    my $log_name = shift;

    die "log_name parameter is required" unless $log_name;

    $self->{log_name} = $log_name;
}

sub expect_result {
    my $self = shift;
    my $expect_result = shift;

    die "expect_result parameter is required" unless $expect_result;

    $self->{expect_result} = $expect_result;

    return $self;
}

sub libvirtd_status {
    my $self = shift;
    my $status = `service libvirtd status`;
    my $_ = $status;

    if (/running/) {
        $self->{libvirtd_status} = 'running';
    } elsif (/stopped/) {
        $self->{libvirtd_status} = 'stopped';
    }

    return $self;
}

sub domain_name {
    my $self = shift;
    my $domain_name = shift;

    die "domain_name parameter is required" unless $domain_name;

    $self->{domain_name} = $domain_name;

    return $self;
}

sub domain_state {
    my $self = shift;
    my $domain_state = shift;

    die "domain_state parameter is required" unless $domain_state;

    $self->{domain_state} = $domain_state;

    return $self;
}

sub action {
    my $self = shift;
    my $action = shift;

    die "action parameter is required" unless $action;

    $self->{action} = $action;

    return $self;
}

sub expect_log {
    my $self = shift;
    my $expect_log = undef;

    my $hook = $self->{name};
    my $action = $self->{action};
    my $domain_name = $self->{domain_name};
    my $domain_state = $self->{domain_state};
    my $libvirtd_status = $self->{libvirtd_status};

    if ($self->{type} eq 'daemon') {
        if ($libvirtd_status eq 'running') {
            if ($action eq 'stop') {
                $expect_log = "$hook - shutdown - shutdown";
            } elsif ($action eq 'restart') {
                $expect_log = "$hook - shutdown - shutdown\n$hook - start - start";
            } elsif ($action eq 'reload') {
                $expect_log = "$hook - reload begin SIGHUP";
            } else {
                die "hooks testing doesn't support $action running libvirtd";
            }
        } else {
            if ($action eq 'start') {
                $expect_log = "$hook - start - start";
            } else {
                die "hooks testing doesn't support $action stopped libvirtd";
            }
        }
    } elsif ($self->{type} eq 'qemu' or $self->{type} eq 'lxc') {
        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 &Sys::Virt::Domain::STATE_SHUTOFF) {
            if ($action eq 'start') {
               $expect_log = "$hook $domain_name start begin -";
            } else {
                die "hooks testing doesn't support $action shutoff domain";
            }

        } else {
            die "hooks testing doesn't support to test a domain in $domain_state state";
        }
    } else {
            die "hooks only support 'qemu' and 'lxc' currently";
    }

    $self->{expect_log} = $expect_log;

    return $self;
}

sub create_hooks_dir {
    my $self = shift;

    unless (-d $self->{conf_dir}) {
        mkdir $self->{conf_dir} or die "failed to create $self->{conf_dir}: $!";
    }
}

sub backup_hook {
    my $self = shift;
    my $date = undef;

    $date = strftime "%Y-%m-%d-%H:%M:%S", localtime;
    my $orig = $self->{name};
    my $dest = $orig."-$date";

    rename $orig, $dest;
}

sub create_hook {
    my $self = shift;
    my $hook = $self->{name};

    $self->backup_hook;

    open HOOK, "> $hook" or die "failed on opening $hook: $!";

    my $str = <<EOF;
#! /bin/bash
echo "\$0" "\$@" >>$self->{log_name}
exit $self->{expect_result}
EOF

    print HOOK $str;
    close HOOK;

    my $mode = (stat($hook))[2];
    chmod($mode | S_IXUSR, $hook) unless -x $hook;
}

sub prepare {
    my $self = shift;

    $self->create_hooks_dir;
    $self->backup_hook;
    $self->create_hook;

    unlink $self->{log_name} if -f $self->{log_name};

    return $self;
}

sub cleanup {
    my $self = shift;
    my $name = $self->{name};

    unlink $name;
    unlink $self->{log_name} if -f $self->{log_name};
}

sub service_libvirtd {
    my $self = shift;
    my $action = $self->{action};

    truncate $self->{log_name}, 0 if -f $self->{log_name};

    die "failed on $action daemon" if system "service libvirtd $action";

    $self->libvirtd_status;
}

sub compare_log {
    my $self = shift;

    my $expect_log = $self->{expect_log};
    my $log_name = $self->{log_name};

    my $actual_log = slurp($log_name);
    chomp $actual_log;

    return 0 unless defined($actual_log);

    ($expect_log eq $actual_log) ? 1 : 0;
}

1;