2020-01-12 18:12:27 -05:00
|
|
|
# frozen_string_literal: true
|
2021-07-07 12:57:42 -04:00
|
|
|
# rubocop:disable Discourse/OnlyTopLevelMultisiteSpecs
|
2020-01-12 18:12:27 -05:00
|
|
|
|
|
|
|
require_relative 'shared_context_for_backup_restore'
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe BackupRestore::UploadsRestorer do
|
2022-07-27 12:14:14 -04:00
|
|
|
include_context "with shared stuff"
|
2020-01-12 18:12:27 -05:00
|
|
|
|
|
|
|
subject { BackupRestore::UploadsRestorer.new(logger) }
|
|
|
|
|
|
|
|
def with_temp_uploads_directory(name: "default", with_optimized: false)
|
|
|
|
Dir.mktmpdir do |directory|
|
|
|
|
path = File.join(directory, "uploads", name)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
FileUtils.mkdir(File.join(path, "optimized")) if with_optimized
|
|
|
|
yield(directory, path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_no_remap(source_site_name: nil, target_site_name:, metadata: [])
|
|
|
|
expect_remaps(
|
|
|
|
source_site_name: source_site_name,
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: metadata
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-03-03 15:10:09 -05:00
|
|
|
def expect_remap(source_site_name: nil, target_site_name:, metadata: [], from:, to:, regex: false, &block)
|
2020-01-12 18:12:27 -05:00
|
|
|
expect_remaps(
|
|
|
|
source_site_name: source_site_name,
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: metadata,
|
2021-03-03 15:10:09 -05:00
|
|
|
remaps: [{ from: from, to: to, regex: regex }],
|
2020-01-12 18:12:27 -05:00
|
|
|
&block
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_remaps(source_site_name: nil, target_site_name:, metadata: [], remaps: [], &block)
|
2021-03-03 15:10:09 -05:00
|
|
|
regex_remaps = remaps.select { |r| r[:regex] }
|
|
|
|
remaps.delete_if { |r| r.delete(:regex) }
|
|
|
|
|
2020-01-12 18:12:27 -05:00
|
|
|
source_site_name ||= metadata.find { |d| d[:name] == "db_name" }&.dig(:value) || "default"
|
|
|
|
|
|
|
|
if source_site_name != target_site_name
|
|
|
|
site_rename = { from: "/uploads/#{source_site_name}/", to: uploads_path(target_site_name) }
|
|
|
|
remaps << site_rename unless remaps.last == site_rename
|
|
|
|
end
|
|
|
|
|
|
|
|
with_temp_uploads_directory(name: source_site_name, with_optimized: true) do |directory, path|
|
|
|
|
yield(directory) if block_given?
|
|
|
|
|
|
|
|
Discourse.store.class.any_instance.expects(:copy_from).with(path).once
|
|
|
|
|
|
|
|
if remaps.blank?
|
|
|
|
DbHelper.expects(:remap).never
|
|
|
|
else
|
|
|
|
DbHelper.expects(:remap).with do |from, to, args|
|
|
|
|
args[:excluded_tables]&.include?("backup_metadata")
|
|
|
|
remaps.shift == { from: from, to: to }
|
|
|
|
end.times(remaps.size)
|
|
|
|
end
|
|
|
|
|
2021-03-03 15:10:09 -05:00
|
|
|
if regex_remaps.blank?
|
|
|
|
DbHelper.expects(:regexp_replace).never
|
|
|
|
else
|
|
|
|
DbHelper.expects(:regexp_replace).with do |from, to, args|
|
|
|
|
args[:excluded_tables]&.include?("backup_metadata")
|
|
|
|
regex_remaps.shift == { from: from, to: to }
|
|
|
|
end.times(regex_remaps.size)
|
|
|
|
end
|
|
|
|
|
2020-01-12 18:12:27 -05:00
|
|
|
if target_site_name == "default"
|
|
|
|
setup_and_restore(directory, metadata)
|
|
|
|
else
|
|
|
|
test_multisite_connection(target_site_name) { setup_and_restore(directory, metadata) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_and_restore(directory, metadata)
|
|
|
|
metadata.each { |d| BackupMetadata.create!(d) }
|
|
|
|
subject.restore(directory)
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploads_path(database)
|
|
|
|
path = File.join("uploads", database)
|
|
|
|
|
2020-04-28 09:03:04 -04:00
|
|
|
path = File.join(path, "test_#{ENV['TEST_ENV_NUMBER'].presence || '0'}")
|
2020-01-12 18:12:27 -05:00
|
|
|
|
|
|
|
"/#{path}/"
|
|
|
|
end
|
|
|
|
|
2021-03-03 15:10:09 -05:00
|
|
|
def s3_url_regex(bucket, path)
|
|
|
|
Regexp.escape("//#{bucket}") + %q*\.s3(?:\.dualstack\.[a-z0-9\-]+?|[.\-][a-z0-9\-]+?)?\.amazonaws\.com* + Regexp.escape(path)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "uploads" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:multisite) { { name: "multisite", value: true } }
|
|
|
|
let!(:no_multisite) { { name: "multisite", value: false } }
|
|
|
|
let!(:source_db_name) { { name: "db_name", value: "foo" } }
|
2021-11-11 11:44:58 -05:00
|
|
|
let!(:base_url) { { name: "base_url", value: "https://test.localhost/forum" } }
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:no_cdn_url) { { name: "cdn_url", value: nil } }
|
|
|
|
let!(:cdn_url) { { name: "cdn_url", value: "https://some-cdn.example.com" } }
|
|
|
|
let(:target_site_name) { target_site_type == multisite ? "second" : "default" }
|
|
|
|
let(:target_hostname) { target_site_type == multisite ? "test2.localhost" : "test.localhost" }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
shared_context "with no uploads" do
|
2020-01-12 18:12:27 -05:00
|
|
|
it "does nothing when temporary uploads directory is missing or empty" do
|
|
|
|
store_class.any_instance.expects(:copy_from).never
|
|
|
|
|
|
|
|
Dir.mktmpdir do |directory|
|
|
|
|
subject.restore(directory)
|
|
|
|
|
|
|
|
FileUtils.mkdir(File.join(directory, "uploads"))
|
|
|
|
subject.restore(directory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "without metadata" do
|
|
|
|
it "correctly remaps uploads" do
|
|
|
|
expect_no_remap(target_site_name: "default")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly remaps when site name is different" do
|
|
|
|
expect_remap(
|
|
|
|
source_site_name: "foo",
|
|
|
|
target_site_name: "default",
|
|
|
|
from: "/uploads/foo/",
|
|
|
|
to: uploads_path("default")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
shared_context "when restoring uploads" do
|
2020-01-12 18:12:27 -05:00
|
|
|
before do
|
|
|
|
Upload.where("id > 0").destroy_all
|
|
|
|
Fabricate(:optimized_image)
|
|
|
|
|
|
|
|
upload = Fabricate(:upload_s3)
|
|
|
|
post = Fabricate(:post, raw: "![#{upload.original_filename}](#{upload.short_url})")
|
|
|
|
post.link_post_uploads
|
|
|
|
|
|
|
|
FileHelper.stubs(:download).returns(file_from_fixtures("logo.png"))
|
|
|
|
FileStore::S3Store.any_instance.stubs(:store_upload).returns do
|
|
|
|
File.join(
|
|
|
|
"//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com",
|
|
|
|
target_site_type == multisite ? "/uploads/#{target_site_name}" : "",
|
|
|
|
"original/1X/bc975735dfc6409c1c2aa5ebf2239949bcbdbd65.png"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
UserAvatar.import_url_for_user("logo.png", Fabricate(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "successfully restores uploads" do
|
|
|
|
SiteIconManager.expects(:ensure_optimized!).once
|
|
|
|
|
|
|
|
with_temp_uploads_directory do |directory, path|
|
|
|
|
store_class.any_instance.expects(:copy_from).with(path).once
|
|
|
|
|
|
|
|
expect { subject.restore(directory) }
|
|
|
|
.to change { OptimizedImage.count }.by_at_most(-1)
|
|
|
|
.and change { Jobs::CreateAvatarThumbnails.jobs.size }.by(1)
|
|
|
|
.and change { Post.where(baked_version: nil).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't generate optimized images when backup contains optimized images" do
|
|
|
|
SiteIconManager.expects(:ensure_optimized!).never
|
|
|
|
|
|
|
|
with_temp_uploads_directory(with_optimized: true) do |directory, path|
|
|
|
|
store_class.any_instance.expects(:copy_from).with(path).once
|
|
|
|
|
|
|
|
expect { subject.restore(directory) }
|
2022-07-19 10:03:03 -04:00
|
|
|
.to not_change { OptimizedImage.count }
|
|
|
|
.and not_change { Jobs::CreateAvatarThumbnails.jobs.size }
|
2020-01-12 18:12:27 -05:00
|
|
|
.and change { Post.where(baked_version: nil).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "common remaps" do
|
|
|
|
it "remaps when `base_url` changes" do
|
|
|
|
Discourse.expects(:base_url).returns("http://localhost").at_least_once
|
|
|
|
|
|
|
|
expect_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, base_url],
|
2021-11-11 11:44:58 -05:00
|
|
|
from: "https://test.localhost/forum",
|
2020-01-12 18:12:27 -05:00
|
|
|
to: "http://localhost"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't remap when `cdn_url` in `backup_metadata` is empty" do
|
|
|
|
expect_no_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, no_cdn_url]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remaps to new `cdn_url` when `cdn_url` changes to a different value" do
|
|
|
|
Discourse.expects(:asset_host).returns("https://new-cdn.example.com").at_least_once
|
|
|
|
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://some-cdn.example.com/", to: "https://new-cdn.example.com/" },
|
|
|
|
{ from: "some-cdn.example.com", to: "new-cdn.example.com" }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remaps to `base_url` when `cdn_url` changes to an empty value" do
|
|
|
|
Discourse.expects(:base_url).returns("http://example.com/discourse").at_least_once
|
|
|
|
Discourse.expects(:asset_host).returns(nil).at_least_once
|
|
|
|
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://some-cdn.example.com/", to: "//example.com/discourse/" },
|
|
|
|
{ from: "some-cdn.example.com", to: "example.com" }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "remaps from local storage" do
|
|
|
|
it "doesn't remap when `s3_base_url` in `backup_metadata` is empty" do
|
|
|
|
expect_no_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, s3_base_url]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't remap when `s3_cdn_url` in `backup_metadata` is empty" do
|
|
|
|
expect_no_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_site_type, s3_cdn_url]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when currently stored locally" do
|
2020-01-12 18:12:27 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.enable_s3_uploads = false
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:store_class) { FileStore::LocalStore }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
include_context "with no uploads"
|
|
|
|
include_context "when restoring uploads"
|
2020-01-12 18:12:27 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with remaps" do
|
2020-01-12 18:12:27 -05:00
|
|
|
include_examples "without metadata"
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when uploads previously stored locally" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:s3_base_url) { { name: "s3_base_url", value: nil } }
|
|
|
|
let!(:s3_cdn_url) { { name: "s3_cdn_url", value: nil } }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { no_multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with uploads previously stored on S3" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:s3_base_url) { { name: "s3_base_url", value: "//old-bucket.s3-us-east-1.amazonaws.com" } }
|
|
|
|
let!(:s3_cdn_url) { { name: "s3_cdn_url", value: "https://s3-cdn.example.com" } }
|
|
|
|
|
|
|
|
shared_examples "regular site remaps from S3" do
|
|
|
|
it "remaps when `s3_base_url` changes" do
|
|
|
|
expect_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [no_multisite, s3_base_url],
|
2021-03-03 15:10:09 -05:00
|
|
|
from: s3_url_regex("old-bucket", "/"),
|
|
|
|
to: uploads_path(target_site_name),
|
|
|
|
regex: true
|
2020-01-12 18:12:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remaps when `s3_cdn_url` changes" do
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [no_multisite, s3_cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://s3-cdn.example.com/", to: "//#{target_hostname}#{uploads_path(target_site_name)}" },
|
|
|
|
{ from: "s3-cdn.example.com", to: target_hostname }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "multisite remaps from S3" do
|
|
|
|
it "remaps when `s3_base_url` changes" do
|
|
|
|
expect_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_db_name, multisite, s3_base_url],
|
2021-03-03 15:10:09 -05:00
|
|
|
from: s3_url_regex("old-bucket", "/"),
|
|
|
|
to: "/",
|
|
|
|
regex: true
|
2020-01-12 18:12:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remaps when `s3_cdn_url` changes" do
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_db_name, multisite, s3_cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://s3-cdn.example.com/", to: "//#{target_hostname}/" },
|
|
|
|
{ from: "s3-cdn.example.com", to: target_hostname }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { no_multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "regular site remaps from S3"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "regular site remaps from S3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "multisite remaps from S3"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "multisite remaps from S3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when currently stored on S3" do
|
2020-01-12 18:12:27 -05:00
|
|
|
before do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2020-01-12 18:12:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:store_class) { FileStore::S3Store }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
include_context "with no uploads"
|
|
|
|
include_context "when restoring uploads"
|
2020-01-12 18:12:27 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with remaps" do
|
2020-01-12 18:12:27 -05:00
|
|
|
include_examples "without metadata"
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with uploads previously stored locally" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:s3_base_url) { { name: "s3_base_url", value: nil } }
|
|
|
|
let!(:s3_cdn_url) { { name: "s3_cdn_url", value: nil } }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { no_multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "remaps from local storage"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with uploads previously stored on S3" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:s3_base_url) { { name: "s3_base_url", value: "//old-bucket.s3-us-east-1.amazonaws.com" } }
|
|
|
|
let!(:s3_cdn_url) { { name: "s3_cdn_url", value: "https://s3-cdn.example.com" } }
|
|
|
|
|
|
|
|
shared_examples "regular site remaps from S3" do
|
|
|
|
it "remaps when `s3_base_url` changes" do
|
|
|
|
expect_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [no_multisite, s3_base_url],
|
2021-03-03 15:10:09 -05:00
|
|
|
from: s3_url_regex("old-bucket", "/"),
|
|
|
|
to: uploads_path(target_site_name),
|
|
|
|
regex: true
|
2020-01-12 18:12:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remaps when `s3_cdn_url` changes" do
|
|
|
|
SiteSetting::Upload.expects(:s3_cdn_url).returns("https://new-s3-cdn.example.com").at_least_once
|
|
|
|
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [no_multisite, s3_cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://s3-cdn.example.com/", to: "https://new-s3-cdn.example.com#{uploads_path(target_site_name)}" },
|
|
|
|
{ from: "s3-cdn.example.com", to: "new-s3-cdn.example.com" }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "multisite remaps from S3" do
|
|
|
|
it "remaps when `s3_base_url` changes" do
|
|
|
|
expect_remap(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_db_name, multisite, s3_base_url],
|
2021-03-03 15:10:09 -05:00
|
|
|
from: s3_url_regex("old-bucket", "/"),
|
|
|
|
to: "/",
|
|
|
|
regex: true
|
2020-01-12 18:12:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when `s3_cdn_url` is configured" do
|
|
|
|
it "remaps when `s3_cdn_url` changes" do
|
|
|
|
SiteSetting::Upload.expects(:s3_cdn_url).returns("http://new-s3-cdn.example.com").at_least_once
|
|
|
|
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_db_name, multisite, s3_cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://s3-cdn.example.com/", to: "//new-s3-cdn.example.com/" },
|
|
|
|
{ from: "s3-cdn.example.com", to: "new-s3-cdn.example.com" }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when `s3_cdn_url` is not configured" do
|
|
|
|
it "remaps to `base_url` when `s3_cdn_url` changes" do
|
|
|
|
SiteSetting::Upload.expects(:s3_cdn_url).returns(nil).at_least_once
|
|
|
|
|
|
|
|
expect_remaps(
|
|
|
|
target_site_name: target_site_name,
|
|
|
|
metadata: [source_db_name, multisite, s3_cdn_url],
|
|
|
|
remaps: [
|
|
|
|
{ from: "https://s3-cdn.example.com/", to: "//#{target_hostname}/" },
|
|
|
|
{ from: "s3-cdn.example.com", to: target_hostname }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { no_multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_name) { "default" }
|
|
|
|
let!(:target_hostname) { "test.localhost" }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "regular site remaps from S3"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_name) { "second" }
|
|
|
|
let!(:target_hostname) { "test2.localhost" }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "regular site remaps from S3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as source" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:source_site_type) { multisite }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with regular site as target" do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { no_multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "multisite remaps from S3"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multisite as target", type: :multisite do
|
2020-01-12 18:12:27 -05:00
|
|
|
let!(:target_site_type) { multisite }
|
|
|
|
|
|
|
|
include_examples "common remaps"
|
|
|
|
include_examples "multisite remaps from S3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-03 15:10:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".s3_regex_string" do
|
|
|
|
def regex_matches(s3_base_url)
|
|
|
|
regex, _ = BackupRestore::UploadsRestorer.s3_regex_string(s3_base_url)
|
|
|
|
expect(Regexp.new(regex)).to match(s3_base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly matches different S3 base URLs" do
|
|
|
|
regex_matches("//some-bucket.s3.amazonaws.com/")
|
|
|
|
regex_matches("//some-bucket.s3.us-west-2.amazonaws.com/")
|
|
|
|
regex_matches("//some-bucket.s3-us-west-2.amazonaws.com/")
|
|
|
|
regex_matches("//some-bucket.s3.dualstack.us-west-2.amazonaws.com/")
|
|
|
|
regex_matches("//some-bucket.s3.cn-north-1.amazonaws.com.cn/")
|
|
|
|
|
|
|
|
regex_matches("//some-bucket.s3.amazonaws.com/foo/")
|
|
|
|
regex_matches("//some-bucket.s3.us-east-2.amazonaws.com/foo/")
|
|
|
|
regex_matches("//some-bucket.s3-us-east-2.amazonaws.com/foo/")
|
|
|
|
regex_matches("//some-bucket.s3.dualstack.us-east-2.amazonaws.com/foo/")
|
|
|
|
regex_matches("//some-bucket.s3.cn-north-1.amazonaws.com.cn/foo/")
|
|
|
|
end
|
2020-01-12 18:12:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when the store doesn't support the copy_from method" do
|
|
|
|
Discourse.stubs(:store).returns(Object.new)
|
|
|
|
|
|
|
|
with_temp_uploads_directory do |directory|
|
|
|
|
expect { subject.restore(directory) }.to raise_error(BackupRestore::UploadsRestoreError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when there are multiple folders in the uploads directory" do
|
|
|
|
with_temp_uploads_directory do |directory|
|
|
|
|
FileUtils.mkdir_p(File.join(directory, "uploads", "foo"))
|
|
|
|
expect { subject.restore(directory) }.to raise_error(BackupRestore::UploadsRestoreError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores 'PaxHeaders' and hidden directories within the uploads directory" do
|
|
|
|
expect_remap(
|
|
|
|
source_site_name: "xylan",
|
|
|
|
target_site_name: "default",
|
|
|
|
from: "/uploads/xylan/",
|
|
|
|
to: uploads_path("default")
|
|
|
|
) do |directory|
|
|
|
|
FileUtils.mkdir_p(File.join(directory, "uploads", "PaxHeaders.27134"))
|
|
|
|
FileUtils.mkdir_p(File.join(directory, "uploads", ".hidden"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|