line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package S3TestUtils; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
537131
|
use strict; |
|
4
|
|
|
|
|
39
|
|
|
4
|
|
|
|
|
116
|
|
4
|
4
|
|
|
4
|
|
36
|
use warnings; |
|
4
|
|
|
|
|
21
|
|
|
4
|
|
|
|
|
110
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
686
|
use Data::Dumper; |
|
4
|
|
|
|
|
9335
|
|
|
4
|
|
|
|
|
270
|
|
7
|
4
|
|
|
4
|
|
32
|
use English qw(-no_match_vars); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
34
|
|
8
|
4
|
|
|
4
|
|
1545
|
use List::Util qw(any); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
367
|
|
9
|
4
|
|
|
4
|
|
2318
|
use Readonly; |
|
4
|
|
|
|
|
16678
|
|
|
4
|
|
|
|
|
235
|
|
10
|
4
|
|
|
4
|
|
1268
|
use Test::More; |
|
4
|
|
|
|
|
205673
|
|
|
4
|
|
|
|
|
37
|
|
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
4
|
|
1599
|
use parent qw(Exporter); |
|
4
|
|
|
|
|
314
|
|
|
4
|
|
|
|
|
33
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# chars |
15
|
|
|
|
|
|
|
Readonly our $EMPTY => q{}; |
16
|
|
|
|
|
|
|
Readonly our $SLASH => q{/}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# booleans |
19
|
|
|
|
|
|
|
Readonly our $TRUE => 1; |
20
|
|
|
|
|
|
|
Readonly our $FALSE => 0; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# mocking services |
23
|
|
|
|
|
|
|
Readonly our $DEFAULT_LOCAL_STACK_HOST => 'localhost:4566'; |
24
|
|
|
|
|
|
|
Readonly our $DEFAULT_MINIO_HOST => 'localhost:9000'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# http codes |
27
|
|
|
|
|
|
|
Readonly our $HTTP_OK => '200'; |
28
|
|
|
|
|
|
|
Readonly our $HTTP_FORBIDDEN => '403'; |
29
|
|
|
|
|
|
|
Readonly our $HTTP_CONFLICT => '409'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# misc |
32
|
|
|
|
|
|
|
Readonly our $TEST_BUCKET_PREFIX => 'net-amazon-s3-test'; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# create a domain name for this if AMAZON_S3_DNS_BUCKET_NAMES is true |
35
|
|
|
|
|
|
|
Readonly our $MOCK_SERVICES_BUCKET_NAME => $TEST_BUCKET_PREFIX . '-test'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Readonly our $PUBLIC_READ_POLICY => <
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
xsi:type="Group"> |
41
|
|
|
|
|
|
|
http://acs.amazonaws.com/groups/global/AllUsers |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
READ |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
END_OF_POLICY |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
48
|
|
|
|
|
|
|
constants => [ |
49
|
|
|
|
|
|
|
qw( |
50
|
|
|
|
|
|
|
$EMPTY |
51
|
|
|
|
|
|
|
$SLASH |
52
|
|
|
|
|
|
|
$TRUE |
53
|
|
|
|
|
|
|
$FALSE |
54
|
|
|
|
|
|
|
$DEFAULT_LOCAL_STACK_HOST |
55
|
|
|
|
|
|
|
$HTTP_OK |
56
|
|
|
|
|
|
|
$HTTP_CONFLICT |
57
|
|
|
|
|
|
|
$HTTP_FORBIDDEN |
58
|
|
|
|
|
|
|
$TEST_BUCKET_PREFIX |
59
|
|
|
|
|
|
|
$MOCK_SERVICES_BUCKET_NAME |
60
|
|
|
|
|
|
|
$PUBLIC_READ_POLICY |
61
|
|
|
|
|
|
|
) |
62
|
|
|
|
|
|
|
], |
63
|
|
|
|
|
|
|
subs => [ |
64
|
|
|
|
|
|
|
qw( |
65
|
|
|
|
|
|
|
add_keys |
66
|
|
|
|
|
|
|
check_test_bucket |
67
|
|
|
|
|
|
|
create_bucket |
68
|
|
|
|
|
|
|
get_s3_service |
69
|
|
|
|
|
|
|
is_aws |
70
|
|
|
|
|
|
|
make_bucket_name |
71
|
|
|
|
|
|
|
set_s3_host |
72
|
|
|
|
|
|
|
) |
73
|
|
|
|
|
|
|
], |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
our @EXPORT_OK = map { @{ $EXPORT_TAGS{$_} } } ( keys %EXPORT_TAGS ); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
######################################################################## |
79
|
|
|
|
|
|
|
sub make_bucket_name { |
80
|
|
|
|
|
|
|
######################################################################## |
81
|
1
|
50
|
|
1
|
0
|
6
|
return $MOCK_SERVICES_BUCKET_NAME |
82
|
|
|
|
|
|
|
if !is_aws(); |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
14
|
my $suffix = eval { |
85
|
1
|
|
|
|
|
169
|
require Data::UUID; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
return lc Data::UUID->new->create_str(); |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
16
|
|
|
|
|
30
|
$suffix //= join $EMPTY, map { ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9 )[$_] } |
91
|
1
|
|
33
|
|
|
11
|
map { int rand 62 } ( 0 .. 15 ); |
|
16
|
|
|
|
|
57
|
|
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
17
|
my $bucket_name = sprintf '%s-%s', $TEST_BUCKET_PREFIX, $suffix; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
8
|
return $bucket_name; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
######################################################################## |
99
|
|
|
|
|
|
|
sub is_aws { |
100
|
|
|
|
|
|
|
######################################################################## |
101
|
|
|
|
|
|
|
return ( $ENV{AMAZON_S3_LOCALSTACK} || $ENV{AMAZON_S3_MINIO} ) |
102
|
1
|
50
|
33
|
1
|
0
|
12
|
? $FALSE |
103
|
|
|
|
|
|
|
: $TRUE; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
######################################################################## |
107
|
|
|
|
|
|
|
sub check_test_bucket { |
108
|
|
|
|
|
|
|
######################################################################## |
109
|
0
|
|
|
0
|
0
|
0
|
my ($s3) = @_; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# list all buckets that I own |
112
|
0
|
|
|
|
|
0
|
my $response = eval { return $s3->buckets; }; |
|
0
|
|
|
|
|
0
|
|
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
0
|
|
|
0
|
if ( $EVAL_ERROR || !$response ) { |
115
|
0
|
|
|
|
|
0
|
diag( |
116
|
|
|
|
|
|
|
Dumper( [ error => [ $response, $s3->err, $s3->errstr, $s3->error ] ] ) |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
BAIL_OUT($EVAL_ERROR); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my ( $owner_id, $owner_displayname ) |
123
|
0
|
|
|
|
|
0
|
= @{$response}{qw(owner_id owner_displayname)}; |
|
0
|
|
|
|
|
0
|
|
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
my $bucket_name = make_bucket_name(); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
my @buckets = map { $_->{bucket} } @{ $response->{buckets} }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
128
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
0
|
|
0
|
if ( any { $_ =~ /$bucket_name/xsm } @buckets ) { |
|
0
|
|
|
|
|
0
|
|
130
|
0
|
|
|
|
|
0
|
BAIL_OUT( 'test bucket already exists: ' . $bucket_name ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
return ( $owner_id, $owner_displayname ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
######################################################################## |
137
|
|
|
|
|
|
|
sub set_s3_host { |
138
|
|
|
|
|
|
|
######################################################################## |
139
|
4
|
|
|
4
|
0
|
492
|
my $host = $ENV{AMAZON_S3_HOST}; |
140
|
|
|
|
|
|
|
|
141
|
4
|
|
50
|
|
|
32
|
$host //= 's3.amazonaws.com'; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
## no critic (RequireLocalizedPunctuationVars) |
144
|
|
|
|
|
|
|
|
145
|
4
|
50
|
|
|
|
34
|
if ( exists $ENV{AMAZON_S3_LOCALSTACK} ) { |
|
|
50
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
0
|
|
0
|
|
|
0
|
$host //= $DEFAULT_LOCAL_STACK_HOST; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
0
|
$ENV{AWS_ACCESS_KEY_ID} = 'test'; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
$ENV{AWS_SECRET_ACCESS_KEY} = 'test'; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
0
|
$ENV{AMAZON_S3_EXPENSIVE_TESTS} = $TRUE; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
0
|
$ENV{AMAZON_S3_SKIP_PERMISSIONS} = $TRUE; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
elsif ( exists $ENV{AMAZON_S3_MINIO} ) { |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
0
|
|
|
0
|
$host //= $DEFAULT_MINIO_HOST; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
0
|
$ENV{AMAZON_S3_SKIP_ACLS} = $TRUE; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
$ENV{AMAZON_S3_EXPENSIVE_TESTS} = $TRUE; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
$ENV{AMAZON_S3_SKIP_REGION_CONSTRAINT_TEST} = $TRUE; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
4
|
|
|
|
|
11
|
return $host; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
######################################################################## |
172
|
|
|
|
|
|
|
sub get_s3_service { |
173
|
|
|
|
|
|
|
######################################################################## |
174
|
0
|
|
|
0
|
0
|
|
my ($host) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $s3 = eval { |
177
|
|
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
if ( $ENV{AMAZON_S3_CREDENTIALS} ) { |
179
|
0
|
|
|
|
|
|
require Amazon::Credentials; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
return Amazon::S3->new( |
182
|
|
|
|
|
|
|
{ credentials => Amazon::Credentials->new, |
183
|
|
|
|
|
|
|
host => $host, |
184
|
|
|
|
|
|
|
secure => is_aws(), |
185
|
|
|
|
|
|
|
dns_bucket_names => $ENV{AMAZON_S3_DNS_BUCKET_NAMES}, |
186
|
0
|
0
|
|
|
|
|
level => $ENV{DEBUG} ? 'trace' : 'error', |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
); |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
else { |
192
|
|
|
|
|
|
|
return Amazon::S3->new( |
193
|
|
|
|
|
|
|
{ aws_access_key_id => $ENV{AWS_ACCESS_KEY_ID}, |
194
|
|
|
|
|
|
|
aws_secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY}, |
195
|
|
|
|
|
|
|
token => $ENV{AWS_SESSION_TOKEN}, |
196
|
|
|
|
|
|
|
host => $host, |
197
|
|
|
|
|
|
|
secure => is_aws(), |
198
|
|
|
|
|
|
|
dns_bucket_names => $ENV{AMAZON_S3_DNS_BUCKET_NAMES}, |
199
|
0
|
0
|
|
|
|
|
level => $ENV{DEBUG} ? 'trace' : 'error', |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
}; |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
return $s3; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
######################################################################## |
209
|
|
|
|
|
|
|
sub create_bucket { |
210
|
|
|
|
|
|
|
######################################################################## |
211
|
0
|
|
|
0
|
0
|
|
my ( $s3, $bucket_name ) = @_; |
212
|
|
|
|
|
|
|
|
213
|
0
|
|
|
|
|
|
$bucket_name = $SLASH . $bucket_name; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
my $bucket_obj |
216
|
0
|
|
|
|
|
|
= eval { return $s3->add_bucket( { bucket => $bucket_name } ); }; |
|
0
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
return $bucket_obj; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
######################################################################## |
222
|
|
|
|
|
|
|
sub add_keys { |
223
|
|
|
|
|
|
|
######################################################################## |
224
|
0
|
|
|
0
|
0
|
|
my ( $bucket_obj, $max_keys, $prefix ) = @_; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
0
|
|
|
|
$prefix //= q{}; |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
|
foreach my $key ( 1 .. $max_keys ) { |
229
|
0
|
|
|
|
|
|
my $keyname = sprintf '%stesting-%02d.txt', $prefix, $key; |
230
|
0
|
|
|
|
|
|
my $value = 'T'; |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
$bucket_obj->add_key( $keyname, $value ); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
return $max_keys; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |