Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds first AWS-SDK Ruby tests #73

Merged
merged 4 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build/ruby/aws-ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
set -e

_init() {
ruby --version
ruby --version
}

# Install Ruby dependencies
installDeps() {
gem install aws-sdk
gem install colorize
}

main() {
Expand Down
292 changes: 215 additions & 77 deletions run/core/aws-sdk-ruby/aws-stub-tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,90 +16,228 @@
#

require 'aws-sdk'
require 'securerandom'
require 'securerandom'
require 'colorize'

region = ENV['SERVER_REGION'] ||= 'SERVER_REGION not set'
endpoint = ENV['SERVER_ENDPOINT'] ||= 'SERVER_ENDPOINT'
access_key_id = ENV['ACCESS_KEY'] ||= 'ACCESS_KEY is not set'
secret_access_key = ENV['SECRET_KEY'] ||= 'SECRET_KEY is not set'
data_dir = ENV['DATA_DIR'] ||= 'DATA_DIR is not set'
enable_https = ENV['ENABLE_HTTPS']
class AWS_SDK_Ruby_Test

def print_title(title)
# Prints the title for the test
puts '=================================================='
msg = "\n*** " + title + "\n"
print msg.blue
end

if enable_https == "1"
endpoint = 'https://' + endpoint
else
endpoint = 'http://' + endpoint
end

# Set up AWS Client
client = 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 print_log(log_msg, arg='')
# Prints a progress log message for
# the on-going test WITHOUT a new line.
# It accepts an arg to print out at the end
# of the progress message
msg = "\t" + log_msg + "%s"
printf(msg.light_black, arg)
end

# Test list buckets #1
begin
client.buckets.limit(1000).each do |b|
puts "#{b.name}"
end
puts "List Buckets Test Pass"
rescue
puts "Test List Buckets # 1 Fails."
end

# Test making a bucket #1
bucket_exists = false
bucket_name = SecureRandom.hex(6)
begin
client.create_bucket(bucket: bucket_name)
puts "Bucket Create Test Pass"
rescue
puts "Bucket Create #1 Fails"
end

# Test bucket exists #1
begin
resp = client.bucket(bucket_name).exists?
if resp == true
puts "Bucket Exists Test Pass"
end
rescue
puts "Bucket Exists Test Fails"
end
def print_logn(log_msg, arg='')
# Prints a progress log message for
# the on-going test WITH a new line.
# It accepts an arg to print out at the end
# of the progress message
msg = "\t" + log_msg + "%s" + "\n"
printf(msg.light_black, arg)
end

# Uploading an object to a bucket
file = data_dir + '/datafile-1-MB'
# Get just the file name
begin
name = File.basename(file)
# Create the object to upload
obj = client.bucket(bucket_name).object(name)
# Upload it
obj.upload_file(file)
puts "Uploading Object Test Pass"
rescue
puts "Uploading Object to Bucket Failed"
end

# Download an object
begin
obj = client.bucket(bucket_name).object('small.file')
# Get the item's content and save it to a file
obj.get(response_target: '/tmp/my-small.file')
puts "Downloading Object from Bucket Pass"
rescue
puts "Downloading Object from Bucket Failed"
end
def print_status(result, e='')
# Prints result/status of the test, as "PASS" or "FAIL".
# It adds the captured error message
# if the result/status is a "FAIL".
e = e.nil? ? nil.to_s : "ERROR: " + e.to_s + "\n"
msg = "*** " + result + "\n" + e
if result == "PASS"
printf(msg.green)
else
printf(msg.red)
end
end


def create_bucket(s3Resource)
# Tests if a bucket can be created for s3
# client instance, "s3Resource".
bucket_name = SecureRandom.hex(6)
bucket = s3Resource.create_bucket(bucket: bucket_name)

return bucket if bucket.exists?
raise "Create bucket failure"
rescue => e
puts "ERROR: ", e
end


def delete_bucket(bucket)
# Tests if a bucket object, "bucket", can be deleted/removed
if bucket.exists?
bucket.objects.each do |obj|
obj.delete obj.key
end
bucket.delete
end
rescue => e
puts "ERROR: Failed to delete bucket:", e
end


def list_buckets_test(s3Resource, s3Client)
# Tests if existing bucket objects for
# s3 client instances (s3Resource and s3Client)
# can be looped through (list functionality)
# It also logs/pritns the total number of bucket objects
# found for each client instance.
print_title "List buckets Test"
begin
i = j = 0
s3Resource.buckets.limit(1000).each do |b|
i += 1
end
print_log("Buckets (Resource) found:", i)
print_logn("- Success!")
s3Client.list_buckets.buckets.each do |b|
j += 1
end
print_log("Buckets (Client) found:", j)
print_logn("- Success!")
state = "PASS"
rescue => e
state = "FAIL"
end
# Clean-up
print_status(state, e)
end


# Delete bucket
begin
bucket_name_temp = SecureRandom.hex(6)
client.create_bucket(bucket: bucket_name_temp)
resp = client.bucket(bucket_name).exists?

if resp == true
client.delete_bucket(bucket: bucket_name_temp)
puts "Bucket Delete Test Pass"
end
rescue
puts "Bucket Delete Test Fails"

def make_remove_bucket_test(s3Resource)
# Tests if a bucket can be made/created.
# If successful, it also tests, if the
# same created bucket can be removed/deleted.
print_title "Make/Remove Bucket Test"
begin
print_log("Making a bucket")
bucket = create_bucket(s3Resource)
if bucket.exists?
state = "PASS"
else
state = "FAIL"
end
print_logn("- Success!")
print_log("Deleting the bucket")
delete_bucket(bucket)
if !bucket.exists?
state = "PASS"
else
state = "FAIL"
end
print_logn("- Success!")
rescue => e
state = "FAIL"
end
print_status(state, e)
end


def upload_object_test(s3Resource, data_dir)
# Tests if an file object can be uploaded
# to s3 using s3 client, "s3Resource" at
# the location, "data_dir"
# It cleans up after the test is done.
file = data_dir + '/datafile-1-MB'
print_title "Upload Object Test"
begin
name = File.basename(file)
# Create the object to upload
bucket = create_bucket(s3Resource)
obj = bucket.object(name)
# Upload it
print_log("Uploading a 1MB file")
obj.upload_file(file)
print_logn("- Success!")
state = "PASS"
rescue => e
state = "FAIL"
end
# Clean-up
print_log "Clean-up"
delete_bucket(bucket)
print_logn("- Success!")
print_status(state, e)
end

def download_object_test(s3Resource,data_dir)
# Tests if a file object can be uplaoded.
# To achieve this goal, it first downloads
# the file object, and then uploads it.
# It cleans up after the test is done.
file = data_dir + '/datafile-1-MB'
destination = '/tmp' + '/datafile-1-MB'
print_title "Download Object Test"
begin
name = File.basename(file)
bucket = create_bucket(s3Resource)
obj = bucket.object(name)
print_log("First uploading a 1MB file")
obj.upload_file(file)
print_logn("- Success!")
# Get the item's content and save it to a file
print_log("Downloading the same object into your local /tmp directory")
obj.get(response_target: destination)
print_logn("- Success!")
state = "PASS"
rescue => e
state = "FAIL"
end
print_log("Clean-up")
delete_bucket(bucket)
system("rm #{destination}")
print_logn("- Success!")
print_status(state, e)
end
end

# 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"
endpoint = ENV['SERVER_ENDPOINT'] ||= 'SERVER_ENDPOINT is not set'

access_key_id = ENV['ACCESS_KEY'] ||= 'ACESS_KEY is not set'
secret_access_key = ENV['SECRET_KEY'] ||= 'SECRET_KEY is not set'

# The location where the bucket and file
# objects are going to be created.
data_dir = ENV['DATA_DIR'] ||= 'DATA_DIR is not set'

# "1/0" value to decide if "HTTPS"
# needs to be used on or not.
enable_https = ENV['ENABLE_HTTPS']

# Add "https://" to "endpoint" if environment
# variable "ENABLE_HTTPS" is turned on
endpoint = enable_https == "1" ? 'https://' + endpoint : 'http://' + endpoint

# Create s3 client instances, "s3Resource" and "s3Client"
s3Resource = Aws::S3::Resource.new(region: region, endpoint: endpoint, access_key_id: access_key_id,
secret_access_key: secret_access_key, force_path_style: true)
s3Client = Aws::S3::Client.new(region: region, endpoint: endpoint, access_key_id: access_key_id,
secret_access_key: secret_access_key, force_path_style: true)

# Create the test class instance and call the tests
aws = AWS_SDK_Ruby_Test.new
aws.list_buckets_test(s3Resource, s3Client)
aws.make_remove_bucket_test(s3Resource)
aws.upload_object_test(s3Resource, data_dir)
aws.download_object_test(s3Resource, data_dir)