summaryrefslogtreecommitdiffstats
path: root/qa/qa_scripts/S3Lib.pm
blob: 6e16bfc7a39b08c0efe91f0e0d73d5659c2c31f9 (plain)
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
#! /usr/bin/perl
=head1 NAME

S3Lib.pm - Perl Module that contains the functions used by S3 test scripts for testing Rados gateway.

=cut

package S3Lib;
use Cwd;
use Exporter;
@ISA = 'Exporter';
@EXPORT_OK = qw(get_hostname get_user_info $rgw_user delete_user _write_log_entry _exit_result get_status);

#==variables ===
my $rgw_user = "qa_user";

# function to execute the command and return output
sub get_cmd_op
{
    my $cmd = shift;
    my $excmd = `$cmd`;
    return $excmd;
}

# Function to check if radosgw is already running
sub get_status {
    my $service = "radosgw";
    my $cmd = "ps -ef | grep $service | grep -v grep";
    my $status = get_cmd_op($cmd);
    if ($status =~ /client.radosgw.gateway/ ){
        return 0;
    }
    return 1;
}

#Function that executes the CLI commands and returns the output of the command

sub get_command_output {
    my $cmd_output = shift;
    open( FH, ">>$test_log" );
    print FH "\"$cmd_output\"\n";
    my $exec_cmd = `$cmd_output 2>&1`;
    print FH "$exec_cmd\n";
    close(FH);
    return $exec_cmd;
}

# Function that enters the given msg to log.txt

sub _write_log_entry {
    my $logmsg = shift;
    open(TC,'>>log.txt');
    print TC "[Log] $logmsg\n";
    close(TC);
}

# Function that creates the test_completed.txt as required by xstudio run at the end of the test

sub _exit_result {
    my $exit_status = shift;
    open (TCOMP, '>>test_completed.txt');
    close (TCOMP);
    exit($exit_status);
}

# Function to create the user "qa_user" and extract the user access_key and secret_key of the user
sub get_user_info
{
    my $cmd = "sudo radosgw-admin user create --uid=$rgw_user --display-name=$rgw_user";
    my $cmd_op = get_command_output($cmd);
    if ($cmd_op !~ /keys/){
        _write_log_entry( "user $rgw_user NOT created" );
        return (0,0);
    }
    _write_log_entry( "user $rgw_user created" );
    my @get_user = (split/,/,$cmd_op);
    foreach (@get_user) {
        if ($_ =~ /access_key/ ){
            $get_acc_key = $_;
        } elsif ($_ =~ /secret_key/ ){
            $get_sec_key = $_;
        }
    }
    my $access_key = $get_acc_key;
    my $acc_key = (split /:/, $access_key)[1];
    $acc_key =~ s/\\//g;
    $acc_key =~ s/ //g;
    $acc_key =~ s/"//g;
    my $secret_key = $get_sec_key;
    my $sec_key = (split /:/, $secret_key)[1];
    chop($sec_key);
    chop($sec_key);
    $sec_key =~ s/\\//g;
    $sec_key =~ s/ //g;
    $sec_key =~ s/"//g;
    return ($acc_key, $sec_key);
}

# Function that deletes the user $rgw_user and write to logfile. 
sub delete_user
{
    my $cmd = "sudo radosgw-admin user rm --uid=$rgw_user";
    my $cmd_op = get_command_output($cmd); 	
    if ($cmd_op !~ /aborting/){
        _write_log_entry( "user $rgw_user deleted" );
    } else {
        _write_log_entry( "user $rgw_user NOT deleted" );
    }
}

# Function to get the hostname
sub get_hostname
{
    my $cmd = "hostname";
    my $get_host = get_command_output($cmd);
    chomp($get_host);
    return($get_host);
}