2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
require "s3_helper"
|
2015-10-11 05:41:23 -04:00
|
|
|
require "rails_helper"
|
2014-09-24 16:52:09 -04:00
|
|
|
|
|
|
|
describe "S3Helper" do
|
2019-06-08 08:40:35 -04:00
|
|
|
let(:client) { Aws::S3::Client.new(stub_responses: true) }
|
|
|
|
|
2018-09-16 20:57:50 -04:00
|
|
|
before(:each) do
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_access_key_id = "abc"
|
|
|
|
SiteSetting.s3_secret_access_key = "def"
|
2014-09-24 16:52:09 -04:00
|
|
|
|
2018-09-16 20:57:50 -04:00
|
|
|
@lifecycle = <<~XML
|
2017-11-12 23:36:45 -05:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
|
|
|
<Rule>
|
|
|
|
<ID>old_rule</ID>
|
|
|
|
<Prefix>projectdocs/</Prefix>
|
|
|
|
<Status>Enabled</Status>
|
|
|
|
<Expiration>
|
|
|
|
<Days>3650</Days>
|
|
|
|
</Expiration>
|
|
|
|
</Rule>
|
|
|
|
<Rule>
|
|
|
|
<ID>purge-tombstone</ID>
|
|
|
|
<Prefix>test/</Prefix>
|
|
|
|
<Status>Enabled</Status>
|
|
|
|
<Expiration>
|
|
|
|
<Days>3650</Days>
|
|
|
|
</Expiration>
|
|
|
|
</Rule>
|
|
|
|
</LifecycleConfiguration>
|
|
|
|
XML
|
2018-09-16 20:57:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can correctly set the purge policy" do
|
|
|
|
SiteSetting.s3_configure_tombstone_policy = true
|
|
|
|
|
|
|
|
stub_request(:get, "http://169.254.169.254/latest/meta-data/iam/security-credentials/").
|
|
|
|
to_return(status: 404, body: "", headers: {})
|
2017-11-12 23:36:45 -05:00
|
|
|
|
|
|
|
stub_request(:get, "https://bob.s3.amazonaws.com/?lifecycle").
|
2018-09-16 20:57:50 -04:00
|
|
|
to_return(status: 200, body: @lifecycle, headers: {})
|
2017-11-12 23:36:45 -05:00
|
|
|
|
|
|
|
stub_request(:put, "https://bob.s3.amazonaws.com/?lifecycle").
|
|
|
|
with do |req|
|
|
|
|
|
|
|
|
hash = Hash.from_xml(req.body.to_s)
|
|
|
|
rules = hash["LifecycleConfiguration"]["Rule"]
|
|
|
|
|
|
|
|
expect(rules.length).to eq(2)
|
2018-09-16 20:57:50 -04:00
|
|
|
expect(rules[1]["Expiration"]["Days"]).to eq("100")
|
2017-11-12 23:36:45 -05:00
|
|
|
# fixes the bad filter
|
|
|
|
expect(rules[0]["Filter"]["Prefix"]).to eq("projectdocs/")
|
|
|
|
end.to_return(status: 200, body: "", headers: {})
|
|
|
|
|
2018-09-16 20:57:50 -04:00
|
|
|
helper = S3Helper.new('bob', 'tomb')
|
|
|
|
helper.update_tombstone_lifecycle(100)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can skip policy update when s3_configure_tombstone_policy is false" do
|
|
|
|
SiteSetting.s3_configure_tombstone_policy = false
|
2017-11-12 23:36:45 -05:00
|
|
|
|
|
|
|
helper = S3Helper.new('bob', 'tomb')
|
|
|
|
helper.update_tombstone_lifecycle(100)
|
|
|
|
end
|
|
|
|
|
2018-09-10 04:34:40 -04:00
|
|
|
describe '#list' do
|
|
|
|
it 'creates the prefix correctly' do
|
|
|
|
{
|
|
|
|
'some/bucket' => 'bucket/testing',
|
|
|
|
'some' => 'testing'
|
|
|
|
}.each do |bucket_name, prefix|
|
2019-06-08 08:40:35 -04:00
|
|
|
s3_helper = S3Helper.new(bucket_name, "", client: client)
|
|
|
|
Aws::S3::Bucket.any_instance.expects(:objects).with(prefix: prefix)
|
2018-09-10 04:34:40 -04:00
|
|
|
s3_helper.list('testing')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-05 07:51:51 -04:00
|
|
|
|
|
|
|
it "should prefix bucket folder path only if not exists" do
|
2019-06-08 08:40:35 -04:00
|
|
|
s3_helper = S3Helper.new('bucket/folder_path', "", client: client)
|
2019-06-05 07:51:51 -04:00
|
|
|
|
2019-06-08 08:40:35 -04:00
|
|
|
object1 = s3_helper.object("original/1X/def.xyz")
|
|
|
|
object2 = s3_helper.object("folder_path/original/1X/def.xyz")
|
2019-06-05 07:51:51 -04:00
|
|
|
|
2019-06-08 08:40:35 -04:00
|
|
|
expect(object1.key).to eq(object2.key)
|
2019-06-05 07:51:51 -04:00
|
|
|
end
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|