/
ftp_spec.rb
271 lines (228 loc) · 8.9 KB
/
ftp_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# encoding: utf-8
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Storage::FTP do
let(:model) { Backup::Model.new(:test_trigger, 'test label') }
let(:storage) do
Backup::Storage::FTP.new(model) do |ftp|
ftp.username = 'my_username'
ftp.password = 'my_password'
ftp.ip = '123.45.678.90'
ftp.keep = 5
end
end
it 'should be a subclass of Storage::Base' do
Backup::Storage::FTP.
superclass.should == Backup::Storage::Base
end
describe '#initialize' do
after { Backup::Storage::FTP.clear_defaults! }
it 'should load pre-configured defaults through Base' do
Backup::Storage::FTP.any_instance.expects(:load_defaults!)
storage
end
it 'should pass the model reference to Base' do
storage.instance_variable_get(:@model).should == model
end
it 'should pass the storage_id to Base' do
storage = Backup::Storage::FTP.new(model, 'my_storage_id')
storage.storage_id.should == 'my_storage_id'
end
it 'should remove any preceeding tilde and slash from the path' do
storage = Backup::Storage::FTP.new(model) do |ftp|
ftp.path = '~/my_backups/path'
end
storage.path.should == 'my_backups/path'
end
context 'when no pre-configured defaults have been set' do
it 'should use the values given' do
storage.username.should == 'my_username'
storage.password.should == 'my_password'
storage.ip.should == '123.45.678.90'
storage.port.should == 21
storage.path.should == 'backups'
storage.passive_mode.should == false
storage.storage_id.should be_nil
storage.keep.should == 5
end
it 'should use default values if none are given' do
storage = Backup::Storage::FTP.new(model)
storage.username.should be_nil
storage.password.should be_nil
storage.ip.should be_nil
storage.port.should == 21
storage.path.should == 'backups'
storage.passive_mode.should == false
storage.storage_id.should be_nil
storage.keep.should be_nil
end
end # context 'when no pre-configured defaults have been set'
context 'when pre-configured defaults have been set' do
before do
Backup::Storage::FTP.defaults do |s|
s.username = 'some_username'
s.password = 'some_password'
s.ip = 'some_ip'
s.port = 'some_port'
s.path = 'some_path'
s.passive_mode = 'some_passive_mode'
s.keep = 'some_keep'
end
end
it 'should use pre-configured defaults' do
storage = Backup::Storage::FTP.new(model)
storage.username.should == 'some_username'
storage.password.should == 'some_password'
storage.ip.should == 'some_ip'
storage.port.should == 'some_port'
storage.path.should == 'some_path'
storage.passive_mode.should == 'some_passive_mode'
storage.storage_id.should be_nil
storage.keep.should == 'some_keep'
end
it 'should override pre-configured defaults' do
storage = Backup::Storage::FTP.new(model) do |s|
s.username = 'new_username'
s.password = 'new_password'
s.ip = 'new_ip'
s.port = 'new_port'
s.path = 'new_path'
s.passive_mode = 'new_passive_mode'
s.keep = 'new_keep'
end
storage.username.should == 'new_username'
storage.password.should == 'new_password'
storage.ip.should == 'new_ip'
storage.port.should == 'new_port'
storage.path.should == 'new_path'
storage.passive_mode.should == 'new_passive_mode'
storage.storage_id.should be_nil
storage.keep.should == 'new_keep'
end
end # context 'when pre-configured defaults have been set'
end # describe '#initialize'
describe '#connection' do
let(:connection) { mock }
it 'should yield a connection to the remote server' do
Net::FTP.expects(:open).with(
'123.45.678.90', 'my_username', 'my_password'
).yields(connection)
storage.send(:connection) do |ftp|
ftp.should be(connection)
end
end
it 'should set passive mode if @passive_mode is true' do
storage.passive_mode = true
Net::FTP.expects(:open).with(
'123.45.678.90', 'my_username', 'my_password'
).yields(connection)
connection.expects(:passive=).with(true)
storage.send(:connection) do |ftp|
ftp.should be(connection)
end
end
it 'should set the Net::FTP_PORT constant' do
storage.port = 40
Net::FTP.expects(:const_defined?).with(:FTP_PORT).returns(true)
Net::FTP.expects(:send).with(:remove_const, :FTP_PORT)
Net::FTP.expects(:send).with(:const_set, :FTP_PORT, 40)
Net::FTP.expects(:open)
storage.send(:connection)
end
end # describe '#connection'
describe '#transfer!' do
let(:connection) { mock }
let(:package) { mock }
let(:s) { sequence '' }
before do
storage.instance_variable_set(:@package, package)
storage.stubs(:storage_name).returns('Storage::FTP')
storage.stubs(:local_path).returns('/local/path')
storage.stubs(:connection).yields(connection)
end
it 'should transfer the package files' do
storage.expects(:remote_path_for).in_sequence(s).with(package).
returns('remote/path')
storage.expects(:create_remote_path).in_sequence(s).with(
'remote/path', connection
)
storage.expects(:files_to_transfer_for).in_sequence(s).with(package).
multiple_yields(
['2011.12.31.11.00.02.backup.tar.enc-aa', 'backup.tar.enc-aa'],
['2011.12.31.11.00.02.backup.tar.enc-ab', 'backup.tar.enc-ab']
)
# first yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::FTP started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc-aa' to '123.45.678.90'."
)
connection.expects(:put).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc-aa'),
File.join('remote/path', 'backup.tar.enc-aa')
)
# second yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::FTP started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc-ab' to '123.45.678.90'."
)
connection.expects(:put).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc-ab'),
File.join('remote/path', 'backup.tar.enc-ab')
)
storage.send(:transfer!)
end
end # describe '#transfer!'
describe '#remove!' do
let(:package) { mock }
let(:connection) { mock }
let(:s) { sequence '' }
before do
storage.stubs(:storage_name).returns('Storage::FTP')
storage.stubs(:connection).yields(connection)
end
it 'should remove the package files' do
storage.expects(:remote_path_for).in_sequence(s).with(package).
returns('remote/path')
storage.expects(:transferred_files_for).in_sequence(s).with(package).
multiple_yields(
['2011.12.31.11.00.02.backup.tar.enc-aa', 'backup.tar.enc-aa'],
['2011.12.31.11.00.02.backup.tar.enc-ab', 'backup.tar.enc-ab']
)
# first yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::FTP started removing " +
"'2011.12.31.11.00.02.backup.tar.enc-aa' from '123.45.678.90'."
)
connection.expects(:delete).in_sequence(s).with(
File.join('remote/path', 'backup.tar.enc-aa')
)
# second yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::FTP started removing " +
"'2011.12.31.11.00.02.backup.tar.enc-ab' from '123.45.678.90'."
)
connection.expects(:delete).in_sequence(s).with(
File.join('remote/path', 'backup.tar.enc-ab')
)
connection.expects(:rmdir).with('remote/path').in_sequence(s)
storage.send(:remove!, package)
end
end # describe '#remove!'
describe '#create_remote_path' do
let(:connection) { mock }
let(:remote_path) { 'backups/folder/another_folder' }
let(:s) { sequence '' }
context 'while properly creating remote directories one by one' do
it 'should rescue any FTPPermErrors and continue' do
connection.expects(:mkdir).in_sequence(s).
with("backups").raises(Net::FTPPermError)
connection.expects(:mkdir).in_sequence(s).
with("backups/folder")
connection.expects(:mkdir).in_sequence(s).
with("backups/folder/another_folder")
expect do
storage.send(:create_remote_path, remote_path, connection)
end.not_to raise_error
end
end
end
end