smarbs 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,171 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','test')
3
+
4
+ require 'script'
5
+ require 'smarbs_test'
6
+
7
+ class TestBackup < Test::Unit::TestCase;
8
+ include SmarbsTest
9
+
10
+ def setup
11
+ clean
12
+ end
13
+
14
+ def teardown
15
+ clean_all
16
+ end
17
+
18
+ def test_relative
19
+ `mkdir -p /tmp/smarbs/backmeup/inside`
20
+ `touch /tmp/smarbs/backmeup/other`
21
+ `echo "-preferences-
22
+ -end of preferences-" > /tmp/smarbs/smarbs/backup1`
23
+ c = ConfigFile.new("/tmp/smarbs/smarbs/backup1")
24
+ c.src=(["/tmp/smarbs/backmeup"])
25
+ c.dest=("/tmp/smarbs/backedup")
26
+ c.limit=(Size.new("200m"))
27
+ c.dirspace =(Size.new("100m"))
28
+ c.relative = false
29
+ c.write
30
+ assert_success do
31
+ Script.new("/tmp/smarbs/smarbs/")
32
+ end
33
+ c.read
34
+ assert_equal Size.new("100m"), c.dirspace
35
+ assert_equal("inside\nother\n", `ls /tmp/smarbs/backedup/smarbsbackup/*-backup.1/backmeup/`)
36
+
37
+ assert_success do
38
+ Script.new("/tmp/smarbs/smarbs/")
39
+ end
40
+ c.read
41
+ assert_equal Size.new("90m"), c.dirspace
42
+
43
+ assert_success do
44
+ Script.new("/tmp/smarbs/smarbs/")
45
+ end
46
+ c.read
47
+ assert_equal Size.new("81m"), c.dirspace
48
+
49
+ c.dirspace = 0
50
+ c.write
51
+ assert_success do
52
+ Script.new("/tmp/smarbs/smarbs/")
53
+ end
54
+ c.read
55
+ assert_equal Size.new("13516.8"), c.dirspace
56
+
57
+ c.relative = true
58
+ c.write
59
+ assert_success do
60
+ Script.new("/tmp/smarbs/smarbs/")
61
+ end
62
+ assert_equal("inside\nother\n", `ls /tmp/smarbs/backedup/smarbsbackup/*-backup.1/tmp/smarbs/backmeup/`)
63
+
64
+ c.src = ["/tmp/smarbs/backmeup/other"]
65
+ c.write
66
+ assert_success do
67
+ Script.new("/tmp/smarbs/smarbs/")
68
+ end
69
+ assert_equal("other\n", `ls /tmp/smarbs/backedup/smarbsbackup/*-backup.1/tmp/smarbs/backmeup/`)
70
+
71
+ c.execute_before = 'touch /tmp/smarbs/i\ touched\ it'
72
+ c.execute_after = 'touch /tmp/smarbs/i\ touched\ it\ too'
73
+ c.write
74
+
75
+ assert_success do
76
+ Script.new("/tmp/smarbs/smarbs/")
77
+ end
78
+ assert_equal(true, File.file?("/tmp/smarbs/i touched it"))
79
+ assert_equal(true, File.file?("/tmp/smarbs/i touched it too"))
80
+
81
+ c.execute_before = 'ls /im/not/existing'
82
+ c.write
83
+ assert_output("The following error occured: execute_before command 'ls /im/not/existing' was not successful, aborting!
84
+ This was the output generated:
85
+ ls: cannot access /im/not/existing: No such file or directory") do
86
+ Script.new("/tmp/smarbs/smarbs/")
87
+ end
88
+
89
+ c.execute_after = 'ls /im/not/existing/too'
90
+ c.write
91
+ assert_output("The following error occured: execute_after command 'ls /im/not/existing/too' was not successful, aborting!
92
+ This was the output generated:
93
+ ls: cannot access /im/not/existing/too: No such file or directory
94
+
95
+ Also, your backup was aborted: execute_before command 'ls /im/not/existing' was not successful, aborting!
96
+ This was the output generated:
97
+ ls: cannot access /im/not/existing: No such file or directory") do
98
+
99
+ Script.new("/tmp/smarbs/smarbs/")
100
+ end
101
+
102
+ c.execute_before = 'touch /tmp/smarbs/i\ touched\ it'
103
+ c.write
104
+ assert_output("The following error occured: execute_after command 'ls /im/not/existing/too' was not successful, aborting!
105
+ This was the output generated:
106
+ ls: cannot access /im/not/existing/too: No such file or directory") do
107
+ Script.new("/tmp/smarbs/smarbs/")
108
+ end
109
+
110
+ clean
111
+ cf = default_configfile
112
+ cf.oldestfirst = true
113
+ cf.maxbackuplevels = 8
114
+ cf.write
115
+ for i in 1..10
116
+ `mkdir -p /tmp/smarbs/backedup/smarbsbackup/a-backup.#{i}`
117
+ end
118
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
119
+
120
+ assert_success do
121
+ Script.new("/tmp/smarbs/smarbs/")
122
+ end
123
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8])
124
+
125
+ cf.maxbackuplevels = 0
126
+ cf.limit = "1.525m"
127
+ cf.write
128
+ assert_success do
129
+ Script.new("/tmp/smarbs/smarbs/")
130
+ end
131
+ assert_backup_dirs([1, 2, 3, 4, 5, 6])
132
+
133
+ clean
134
+ cf = default_configfile
135
+ for i in 0..3
136
+ `mkdir -p /tmp/smarbs/backedup/smarbsbackup/a-backup.#{i}`
137
+ end
138
+ assert_fail "Aborted backup directory 'a-backup.0' left in /tmp/smarbs/backedup/smarbsbackup/\nMove or delete it to continue!" do
139
+ Script.new("/tmp/smarbs/smarbs")
140
+ end
141
+ assert_backup_dirs([0, 1, 2, 3])
142
+
143
+
144
+ `rm -r /tmp/smarbs/backedup/smarbsbackup/a-backup.0`
145
+ assert_success do
146
+ Script.new("/tmp/smarbs/smarbs")
147
+ end
148
+ assert_backup_dirs([1, 2, 3, 4])
149
+
150
+
151
+ cf.ignore_leftovers=true
152
+ cf.write
153
+ assert_success do
154
+ Script.new("/tmp/smarbs/smarbs")
155
+ end
156
+ assert_backup_dirs([1, 2, 3, 4, 5])
157
+
158
+
159
+ `mkdir -p /tmp/smarbs/backedup/smarbsbackup/a-backup.0`
160
+ assert_success "Warning: Aborted backup directory a-backup.0 detected.\nResuming backup there because of ignore_leftovers option." do
161
+ Script.new("/tmp/smarbs/smarbs")
162
+ end
163
+ assert_backup_dirs([1, 2, 3, 4, 5, 6])
164
+
165
+ assert_not_equal "a-backup.1", SmarbsBackupDir.new("/tmp/smarbs/backedup/smarbsbackup/").files.sort[2]
166
+
167
+ assert_success do
168
+ Script.new("/tmp/smarbs/smarbs")
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,500 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','test')
3
+
4
+ require 'test/unit'
5
+ require 'helpers'
6
+ require 'smarbs_test'
7
+ require 'log'
8
+
9
+ class TestConfigString < Test::Unit::TestCase;
10
+ include SmarbsTest
11
+
12
+ def test_configstring
13
+ # One Value
14
+ cs = ConfigString.new("before", "", "desc1\ndesc2", "do nothing")
15
+ assert_equal "do nothing", cs.value
16
+ assert_equal "desc1\ndesc2\nbefore do nothing", cs.to_s
17
+
18
+ cs.set("do something")
19
+ assert_equal "do something", cs.value
20
+
21
+ assert_raise_message("Only one 'before' allowed!") do
22
+ cs.set(["do 1", "do 2"])
23
+ end
24
+
25
+ assert_raise_message("Only one 'before' allowed!") do
26
+ cs.add("error")
27
+ end
28
+
29
+ cs.clear
30
+ assert_equal "desc1\ndesc2\nbefore do nothing", cs.to_s
31
+
32
+ cs.add("abc")
33
+ assert_equal "abc", cs.value
34
+
35
+ # Many values
36
+ cs = ConfigString.new("before", "list", "desc1\ndesc2", ["do 1", "do 2"])
37
+ assert_equal ["do 1", "do 2"], cs.value
38
+ assert_equal "desc1\ndesc2\nbefore do 1\nbefore do 2", cs.to_s
39
+
40
+ cs.set("do something")
41
+ assert_equal ["do something"], cs.value
42
+
43
+ cs.set(["do 3", "do 4", "do 5"])
44
+ assert_equal ["do 3", "do 4", "do 5"], cs.value
45
+
46
+ cs.add(["do 5","do 7"])
47
+ assert_equal ["do 3", "do 4", "do 5", "do 7"], cs.value
48
+
49
+ assert_raise_message("Only one 'before' allowed!") do
50
+ ConfigString.new("before", "listen,nolist,kelistliste", "", ["do 1","do 2"])
51
+ end
52
+
53
+ assert(ConfigString.new("before", "listen,nolist,list,kelistliste,", "", ["do 1","do 2"]))
54
+
55
+ cs = ConfigString.new("before", "", "desc1\ndesc2", " ")
56
+ assert_equal "desc1\ndesc2\nbefore ", cs.to_s
57
+ end
58
+ end
59
+
60
+ class TestConfigDirectory < Test::Unit::TestCase;
61
+ include SmarbsTest
62
+
63
+ def test_configdirectory
64
+ cs = ConfigDirectory.new("dir", "list", "desc1\ndesc2", " /home/jan/ ")
65
+ assert_equal ["/home/jan"], cs.value
66
+
67
+ cs.add("/ ")
68
+ assert_equal ["/"], cs.value
69
+
70
+ cs.add("/home/jan ")
71
+ assert_equal ["/", "/home/jan"], cs.value
72
+
73
+ cs.set(["/home", "/home2 / "])
74
+ assert_equal ["/home", "/home2"], cs.value
75
+ end
76
+ end
77
+
78
+ class TestConfigSize < Test::Unit::TestCase;
79
+ include SmarbsTest
80
+
81
+ def test_configsize
82
+ cs = ConfigSize.new("size", "", "desc1\ndesc2", "1k")
83
+ assert_instance_of(Size, cs.value)
84
+ assert_equal 1024, cs.value.b
85
+
86
+ cs.set(Size.new("1m"))
87
+ assert_instance_of(Size, cs.value)
88
+ assert_equal 1024*1024, cs.value.b
89
+
90
+ assert_equal cs.to_s, "desc1\ndesc2\nsize 1.0m"
91
+ end
92
+ end
93
+
94
+ class TestConfigBoolean < Test::Unit::TestCase;
95
+ include SmarbsTest
96
+
97
+ def test_configboolean
98
+ cs = ConfigBoolean.new("own", "", "desc", "YES")
99
+ assert_equal true, cs.value
100
+ assert_equal cs.to_s, "desc\nown yes"
101
+
102
+ cs.set(false)
103
+ assert_equal false, cs.value
104
+ assert_equal cs.to_s, "desc\nown no"
105
+
106
+ cs.clear
107
+ cs.add("yes")
108
+ assert_equal cs.value, true
109
+ end
110
+ end
111
+
112
+ class TestConfigInteger < Test::Unit::TestCase;
113
+ include SmarbsTest
114
+
115
+ def test_configinteger
116
+ cs = ConfigInteger.new("num", "", "desc", "3")
117
+ assert_equal 3, cs.value
118
+ assert_equal cs.to_s, "desc\nnum 3"
119
+
120
+ cs.set(9)
121
+ assert_equal 9, cs.value
122
+
123
+ assert_raise_message("Format 33.8 not valid for an integer in 'num'!") do
124
+ cs.set("33.8")
125
+ end
126
+
127
+ cs.set(-9)
128
+ assert_equal(-9, cs.value)
129
+
130
+ cs = ConfigInteger.new("num", "positive", "desc", "3")
131
+ assert_raise_message("'num' must be positive, you set it to -9!") do #TODO all this: configfile errors
132
+ cs.set(-9)
133
+ end
134
+ end
135
+ end
136
+
137
+ class TestConfigExclude < Test::Unit::TestCase;
138
+ include SmarbsTest
139
+
140
+ def test_configexclude
141
+ cs = ConfigExclude.new("exclude", "", "desc", ["this","that"])
142
+ assert_equal ["this", "that"], cs.value
143
+ end
144
+ end
145
+
146
+ class TestSmarbsBackupDir < Test::Unit::TestCase
147
+ include SmarbsTest
148
+
149
+ def setup
150
+ clean
151
+ end
152
+
153
+ def teardown
154
+ clean_all
155
+ end
156
+
157
+ def test_smarbsbackupdir
158
+ `mkdir /tmp/smarbs/smarbsbackup`
159
+ dir=SmarbsBackupDir.new("/tmp/smarbs/smarbsbackup")
160
+ dir.remove_except_one
161
+ assert_equal(dir.files, [])
162
+
163
+ `mkdir /tmp/smarbs/smarbsbackup/1023-backup.20`
164
+ `mkdir /tmp/smarbs/smarbsbackup/1022313-backup.2`
165
+ `mkdir /tmp/smarbs/smarbsbackup/1033-backup.33`
166
+ `mkdir /tmp/smarbs/smarbsbackup/10cc233-backup.238`
167
+ dir=SmarbsBackupDir.new("/tmp/smarbs/smarbsbackup")
168
+ dir.remove_except_one
169
+ assert_equal(dir.files, ["1022313-backup.2"])
170
+
171
+ clean
172
+ `mkdir -p /tmp/smarbs/smarbsbackup`
173
+ `touch /tmp/smarbs/smarbsbackup/232-backup.0`
174
+ assert_raise_message "File '232-backup.0' left in the destination directory /tmp/smarbs/smarbsbackup/!" do
175
+ SmarbsBackupDir.new("/tmp/smarbs/smarbsbackup")
176
+ end
177
+
178
+ clean
179
+ `mkdir -p /tmp/smarbs/smarbsbackup/1023-backup.20`
180
+ `mkdir /tmp/smarbs/smarbsbackup/10233-backup.2`
181
+ `touch /tmp/smarbs/smarbsbackup/2928.txt`
182
+ assert_raise_message "File '2928.txt' left in the destination directory /tmp/smarbs/smarbsbackup/!" do
183
+ SmarbsBackupDir.new("/tmp/smarbs/smarbsbackup")
184
+ end
185
+
186
+ clean
187
+ `mkdir -p /tmp/smarbs/test_directory/alpha`
188
+ assert_raise_message "/tmp/smarbs/test_directory/alpha has wrong format!" do
189
+ SmarbsBackupDir.new("/tmp/smarbs/test_directory")
190
+ end
191
+
192
+ `rm -r /tmp/smarbs/test_directory/alpha`
193
+ `mkdir -p /tmp/smarbs/test_directory/a-backup.`
194
+ assert_raise_message "/tmp/smarbs/test_directory/a-backup. has wrong format!" do
195
+ SmarbsBackupDir.new("/tmp/smarbs/test_directory")
196
+ end
197
+
198
+ `rm -r /tmp/smarbs/test_directory/a-backup.`
199
+ `mkdir -p /tmp/smarbs/test_directory/-backup.3`
200
+ assert_equal ["-backup.3"], SmarbsBackupDir.new("/tmp/smarbs/test_directory").files
201
+
202
+ `rm -r /tmp/smarbs/test_directory/-backup.3`
203
+ `mkdir -p /tmp/smarbs/test_directory/a-backup.3b2`
204
+ assert_raise_message "/tmp/smarbs/test_directory/a-backup.3b2 has wrong format in number!" do
205
+ SmarbsBackupDir.new("/tmp/smarbs/test_directory")
206
+ end
207
+
208
+ `rm -r /tmp/smarbs/test_directory/a-backup.3b2`
209
+ `mkdir -p /tmp/smarbs/test_directory/a-backup.0`
210
+ assert_raise_message "\nAborted backup directory 'a-backup.0' left in /tmp/smarbs/test_directory/\nMove or delete it to continue!", NoConfigException do
211
+ SmarbsBackupDir.new("/tmp/smarbs/test_directory")
212
+ end
213
+
214
+ assert_equal "a-backup.0", SmarbsBackupDir.new("/tmp/smarbs/test_directory", true).directory_left
215
+
216
+ `rm -r /tmp/smarbs/test_directory/a-backup.0`
217
+ `mkdir -p /tmp/smarbs/test_directory/a-backup.2 /tmp/smarbs/test_directory/a-backup.3 /tmp/smarbs/test_directory/a-backup.6 /tmp/smarbs/test_directory/a-backup.9`
218
+ SmarbsBackupDir.new("/tmp/smarbs/test_directory").remove_except_one
219
+ assert_equal(`ls /tmp/smarbs/test_directory`, "a-backup.2\n")
220
+
221
+ assert_equal false, SmarbsBackupDir.new("/tmp/smarbs/test_directory", true).directory_left
222
+
223
+ assert_raise_message "/etc/smarbs/ is not writable!" do
224
+ SmarbsBackupDir.new("/etc/smarbs")
225
+ end
226
+
227
+ clean
228
+ for i in 1..10
229
+ `mkdir -p /tmp/smarbs/backedup/smarbsbackup/a-backup.#{i}`
230
+ end
231
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
232
+ dir = SmarbsBackupDir.new("/tmp/smarbs/backedup/smarbsbackup")
233
+ assert_output("...removing backup.3...") {dir.delete(Log.new("", true), 0)}
234
+ assert_backup_dirs([1, 2, 4, 5, 6, 7, 8, 9, 10])
235
+
236
+ assert_output("...removing backup.5...") {dir.delete(Log.new("", true), 0)}
237
+ assert_backup_dirs([1, 2, 4, 6, 7, 8, 9, 10])
238
+
239
+ assert_output("...removing backup.6...") {dir.delete(Log.new("", true), 0)}
240
+ assert_backup_dirs([1, 2, 4, 7, 8, 9, 10])
241
+
242
+ assert_output("...removing backup.7...") {dir.delete(Log.new("", true), 0)}
243
+ assert_backup_dirs([1, 2, 4, 8, 9, 10])
244
+
245
+ dir.increment
246
+ assert_backup_dirs([2, 3, 5, 9, 10, 11])
247
+
248
+ assert_output("...removing backup.10...") {dir.delete(Log.new("", true), 0)}
249
+ assert_backup_dirs([2, 3, 5, 9, 11])
250
+
251
+ assert_output("...removing backup.11...") {dir.delete(Log.new("", true), 3)}
252
+ assert_backup_dirs([2, 3, 5, 9])
253
+
254
+ assert_output("...removing backup.9...") {dir.delete(Log.new("", true), 3)}
255
+ assert_backup_dirs([2, 3, 5])
256
+
257
+ assert_raise_message("Not enough space available, next deletion would go below your minbackuplevels of 3!") do
258
+ dir.delete(Log.new("", true), 3)
259
+ end
260
+ assert_backup_dirs([2, 3, 5])
261
+
262
+ dir.increment
263
+ assert_backup_dirs([3, 4, 6])
264
+
265
+ assert_output("...removing backup.6...") {dir.delete(Log.new("", true), 0)}
266
+ assert_backup_dirs([3, 4])
267
+
268
+ assert_output("...removing backup.4...") {dir.delete(Log.new("", true), 0)}
269
+ assert_backup_dirs([3])
270
+
271
+ dir.increment
272
+ assert_backup_dirs([4])
273
+
274
+ assert_raise_message("Not enough space available to do an incremental backup!") do
275
+ dir.delete(Log.new("", true), 0)
276
+ end
277
+ assert_backup_dirs([4])
278
+
279
+ clean
280
+ for i in 1..10
281
+ `mkdir -p /tmp/smarbs/backedup/smarbsbackup/a-backup.#{i}`
282
+ end
283
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
284
+ dir = SmarbsBackupDir.new("/tmp/smarbs/backedup/smarbsbackup")
285
+ assert_output("...removing backup.10...") {dir.delete(Log.new("", true), 0, true)}
286
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8, 9])
287
+
288
+ assert_output("...removing backup.9...") {dir.delete(Log.new("", true), 0, true)}
289
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7, 8])
290
+
291
+ assert_output("...removing backup.8...") {dir.delete(Log.new("", true), 0, true)}
292
+ assert_backup_dirs([1, 2, 3, 4, 5, 6, 7])
293
+ end
294
+ end
295
+
296
+ class TestConfigTemplate < Test::Unit::TestCase
297
+ include SmarbsTest
298
+
299
+ def setup
300
+ clean
301
+ end
302
+
303
+ def teardown
304
+ clean_all
305
+ end
306
+
307
+ def test_config_template
308
+ t = ConfigTemplate.new
309
+ assert_raise_message "Order still empty!", InternalError do
310
+ t.parse_section("src: String\n -- Description1\n -- Description 2\nsrc the_source")
311
+ end
312
+ t.parse_section("=preferences=")
313
+ assert_equal "----------------------------------preferences-----------------------------------\n\n", t.to_s
314
+
315
+ t.parse_section("src: String\n -- Description1\n -- Description 2\n -- src test\nsrc the_source")
316
+ assert_kind_of String, src = t.src
317
+ assert_equal "the_source", src
318
+ assert_equal "----------------------------------preferences-----------------------------------
319
+
320
+ -- Description1
321
+ -- Description 2
322
+ -- src test
323
+ src the_source\n\n", t.to_s
324
+
325
+ t.src = "the_other_source"
326
+ t.parse_section("i: Integer(positive)\ni 9")
327
+ assert_raise_message "'i' must be positive, you set it to -3!" do
328
+ t.i = -3
329
+ end
330
+
331
+ t.parse_section("=advanced=")
332
+ t.parse_section("a: Size(list)\n -- desc\na 1m\na 2m")
333
+
334
+ assert_equal \
335
+ "----------------------------------preferences-----------------------------------
336
+
337
+ -- Description1
338
+ -- Description 2
339
+ -- src test
340
+ src the_other_source
341
+
342
+
343
+ i 9
344
+
345
+ ------------------------------------advanced------------------------------------
346
+
347
+ -- desc
348
+ a 1.0m
349
+ a 2.0m
350
+
351
+ ", t.to_s
352
+
353
+ assert_raise NoMethodError do
354
+ t.not_existing=3
355
+ end
356
+ assert_raise NoMethodError do
357
+ t.not_existing
358
+ end
359
+
360
+ u = ConfigTemplate.new
361
+
362
+ `echo '=preferences=
363
+
364
+ src: String
365
+ -- Description1
366
+ -- Description 2
367
+ -- src test
368
+ src the_other_source
369
+
370
+ i: Integer(positive)
371
+ i 9
372
+
373
+ =advanced=
374
+
375
+ a: Size(list)
376
+ -- desc
377
+ a 1m
378
+ a 2m
379
+
380
+ ' > /tmp/smarbs/configuration.txt`
381
+
382
+ u.read_template("/tmp/smarbs/configuration.txt")
383
+ assert_equal t.to_s, u.to_s
384
+
385
+ text = "
386
+ i 10
387
+ a 18m
388
+
389
+ -- a 19m
390
+ a 20m
391
+ -- j afug
392
+
393
+ -- comment
394
+ "
395
+ u.parse(text)
396
+ assert_equal 10, u.i
397
+ assert_equal [Size.new("18m"),Size.new("20m")], u.a
398
+ assert_equal "the_other_source", u.src
399
+
400
+ assert_raise_message "Only one 'i' allowed!" do
401
+ u.parse("i 10
402
+ a 18m
403
+ i 11")
404
+ end
405
+
406
+ assert_raise_message "No option 'j' existing!" do
407
+ u.parse("src aaa
408
+ a 18m
409
+ j 11")
410
+ end
411
+
412
+ v = ConfigTemplate.new
413
+ r = ConfigTemplate.new
414
+ v.read_template("/tmp/smarbs/configuration.txt")
415
+ r.read_template("/tmp/smarbs/configuration.txt")
416
+ r.parse(text)
417
+ v.parse(text.split("\n"))
418
+
419
+ assert_equal r.to_s, v.to_s
420
+ end
421
+ end
422
+
423
+ class TestConfigFile < Test::Unit::TestCase
424
+ include SmarbsTest
425
+
426
+ def setup
427
+ clean
428
+ end
429
+
430
+ def teardown
431
+ clean_all
432
+ end
433
+
434
+ def test_config_file
435
+ `echo "=preferences=
436
+
437
+ src: Directory(list)
438
+ -- One or more directories to the source (= directory or file you need a backup
439
+ -- backup from. One directory / file per line only.
440
+ src /directory/to/source1
441
+ src /directory/to/source2
442
+
443
+ dest: Directory
444
+ -- One (!) destination directory:
445
+ dest /directory/to/the/destination
446
+
447
+ limit: Size
448
+ -- Limit of space to use for the full and all the incremental backups:
449
+ -- (ignored when using "ownpartition", see below)
450
+ limit 20m
451
+
452
+ =advanced=
453
+
454
+ execute_before: String
455
+ -- A command to execute (in a shell) before the backup is done.
456
+ -- Example:
457
+ -- execute_before mount /directory/to/source1
458
+ execute_before" > /tmp/smarbs/configuration.txt`
459
+
460
+ assert_raise_message("/etc is not writable!") do
461
+ ConfigFile.new("/etc/configfile")
462
+ end
463
+
464
+ assert_raise_message("/etc/fstab~ is not writable!") do
465
+ ConfigFile.new("/etc/fstab~")
466
+ end
467
+
468
+ cf = nil
469
+ assert_output("First run:\nConfig file created, edit /tmp/smarbs/smarbs/backup1") do
470
+ cf = ConfigFile.new("/tmp/smarbs/smarbs/backup1")
471
+ end
472
+
473
+ assert_equal "Smarbs space-managing-rsync-backup-script 0.9.1 (4. September 2009)\nrsync and ruby are required!\n\nFor each different backup you should create a file like this in the directory\n/home/\"user\"/.smarbs/ (or /etc/smarbs/ if you are root).\n\nYou can start a backup using \"smarbs backup1\" or \"smarbs homebackup\" assuming\nthere is a file named \"backup1\" or \"homebackup\" in the smarbs folder.\nA simple \"smarbs\" or a \"smarbs all\" will do a backup with all the different\nconfigurations in the smarbs directory.\n\nSizes: 2394 Bytes (B) => 2394\n 32 Kibibytes (MiB) => 32k\n 54 Mebibytes (MiB) => 54m\n 2 Gibibytes (KiB) => 2g\n\n", cf.intro
474
+
475
+ assert_equal "-------------------------------end of preferences-------------------------------\n\nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nSee <http://www.gnu.org/licenses/> for a copy of the GNU General Public License.\n\nFor any suggestions, comments or bug reports, write to rggjan@gmail.com", cf.outro
476
+ assert_equal "backup1", cf.filename
477
+
478
+ assert_equal ["/directory/to/source1","/directory/to/source2"], cf.src
479
+ cf.src = ["/directory/to/other_source"]
480
+ assert_equal ["/directory/to/other_source"], cf.src
481
+ cf.write
482
+ cf.read
483
+ assert_equal ["/directory/to/other_source"], cf.src
484
+
485
+ assert_output("", :exact) do
486
+ cf = ConfigFile.new("/tmp/smarbs/smarbs/backup1")
487
+ end
488
+ assert_equal ["/directory/to/other_source"], cf.src
489
+ `echo "abc
490
+ -preferences-
491
+ def
492
+ ghi
493
+ advanced preferences-
494
+ jkl" > /tmp/smarbs/smarbs/backup1`
495
+
496
+ assert_raise_message("Configfile /tmp/smarbs/smarbs/backup1 is invalid. Please delete it!") do
497
+ ConfigFile.new("/tmp/smarbs/smarbs/backup1")
498
+ end
499
+ end
500
+ end