1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::Projects::Pipelines::RepositoryBundlePipeline do
let_it_be(:source) { create(:project, :repository) }
let(:portable) { create(:project) }
let(:tmpdir) { Dir.mktmpdir }
let(:bundle_path) { File.join(tmpdir, 'repository.bundle') }
let(:entity) { create(:bulk_import_entity, :project_entity, project: portable, source_full_path: 'test') }
let(:tracker) { create(:bulk_import_tracker, entity: entity) }
let(:context) { BulkImports::Pipeline::Context.new(tracker) }
subject(:pipeline) { described_class.new(context) }
before do
source.repository.bundle_to_disk(bundle_path)
allow(Dir).to receive(:mktmpdir).with('bulk_imports').and_return(tmpdir)
end
after do
FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
end
describe '#run' do
before do
allow(pipeline).to receive(:extract).and_return(BulkImports::Pipeline::ExtractedData.new(data: [bundle_path]))
end
it 'imports repository into destination project and removes tmpdir' do
expect(portable.repository).to receive(:create_from_bundle).with(bundle_path).and_call_original
pipeline.run
expect(portable.repository.exists?).to eq(true)
expect(Dir.exist?(tmpdir)).to eq(false)
end
context 'when something goes wrong during import' do
it 'marks entity as failed' do
allow(pipeline).to receive(:load).and_raise(StandardError)
pipeline.run
expect(entity.failed?).to eq(true)
end
end
end
describe '#extract' do
it 'downloads & extracts repository bundle filepath' do
download_service = instance_double("BulkImports::FileDownloadService")
decompression_service = instance_double("BulkImports::FileDecompressionService")
extraction_service = instance_double("BulkImports::ArchiveExtractionService")
expect(BulkImports::FileDownloadService)
.to receive(:new)
.with(
configuration: context.configuration,
relative_url: "/#{entity.pluralized_name}/test/export_relations/download?relation=repository",
tmpdir: tmpdir,
filename: 'repository.tar.gz')
.and_return(download_service)
expect(BulkImports::FileDecompressionService)
.to receive(:new)
.with(tmpdir: tmpdir, filename: 'repository.tar.gz')
.and_return(decompression_service)
expect(BulkImports::ArchiveExtractionService)
.to receive(:new)
.with(tmpdir: tmpdir, filename: 'repository.tar')
.and_return(extraction_service)
expect(download_service).to receive(:execute)
expect(decompression_service).to receive(:execute)
expect(extraction_service).to receive(:execute)
extracted_data = pipeline.extract(context)
expect(extracted_data.data).to contain_exactly(bundle_path)
end
end
describe '#load' do
before do
allow(pipeline)
.to receive(:extract)
.and_return(BulkImports::Pipeline::ExtractedData.new(data: [bundle_path]))
end
it 'creates repository from bundle' do
expect(portable.repository).to receive(:create_from_bundle).with(bundle_path).and_call_original
pipeline.load(context, bundle_path)
expect(portable.repository.exists?).to eq(true)
end
context 'when file does not exist' do
it 'returns' do
expect(portable.repository).not_to receive(:create_from_bundle)
pipeline.load(context, File.join(tmpdir, 'bogus'))
expect(portable.repository.exists?).to eq(false)
end
end
context 'when path is directory' do
it 'returns' do
expect(portable.repository).not_to receive(:create_from_bundle)
pipeline.load(context, tmpdir)
expect(portable.repository.exists?).to eq(false)
end
end
context 'when path is symlink' do
it 'returns' do
symlink = File.join(tmpdir, 'symlink')
FileUtils.ln_s(File.join(tmpdir, bundle_path), symlink)
expect(portable.repository).not_to receive(:create_from_bundle)
pipeline.load(context, symlink)
expect(portable.repository.exists?).to eq(false)
end
end
context 'when path is not under tmpdir' do
it 'returns' do
expect { pipeline.load(context, '/home/test.txt') }
.to raise_error(StandardError, 'path /home/test.txt is not allowed')
end
end
context 'when path is being traversed' do
it 'raises an error' do
expect { pipeline.load(context, File.join(tmpdir, '..')) }
.to raise_error(Gitlab::Utils::PathTraversalAttackError, 'Invalid path')
end
end
end
describe '#after_run' do
it 'removes tmpdir' do
allow(FileUtils).to receive(:remove_entry).and_call_original
expect(FileUtils).to receive(:remove_entry).with(tmpdir).and_call_original
pipeline.after_run(nil)
expect(Dir.exist?(tmpdir)).to eq(false)
end
context 'when tmpdir does not exist' do
it 'does not attempt to remove tmpdir' do
FileUtils.remove_entry(tmpdir)
expect(FileUtils).not_to receive(:remove_entry).with(tmpdir)
pipeline.after_run(nil)
end
end
end
end
|