Skip to content

Commit

Permalink
Add transition tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BasilMawejje committed Aug 26, 2021
1 parent 302f212 commit 016ec4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 3 additions & 4 deletions app/models/conditions_response/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,12 @@ class << self
end
end

# method signature => s3_lifecycle_rules(string, string, string, {days: integer, storage_class: upcase string in STORAGE_CLASSES list})
# e.g s3_lifecycle_rules('bucket', 'bucket_full_prefix', 'enabled', {days: 30, storage_class: "STANDARD_IA"}, {days: 90, storage_class: "GLACIER"})
# s3_lifecycle_rules('bucket', 'bucket_full_prefix', 'enabled', {days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'})
def self.s3_lifecycle_rules(bucket, bucket_full_prefix, status, *storage_rules_kwargs)
client = Aws::S3::Client.new(region: ENV['SHF_AWS_S3_BACKUP_REGION'],
credentials: Aws::Credentials.new(ENV['SHF_AWS_S3_BACKUP_KEY_ID'], ENV['SHF_AWS_S3_BACKUP_SECRET_ACCESS_KEY']))

storage_class_list = storage_rules_kwargs.map{|h| h.values.last}
storage_class_list = storage_rules_kwargs.flatten.map{|h| h.values.last}
unless storage_class_is_valid? storage_class_list
client.put_bucket_lifecycle_configuration({
bucket: bucket,
Expand All @@ -321,7 +320,7 @@ def self.s3_lifecycle_rules(bucket, bucket_full_prefix, status, *storage_rules_k
prefix: bucket_full_prefix
},
id: ENV['SHF_AWS_S3_BACKUP_KEY_ID'],
status: status.capitalize, # String showing 'Enabled' or 'Disabled'
status: status.capitalize,
transitions: storage_rules_kwargs
}
]
Expand Down
28 changes: 24 additions & 4 deletions spec/models/conditions_response/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
let(:mock_bucket_object) { double('Aws::S3::Object', upload_file: true) }
let(:mock_bucket) { double('Aws::S3::Bucket', object: mock_bucket_object) }
let(:mock_s3) { double('Aws::S3::Resource', bucket: mock_bucket) }
let(:mock_s3_client) { instance_double('Aws::S3::Client') }
let(:bucket_name) { 'bucket_name' }
let(:bucket_full_prefix) { 'bucket/top/prefix' }

Expand Down Expand Up @@ -1306,8 +1307,21 @@ def create_faux_backup_file(backups_dir, file_prefix)
end

describe 's3_lifecycle_rules(bucket, bucket_full_prefix, status, *storage_rules_kwargs)' do
let(:invalid_storage_class_list) { ["INVALID_STORAGE_CLASS", "OTHER_INVALID_STORAGE_CLASS"] }
let(:another_invalid_storage_class_list) { ["INVALID_STORAGE_CLASS", "STANDARD_IA", "GLACIER"] }
let(:invalid_storage_class_list) { ['INVALID_STORAGE_CLASS', 'OTHER_INVALID_STORAGE_CLASS'] }
let(:another_invalid_storage_class_list) { ['INVALID_STORAGE_CLASS', 'STANDARD_IA', 'GLACIER'] }
let(:status) { 'Enabled' }
let(:storage_rules) { [{days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'}] }

let(:mock_s3_client) do
client = Aws::S3::Client.new(stub_responses: true)
client.stub_responses(
:put_bucket_lifecycle_configuration, ->(context) {
bucket = context.params[:bucket]
lifecycle_configuration = context.params[:lifecycle_configuration][:rules]
}
)
client
end

it "returns 'Invalid storage class' for a list containing only invalid storage classes" do
expect(described_class.storage_class_is_valid? invalid_storage_class_list).to eq("Invalid storage class")
Expand All @@ -1317,12 +1331,18 @@ def create_faux_backup_file(backups_dir, file_prefix)
expect(described_class.storage_class_is_valid? another_invalid_storage_class_list).to eq("Invalid storage class")
end

it "fails for empty storage classes list" do
it "returns 'Empty storage class' for empty storage classes list" do
expect(described_class.storage_class_is_valid? []).to eq("Empty storage class")
end

it 'returns the correct lifecycle rules transitions' do
#TODO: Add this test
put_lifecycle_data = mock_s3_client.put_bucket_lifecycle_configuration(bucket: bucket_name, lifecycle_configuration: {rules: [{status: status, transitions: storage_rules}]})
expect(mock_s3_client).to receive(:get_bucket_lifecycle_configuration).with({bucket: bucket_name}).and_return(put_lifecycle_data)
obj = mock_s3_client.get_bucket_lifecycle_configuration(bucket: bucket_name)

expect(obj[0][:status]).to eq 'Enabled'
expect(obj[0][:transitions].count).to eq 2
expect(obj[0][:transitions]).to eq [{days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'}]
end
end
end
Expand Down

0 comments on commit 016ec4e

Please sign in to comment.