-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathdata_integrity_utils.pm
88 lines (69 loc) · 2.5 KB
/
data_integrity_utils.pm
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
=head1 data_integrity_utils
Library to verify image data integrity by comparing SHA256 checksums
=cut
# SUSE's openQA tests
#
# Copyright 2018 SUSE LLC
# SPDX-License-Identifier: FSFAP
# Summary: Library to verify image data integrity by comparing SHA256 checksums.
# Maintainer: Joaquín Rivera <[email protected]>
package data_integrity_utils;
use base Exporter;
use Exporter;
use strict;
use warnings;
use testapi;
use Utils::Backends;
use File::Basename;
use Digest::file 'digest_file_hex';
use version_utils qw(is_vmware);
our @EXPORT = qw(verify_checksum get_image_digest);
=head2 get_image_digest
get_image_digest($image_path);
Returns image digest. Image path C<$image_path> is the parameter which is used to get image digest.
Digest retrieval is platform-specific depends.
=cut
sub get_image_digest {
my ($image_path) = shift;
my $digest;
if (is_svirt) {
$digest = console('svirt')->get_cmd_output("sha256sum $image_path", {domain => is_vmware() ? 'sshVMwareServer' : undef});
# On Hyper-V the hash starts with '\'
my $start = check_var('VIRSH_VMM_FAMILY', 'hyperv') ? 1 : 0;
$digest = substr $digest, $start, 64; # extract SHA256 from the output
}
else {
$digest = digest_file_hex($image_path, "SHA-256");
}
return $digest;
}
=head2 verify_checksum
verify_checksum($dir_path);
Verify image data integrity by comparing SHA256 checksums.
Directory path C<$dir_path> is the parameter which is a part of image path '$image_path' if exists.
Returns error message in case of failure, empty string in case of success.
=cut
sub verify_checksum {
my ($dir_path) = shift;
my $error = '';
diag "Comparing data integrity calculated with SHA256 digest against checksum from IBS/OBS via rsync.pl";
foreach my $image (grep { /^CHECKSUM_/ } keys %bmwqemu::vars) {
my $checksum = get_required_var $image;
$image =~ s/CHECKSUM_//;
my $image_path = get_required_var $image;
$image_path = $dir_path . basename($image_path) if $dir_path;
my $digest = get_image_digest($image_path);
unless ($digest) {
$error .= "Failed to calculate checksum for $image located in: $image_path\n";
next;
}
if ($checksum eq $digest) {
diag("$image OK\n$image_path: OK");
} else {
$error .= "SHA256 checksum does not match for $image:\n\tCalculated: $digest\n\tExpected: $checksum\n";
}
}
diag($error) if $error;
return $error;
}
1;