dump 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -15
  2. data/.rubocop_todo.yml +1 -1
  3. data/Gemfile +6 -1
  4. data/LICENSE.txt +1 -1
  5. data/README.markdown +2 -2
  6. data/dump.gemspec +2 -2
  7. data/lib/dump.rb +86 -2
  8. data/lib/{dump_rake/archive_tar_minitar_fix.rb → dump/archive_tar_minitar.rb} +0 -0
  9. data/lib/{dump_rake → dump}/assets.rb +6 -4
  10. data/lib/dump/capistrano/v2.rb +34 -34
  11. data/lib/{dump_rake → dump}/continious_timeout.rb +1 -1
  12. data/lib/{dump_rake → dump}/env.rb +4 -4
  13. data/lib/{dump_rake → dump}/env/filter.rb +1 -1
  14. data/lib/dump/rails_root.rb +19 -0
  15. data/lib/{dump_rake/dump_reader.rb → dump/reader.rb} +25 -17
  16. data/lib/{dump_rake/dump.rb → dump/snapshot.rb} +9 -5
  17. data/lib/{dump_rake → dump}/table_manipulation.rb +28 -14
  18. data/lib/{dump_rake/dump_writer.rb → dump/writer.rb} +13 -5
  19. data/lib/tasks/assets.rake +4 -4
  20. data/lib/tasks/dump.rake +10 -10
  21. data/script/update_readme +3 -3
  22. data/spec/cycle_spec.rb +78 -84
  23. data/spec/{lib/dump_rake → dump}/env/filter_spec.rb +14 -14
  24. data/spec/dump/env_spec.rb +139 -0
  25. data/spec/{lib/dump_rake → dump}/rails_root_spec.rb +11 -13
  26. data/spec/{lib/dump_rake/dump_reader_spec.rb → dump/reader_spec.rb} +89 -89
  27. data/spec/dump/snapshot_spec.rb +290 -0
  28. data/spec/{lib/dump_rake → dump}/table_manipulation_spec.rb +54 -55
  29. data/spec/{lib/dump_rake/dump_writer_spec.rb → dump/writer_spec.rb} +41 -42
  30. data/spec/dump_spec.rb +327 -0
  31. data/spec/recipes/dump_spec.rb +92 -93
  32. data/spec/spec_helper.rb +0 -3
  33. data/spec/tasks/assets_spec.rb +16 -15
  34. data/spec/tasks/dump_spec.rb +30 -29
  35. metadata +75 -98
  36. data/.autotest +0 -13
  37. data/lib/dump_rake.rb +0 -94
  38. data/lib/dump_rake/rails_root.rb +0 -13
  39. data/spec/lib/dump_rake/dump_spec.rb +0 -289
  40. data/spec/lib/dump_rake/env_spec.rb +0 -139
  41. data/spec/lib/dump_rake_spec.rb +0 -326
  42. data/spec/spec.opts +0 -4
@@ -1,39 +1,39 @@
1
1
  require 'spec_helper'
2
- require 'dump_rake'
2
+ require 'dump/writer'
3
3
 
4
- DumpWriter = DumpRake::DumpWriter
5
- describe DumpWriter do
4
+ Writer = Dump::Writer
5
+ describe Writer do
6
6
  describe 'create' do
7
- it 'should create selves instance and open' do
7
+ it 'creates instance and open' do
8
8
  @dump = double('dump')
9
9
  expect(@dump).to receive(:open)
10
- expect(DumpWriter).to receive(:new).with('/abc/123.tmp').and_return(@dump)
11
- DumpWriter.create('/abc/123.tmp')
10
+ expect(described_class).to receive(:new).with('/abc/123.tmp').and_return(@dump)
11
+ described_class.create('/abc/123.tmp')
12
12
  end
13
13
 
14
- it 'should call dump subroutines' do
14
+ it 'calls dump subroutines' do
15
15
  @dump = double('dump')
16
16
  allow(@dump).to receive(:open).and_yield(@dump)
17
17
  allow(@dump).to receive(:silence).and_yield
18
- allow(DumpWriter).to receive(:new).and_return(@dump)
18
+ allow(described_class).to receive(:new).and_return(@dump)
19
19
 
20
20
  expect(@dump).to receive(:write_schema).ordered
21
21
  expect(@dump).to receive(:write_tables).ordered
22
22
  expect(@dump).to receive(:write_assets).ordered
23
23
  expect(@dump).to receive(:write_config).ordered
24
24
 
25
- DumpWriter.create('/abc/123.tmp')
25
+ described_class.create('/abc/123.tmp')
26
26
  end
27
27
  end
28
28
 
29
29
  describe 'open' do
30
- it 'should create dir for dump' do
30
+ it 'creates dir for dump' do
31
31
  allow(Zlib::GzipWriter).to receive(:open)
32
32
  expect(FileUtils).to receive(:mkpath).with('/abc/def/ghi')
33
- DumpWriter.new('/abc/def/ghi/123.tgz').open
33
+ described_class.new('/abc/def/ghi/123.tgz').open
34
34
  end
35
35
 
36
- it 'should set stream to gzipped tar writer' do
36
+ it 'sets stream to gzipped tar writer' do
37
37
  allow(FileUtils).to receive(:mkpath)
38
38
  @gzip = double('gzip')
39
39
  @stream = double('stream')
@@ -41,7 +41,7 @@ describe DumpWriter do
41
41
  expect(Archive::Tar::Minitar::Output).to receive(:open).with(@gzip).and_yield(@stream)
42
42
  expect(@gzip).to receive(:mtime=).with(Time.utc(2000))
43
43
 
44
- @dump = DumpWriter.new('123.tgz')
44
+ @dump = described_class.new('123.tgz')
45
45
  expect(@dump).to receive(:lock).and_yield
46
46
  @dump.open do |dump|
47
47
  expect(dump).to eq(@dump)
@@ -55,14 +55,14 @@ describe DumpWriter do
55
55
  @tar = double('tar')
56
56
  @stream = double('stream', :tar => @tar)
57
57
  @config = {:tables => {}}
58
- @dump = DumpWriter.new('123.tgz')
58
+ @dump = described_class.new('123.tgz')
59
59
  allow(@dump).to receive(:stream).and_return(@stream)
60
60
  allow(@dump).to receive(:config).and_return(@config)
61
61
  allow(Progress).to receive(:io).and_return(StringIO.new)
62
62
  end
63
63
 
64
64
  describe 'create_file' do
65
- it 'should create temp file, yield it for writing, create file in tar and write it there' do
65
+ it 'creates temp file, yields it for writing, creates file in tar and writes it there' do
66
66
  @temp = double('temp', :open => true, :length => 6, :read => 'qwfpgj')
67
67
  expect(@temp).to receive(:write).with('qwfpgj')
68
68
  allow(@temp).to receive(:eof?).and_return(false, true)
@@ -81,19 +81,19 @@ describe DumpWriter do
81
81
  end
82
82
 
83
83
  describe 'write_schema' do
84
- it 'should create file schema.rb' do
84
+ it 'creates file schema.rb' do
85
85
  expect(@dump).to receive(:create_file).with('schema.rb')
86
86
  @dump.write_schema
87
87
  end
88
88
 
89
- it 'should set ENV[SCHEMA] to path of returned file' do
89
+ it 'sets ENV[SCHEMA] to path of returned file' do
90
90
  @file = double('file', :path => 'db/schema.rb')
91
91
  allow(@dump).to receive(:create_file).and_yield(@file)
92
- expect(DumpRake::Env).to receive(:with_env).with('SCHEMA' => 'db/schema.rb')
92
+ expect(Dump::Env).to receive(:with_env).with('SCHEMA' => 'db/schema.rb')
93
93
  @dump.write_schema
94
94
  end
95
95
 
96
- it 'should call rake task db:schema:dump' do
96
+ it 'calls rake task db:schema:dump' do
97
97
  @file = double('file', :path => 'db/schema.rb')
98
98
  allow(@dump).to receive(:create_file).and_yield(@file)
99
99
  @task = double('task')
@@ -104,13 +104,13 @@ describe DumpWriter do
104
104
  end
105
105
 
106
106
  describe 'write_tables' do
107
- it 'should verify connection' do
107
+ it 'verifies connection' do
108
108
  allow(@dump).to receive(:tables_to_dump).and_return([])
109
109
  expect(@dump).to receive(:verify_connection)
110
110
  @dump.write_tables
111
111
  end
112
112
 
113
- it 'should call write_table for each table returned by tables_to_dump' do
113
+ it 'calls write_table for each table returned by tables_to_dump' do
114
114
  allow(@dump).to receive(:verify_connection)
115
115
  allow(@dump).to receive(:tables_to_dump).and_return(%w[first second])
116
116
 
@@ -122,20 +122,20 @@ describe DumpWriter do
122
122
  end
123
123
 
124
124
  describe 'write_table' do
125
- it 'should get row count and store it to config' do
125
+ it 'gets row count and store it to config' do
126
126
  expect(@dump).to receive(:table_row_count).with('first').and_return(666)
127
127
  allow(@dump).to receive(:create_file)
128
128
  @dump.write_table('first')
129
129
  expect(@config[:tables]['first']).to eq(666)
130
130
  end
131
131
 
132
- it 'should create_file' do
132
+ it 'create_files' do
133
133
  allow(@dump).to receive(:table_row_count).and_return(666)
134
134
  expect(@dump).to receive(:create_file)
135
135
  @dump.write_table('first')
136
136
  end
137
137
 
138
- it 'should dump column names and values of each row' do
138
+ it 'dumps column names and values of each row' do
139
139
  @column_definitions = [
140
140
  double('column', :name => 'id'),
141
141
  double('column', :name => 'name'),
@@ -171,21 +171,21 @@ describe DumpWriter do
171
171
  allow(@dump).to receive(:assets_root_link).and_yield('/tmp', 'assets')
172
172
  end
173
173
 
174
- it 'should call assets_to_dump' do
174
+ it 'calls assets_to_dump' do
175
175
  expect(@dump).to receive(:assets_to_dump).and_return([])
176
176
  @dump.write_assets
177
177
  end
178
178
 
179
- it 'should change root to rails app root' do
179
+ it 'changes root to rails app root' do
180
180
  @file = double('file')
181
181
  allow(@dump).to receive(:assets_to_dump).and_return(%w[images videos])
182
182
  allow(@dump).to receive(:create_file).and_yield(@file)
183
183
 
184
- expect(Dir).to receive(:chdir).with(DumpRake::RailsRoot)
184
+ expect(Dir).to receive(:chdir).with(Dump.rails_root)
185
185
  @dump.write_assets
186
186
  end
187
187
 
188
- it 'should put assets to config' do
188
+ it 'puts assets to config' do
189
189
  @file = double('file')
190
190
  allow(@dump).to receive(:assets_to_dump).and_return(%w[images/* videos])
191
191
  allow(@dump).to receive(:create_file).and_yield(@file)
@@ -200,7 +200,7 @@ describe DumpWriter do
200
200
  expect(@config[:assets]).to eq({'images/a' => counts, 'images/b' => counts, 'videos' => counts})
201
201
  end
202
202
 
203
- it 'should use glob to find files' do
203
+ it 'uses glob to find files' do
204
204
  @file = double('file')
205
205
  allow(@dump).to receive(:assets_to_dump).and_return(%w[images/* videos])
206
206
  allow(@dump).to receive(:create_file).and_yield(@file)
@@ -216,7 +216,7 @@ describe DumpWriter do
216
216
  @dump.write_assets
217
217
  end
218
218
 
219
- it 'should pack each file from assets_root_link' do
219
+ it 'packs each file from assets_root_link' do
220
220
  @file = double('file')
221
221
  allow(@dump).to receive(:assets_to_dump).and_return(%w[images/* videos])
222
222
  allow(@dump).to receive(:create_file).and_yield(@file)
@@ -234,7 +234,7 @@ describe DumpWriter do
234
234
  @dump.write_assets
235
235
  end
236
236
 
237
- it 'should pack each file' do
237
+ it 'packs each file' do
238
238
  @file = double('file')
239
239
  allow(@dump).to receive(:assets_to_dump).and_return(%w[images/* videos])
240
240
  allow(@dump).to receive(:create_file).and_yield(@file)
@@ -254,7 +254,7 @@ describe DumpWriter do
254
254
  @dump.write_assets
255
255
  end
256
256
 
257
- it 'should not raise if something fails when packing' do
257
+ it 'does not raise if something fails when packing' do
258
258
  @file = double('file')
259
259
  allow(@dump).to receive(:assets_to_dump).and_return(%w[videos])
260
260
  allow(@dump).to receive(:create_file).and_yield(@file)
@@ -272,16 +272,15 @@ describe DumpWriter do
272
272
  @dump.write_assets
273
273
  end
274
274
  end
275
-
276
275
  end
277
276
 
278
277
  describe 'write_config' do
279
- it 'should create file config' do
278
+ it 'creates file config' do
280
279
  expect(@dump).to receive(:create_file).with('config')
281
280
  @dump.write_config
282
281
  end
283
282
 
284
- it 'should dump column names and values of each row' do
283
+ it 'dumps column names and values of each row' do
285
284
  @file = double('file')
286
285
  allow(@dump).to receive(:create_file).and_yield(@file)
287
286
  @config.replace({:tables => {'first' => 1, 'second' => 2}, :assets => %w[images videos]})
@@ -292,34 +291,34 @@ describe DumpWriter do
292
291
  end
293
292
 
294
293
  describe 'assets_to_dump' do
295
- it 'should call rake task assets' do
294
+ it 'calls rake task assets' do
296
295
  @task = double('task')
297
296
  expect(Rake::Task).to receive(:[]).with('assets').and_return(@task)
298
297
  expect(@task).to receive(:invoke)
299
298
  @dump.assets_to_dump
300
299
  end
301
300
 
302
- it 'should return array of assets if separator is colon' do
301
+ it 'returns array of assets if separator is colon' do
303
302
  @task = double('task')
304
303
  allow(Rake::Task).to receive(:[]).and_return(@task)
305
304
  allow(@task).to receive(:invoke)
306
- DumpRake::Env.with_env(:assets => 'images:videos') do
305
+ Dump::Env.with_env(:assets => 'images:videos') do
307
306
  expect(@dump.assets_to_dump).to eq(%w[images videos])
308
307
  end
309
308
  end
310
309
 
311
- it 'should return array of assets if separator is comma' do
310
+ it 'returns array of assets if separator is comma' do
312
311
  @task = double('task')
313
312
  allow(Rake::Task).to receive(:[]).and_return(@task)
314
313
  allow(@task).to receive(:invoke)
315
- DumpRake::Env.with_env(:assets => 'images,videos') do
314
+ Dump::Env.with_env(:assets => 'images,videos') do
316
315
  expect(@dump.assets_to_dump).to eq(%w[images videos])
317
316
  end
318
317
  end
319
318
 
320
- it 'should return empty array if calling rake task assets raises an exception' do
319
+ it 'returns empty array if calling rake task assets raises an exception' do
321
320
  allow(Rake::Task).to receive(:[]).and_raise('task assets not found')
322
- DumpRake::Env.with_env(:assets => 'images:videos') do
321
+ Dump::Env.with_env(:assets => 'images:videos') do
323
322
  expect(@dump.assets_to_dump).to eq([])
324
323
  end
325
324
  end
@@ -0,0 +1,327 @@
1
+ require 'spec_helper'
2
+ require 'dump'
3
+
4
+ describe Dump do
5
+ describe 'versions' do
6
+ it 'calls Snapshot.list if called without version' do
7
+ expect(Dump::Snapshot).to receive(:list).and_return([])
8
+ described_class.versions
9
+ end
10
+
11
+ it 'calls Snapshot.list with options if called with version' do
12
+ expect(Dump::Snapshot).to receive(:list).with(:like => '123').and_return([])
13
+ described_class.versions(:like => '123')
14
+ end
15
+
16
+ it 'prints versions' do
17
+ expect(Dump::Snapshot).to receive(:list).and_return(%w[123.tgz 456.tgz])
18
+ expect(grab_output do
19
+ described_class.versions
20
+ end[:stdout]).to eq("123.tgz\n456.tgz\n")
21
+ end
22
+
23
+ it 'does not show summary if not asked for' do
24
+ dumps = %w[123.tgz 456.tgz].map do |s|
25
+ dump = double("dump_#{s}", :path => double("dump_#{s}_path"))
26
+ expect(Dump::Reader).not_to receive(:summary)
27
+ dump
28
+ end
29
+
30
+ expect(Dump::Snapshot).to receive(:list).and_return(dumps)
31
+ grab_output do
32
+ expect($stderr).not_to receive(:puts)
33
+ described_class.versions
34
+ end
35
+ end
36
+
37
+ it 'shows summary if asked for' do
38
+ dumps = %w[123.tgz 456.tgz].map do |s|
39
+ dump = double("dump_#{s}", :path => double("dump_#{s}_path"))
40
+ expect(Dump::Reader).to receive(:summary).with(dump.path)
41
+ dump
42
+ end
43
+
44
+ expect(Dump::Snapshot).to receive(:list).and_return(dumps)
45
+ grab_output do
46
+ expect($stderr).not_to receive(:puts)
47
+ described_class.versions(:summary => '1')
48
+ end
49
+ end
50
+
51
+ it 'shows summary with scmema if asked for' do
52
+ dumps = %w[123.tgz 456.tgz].map do |s|
53
+ dump = double("dump_#{s}", :path => double("dump_#{s}_path"))
54
+ expect(Dump::Reader).to receive(:summary).with(dump.path, :schema => true)
55
+ dump
56
+ end
57
+
58
+ expect(Dump::Snapshot).to receive(:list).and_return(dumps)
59
+ grab_output do
60
+ expect($stderr).not_to receive(:puts)
61
+ described_class.versions(:summary => '2')
62
+ end
63
+ end
64
+
65
+ it 'shows output to stderr if summary raises error' do
66
+ allow(Dump::Reader).to receive(:summary)
67
+ dumps = %w[123.tgz 456.tgz].map do |s|
68
+ double("dump_#{s}", :path => double("dump_#{s}_path"))
69
+ end
70
+ expect(Dump::Reader).to receive(:summary).with(dumps[1].path).and_raise('terrible error')
71
+
72
+ expect(Dump::Snapshot).to receive(:list).and_return(dumps)
73
+ grab_output do
74
+ allow($stderr).to receive(:puts)
75
+ expect($stderr).to receive(:puts) do |s|
76
+ expect(s['terrible error']).not_to be_nil
77
+ end
78
+ described_class.versions(:summary => 'true')
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'create' do
84
+ describe 'naming' do
85
+ it "creates file in 'rails app root'/dump" do
86
+ allow(File).to receive(:rename)
87
+ expect(Dump::Writer).to receive(:create) do |path|
88
+ expect(File.dirname(path)).to eq(File.join(described_class.rails_root, 'dump'))
89
+ end
90
+ grab_output do
91
+ described_class.create
92
+ end
93
+ end
94
+
95
+ it "creates file with name like 'yyyymmddhhmmss.tmp' when called without description" do
96
+ allow(File).to receive(:rename)
97
+ expect(Dump::Writer).to receive(:create) do |path|
98
+ expect(File.basename(path)).to match(/^\d{14}\.tmp$/)
99
+ end
100
+ grab_output do
101
+ described_class.create
102
+ end
103
+ end
104
+
105
+ it "creates file with name like 'yyyymmddhhmmss-Some text and _.tmp' when called with description 'Some text and !@'" do
106
+ allow(File).to receive(:rename)
107
+ expect(Dump::Writer).to receive(:create) do |path|
108
+ expect(File.basename(path)).to match(/^\d{14}-Some text and _\.tmp$/)
109
+ end
110
+ grab_output do
111
+ described_class.create(:desc => 'Some text and !@')
112
+ end
113
+ end
114
+
115
+ it "creates file with name like 'yyyymmddhhmmss@super tag,second.tmp' when called with description 'Some text and !@'" do
116
+ allow(File).to receive(:rename)
117
+ expect(Dump::Writer).to receive(:create) do |path|
118
+ expect(File.basename(path)).to match(/^\d{14}-Some text and _\.tmp$/)
119
+ end
120
+ grab_output do
121
+ described_class.create(:desc => 'Some text and !@')
122
+ end
123
+ end
124
+
125
+ it 'renames file after creating' do
126
+ expect(File).to receive(:rename) do |tmp_path, tgz_path|
127
+ expect(File.basename(tmp_path)).to match(/^\d{14}-Some text and _\.tmp$/)
128
+ expect(File.basename(tgz_path)).to match(/^\d{14}-Some text and _\.tgz$/)
129
+ end
130
+ allow(Dump::Writer).to receive(:create)
131
+ grab_output do
132
+ described_class.create(:desc => 'Some text and !@')
133
+ end
134
+ end
135
+
136
+ it 'outputs file name' do
137
+ allow(File).to receive(:rename)
138
+ allow(Dump::Writer).to receive(:create)
139
+ expect(grab_output do
140
+ described_class.create(:desc => 'Some text and !@')
141
+ end[:stdout]).to match(/^\d{14}-Some text and _\.tgz$/)
142
+ end
143
+ end
144
+
145
+ describe 'writing' do
146
+ it 'dumps schema, tables, assets' do
147
+ allow(File).to receive(:rename)
148
+ @dump = double('dump')
149
+ expect(Dump::Writer).to receive(:create)
150
+
151
+ grab_output do
152
+ described_class.create
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ describe 'restore' do
159
+ describe 'without version' do
160
+ it 'calls Snapshot.list' do
161
+ allow(Dump::Snapshot).to receive(:list)
162
+ expect(Dump::Snapshot).to receive(:list).and_return([])
163
+ grab_output do
164
+ described_class.restore
165
+ end
166
+ end
167
+
168
+ it 'does not call Reader.restore and should call Snapshot.list and output it to $stderr if there are no versions at all' do
169
+ allow(Dump::Snapshot).to receive(:list).and_return([])
170
+ expect(Dump::Reader).not_to receive(:restore)
171
+ all_dumps = double('all_dumps')
172
+ expect(Dump::Snapshot).to receive(:list).with(no_args).and_return(all_dumps)
173
+ grab_output do
174
+ expect($stderr).to receive(:puts).with(kind_of(String))
175
+ expect($stderr).to receive(:puts).with(all_dumps)
176
+ described_class.restore
177
+ end
178
+ end
179
+
180
+ it 'does not call Reader.restore and should call Snapshot.list and output it to $stderr if there are no versions at all' do
181
+ allow(Dump::Snapshot).to receive(:list).and_return([])
182
+ expect(Dump::Reader).not_to receive(:restore)
183
+ all_dumps = double('all_dumps')
184
+ expect(Dump::Snapshot).to receive(:list).with(no_args).and_return(all_dumps)
185
+ grab_output do
186
+ expect($stderr).to receive(:puts).with(kind_of(String))
187
+ expect($stderr).to receive(:puts).with(all_dumps)
188
+ described_class.restore('213')
189
+ end
190
+ end
191
+
192
+ it 'calls Reader.restore if there are versions' do
193
+ @dump = double('dump', :path => 'dump/213.tgz')
194
+ expect(Dump::Snapshot).to receive(:list).once.and_return([@dump])
195
+ expect(Dump::Reader).to receive(:restore).with('dump/213.tgz')
196
+ grab_output do
197
+ expect($stderr).not_to receive(:puts)
198
+ described_class.restore
199
+ end
200
+ end
201
+ end
202
+
203
+ describe 'with version' do
204
+ it 'calls Snapshot.list with options' do
205
+ allow(Dump::Snapshot).to receive(:list)
206
+ expect(Dump::Snapshot).to receive(:list).with(:like => '213').and_return([])
207
+ grab_output do
208
+ described_class.restore(:like => '213')
209
+ end
210
+ end
211
+
212
+ it 'does not call Reader.restore and should call versions if desired version not found' do
213
+ allow(Dump::Snapshot).to receive(:list).and_return([])
214
+ expect(Dump::Reader).not_to receive(:restore)
215
+ all_dumps = double('all_dumps')
216
+ expect(Dump::Snapshot).to receive(:list).with(no_args).and_return(all_dumps)
217
+ grab_output do
218
+ expect($stderr).to receive(:puts).with(kind_of(String))
219
+ expect($stderr).to receive(:puts).with(all_dumps)
220
+ described_class.restore('213')
221
+ end
222
+ end
223
+
224
+ it 'calls Reader.restore if there is desired version' do
225
+ @dump = double('dump', :path => 'dump/213.tgz')
226
+ expect(Dump::Snapshot).to receive(:list).once.and_return([@dump])
227
+ expect(Dump::Reader).to receive(:restore).with('dump/213.tgz')
228
+ expect(described_class).not_to receive(:versions)
229
+ grab_output do
230
+ expect($stderr).not_to receive(:puts)
231
+ described_class.restore(:like => '213')
232
+ end
233
+ end
234
+
235
+ it 'calls Reader.restore on last version if found multiple matching versions' do
236
+ @dump_a = double('dump_a', :path => 'dump/213-a.tgz')
237
+ @dump_b = double('dump_b', :path => 'dump/213-b.tgz')
238
+ expect(Dump::Snapshot).to receive(:list).once.and_return([@dump_a, @dump_b])
239
+ expect(Dump::Reader).to receive(:restore).with('dump/213-b.tgz')
240
+ grab_output do
241
+ expect($stderr).not_to receive(:puts)
242
+ described_class.restore(:like => '213')
243
+ end
244
+ end
245
+ end
246
+ end
247
+
248
+ describe 'cleanup' do
249
+ it 'calls ask for all files in dump dir and for dumps' do
250
+ expect(Dump::Snapshot).to receive(:list).with(:all => true).and_return([])
251
+ expect(Dump::Snapshot).to receive(:list).with({}).and_return([])
252
+ described_class.cleanup
253
+ end
254
+
255
+ it 'calls Snapshot.list with options if called with version and tags' do
256
+ expect(Dump::Snapshot).to receive(:list).with(:like => '123', :tags => 'a,b,c', :all => true).and_return([])
257
+ expect(Dump::Snapshot).to receive(:list).with(:like => '123', :tags => 'a,b,c').and_return([])
258
+ described_class.cleanup(:like => '123', :tags => 'a,b,c')
259
+ end
260
+
261
+ {
262
+ {} => [0..4],
263
+ {:leave => '3'} => [0..6],
264
+ {:leave => '5'} => [0..4],
265
+ {:leave => '9'} => [0],
266
+ {:leave => '10'} => [],
267
+ {:leave => '15'} => [],
268
+ {:leave => 'none'} => [0..9],
269
+ }.each do |options, ids|
270
+ it "calls delete #{ids} dumps when called with #{options}" do
271
+ dumps = %w[a b c d e f g h i j].map do |s|
272
+ double("dump_#{s}", :ext => 'tgz', :path => double("dump_#{s}_path"))
273
+ end
274
+ tmp_dumps = %w[a b c].map do |s|
275
+ double("tmp_dump_#{s}", :ext => 'tmp', :path => double("tmp_dump_#{s}_path"))
276
+ end
277
+ all_dumps = tmp_dumps[0, 1] + dumps[0, 5] + tmp_dumps[1, 1] + dumps[5, 5] + tmp_dumps[2, 1]
278
+
279
+ (dumps.values_at(*ids) + [tmp_dumps[0], tmp_dumps[2]]).each do |dump|
280
+ expect(dump).to receive(:lock).and_yield
281
+ expect(dump.path).to receive(:unlink)
282
+ end
283
+ [tmp_dumps[1]].each do |dump|
284
+ expect(dump).to receive(:lock)
285
+ expect(dump.path).not_to receive(:unlink)
286
+ end
287
+ (dumps - dumps.values_at(*ids)).each do |dump|
288
+ expect(dump).not_to receive(:lock)
289
+ expect(dump.path).not_to receive(:unlink)
290
+ end
291
+
292
+ expect(Dump::Snapshot).to receive(:list).with(hash_including(:all => true)).and_return(all_dumps)
293
+ expect(Dump::Snapshot).to receive(:list).with(hash_not_including(:all => true)).and_return(dumps)
294
+ grab_output do
295
+ described_class.cleanup({:like => '123', :tags => 'a,b,c'}.merge(options))
296
+ end
297
+ end
298
+ end
299
+
300
+ it 'prints to stderr if can not delete dump' do
301
+ dumps = %w[a b c d e f g h i j].map do |s|
302
+ dump = double("dump_#{s}", :ext => 'tgz', :path => double("dump_#{s}_path"))
303
+ allow(dump).to receive(:lock).and_yield
304
+ allow(dump.path).to receive(:unlink)
305
+ dump
306
+ end
307
+
308
+ expect(dumps[3].path).to receive(:unlink).and_raise('Horrible error')
309
+
310
+ allow(Dump::Snapshot).to receive(:list).and_return(dumps)
311
+ grab_output do
312
+ allow($stderr).to receive(:puts)
313
+ expect($stderr).to receive(:puts) do |s|
314
+ expect(s[dumps[3].path.to_s]).not_to be_nil
315
+ expect(s['Horrible error']).not_to be_nil
316
+ end
317
+ described_class.cleanup
318
+ end
319
+ end
320
+
321
+ it "raises if called with :leave which is not a number or 'none'" do
322
+ expect do
323
+ described_class.cleanup(:leave => 'nothing')
324
+ end.to raise_error
325
+ end
326
+ end
327
+ end