1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/service/mirror_service_test.rb

87 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "service/shared_service_tests"
class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase
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}") ]
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") }
SERVICE = ActiveStorage::Service.configure :mirror, config
include ActiveStorage::Service::SharedServiceTests
test "name" do
assert_equal :mirror, @service.name
end
2017-07-06 07:53:26 -04:00
test "uploading to all services" do
key = SecureRandom.base58(24)
data = "Something else entirely!"
io = StringIO.new(data)
checksum = Digest::MD5.base64digest(data)
@service.upload key, io.tap(&:read), checksum: checksum
assert_predicate io, :eof?
assert_equal data, @service.primary.download(key)
@service.mirrors.each do |mirror|
assert_equal data, mirror.download(key)
end
ensure
@service.delete key
end
2017-07-06 07:53:26 -04:00
test "downloading from primary service" do
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
@service.delete @key
assert_not SERVICE.primary.exist?(@key)
SERVICE.mirrors.each do |mirror|
assert_not mirror.exist?(@key)
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
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
test "path for file in primary service" do
assert_equal @service.primary.path_for(@key), @service.path_for(@key)
end
end