File: 420-vol-upload-all.pl

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 (154 lines) | stat: -rw-r--r-- 3,743 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
# -*- perl -*-
#
# Copyright (C) 2011 Red Hat, Inc.
# Copyright (C) 2011 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

storage/400-vol-download.pl - storage volume download

=head1 DESCRIPTION

The test case validates that it is possible to download storage
volumes using blocking virStreamRecv API calls.

=cut

use strict;
use warnings;

use Test::More tests => 28;

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

my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END {
    $tck->cleanup if $tck;
}


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


diag "Defining transient storage pool";
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 $volxml = $tck->generic_volume("tck", "raw", 1024*1024*2)->allocation(1024*1024*2)->as_xml;


my $vol;

ok_volume(sub { $vol = $pool->create_volume($volxml) }, "create raw volume");

my $path = xpath($vol, "string(/volume/target/path)");

my $oldfile = catfile($tck->bucket_dir("vol-stream"), "local.img");
unlink $oldfile;

open FILE, ">$oldfile"
    or die "cannot create $oldfile: $!";

for (my $j = 0 ; $j < 1024 ; $j++) {
    # 64 bytes
    my $str = join('', ('a'..'z', 'A'..'Z', '0'..'9', '.',"\n"));
    # 1 kb
    my $data = join('', $str, $str, $str, $str,
		    $str, $str, $str, $str,
		    $str, $str, $str, $str,
		    $str, $str, $str, $str);
    print FILE $data;
}
close FILE or die "cannot save $oldfile: $!";

my $olddigest = &digest($oldfile, 0, 0);

&onetest(0, 0);
&onetest(0, 1024*1024);
&onetest(1024*1024, 0);
&onetest(1024*1024, 1024*1024);

unlink $oldfile;

sub onetest {
    my $offset = shift;
    my $length = shift;

    my $origdigestpre = $offset ? &digest($path, 0, $offset) : "";
    my $origdigestpost = &digest($path, $offset + 1024*1024, 0);

    my $st = $conn->new_stream();

    open FILE, "<$oldfile" or die "cannot read $oldfile: $!";

    sub foo {
	my $st = $_[0];
	my $nbytes = $_[2];
        my $rv = sysread FILE, $_[1], $nbytes;
	return $rv;
    };
    lives_ok(sub { $vol->upload($st, $offset, $length) }, "started upload");
    lives_ok(sub { $st->send_all(\&foo); }, "sent all data");
    lives_ok(sub { $st->finish(); }, "finished stream");

    close FILE or die "cannot close $oldfile: $!";

    my $newdigest = &digest($path, $offset, 1024*1024);
    my $newdigestpre = $offset ? &digest($path, 0, $offset) : "";
    my $newdigestpost = &digest($path, $offset + 1024*1024, 0);

    is($origdigestpre, $newdigestpre, "File pre region digest matches");
    is($olddigest, $newdigest, "File digests match");
    is($origdigestpost, $newdigestpost, "File post region digest matches");
}


sub digest {
    my $file = shift;
    my $offset = shift;
    my $length = shift;

    open FILE, "<$file" or die "cannot open $file: $!";

    my $digest = Digest->new("MD5");

    my $done = 0;
    seek FILE, $offset, SEEK_SET;
    while (1) {
	my $want = 1024;
	if ($length && (($length - $done) < $want)) {
	    $want = ($length - $done);
	}
	my $str;
	my $got = sysread FILE, $str, $want;
	last if $got == 0;
	$done += $got;
	$digest->add($str);
    }

    close FILE;

    return $digest->hexdigest;
}