-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathaws-stub-tests.rb
More file actions
executable file
·865 lines (762 loc) · 28.4 KB
/
aws-stub-tests.rb
File metadata and controls
executable file
·865 lines (762 loc) · 28.4 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#!/usr/bin/env ruby
#
# Mint (C) 2017 Minio, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'aws-sdk'
require 'securerandom'
require 'net/http'
require 'multipart_body'
# For aws-sdk ruby tests to run, setting the following
# environment variables is mandatory.
# SERVER_ENDPOINT: <ip:port> address of the minio server tests will run against
# ACCESS_KEY: access key for the minio server
# SECRET_KEY: secreet key for the minio server
# SERVER_REGION: region minio server is setup to run
# ENABLE_HTTPS: (1|0) turn on/off to specify https or
# http services minio server is running on
# MINT_DATA_DIR: Data directory where test data files are stored
class AwsSdkRubyTest
# Set variables necessary to create an s3 client instance.
# Get them from the environment variables
# Region information, eg. "us-east-1"
region = ENV['SERVER_REGION'] ||= 'SERVER_REGION is not set'
# Minio server, eg. "play.minio.io:9000"
access_key_id = ENV['ACCESS_KEY'] ||= 'ACCESS_KEY is not set'
secret_access_key = ENV['SECRET_KEY'] ||= 'SECRET_KEY is not set'
enable_https = ENV['ENABLE_HTTPS']
end_point = ENV['SERVER_ENDPOINT'] ||= 'SERVER_ENDPOINT is not set'
endpoint = enable_https == '1' ? 'https://' + end_point : 'http://' + end_point
# Create s3 resource instance,"s3"
@@s3 = Aws::S3::Resource.new(
region: region,
endpoint: endpoint,
access_key_id: access_key_id,
secret_access_key: secret_access_key,
force_path_style: true)
def initialize_log_output(meth, alert = nil)
# Initialize and return log content in log_output hash table
# Collect args in args_arr
args_arr = method(meth).parameters.flatten.map(&:to_s)
.reject { |x| x == 'req' || x == 'opt' }
# Create and return log output content
{ name: 'aws-sdk-ruby',
function: "#{meth}(#{args_arr.join(',')})", # method name and arguments
args: args_arr, # array of arg names. This'll be replaced with a
# a arg/value pairs insdie the caller method
duration: 0, # test runtime duration in seconds
alert: alert,
message: nil,
error: nil }
end
def random_bucket_name
'aws-sdk-ruby-bucket-' + SecureRandom.hex(6)
end
def calculate_duration(t2, t1)
# Durations are in miliseconds, with precision of 2 decimal places
((t2 - t1) * 1000).round(2)
end
def print_log(log_output, start_time)
# Calculate duration in miliseconds
log_output[:duration] = calculate_duration(Time.now, start_time)
# Get rid of the log_output fields if nil
puts log_output.delete_if{|k, value| value == nil}.to_json
# Exit at the first failure
exit 1 if log_output[:status] == 'FAIL'
end
def cleanUp(buckets, log_output)
# Removes objects and bucket if bucket exists
bucket_name = ''
buckets.each do |b|
bucket_name = b
if bucketExistsWrapper(b, log_output)
removeObjectsWrapper(b, log_output)
removeBucketWrapper(b, log_output)
end
end
rescue => e
raise "Failed to clean-up bucket '#{bucket_name}', #{e}"
end
#
# API commands/methods
#
def makeBucket(bucket_name)
# Creates a bucket, "bucket_name"
# on S3 client , "s3".
# Returns bucket_name if already exists
@@s3.bucket(bucket_name).exists? ? @@s3.bucket(bucket_name) : @@s3.create_bucket(bucket: bucket_name)
rescue => e
raise e
end
def makeBucketWrapper(bucket_name, log_output)
makeBucket(bucket_name)
rescue => e
log_output[:function] = "makeBucket(bucket_name)"
log_output[:args] = {'bucket_name': bucket_name}
raise e
end
def removeBucket(bucket_name)
# Deletes/removes bucket, "bucket_name" on S3 client, "s3"
@@s3.bucket(bucket_name).delete
rescue => e
raise e
end
def removeBucketWrapper(bucket_name, log_output)
removeBucket(bucket_name)
rescue => e
log_output[:function] = "removeBucket(bucket_name)"
log_output[:args] = {'bucket_name': bucket_name}
raise e
end
def putObject(bucket_name, file)
# Creates "file" (full path) in bucket, "bucket_name",
# on S3 client, "s3"
file_name = File.basename(file)
@@s3.bucket(bucket_name).object(file_name).upload_file(file)
rescue => e
raise e
end
def putObjectWrapper(bucket_name, file, log_output)
putObject(bucket_name, file)
rescue => e
log_output[:function] = "putObject(bucket_name, file)"
log_output[:args] = {'bucket_name': bucket_name,
'file': file}
raise e
end
def getObject(bucket_name, file, destination)
# Gets/Downloads file, "file",
# from bucket, "bucket_name", of S3 client, "s3"
file_name = File.basename(file)
dest = File.join(destination, file_name)
@@s3.bucket(bucket_name).object(file_name).get(response_target: dest)
rescue => e
raise e
end
def getObjectWrapper(bucket_name, file, destination, log_output)
getObject(bucket_name, file, destination)
rescue => e
log_output[:function] = "getObject(bucket_name, file)"
log_output[:args] = {'bucket_name': bucket_name,
'file': file,
'destination': destination}
raise e
end
def copyObject(source_bucket_name, target_bucket_name, source_file_name, target_file_name = '')
# Copies file, "file_name", from source bucket,
# "source_bucket_name", to target bucket,
# "target_bucket_name", on S3 client, "s3"
target_file_name = source_file_name if target_file_name.empty?
source = @@s3.bucket(source_bucket_name)
target = @@s3.bucket(target_bucket_name)
source_obj = source.object(source_file_name)
target_obj = target.object(target_file_name)
source_obj.copy_to(target_obj)
rescue => e
raise e
end
def copyObjectWrapper(source_bucket_name, target_bucket_name, source_file_name, target_file_name = '', log_output)
copyObject(source_bucket_name, target_bucket_name, source_file_name, target_file_name)
rescue => e
log_output[:function] = 'copyObject(source_bucket_name, target_bucket_name, source_file_name, target_file_name = '')'
log_output[:args] = {'source_bucket_name': source_bucket_name,
'target_bucket_name': target_bucket_name,
'source_file_name': source_file_name,
'target_file_name': target_file_name}
raise e
end
def removeObject(bucket_name, file)
# Deletes file in bucket,
# "bucket_name", on S3 client, "s3".
# If file, "file_name" does not exist,
# it quietly returns without any error message
@@s3.bucket(bucket_name).object(file).delete
rescue => e
raise e
end
def removeObjectWrapper(bucket_name, file_name, log_output)
removeObject(bucket_name, file_name)
rescue => e
log_output[:function] = "removeObject(bucket_name, file_name)"
log_output[:args] = {'bucket_name': bucket_name,
'file_name': file_name}
raise e
end
def removeObjects(bucket_name)
# Deletes all files in bucket, "bucket_name"
# on S3 client, "s3"
file_name = ''
@@s3.bucket(bucket_name).objects.each do |obj|
file_name = obj.key
obj.delete
end
rescue => e
raise "File name: '#{file_name}', #{e}"
end
def removeObjectsWrapper(bucket_name, log_output)
removeObjects(bucket_name)
rescue => e
log_output[:function] = 'removeObjects(bucket_name)'
log_output[:args] = {'bucket_name': bucket_name}
raise e
end
def listBuckets
# Returns an array of bucket names on S3 client, "s3"
bucket_name_list = []
@@s3.buckets.each do |b|
bucket_name_list.push(b.name)
end
return bucket_name_list
rescue => e
raise e
end
def listBucketsWrapper(log_output)
listBuckets
rescue => e
log_output[:function] = 'listBuckets'
log_output[:args] = {}
raise e
end
def listObjects(bucket_name)
# Returns an array of object/file names
# in bucket, "bucket_name", on S3 client, "s3"
object_list = []
@@s3.bucket(bucket_name).objects.each do |obj|
object_list.push(obj.key)
end
return object_list
rescue => e
raise e
end
def listObjectsWrapper(bucket_name, log_output)
listObjects(bucket_name)
rescue => e
log_output[:function] = 'listObjects(bucket_name)'
log_output[:args] = {'bucket_name': bucket_name}
raise e
end
def statObject(bucket_name, file_name)
return @@s3.bucket(bucket_name).object(file_name).exists?
rescue => e
raise e
end
def statObjectWrapper(bucket_name, file_name, log_output)
statObject(bucket_name, file_name)
rescue => e
log_output[:function] = 'statObject(bucket_name, file_name)'
log_output[:args] = {'bucket_name': bucket_name,
'file_name': file_name}
raise e
end
def bucketExists?(bucket_name)
# Returns true if bucket, "bucket_name", exists,
# false otherwise
return @@s3.bucket(bucket_name).exists?
rescue => e
raise e
end
def bucketExistsWrapper(bucket_name, log_output)
bucketExists?(bucket_name)
rescue => e
log_output[:function] = 'bucketExists?(bucket_name)'
log_output[:args] = {'bucket_name': bucket_name}
raise e
end
def presignedGet(bucket_name, file_name)
# Returns download/get url
obj = @@s3.bucket(bucket_name).object(file_name)
return obj.presigned_url(:get, expires_in: 600)
rescue => e
raise e
end
def presignedGetWrapper(bucket_name, file_name, log_output)
presignedGet(bucket_name, file_name)
rescue => e
log_output[:function] = 'presignedGet(bucket_name, file_name)'
log_output[:args] = {'bucket_name': bucket_name,
'file_name': file_name}
raise e
end
def presignedPut(bucket_name, file_name)
# Returns put url
obj = @@s3.bucket(bucket_name).object(file_name)
return obj.presigned_url(:put, expires_in: 600)
rescue => e
raise e
end
def presignedPutWrapper(bucket_name, file_name, log_output)
presignedPut(bucket_name, file_name)
rescue => e
log_output[:function] = 'presignedPut(bucket_name, file_name)'
log_output[:args] = {'bucket_name': bucket_name,
'file_name': file_name}
raise e
end
def presignedPost(bucket_name, file_name, expires_in_sec, max_byte_size)
# Returns upload/post url
obj = @@s3.bucket(bucket_name).object(file_name)
return obj.presigned_post(expires: Time.now + expires_in_sec,
content_length_range: 1..max_byte_size)
rescue => e
raise e
end
def presignedPostWrapper(bucket_name, file_name, expires_in_sec, max_byte_size, log_output)
presignedPost(bucket_name, file_name, expires_in_sec, max_byte_size)
rescue => e
log_output[:function] = 'presignedPost(bucket_name, file_name, expires_in_sec, max_byte_size)'
log_output[:args] = {'bucket_name': bucket_name,
'file_name': file_name,
'expires_in_sec': expires_in_sec,
'max_byte_size': max_byte_size}
raise e
end
# To be addressed. S3 API 'get_bucket_policy' does not work!
# def getBucketPolicy(bucket_name)
# # Returns bucket policy
# return @@s3.bucket(bucket_name).get_bucket_policy
# rescue => e
# raise e
# end
#
# Test case methods
#
def listBucketsTest
# Tests listBuckets api command by creating
# new buckets from bucket_name_list
# get 2 different random bucket names and create a list
bucket_name_list = [random_bucket_name, random_bucket_name]
# Initialize hash table, 'log_output'
log_output = initialize_log_output('listBuckets')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
prev_total_buckets = listBucketsWrapper(log_output).length
new_buckets = bucket_name_list.length
bucket_name_list.each do |b|
makeBucketWrapper(b, log_output)
end
new_total_buckets = prev_total_buckets + new_buckets
if new_total_buckets >= prev_total_buckets + new_buckets
log_output[:status] = 'PASS'
else
log_output[:error] = 'Could not find expected number of buckets'
log_output[:status] = 'FAIL'
end
cleanUp(bucket_name_list, log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def makeBucketTest
# Tests makeBucket api command.
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('makeBucket')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
if bucketExistsWrapper(bucket_name, log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = 'Bucket expected to be created does not exist'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def bucketExistsNegativeTest
# Tests bucketExists api command.
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('bucketExists?')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
if !bucketExistsWrapper(bucket_name, log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = "Failed to return 'false' for a non-existing bucket"
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def removeBucketTest
# Tests removeBucket api command.
# get a random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('removeBucket')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
removeBucketWrapper(bucket_name, log_output)
if !bucketExistsWrapper(bucket_name, log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = 'Bucket expected to be removed still exists'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def putObjectTest(file)
# Tests putObject api command by uploading a file
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('putObject')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
putObjectWrapper(bucket_name, file, log_output)
if statObjectWrapper(bucket_name, File.basename(file), log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = "Status for the created object returned 'false'"
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def removeObjectTest(file)
# Tests removeObject api command by uploading and removing a file
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('removeObject')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
putObjectWrapper(bucket_name, file, log_output)
removeObjectWrapper(bucket_name, File.basename(file), log_output)
if !statObjectWrapper(bucket_name, File.basename(file), log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = "Status for the removed object returned 'true'"
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def getObjectTest(file, destination)
# Tests getObject api command
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('getObject')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
putObjectWrapper(bucket_name, file, log_output)
getObjectWrapper(bucket_name, file, destination, log_output)
if system("ls -l #{destination} > /dev/null")
log_output[:status] = 'PASS'
else
log_output[:error] = "Downloaded object does not exist at #{destination}"
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def listObjectsTest(file_list)
# Tests listObjects api command
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('listObjects')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
# Put all objects into the bucket
file_list.each do |f|
putObjectWrapper(bucket_name, f, log_output)
end
# Total number of files uploaded
expected_no = file_list.length
# Actual number is what api returns
actual_no = listObjectsWrapper(bucket_name, log_output).length
# Compare expected and actual values
if expected_no == actual_no
log_output[:status] = 'PASS'
else
log_output[:error] = 'Expected and actual number of listed files/objects do not match!'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def copyObjectTest(data_dir, source_file_name, target_file_name = '')
# Tests copyObject api command
# get random bucket names
source_bucket_name = random_bucket_name
target_bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('copyObject')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
target_file_name = source_file_name if target_file_name.empty?
makeBucketWrapper(source_bucket_name, log_output)
makeBucketWrapper(target_bucket_name, log_output)
putObjectWrapper(source_bucket_name,
File.join(data_dir, source_file_name), log_output)
copyObjectWrapper(source_bucket_name, target_bucket_name,
source_file_name, target_file_name, log_output)
# Check if copy worked fine
if statObjectWrapper(target_bucket_name, target_file_name, log_output)
log_output[:status] = 'PASS'
else
log_output[:error] = 'Copied file could not be found in the expected location'
log_output[:status] = 'FAIL'
end
cleanUp([source_bucket_name, target_bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def presignedGetObjectTest(data_dir, file_name)
# Tests presignedGetObject api command
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('presignedGet')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
file = File.join(data_dir, file_name)
# Get check sum value without the file name
cksum_orig = `cksum #{file}`.split[0..1]
putObjectWrapper(bucket_name, file, log_output)
get_url = presignedGetWrapper(bucket_name, file_name, log_output)
# Download the file using the URL
# generated by presignedGet api command
`wget -O /tmp/#{file_name} '#{get_url}' > /dev/null 2>&1`
# Get check sum value for the downloaded file
# Split to get rid of the file name
cksum_new = `cksum /tmp/#{file_name}`.split[0..1]
# Check if check sum values for the orig file
# and the downloaded file match
if cksum_orig == cksum_new
log_output[:status] = 'PASS'
else
log_output[:error] = 'Check sum values do NOT match'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def presignedPutObjectTest(data_dir, file_name)
# Tests presignedPutObject api command
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('presignedPut')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
file = File.join(data_dir, file_name)
# Get check sum value and
# split to get rid of the file name
cksum_orig = `cksum #{file}`.split[0..1]
# Generate presigned Put URL and parse it
uri = URI.parse(presignedPutWrapper(bucket_name, file_name, log_output))
request = Net::HTTP::Put.new(uri.request_uri, 'content-type' => 'application/octet-stream')
request.body = IO.read(File.join(data_dir, file_name))
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if ENV['ENABLE_HTTPS'] == '1'
http.request(request)
if statObjectWrapper(bucket_name, file_name, log_output)
getObjectWrapper(bucket_name, file_name, '/tmp', log_output)
cksum_new = `cksum /tmp/#{file_name}`.split[0..1]
# Check if check sum values of the orig file
# and the downloaded file match
if cksum_orig == cksum_new
log_output[:status] = 'PASS'
else
log_output[:error] = 'Check sum values do NOT match'
log_output[:status] = 'FAIL'
end
else
log_output[:error] = 'Expected to be created object does NOT exist'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
def presignedPostObjectTest(data_dir, file_name,
expires_in_sec, max_byte_size)
# Tests presignedPostObject api command
# get random bucket name
bucket_name = random_bucket_name
# Initialize hash table, 'log_output'
log_output = initialize_log_output('presignedPost')
# Prepare arg/value hash table and set it in log_output
arg_value_hash = {}
log_output[:args].each { |x| arg_value_hash[:"#{x}"] = eval x.to_s }
log_output[:args] = arg_value_hash
begin
start_time = Time.now
makeBucketWrapper(bucket_name, log_output)
file = File.join(data_dir, file_name)
# Get check sum value and split it
# into parts to get rid of the file name
cksum_orig = `cksum #{file}`.split[0..1]
# Create the presigned POST url
post = presignedPostWrapper(bucket_name, file_name,
expires_in_sec, max_byte_size, log_output)
# Prepare multi parts array for POST command request
file_part = Part.new name: 'file',
body: IO.read(File.join(data_dir, file_name)),
filename: file_name,
content_type: 'application/octet-stream'
parts = []
# Add POST fields into parts array
post.fields.each do |field, value|
parts.push(Part.new(field, value))
end
parts.push(file_part)
boundary = "---------------------------#{rand(10_000_000_000_000_000)}"
body_parts = MultipartBody.new parts, boundary
# Parse presigned Post URL
uri = URI.parse(post.url)
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if ENV['ENABLE_HTTPS'] == '1'
request = Net::HTTP::Post.new(uri.request_uri)
request.body = body_parts.to_s
request.content_type = "multipart/form-data; boundary=#{boundary}"
# Send the request
log_output[:error] = http.request(request)
if statObjectWrapper(bucket_name, file_name, log_output)
getObjectWrapper(bucket_name, file_name, '/tmp', log_output)
cksum_new = `cksum /tmp/#{file_name}`.split[0..1]
# Check if check sum values of the orig file
# and the downloaded file match
if cksum_orig == cksum_new
log_output[:status] = 'PASS'
# FIXME: HTTP No Content error, status code=204 is returned as error
log_output[:error] = nil
else
log_output[:error] = 'Check sum values do NOT match'
log_output[:status] = 'FAIL'
end
else
log_output[:error] = 'Expected to be created object does NOT exist'
log_output[:status] = 'FAIL'
end
cleanUp([bucket_name], log_output)
rescue => log_output[:error]
log_output[:status] = 'FAIL'
end
print_log(log_output, start_time)
end
end
# MAIN CODE
# Create test Class instance and call the tests
aws = AwsSdkRubyTest.new
file_name1 = 'datafile-1-kB'
file_new_name = 'datafile-1-kB-copy'
file_name_list = ['datafile-1-kB', 'datafile-1-b', 'datafile-6-MB']
# Add data_dir in front of each file name in file_name_list
# The location where the bucket and file
# objects are going to be created.
data_dir = ENV['MINT_DATA_DIR'] ||= 'MINT_DATA_DIR is not set'
file_list = file_name_list.map { |f| File.join(data_dir, f) }
destination = '/tmp'
aws.listBucketsTest
aws.listObjectsTest(file_list)
aws.makeBucketTest
aws.bucketExistsNegativeTest
aws.removeBucketTest
aws.putObjectTest(File.join(data_dir, file_name1))
aws.removeObjectTest(File.join(data_dir, file_name1))
aws.getObjectTest(File.join(data_dir, file_name1), destination)
aws.copyObjectTest(data_dir, file_name1)
aws.copyObjectTest(data_dir, file_name1, file_new_name)
aws.presignedGetObjectTest(data_dir, file_name1)
aws.presignedPutObjectTest(data_dir, file_name1)
aws.presignedPostObjectTest(data_dir, file_name1, 60, 3*1024*1024)