Commit ce1893e3 authored by nmilojevic1's avatar nmilojevic1

Add specs for object_storage

- Extract to shared context and shared example
- Fix import_rake_task_spec
- Rescue Exception in export_rake task
parent beff16e3
......@@ -22,6 +22,8 @@ module Gitlab
return error(project.import_export_shared.errors.join(', ')) if project.import_export_shared.errors.any?
success('Done!')
rescue Gitlab::ImportExport::Error => e
error(e.message)
end
private
......
......@@ -10,6 +10,7 @@ describe Gitlab::ImportExport::Project::ExportTask do
let(:file_path) { 'spec/fixtures/gitlab/import_export/test_project_export.tar.gz' }
let(:project) { create(:project, creator: user, namespace: user.namespace) }
let(:project_name) { project.name }
let(:rake_task) { described_class.new(task_params) }
let(:task_params) do
{
......@@ -21,7 +22,7 @@ describe Gitlab::ImportExport::Project::ExportTask do
}
end
subject { described_class.new(task_params).export }
subject { rake_task.export }
context 'when project is found' do
let(:project) { create(:project, creator: user, namespace: user.namespace) }
......@@ -29,9 +30,13 @@ describe Gitlab::ImportExport::Project::ExportTask do
around do |example|
example.run
ensure
File.delete(file_path)
File.delete(file_path) if File.exists?(file_path)
end
include_context 'rake task object storage shared context'
it_behaves_like 'rake task with disabled object_storage', ::Projects::ImportExport::ExportService, :success
it 'performs project export successfully' do
expect { subject }.to output(/Done!/).to_stdout
......@@ -39,8 +44,6 @@ describe Gitlab::ImportExport::Project::ExportTask do
expect(File).to exist(file_path)
end
it_behaves_like 'measurable'
end
context 'when project is not found' do
......@@ -66,4 +69,32 @@ describe Gitlab::ImportExport::Project::ExportTask do
expect(subject).to eq(false)
end
end
context 'when after export strategy fails' do
before do
allow_next_instance_of(Gitlab::ImportExport::AfterExportStrategies::MoveFileStrategy) do |after_export_strategy|
allow(after_export_strategy).to receive(:strategy_execute).and_raise(Gitlab::ImportExport::AfterExportStrategies::BaseAfterExportStrategy::StrategyError)
end
end
it 'error is logged' do
expect(rake_task).to receive(:error).and_call_original
expect(subject).to eq(false)
end
end
context 'when saving services fail' do
before do
allow_next_instance_of(::Projects::ImportExport::ExportService) do |service|
allow(service).to receive(:execute).and_raise(Gitlab::ImportExport::Error)
end
end
it 'error is logged' do
expect(rake_task).to receive(:error).and_call_original
expect(subject).to eq(false)
end
end
end
......@@ -8,7 +8,7 @@ describe Gitlab::ImportExport::Project::ImportTask, :request_store do
let!(:user) { create(:user, username: username) }
let(:measurement_enabled) { false }
let(:project) { Project.find_by_full_path("#{namespace_path}/#{project_name}") }
let(:import_task) { described_class.new(task_params) }
let(:rake_task) { described_class.new(task_params) }
let(:task_params) do
{
username: username,
......@@ -19,29 +19,16 @@ describe Gitlab::ImportExport::Project::ImportTask, :request_store do
}
end
before do
allow(Settings.uploads.object_store).to receive(:[]=).and_call_original
end
around do |example|
old_direct_upload_setting = Settings.uploads.object_store['direct_upload']
old_background_upload_setting = Settings.uploads.object_store['background_upload']
Settings.uploads.object_store['direct_upload'] = true
Settings.uploads.object_store['background_upload'] = true
example.run
Settings.uploads.object_store['direct_upload'] = old_direct_upload_setting
Settings.uploads.object_store['background_upload'] = old_background_upload_setting
end
subject { import_task.import }
subject { rake_task.import }
context 'when project import is valid' do
let(:project_name) { 'import_rake_test_project' }
let(:file_path) { 'spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz' }
include_context 'rake task object storage shared context'
it_behaves_like 'rake task with disabled object_storage', ::Projects::GitlabProjectsImportService, :execute_sidekiq_job
it 'performs project import successfully' do
expect { subject }.to output(/Done!/).to_stdout
expect { subject }.not_to raise_error
......@@ -53,28 +40,6 @@ describe Gitlab::ImportExport::Project::ImportTask, :request_store do
expect(project.import_state.status).to eq('finished')
end
it 'disables direct & background upload only during project creation' do
expect_next_instance_of(Projects::GitlabProjectsImportService) do |service|
expect(service).to receive(:execute).and_wrap_original do |m|
expect(Settings.uploads.object_store['background_upload']).to eq(false)
expect(Settings.uploads.object_store['direct_upload']).to eq(false)
m.call
end
end
expect(import_task).to receive(:execute_sidekiq_job).and_wrap_original do |m|
expect(Settings.uploads.object_store['background_upload']).to eq(true)
expect(Settings.uploads.object_store['direct_upload']).to eq(true)
expect(Settings.uploads.object_store).not_to receive(:[]=).with('backgroud_upload', false)
expect(Settings.uploads.object_store).not_to receive(:[]=).with('direct_upload', false)
m.call
end
subject
end
it_behaves_like 'measurable'
end
......
# frozen_string_literal: true
RSpec.shared_context 'rake task object storage shared context' do
before do
allow(Settings.uploads.object_store).to receive(:[]=).and_call_original
end
around do |example|
old_direct_upload_setting = Settings.uploads.object_store['direct_upload']
old_background_upload_setting = Settings.uploads.object_store['background_upload']
Settings.uploads.object_store['direct_upload'] = true
Settings.uploads.object_store['background_upload'] = true
example.run
Settings.uploads.object_store['direct_upload'] = old_direct_upload_setting
Settings.uploads.object_store['background_upload'] = old_background_upload_setting
end
end
# frozen_string_literal: true
RSpec.shared_examples 'rake task with disabled object_storage' do |service_class, method|
it 'disables direct & background upload only for service call' do
expect_next_instance_of(service_class) do |service|
expect(service).to receive(:execute).and_wrap_original do |m|
expect(Settings.uploads.object_store['background_upload']).to eq(false)
expect(Settings.uploads.object_store['direct_upload']).to eq(false)
m.call
end
end
expect(rake_task).to receive(method).and_wrap_original do |m, *args|
expect(Settings.uploads.object_store['background_upload']).to eq(true)
expect(Settings.uploads.object_store['direct_upload']).to eq(true)
expect(Settings.uploads.object_store).not_to receive(:[]=).with('backgroud_upload', false)
expect(Settings.uploads.object_store).not_to receive(:[]=).with('direct_upload', false)
m.call(*args)
end
subject
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment