File: 200-qcow2-single-backing-file.t

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 (185 lines) | stat: -rw-r--r-- 5,320 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
# -*- perl -*-
#
# Copyright (C) 2009-2010 Red Hat, Inc.
# Copyright (C) 2009 Daniel P. Berrange
#
# 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

qemu/200-qcow2-single-backing-file.t - Check handling of 1 level backing file

=head1 DESCRIPTION

In this test are two files

 - back.img is a raw file
 - main.img is a qcow2 file, with backing file pointing to back.img

The guest is configured to point at 'main.img' but
is told that it is a raw file.

Thus, libvirt's security drivers should *not* grant
access to the back.img file & the guest should not
see the back.img data or have any error.

=cut

use strict;
use warnings;

use Test::More tests => 20;

use Sys::Virt::TCK;
use Test::Exception;
use File::Spec::Functions qw(catfile);
use File::stat;

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: {
    skip "Only relevant to QEMU driver", 20 unless $conn->get_type() eq "QEMU";
    skip "Only relevant when run as root", 20 unless $< == 0;
    skip "Only relevant for system driver", 20 unless
	$conn->get_uri() =~ m/system/;


    my $xml = $tck->generic_pool("dir")
	->mode("0755")->as_xml;


    diag "Defining transient storage pool $xml";
    my $pool;
    ok_pool(sub { $pool = $conn->define_storage_pool($xml) }, "define transient storage pool");

    lives_ok(sub { $pool->build(0) }, "built storage pool");

    lives_ok(sub { $pool->create }, "started storage pool");


    my $volbackxml = $tck->generic_volume("tck-back", "raw", 1024*1024*50)
	->allocation(0)->as_xml;

    my $st;

    my ($volback, $pathback);
    diag "back $volbackxml";
    ok_volume(sub { $volback = $pool->create_volume($volbackxml) }, "create raw backing file volume");

    $pathback = xpath($volback, "string(/volume/target/path)");
    $st = stat($pathback);

    ok($st, "path $pathback exists");

    is($st->size, 1024*1024*50, "size is 50M");


    my $volmainxml = $tck->generic_volume("tck-main", "qcow2", 1024*1024*50)
	->backing_file($pathback)
	->backing_format("raw")
	->allocation(0)->as_xml;


    my ($volmain, $pathmain);
    diag "main $volmainxml";
    ok_volume(sub { $volmain = $pool->create_volume($volmainxml) }, "create qcow2 backing file volume");

    $pathmain = xpath($volmain, "string(/volume/target/path)");
    $st = stat($pathmain);

    ok($st, "path $pathmain exists");

    ok($st->size < 1024*1024, "size is < 1M");


    # We point the guest at a qcow2 image, but tell it that is
    # is raw. Thus *nothing* should ever try to open the backing
    # store in this qcow2 image.
    $xml = $tck->generic_domain("tck")
	->disk(format => { name => "qemu", type => "raw" },
	       type => "file",
	       src => $pathmain,
	       dst => "vdb")
	->as_xml;

    diag "Defining an inactive domain config $xml";
    my $dom;
    ok_domain(sub { $dom = $conn->define_domain($xml) }, "defined persistent domain config");

    warn "Disk " . xpath($dom, "/domain/devices/disk[0]/driver/\@type");
    warn "Disk " . xpath($dom, "/domain/devices/disk[1]/driver/\@type");

    diag "Starting inactive domain config";
    $dom->create;
    ok($dom->get_id() > 0, "running domain has an ID > 0");


    diag "Trying another domain lookup by name";
    my $dom1;
    ok_domain(sub { $dom1 = $conn->get_domain_by_name("tck") }, "the running domain object");
    ok($dom1->get_id() > 0, "running domain has an ID > 0");

    open PID, "/var/run/libvirt/qemu/tck.pid"
	or die "cannot read PID /var/run/libvirt/qemu/tck.pid: $!";
    my $pid = <PID>;
    chomp $pid;
    close PID;

    open STAT, "/proc/$pid/status"
	or die "cannot read status /proc/$pid/status: $!";

    my ($gid, $uid);
    while (<STAT>) {
	if (/Uid:\s*(\d+)/) {
	    $uid = $1;
	} elsif (/Gid:\s*(\d+)/) {
	    $gid = $1;
	}
    }
    close STAT;

    $st = stat($pathmain);
    ok($st, "path $pathmain exists");
    diag "UID:GID on $pathmain is " . $st->uid . ":" . $st->gid;
    my $aclmain = `getfacl -n $pathmain`;
    diag "ACL on $pathmain is $aclmain";

    ok (($aclmain =~ "user:$uid:rw-") ||
	($st->uid == $uid), "Ownership or ACL of file $pathmain allows $uid");
    ok (($aclmain =~ "group:$gid:rw-") ||
	($st->gid == $gid), "Ownership or ACL of file $pathmain allows $gid");


    $st = stat($pathback);
    ok($st, "path $pathback exists");
    diag "UID:GID on $pathback is " . $st->uid . ":" . $st->gid;
    my $aclback = `getfacl -n $pathback`;
    diag "ACL on $pathback is $aclback";

    ok (($aclback !~ "user:$uid:") &&
	(($uid == $<) || $st->uid != $uid),
	"Ownership or ACL of file $pathback does not allow $uid");
    ok (($aclback !~ "group:$gid:") &&
	(($gid == $(+0) || $st->gid != $gid),
	"Ownership or ACL of file $pathback does not allow $gid");

    diag "Destroying the running domain";
    $dom->destroy();

    diag "Undefining the inactive domain config";
    $dom->undefine;

    ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain", 42);
}