Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
59d3e84f
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
59d3e84f
编写于
5月 08, 2018
作者:
M
Micaël Bergeron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove the `Upload` redefinition in `MigrateUploadsWorker`
上级
9618e49b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
95 addition
and
149 deletion
+95
-149
app/workers/object_storage/migrate_uploads_worker.rb
app/workers/object_storage/migrate_uploads_worker.rb
+0
-79
spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb
...ers/workers/object_storage/migrate_uploads_worker_spec.rb
+95
-70
未找到文件。
app/workers/object_storage/migrate_uploads_worker.rb
浏览文件 @
59d3e84f
...
...
@@ -9,85 +9,6 @@ module ObjectStorage
SanityCheckError
=
Class
.
new
(
StandardError
)
class
Upload
<
ActiveRecord
::
Base
# Upper limit for foreground checksum processing
CHECKSUM_THRESHOLD
=
100
.
megabytes
belongs_to
:model
,
polymorphic:
true
# rubocop:disable Cop/PolymorphicAssociations
validates
:size
,
presence:
true
validates
:path
,
presence:
true
validates
:model
,
presence:
true
validates
:uploader
,
presence:
true
before_save
:calculate_checksum!
,
if: :foreground_checksummable?
after_commit
:schedule_checksum
,
if: :checksummable?
scope
:stored_locally
,
->
{
where
(
store:
[
nil
,
ObjectStorage
::
Store
::
LOCAL
])
}
scope
:stored_remotely
,
->
{
where
(
store:
ObjectStorage
::
Store
::
REMOTE
)
}
def
self
.
hexdigest
(
path
)
Digest
::
SHA256
.
file
(
path
).
hexdigest
end
def
absolute_path
raise
ObjectStorage
::
RemoteStoreError
,
"Remote object has no absolute path."
unless
local?
return
path
unless
relative_path?
uploader_class
.
absolute_path
(
self
)
end
def
calculate_checksum!
self
.
checksum
=
nil
return
unless
checksummable?
self
.
checksum
=
self
.
class
.
hexdigest
(
absolute_path
)
end
def
build_uploader
(
mounted_as
=
nil
)
uploader_class
.
new
(
model
,
mounted_as
).
tap
do
|
uploader
|
uploader
.
upload
=
self
uploader
.
retrieve_from_store!
(
identifier
)
end
end
def
exist?
File
.
exist?
(
absolute_path
)
end
def
local?
return
true
if
store
.
nil?
store
==
ObjectStorage
::
Store
::
LOCAL
end
private
def
checksummable?
checksum
.
nil?
&&
local?
&&
exist?
end
def
foreground_checksummable?
checksummable?
&&
size
<=
CHECKSUM_THRESHOLD
end
def
schedule_checksum
UploadChecksumWorker
.
perform_async
(
id
)
end
def
relative_path?
!
path
.
start_with?
(
'/'
)
end
def
identifier
File
.
basename
(
path
)
end
def
uploader_class
Object
.
const_get
(
uploader
)
end
end
class
MigrationResult
attr_reader
:upload
attr_accessor
:error
...
...
spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb
浏览文件 @
59d3e84f
...
...
@@ -7,113 +7,138 @@ describe ObjectStorage::MigrateUploadsWorker, :sidekiq do
end
end
let!
(
:projects
)
{
create_list
(
:project
,
10
,
:with_avatar
)
}
let
(
:uploads
)
{
Upload
.
all
}
let
(
:model_class
)
{
Project
}
let
(
:
mounted_as
)
{
:avatar
}
let
(
:
uploads
)
{
Upload
.
all
}
let
(
:to_store
)
{
ObjectStorage
::
Store
::
REMOTE
}
before
do
stub_uploads_object_storage
(
AvatarUploader
)
end
describe
'.enqueue!'
do
def
enqueue!
described_class
.
enqueue!
(
uploads
,
Project
,
mounted_as
,
to_store
)
end
shared_examples
"correctly"
do
describe
'.enqueue!'
do
def
enqueue!
described_class
.
enqueue!
(
uploads
,
Project
,
mounted_as
,
to_store
)
end
it
'is guarded by .sanity_check!'
do
expect
(
described_class
).
to
receive
(
:perform_async
)
expect
(
described_class
).
to
receive
(
:sanity_check!
)
it
'is guarded by .sanity_check!'
do
expect
(
described_class
).
to
receive
(
:perform_async
)
expect
(
described_class
).
to
receive
(
:sanity_check!
)
enqueue!
end
enqueue!
end
context
'sanity_check! fails'
do
include_context
'sanity_check! fails'
context
'sanity_check! fails'
do
include_context
'sanity_check! fails'
it
'does not enqueue a job'
do
expect
(
described_class
).
not_to
receive
(
:perform_async
)
it
'does not enqueue a job'
do
expect
(
described_class
).
not_to
receive
(
:perform_async
)
expect
{
enqueue!
}.
to
raise_error
(
described_class
::
SanityCheckError
)
expect
{
enqueue!
}.
to
raise_error
(
described_class
::
SanityCheckError
)
end
end
end
end
describe
'.sanity_check!'
do
shared_examples
'raises a SanityCheckError'
do
let
(
:mount_point
)
{
nil
}
describe
'.sanity_check!'
do
shared_examples
'raises a SanityCheckError'
do
let
(
:mount_point
)
{
nil
}
it
do
expect
{
described_class
.
sanity_check!
(
uploads
,
model_class
,
mount_point
)
}
.
to
raise_error
(
described_class
::
SanityCheckError
)
it
do
expect
{
described_class
.
sanity_check!
(
uploads
,
model_class
,
mount_point
)
}
.
to
raise_error
(
described_class
::
SanityCheckError
)
end
end
end
context
'uploader types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
uploader:
'FileUploader'
)
}
before
do
stub_const
(
"WrongModel"
,
Class
.
new
)
end
include_examples
'raises a SanityCheckError'
end
context
'uploader types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
uploader:
'GitlabUploader'
)
}
context
'model types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
model_type:
'Potato'
)
}
include_examples
'raises a SanityCheckError'
end
include_examples
'raises a SanityCheckError'
end
context
'model types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
model_type:
'WrongModel'
)
}
context
'mount point not found'
do
include_examples
'raises a SanityCheckError'
do
let
(
:mount_point
)
{
:potato
}
include_examples
'raises a SanityCheckError'
end
end
end
describe
'#perform
'
do
def
perform
described_class
.
new
.
perform
(
uploads
.
ids
,
model_class
.
to_s
,
mounted_as
,
to_store
)
rescue
ObjectStorage
::
MigrateUploadsWorker
::
Report
::
MigrationFailures
# swallow
context
'mount point not found
'
do
include_examples
'raises a SanityCheckError'
do
let
(
:mount_point
)
{
:potato
}
end
end
end
shared_examples
'outputs correctly'
do
|
success:
0
,
failures:
0
|
total
=
success
+
failures
describe
'#perform'
do
def
perform
described_class
.
new
.
perform
(
uploads
.
ids
,
model_class
.
to_s
,
mounted_as
,
to_store
)
rescue
ObjectStorage
::
MigrateUploadsWorker
::
Report
::
MigrationFailures
# swallow
end
shared_examples
'outputs correctly'
do
|
success:
0
,
failures:
0
|
total
=
success
+
failures
if
success
>
0
it
'outputs the reports'
do
expect
(
Rails
.
logger
).
to
receive
(
:info
).
with
(
%r{Migrated
#{
success
}
/
#{
total
}
files}
)
if
success
>
0
it
'outputs the reports'
do
expect
(
Rails
.
logger
).
to
receive
(
:info
).
with
(
%r{Migrated
#{
success
}
/
#{
total
}
files}
)
perform
perform
end
end
end
if
failures
>
0
it
'outputs upload failures'
do
expect
(
Rails
.
logger
).
to
receive
(
:warn
).
with
(
/Error .* I am a teapot/
)
if
failures
>
0
it
'outputs upload failures'
do
expect
(
Rails
.
logger
).
to
receive
(
:warn
).
with
(
/Error .* I am a teapot/
)
perform
perform
end
end
end
end
it_behaves_like
'outputs correctly'
,
success:
10
it_behaves_like
'outputs correctly'
,
success:
10
it
'migrates files'
do
perform
it
'migrates files'
do
perform
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
0
)
end
aggregate_failures
do
projects
.
each
do
|
project
|
expect
(
project
.
reload
.
avatar
.
upload
.
local?
).
to
be_falsey
context
'migration is unsuccessful'
do
before
do
allow_any_instance_of
(
ObjectStorage
::
Concern
)
.
to
receive
(
:migrate!
).
and_raise
(
CarrierWave
::
UploadError
,
"I am a teapot."
)
end
it_behaves_like
'outputs correctly'
,
failures:
10
end
end
end
context
'migration is unsuccessful'
do
before
do
allow_any_instance_of
(
ObjectStorage
::
Concern
).
to
receive
(
:migrate!
).
and_raise
(
CarrierWave
::
UploadError
,
"I am a teapot."
)
end
context
"for AvatarUploader"
do
let!
(
:projects
)
{
create_list
(
:project
,
10
,
:with_avatar
)
}
let
(
:mounted_as
)
{
:avatar
}
it_behaves_like
'outputs correctly'
,
failures:
10
before
do
stub_uploads_object_storage
(
AvatarUploader
)
end
it_behaves_like
"correctly"
end
context
"for FileUploader"
do
let!
(
:projects
)
{
create_list
(
:project
,
10
)
}
let
(
:secret
)
{
SecureRandom
.
hex
}
let
(
:mounted_as
)
{
nil
}
before
do
stub_uploads_object_storage
(
FileUploader
)
projects
.
map
do
|
project
|
uploader
=
FileUploader
.
new
(
project
)
uploader
.
store!
(
fixture_file_upload
(
'spec/fixtures/doc_sample.txt'
))
end
end
it_behaves_like
"correctly"
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录