Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / rgw / s3_utilities.pm
1 # Common subroutines shared by the s3 testing code
2 my $sec;
3 my $min;
4 my $hour;
5 my $mon;
6 my $year;
7 my $mday;
8 my $wday;
9 my $yday;
10 my $isdst;
11 my $PASS_CNT = 0;
12 my $FAIL_CNT = 0;
13
14 our $radosgw_admin = $ENV{RGW_ADMIN}||"sudo radosgw-admin";
15
16 # function to get the current time stamp from the test set up
17 sub get_timestamp {
18    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
19    if ($mon < 10) { $mon = "0$mon"; }
20    if ($hour < 10) { $hour = "0$hour"; }
21    if ($min < 10) { $min = "0$min"; }
22    if ($sec < 10) { $sec = "0$sec"; }
23    $year=$year+1900;
24    return $year . '_' . $mon . '_' . $mday . '_' . $hour . '_' . $min . '_' . $sec;
25 }
26
27 # Function to check if radosgw is already running
28 sub get_status {
29     my $service = "radosgw";
30     my $cmd = "pgrep $service";
31     my $status = get_cmd_op($cmd);
32     if ($status =~ /\d+/ ){
33         return 0;
34     }
35     return 1;
36 }
37
38 # function to execute the command and return output
39 sub get_cmd_op
40 {
41     my $cmd = shift;
42     my $excmd = `$cmd`;
43     return $excmd;
44 }
45
46 #Function that executes the CLI commands and returns the output of the command
47 sub get_command_output {
48     my $cmd_output = shift;
49     open( FH, ">>$test_log" );
50     print FH "\"$cmd_output\"\n";
51     my $exec_cmd = `$cmd_output 2>&1`;
52     print FH "$exec_cmd\n";
53     close(FH);
54     return $exec_cmd;
55 }
56
57 # Function to get the hostname
58 sub get_hostname
59 {
60     my $cmd = "hostname";
61     my $get_host = get_command_output($cmd);
62     chomp($get_host);
63     return($get_host);
64 }
65
66 sub pass {
67     my ($comment) = @_;
68     print "Comment required." unless length $comment;
69     chomp $comment;
70     print_border2();
71     print "Test case: $TC_CNT PASSED - $comment \n";
72     print_border2();
73     $PASS_CNT++;
74 }
75
76 sub fail {
77     my ($comment) = @_;
78     print "Comment required." unless length $comment;
79     chomp $comment;
80     print_border2();
81     print "Test case: $TC_CNT FAILED - $comment \n";
82     print_border2();
83     $FAIL_CNT++;
84 }
85
86 sub print_border2 {
87     print "~" x 90 . "\n";
88 }
89
90 # Function to create the user "qa_user" and extract the user access_key and secret_key of the user
91 sub get_user_info
92 {
93     my ($rgw_user) = @_;
94     my $cmd = "$radosgw_admin user create --uid=$rgw_user --display-name=$rgw_user";
95     my $cmd_op = get_command_output($cmd);
96     if ($cmd_op !~ /keys/){
97         return (0,0);
98     }
99     my @get_user = (split/\n/,$cmd_op);
100     foreach (@get_user) {
101         if ($_ =~ /access_key/ ){
102             $get_acc_key = $_;
103         } elsif ($_ =~ /secret_key/ ){
104             $get_sec_key = $_;
105         }
106     }
107     my $access_key = $get_acc_key;
108     my $acc_key = (split /:/, $access_key)[1];
109     $acc_key =~ s/\\//g;
110     $acc_key =~ s/ //g;
111     $acc_key =~ s/"//g;
112     $acc_key =~ s/,//g;
113     my $secret_key = $get_sec_key;
114     my $sec_key = (split /:/, $secret_key)[1];
115     $sec_key =~ s/\\//g;
116     $sec_key =~ s/ //g;
117     $sec_key =~ s/"//g;
118     $sec_key =~ s/,//g;
119     return ($acc_key, $sec_key);
120 }
121
122 # Function that deletes the given user and all associated user data 
123 sub purge_data
124 {
125     my ($rgw_user) = @_;
126     my $cmd = "$radosgw_admin user rm --uid=$rgw_user --purge-data";
127     my $cmd_op = get_command_output($cmd);
128     if ($cmd_op !~ /./){
129         print "user $rgw_user deleted\n";
130     } else {
131         print "user $rgw_user NOT deleted\n";
132         return 1;
133     }
134     return 0;
135 }
136
137 # Function to get the Ceph and distro info
138 sub ceph_os_info
139 {
140         my $ceph_v = get_command_output ( "ceph -v" );
141         my @ceph_arr = split(" ",$ceph_v);
142         $ceph_v = "Ceph Version:   $ceph_arr[2]";
143         my $os_distro = get_command_output ( "lsb_release -d" );
144         my @os_arr = split(":",$os_distro);
145         $os_distro = "Linux Flavor:$os_arr[1]";
146         return ($ceph_v, $os_distro);
147 }
148
149 # Execute the test case based on the input to the script
150 sub create_file {
151     my ($file_size, $part) = @_;
152     my $cnt;
153     $mytestfilename = "$file_size.$part"; 
154     $testfileloc = "/tmp/".$mytestfilename;
155     if ($file_size == '10Mb'){
156         $cnt = 1;
157     } elsif ($file_size == '100Mb'){
158         $cnt = 10;
159     } elsif ($file_size == '500Mb'){
160         $cnt = 50;
161     } elsif ($file_size == '1Gb'){
162         $cnt = 100;
163     } elsif ($file_size == '2Gb'){
164         $cnt = 200;
165     } 
166     my $ret = system("dd if=/dev/zero of=$testfileloc bs=10485760 count=$cnt");
167     if ($ret) { exit 1 };
168     return 0;
169 }
170
171 sub run_s3
172 {
173 # Run tests for the S3 functionality
174     # Modify access key and secret key to suit the user account
175     my ($user) = @_;
176     our ( $access_key, $secret_key ) = get_user_info($user);
177     if ( ($access_key) && ($secret_key) ) {
178        $s3 = Amazon::S3->new(
179             {
180                 aws_access_key_id     => $access_key,
181                 aws_secret_access_key => $secret_key,
182                 host                  => $hostname,
183                 secure                => 0,
184                 retry                 => 1,
185             }
186       );
187     }
188
189 our $bucketname = 'buck_'.get_timestamp();
190 # create a new bucket (the test bucket)
191 our $bucket = $s3->add_bucket( { bucket => $bucketname } )
192       or die $s3->err. "bucket $bucketname create failed\n". $s3->errstr;
193     print "Bucket Created: $bucketname \n";
194     return 0;
195 }
196
197 # delete keys
198 sub delete_keys {
199    (($bucket->delete_key($_[0])) and return 0) or return 1;
200 }
201
202 # Readd the file back to bucket 
203 sub readd_file {
204     system("dd if=/dev/zero of=/tmp/10MBfile1 bs=10485760 count=1");
205     $mytestfilename1 = '10MBfile1';
206     print "readding file to bucket: $mytestfilename1\n";
207     ((($bucket->add_key_filename( $mytestfilename1, $testfileloc,
208         { content_type => 'text/plain', },
209     )) and (print "readding file success\n") and return 0) or (return 1));
210 }
211
212 # check if rgw service is already running
213 sub check
214 {
215     my $state = get_status();
216     if ($state) {
217         exit 1;
218     }
219 }
220 1