Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / rgw / s3_multipart_upload.pl
1 #! /usr/bin/perl
2
3 =head1 NAME
4
5 s3_multipart_upload.pl - Script to test rgw multipart upload using s3 interface.
6
7 =head1 SYNOPSIS
8
9 Use:
10         perl s3_multipart_upload.pl [--help]
11
12 Examples:
13         perl s3_multipart_upload.pl
14         or
15         perl s3_multipart_upload.pl  --help
16
17 =head1 DESCRIPTION
18
19 This script intends to test the rgw multipart upload followed by a download
20 and verify checksum using s3 interface and reports test results
21
22 =head1 ARGUMENTS
23
24 s3_multipart_upload.pl takes the following arguments:
25    --help
26    (optional) Displays the usage message.
27
28 =cut
29
30 use Amazon::S3;
31 use Data::Dumper;
32 use IO::File;
33 use Getopt::Long;
34 use Digest::MD5;
35 use Pod::Usage();
36 use FindBin;
37 use lib $FindBin::Bin;
38 use s3_utilities;
39 use Net::Domain qw(hostfqdn);
40
41 my $help;
42
43 Getopt::Long::GetOptions(
44     'help' => \$help
45 );
46 Pod::Usage::pod2usage(-verbose => 1) && exit if ($help);
47
48 #== local variables ===
49 my $s3;
50 my $hostdom  = $ENV{RGW_FQDN}||hostfqdn();
51 my $port     = $ENV{RGW_PORT}||7280;
52 our $hostname = "$hostdom:$port";
53 our $testfileloc;
54 our $mytestfilename;
55
56 # upload a file to the bucket
57 sub upload_file {
58     my ($fsize, $i) = @_;
59     create_file($fsize, $i);
60     print "adding file to bucket $bucketname: $mytestfilename\n";
61     ($bucket->add_key_filename( $mytestfilename, $testfileloc,
62         { content_type => 'text/plain', },
63     ) and (print "upload file successful\n" ) and return 0 ) or (print "upload failed\n" and return 1);
64 }
65
66 # delete the bucket
67 sub delete_bucket {
68    ($bucket->delete_bucket) and (print "bucket delete succeeded \n") or die $s3->err . "delete bucket failed\n" . $s3->errstr;
69 }
70
71 # Function to perform multipart upload of given file size to the user bucket via s3 interface
72 sub multipart_upload
73 {
74     my ($size, $parts) = @_;
75     # generate random user every time
76     my $user = rand();
77     # Divide the file size in to equal parts and upload to bucket in multiple parts
78     my $fsize = ($size/$parts);
79     my $fsize1;
80     run_s3($user);
81     if ($parts == 10){
82         $fsize1 = '100Mb';
83     } elsif ($parts == 100){
84         $fsize1 = '10Mb';
85     }
86     foreach my $i(1..$parts){
87        print "uploading file - part $i \n";
88        upload_file($fsize1, $i);
89     }
90     fetch_file_from_bucket($fsize1, $parts);
91     compare_cksum($fsize1, $parts);
92     purge_data($user);
93 }
94
95 # Function to download the files from bucket to verify there is no data corruption
96 sub fetch_file_from_bucket
97 {
98     # fetch file from the bucket
99     my ($fsize, $parts) = @_;
100     foreach my $i(1..$parts){
101     my $src_file = "$fsize.$i";
102     my $dest_file = "/tmp/downloadfile.$i";
103     print
104       "Downloading $src_file from bucket to $dest_file \n";
105     $response =
106       $bucket->get_key_filename( $src_file, GET,
107         $dest_file )
108       or die $s3->err . ": " . $s3->errstr;
109     }
110 }
111
112 # Compare the source file with destination file and verify checksum to ensure
113 # the files are not corrupted
114 sub compare_cksum
115 {
116     my ($fsize, $parts)=@_;
117     my $md5    = Digest::MD5->new;
118     my $flag = 0;
119     foreach my $i (1..$parts){
120         my $src_file = "/tmp/"."$fsize".".$i";
121         my $dest_file = "/tmp/downloadfile".".$i";
122         open( FILE, $src_file )
123          or die "Error: Could not open $src_file for MD5 checksum...";
124         open( DLFILE, $dest_file )
125          or die "Error: Could not open $dest_file for MD5 checksum.";
126         binmode(FILE);
127         binmode(DLFILE);
128         my $md5sum   = $md5->addfile(*FILE)->hexdigest;
129         my $md5sumdl = $md5->addfile(*DLFILE)->hexdigest;
130         close FILE;
131         close DLFILE;
132         # compare the checksums
133         if ( $md5sum eq $md5sumdl ) {
134             $flag++;
135         }
136     }
137     if ($flag == $parts){
138        pass("checksum verification for multipart upload passed" );
139     }else{
140        fail("checksum verification for multipart upload failed" );
141     }
142 }
143
144 #== Main starts here===
145 ceph_os_info();
146 check();
147 # The following test runs multi part upload of file size 1Gb in 10 parts
148 multipart_upload('1048576000', 10);
149 # The following test runs multipart upload of 1 Gb file in 100 parts
150 multipart_upload('1048576000', 100);
151 print "OK";