-
Notifications
You must be signed in to change notification settings - Fork 2
/
moab_to_catalog_spec.rb
242 lines (210 loc) · 9.16 KB
/
moab_to_catalog_spec.rb
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
require 'rails_helper'
require_relative "../../../lib/audit/moab_to_catalog.rb"
RSpec.describe MoabToCatalog do
let(:storage_dir) { 'spec/fixtures/storage_root01/moab_storage_trunk' }
let(:endpoint) { Endpoint.find_by!(storage_location: storage_dir) }
before do
PreservationPolicy.seed_from_config
end
describe "#check_existence_from_disk" do
let(:m2c) { described_class.new }
it 'calls check_existence once per storage root' do
expect(described_class).to receive(:check_existence).exactly(Settings.moab.storage_roots.count).times
m2c.check_existence_from_disk
end
it 'calls check_existence with the right arguments' do
Settings.moab.storage_roots.each do |storage_root|
expect(described_class).to receive(:check_existence).with("#{storage_root[1]}/#{Settings.moab.storage_trunk}")
end
m2c.check_existence_from_disk
end
end
describe "#seed_from_disk" do
let(:m2c) { described_class.new }
it 'calls seed_catalog once per storage root' do
expect(described_class).to receive(:seed_catalog).exactly(Settings.moab.storage_roots.count).times
m2c.seed_from_disk
end
it 'calls seed_catalog with the right arguments' do
Settings.moab.storage_roots.each do |storage_root|
expect(described_class).to receive(:seed_catalog).with("#{storage_root[1]}/#{Settings.moab.storage_trunk}")
end
m2c.seed_from_disk
end
end
describe "#seed_from_disk_with_profiling" do
let(:m2c) { described_class.new }
it "spins up a profiler, calling profiling and printing methods on it" do
mock_profiler = instance_double(Profiler)
expect(Profiler).to receive(:new).and_return(mock_profiler)
expect(mock_profiler).to receive(:prof)
expect(mock_profiler).to receive(:print_results_flat)
m2c.seed_from_disk_with_profiling
end
end
describe ".check_existence" do
let(:subject) { described_class.check_existence(storage_dir, true) }
it "calls 'find_moab_paths' with appropriate argument" do
expect(Stanford::MoabStorageDirectory).to receive(:find_moab_paths).with(storage_dir)
subject
end
it 'gets moab current version from Moab::StorageObject' do
moab = instance_double(Moab::StorageObject)
allow(moab).to receive(:storage_root=)
allow(moab).to receive(:object_pathname).and_return(storage_dir)
allow(moab).to receive(:size)
expect(moab).to receive(:current_version_id).at_least(1).times
allow(Moab::StorageObject).to receive(:new).and_return(moab)
expect(Moab::StorageServices).not_to receive(:new)
subject
end
it 'gets moab size from Moab::StorageObject' do
moab = instance_double(Moab::StorageObject)
allow(moab).to receive(:storage_root=)
allow(moab).to receive(:object_pathname).and_return(storage_dir)
allow(moab).to receive(:current_version_id)
expect(moab).to receive(:size).at_least(1).times
allow(Moab::StorageObject).to receive(:new).and_return(moab)
expect(Moab::StorageServices).not_to receive(:new)
subject
end
context "(calls create or confirm_version)" do
let(:expected_argument_list) do
[
{ druid: 'bj102hs9687', storage_root_current_version: 3 },
{ druid: 'bz514sm9647', storage_root_current_version: 3 },
{ druid: 'jj925bx9565', storage_root_current_version: 2 }
]
end
before do
expected_argument_list.each do |arg_hash|
po_handler = instance_double('PreservedObjectHandler')
arg_hash[:po_handler] = po_handler
allow(PreservedObjectHandler).to receive(:new).with(
arg_hash[:druid],
arg_hash[:storage_root_current_version],
instance_of(Integer),
endpoint
).and_return(po_handler)
end
end
context 'object does not exist' do
it 'calls #create when expect_to_create is true' do
expected_argument_list.each do |arg_hash|
expect(PreservedObject).to receive(:exists?).with(druid: arg_hash[:druid]).and_return(false)
exp_msg = "druid: #{arg_hash[:druid]} expected to exist in catalog but was not found"
expect(Rails.logger).to receive(:error).with(exp_msg)
expect(arg_hash[:po_handler]).to receive(:create)
end
described_class.check_existence(storage_dir, true)
end
it 'does not call #create when expect_to_create is false' do
expected_argument_list.each do |arg_hash|
expect(PreservedObject).to receive(:exists?).with(druid: arg_hash[:druid]).and_return(false)
exp_msg = "druid: #{arg_hash[:druid]} expected to exist in catalog but was not found"
expect(Rails.logger).to receive(:error).with(exp_msg)
expect(arg_hash[:po_handler]).not_to receive(:create)
end
described_class.check_existence(storage_dir) # expect_to_create is false by default
end
end
it "calls #confirm_version if object exists" do
expected_argument_list.each do |arg_hash|
expect(PreservedObject).to receive(:exists?).with(druid: arg_hash[:druid]).and_return(true)
expect(arg_hash[:po_handler]).to receive(:confirm_version)
end
subject
end
end
it "return correct number of results" do
expect(subject.count).to eq 3
end
it "storage directory doesn't exist (misspelling, read write permissions)" do
allow(Endpoint).to receive(:find_by!).and_return(instance_double(Endpoint))
expect { described_class.check_existence('spec/fixtures/moab_strge_root') }.to raise_error(
SystemCallError, /No such file or directory/
)
end
it "storage directory exists but it is empty" do
storage_dir = 'spec/fixtures/empty/moab_storage_trunk'
expect(described_class.check_existence(storage_dir)).to eq []
end
end
describe ".seed_catalog" do
let(:subject) { described_class.seed_catalog(storage_dir) }
it "calls 'find_moab_paths' with appropriate argument" do
expect(Stanford::MoabStorageDirectory).to receive(:find_moab_paths).with(storage_dir)
subject
end
it 'gets moab current version from Moab::StorageObject' do
moab = instance_double(Moab::StorageObject)
allow(moab).to receive(:storage_root=)
allow(moab).to receive(:object_pathname).and_return(storage_dir)
allow(moab).to receive(:size)
expect(moab).to receive(:current_version_id).at_least(1).times
allow(Moab::StorageObject).to receive(:new).and_return(moab)
expect(Moab::StorageServices).not_to receive(:new)
subject
end
it 'gets moab size from Moab::StorageObject' do
moab = instance_double(Moab::StorageObject)
allow(moab).to receive(:storage_root=)
allow(moab).to receive(:object_pathname).and_return(storage_dir)
allow(moab).to receive(:current_version_id)
expect(moab).to receive(:size).at_least(1).times
allow(Moab::StorageObject).to receive(:new).and_return(moab)
expect(Moab::StorageServices).not_to receive(:new)
subject
end
context "(creates or errors)" do
let(:expected_argument_list) do
[
{ druid: 'bj102hs9687', storage_root_current_version: 3 },
{ druid: 'bz514sm9647', storage_root_current_version: 3 },
{ druid: 'jj925bx9565', storage_root_current_version: 2 }
]
end
before do
expected_argument_list.each do |arg_hash|
po_handler = instance_double('PreservedObjectHandler')
arg_hash[:po_handler] = po_handler
allow(PreservedObjectHandler).to receive(:new).with(
arg_hash[:druid],
arg_hash[:storage_root_current_version],
instance_of(Integer),
endpoint
).and_return(po_handler)
end
end
it "call #create_with_validation if object does not exist" do
expected_argument_list.each do |arg_hash|
expect(PreservedObject).to receive(:exists?).with(druid: arg_hash[:druid]).and_return(false)
expect(arg_hash[:po_handler]).to receive(:create_with_validation)
end
subject
end
it "error if object exists" do
expected_argument_list.each do |arg_hash|
allow(PreservedObject).to receive(:exists?).with(druid: arg_hash[:druid]).and_return(true)
exp_msg = "druid: #{arg_hash[:druid]} NOT expected to exist in catalog but was found"
expect(Rails.logger).to receive(:error).with(exp_msg)
expect(arg_hash[:po_handler]).not_to receive(:create_with_validation)
end
subject
end
end
it "return correct number of results" do
expect(subject.count).to eq 3
end
it "storage directory doesn't exist (misspelling, read write permissions)" do
allow(Endpoint).to receive(:find_by!).and_return(instance_double(Endpoint))
expect { described_class.check_existence('spec/fixtures/moab_strge_root') }.to raise_error(
SystemCallError, /No such file or directory/
)
end
it "storage directory exists but it is empty" do
storage_dir = 'spec/fixtures/empty/moab_storage_trunk'
expect(described_class.check_existence(storage_dir)).to eq []
end
end
end