2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
require "service/shared_service_tests"
|
|
|
|
|
|
|
|
class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase
|
2017-07-08 19:55:50 -04:00
|
|
|
mirror_config = (1..3).map do |i|
|
|
|
|
[ "mirror_#{i}",
|
|
|
|
service: "Disk",
|
2018-01-16 20:32:02 -05:00
|
|
|
root: Dir.mktmpdir("active_storage_tests_mirror_#{i}") ]
|
2017-07-08 19:55:50 -04:00
|
|
|
end.to_h
|
|
|
|
|
|
|
|
config = mirror_config.merge \
|
2018-01-15 13:06:17 -05:00
|
|
|
mirror: { service: "Mirror", primary: "primary", mirrors: mirror_config.keys },
|
2018-01-16 20:32:02 -05:00
|
|
|
primary: { service: "Disk", root: Dir.mktmpdir("active_storage_tests_primary") }
|
2017-07-06 06:22:44 -04:00
|
|
|
|
2017-07-08 19:55:50 -04:00
|
|
|
SERVICE = ActiveStorage::Service.configure :mirror, config
|
2017-07-06 06:22:44 -04:00
|
|
|
|
|
|
|
include ActiveStorage::Service::SharedServiceTests
|
|
|
|
|
2017-07-06 07:53:26 -04:00
|
|
|
test "uploading to all services" do
|
2018-12-20 12:44:01 -05:00
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
io = StringIO.new(data)
|
|
|
|
checksum = Digest::MD5.base64digest(data)
|
2017-07-06 06:22:44 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
@service.upload key, io.tap(&:read), checksum: checksum
|
|
|
|
assert_predicate io, :eof?
|
2018-01-25 20:37:00 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_equal data, @service.primary.download(key)
|
|
|
|
@service.mirrors.each do |mirror|
|
|
|
|
assert_equal data, mirror.download(key)
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
ensure
|
|
|
|
@service.delete key
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 07:53:26 -04:00
|
|
|
test "downloading from primary service" do
|
2018-01-25 20:37:00 -05:00
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
checksum = Digest::MD5.base64digest(data)
|
|
|
|
|
|
|
|
@service.primary.upload key, StringIO.new(data), checksum: checksum
|
2017-07-06 07:53:26 -04:00
|
|
|
|
|
|
|
assert_equal data, @service.download(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deleting from all services" do
|
2018-06-25 18:49:26 -04:00
|
|
|
@service.delete @key
|
2018-01-25 20:37:00 -05:00
|
|
|
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_not SERVICE.primary.exist?(@key)
|
2017-07-08 19:55:50 -04:00
|
|
|
SERVICE.mirrors.each do |mirror|
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_not mirror.exist?(@key)
|
2017-07-08 18:41:14 -04:00
|
|
|
end
|
2017-07-06 07:53:26 -04:00
|
|
|
end
|
|
|
|
|
2019-05-22 15:07:35 -04:00
|
|
|
test "mirroring a file from the primary service to secondary services where it doesn't exist" do
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
checksum = Digest::MD5.base64digest(data)
|
|
|
|
|
|
|
|
@service.primary.upload key, StringIO.new(data), checksum: checksum
|
|
|
|
@service.mirrors.third.upload key, StringIO.new("Surprise!")
|
|
|
|
|
|
|
|
@service.mirror key, checksum: checksum
|
|
|
|
assert_equal data, @service.mirrors.first.download(key)
|
|
|
|
assert_equal data, @service.mirrors.second.download(key)
|
|
|
|
assert_equal "Surprise!", @service.mirrors.third.download(key)
|
|
|
|
end
|
|
|
|
|
2017-07-06 07:53:26 -04:00
|
|
|
test "URL generation in primary service" do
|
2017-09-28 16:43:37 -04:00
|
|
|
filename = ActiveStorage::Filename.new("test.txt")
|
|
|
|
|
2017-07-19 13:58:23 -04:00
|
|
|
freeze_time do
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_equal @service.primary.url(@key, expires_in: 2.minutes, disposition: :inline, filename: filename, content_type: "text/plain"),
|
|
|
|
@service.url(@key, expires_in: 2.minutes, disposition: :inline, filename: filename, content_type: "text/plain")
|
2017-07-06 07:53:26 -04:00
|
|
|
end
|
|
|
|
end
|
2019-02-13 12:59:48 -05:00
|
|
|
|
|
|
|
test "path for file in primary service" do
|
|
|
|
assert_equal @service.primary.path_for(@key), @service.path_for(@key)
|
|
|
|
end
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|