2020-01-12 17:12:27 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
require_relative "shared_context_for_backup_restore"
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe BackupRestore::MetaDataHandler do
|
2022-07-27 11:14:14 -05:00
|
|
|
include_context "with shared stuff"
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
let!(:backup_filename) { "discourse-2019-11-18-143242-v20191108000414.tar.gz" }
|
2020-01-12 17:12:27 -06:00
|
|
|
|
|
|
|
def with_metadata_file(content)
|
|
|
|
Dir.mktmpdir do |directory|
|
|
|
|
if !content.nil?
|
|
|
|
path = File.join(directory, BackupRestore::MetaDataHandler::METADATA_FILE)
|
|
|
|
File.write(path, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
yield(directory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_metadata(filename, tmp_directory)
|
|
|
|
BackupRestore::MetaDataHandler.new(logger, filename, tmp_directory).validate
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "metadata file" do
|
2020-01-15 09:41:02 -06:00
|
|
|
it "extracts metadata from file when metadata file exists" do
|
|
|
|
metadata = '{"source":"discourse","version":20160329101122}'
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
with_metadata_file(metadata) do |dir|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(validate_metadata(backup_filename, dir)).to include(version: 20_160_329_101_122)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
it "raises an exception when the metadata file contains invalid JSON" do
|
2021-05-20 20:43:47 -05:00
|
|
|
corrupt_metadata = '{"version":20160329101122'
|
2020-01-15 09:41:02 -06:00
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
with_metadata_file(corrupt_metadata) do |dir|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { validate_metadata(backup_filename, dir) }.to raise_error(
|
|
|
|
BackupRestore::MetaDataError,
|
|
|
|
)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when the metadata file is empty" do
|
2023-01-09 05:18:21 -06:00
|
|
|
with_metadata_file("") do |dir|
|
|
|
|
expect { validate_metadata(backup_filename, dir) }.to raise_error(
|
|
|
|
BackupRestore::MetaDataError,
|
|
|
|
)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
it "raises an exception when the metadata file contains an invalid version number" do
|
|
|
|
metadata = '{"source":"discourse","version":"1abcdefghijklm"}'
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
with_metadata_file(metadata) do |dir|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { validate_metadata(backup_filename, dir) }.to raise_error(
|
|
|
|
BackupRestore::MetaDataError,
|
|
|
|
)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
it "raises an exception when the metadata file contains an empty version number" do
|
|
|
|
metadata = '{"source":"discourse","version":""}'
|
|
|
|
|
|
|
|
with_metadata_file(metadata) do |dir|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { validate_metadata(backup_filename, dir) }.to raise_error(
|
|
|
|
BackupRestore::MetaDataError,
|
|
|
|
)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "filename" do
|
2020-01-15 09:41:02 -06:00
|
|
|
it "extracts metadata from filename when metadata file does not exist" do
|
|
|
|
with_metadata_file(nil) do |dir|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(validate_metadata(backup_filename, dir)).to include(version: 20_191_108_000_414)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2020-01-15 09:41:02 -06:00
|
|
|
it "raises an exception when the filename contains no version number" do
|
2023-01-09 05:18:21 -06:00
|
|
|
filename = "discourse-2019-11-18-143242.tar.gz"
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { validate_metadata(filename, nil) }.to raise_error(BackupRestore::MetaDataError)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when the filename contains an invalid version number" do
|
2023-01-09 05:18:21 -06:00
|
|
|
filename = "discourse-2019-11-18-143242-v123456789.tar.gz"
|
|
|
|
expect { validate_metadata(filename, nil) }.to raise_error(BackupRestore::MetaDataError)
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
filename = "discourse-2019-11-18-143242-v1abcdefghijklm.tar.gz"
|
|
|
|
expect { validate_metadata(filename, nil) }.to raise_error(BackupRestore::MetaDataError)
|
2020-01-15 09:41:02 -06:00
|
|
|
end
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when the backup's version is newer than the current version" do
|
2023-01-09 05:18:21 -06:00
|
|
|
new_backup_filename = "discourse-2019-11-18-143242-v20191113193141.sql.gz"
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
BackupRestore.expects(:current_version).returns(20_191_025_005_204).once
|
2020-01-12 17:12:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { validate_metadata(new_backup_filename, nil) }.to raise_error(
|
|
|
|
BackupRestore::MigrationRequiredError,
|
|
|
|
)
|
2020-01-12 17:12:27 -06:00
|
|
|
end
|
|
|
|
end
|