SPEC: add tests for `VacateLegacyPrefixBackups` onceoff job.

This commit is contained in:
Vinoth Kannan 2020-05-29 00:02:19 +05:30
parent caffd002b9
commit 3037617327
4 changed files with 67 additions and 5 deletions

View File

@ -3,7 +3,8 @@
module Jobs
class VacateLegacyPrefixBackups < ::Jobs::Onceoff
def execute_onceoff(args)
BackupRestore::S3BackupStore.create.vacate_legacy_prefix if SiteSetting.backup_location == BackupLocationSiteSetting::S3
args ||= {}
BackupRestore::S3BackupStore.create(s3_options: args[:s3_options]).vacate_legacy_prefix if SiteSetting.backup_location == BackupLocationSiteSetting::S3
end
end
end

View File

@ -7,7 +7,7 @@ module BackupRestore
def initialize(opts = {})
@s3_options = S3Helper.s3_options(SiteSetting)
@s3_options.merge!(opts[:s3_options]) if opts[:s3_options]
@s3_helper = S3Helper.new(s3_bucket_name_with_prefix, '', @s3_options)
@s3_helper = S3Helper.new(s3_bucket_name_with_prefix, '', @s3_options.clone)
end
def remote?
@ -52,7 +52,7 @@ module BackupRestore
end
def vacate_legacy_prefix
legacy_s3_helper = S3Helper.new(s3_bucket_name_with_legacy_prefix, '', @s3_options)
legacy_s3_helper = S3Helper.new(s3_bucket_name_with_legacy_prefix, '', @s3_options.clone)
legacy_keys = legacy_s3_helper.list.map { |o| o.key }
legacy_keys.each do |legacy_key|
bucket, prefix = s3_bucket_name_with_prefix.split('/', 2)
@ -61,7 +61,7 @@ module BackupRestore
bucket: bucket,
key: File.join(prefix, legacy_key.split('/').last)
})
legacy_s3_helper.remove(legacy_key, false)
legacy_s3_helper.delete_object(legacy_key)
end
end

View File

@ -63,7 +63,12 @@ class S3Helper
# delete the file
s3_filename.prepend(multisite_upload_path) if Rails.configuration.multisite
s3_bucket.object(get_path_for_s3_upload(s3_filename)).delete
delete_object(get_path_for_s3_upload(s3_filename))
rescue Aws::S3::Errors::NoSuchKey
end
def delete_object(key)
s3_bucket.object(key).delete
rescue Aws::S3::Errors::NoSuchKey
end

View File

@ -0,0 +1,56 @@
# frozen_string_literal: true
require "s3_helper"
require 'rails_helper'
describe Jobs::VacateLegacyPrefixBackups, type: :multisite do
let(:bucket_name) { "backupbucket" }
before(:all) do
@s3_client = Aws::S3::Client.new(stub_responses: true)
@s3_options = { client: @s3_client }
@objects = []
create_backups
@s3_client.stub_responses(:list_objects_v2, -> (context) do
{ contents: objects_with_prefix(context) }
end)
end
before do
SiteSetting.enable_s3_uploads = true
SiteSetting.s3_access_key_id = "abc"
SiteSetting.s3_secret_access_key = "def"
SiteSetting.s3_backup_bucket = bucket_name
SiteSetting.backup_location = BackupLocationSiteSetting::S3
end
it "copies the backups from legacy path to new path" do
@objects.each do |object|
legacy_key = object[:key]
legacy_object = @s3_client.get_object(bucket: bucket_name, key: legacy_key)
@s3_client.expects(:copy_object).with({
copy_source: File.join(bucket_name, legacy_key),
bucket: bucket_name,
key: legacy_key.sub(/^backups\//, "")
})
@s3_client.expects(:delete_object).with(bucket: bucket_name, key: legacy_key).returns(legacy_object)
end
described_class.new.execute_onceoff(s3_options: @s3_options)
end
def objects_with_prefix(context)
prefix = context.params[:prefix]
@objects.select { |obj| obj[:key].start_with?(prefix) }
end
def create_backups
@objects.clear
@objects << { key: "backups/default/b.tar.gz", size: 17, last_modified: Time.parse("2018-09-13T15:10:00Z") }
@objects << { key: "backups/default/filename.tar.gz", size: 17, last_modified: Time.parse("2019-10-18T17:20:00Z") }
end
end