This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
executable file
·641 lines (609 loc) · 19.8 KB
/
test.py
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# THIS TEST MUST BE RUN AS ROOT
#
import unittest
import os
import tempfile
import shutil
import re
from subprocess import (
Popen,
PIPE,
check_output,
CalledProcessError,
STDOUT)
J = os.path.join
D = os.path.dirname
CWD = os.path.abspath(D(__file__))
COMMON = u'''#!/usr/bin/env bash
chown -R postgres "{dir}"
cp $0 /fooo
export DB_SMART_BACKUP_AS_FUNCS=1
CWD={CWD}
TOP_BACKUPDIR={TOP_BACKUPDIR}
GLOBAL_SUBDIR={GLOBAL_SUBDIR}
BACKUP_TYPE={BACKUP_TYPE}
comp=bz2
SOURCE={SOURCE}
if [[ -n $SOURCE ]];then
. "{cwd}/db_smart_backup.sh"
fi
dofic() {{
COMP=${{COMP:-xz}}
db="${{1:-${{DB:-"foomoobar"}}}}"
create_db_directories "${{db}}"
local fn="$(get_backupdir)/${{db}}/dumps/${{db}}_${{DATE}}.sql"
touch "$fn" "$(get_compressed_name "$fn")"
link_into_dirs "${{db}}" "$fn"
}}
'''
NO_COMPRESS = u'''
do_compression() {{
echo "do_compression $@"
}}
'''
NO_DB = u'''
do_hook() {{
echo "do_hook $@"
}}
do_db_backup() {{
echo "do_db_backup $@"
}}
'''
NO_ROTATE = u'''
do_rotate() {{
echo "do_rotate $@"
}}
'''
NO_ORPHAN = u'''
cleanup_orphans() {{
echo "cleanup_orphans $@"
}}
'''
NO_MAIL = u'''
do_sendmail() {{
echo "do_sendmail $@"
}}
'''
class TestCase(unittest.TestCase):
def exec_script(self,
string,
common=True,
no_compress=True,
no_orphan=True,
no_rotate=True,
no_db=True,
no_mail=True,
source=True,
stderr=None):
pt = os.path.join(self.dir, 'w.sh')
if not isinstance(string, unicode):
string = string.decode('utf-8')
script = u''
if common:
script += COMMON
if no_rotate:
script += NO_ROTATE
if no_orphan:
script += NO_ORPHAN
if no_compress:
script += NO_COMPRESS
if no_db:
script += NO_DB
if no_mail:
script += NO_MAIL
script += u'\ncd "{0}"\n'.format(self.dir)
script += string
script += u'\ncd "{0}"\n'.format(self.dir)
with open(pt, 'w') as fic:
opts = self.opts.copy()
opts.update({
'SOURCE': (source) and u'1' or u'',
})
sscript = script.format(**opts).encode('utf-8')
fic.write(sscript)
os.chmod(pt, 0755)
try:
if stderr:
stderr = STDOUT
ret = check_output([pt], shell=True, stderr=stderr)
except CalledProcessError, ex:
ret = ex.output
return ret
def setUp(self):
self.env = os.environ.copy()
self.dir = tempfile.mkdtemp()
self.opts = {
'cwd': unicode(CWD),
'CWD': unicode(CWD),
'dir': unicode(self.dir),
'TOP_BACKUPDIR': unicode(os.path.join(self.dir, "pgbackups")),
'BACKUP_TYPE': u"postgresql",
'GLOBAL_SUBDIR': u"__GLOBAL__",
}
def tearDown(self):
if os.path.exists(self.dir):
shutil.rmtree(self.dir)
os.environ.update(self.env)
def test_Compressor(self):
TEST = '''
COMP="" COMPS="xz nocomp" XZ="/null";set_compressor;echo comp:$COMP;
COMP="xz" COMPS="xz" XZ="" ;set_compressor;echo comp:$COMP;
'''
ret = self.exec_script(TEST, stderr=True)
self.assertEqual(
'comp:nocomp\n'
'comp:xz\n', ret)
TEST = '''
COMP="" COMPS="bzip2 nocomp" BZIP2="/null";set_compressor;echo comp:$COMP;
COMP="bzip2" COMPS="bzip2" BZIP2="" ;set_compressor;echo comp:$COMP;
'''
ret = self.exec_script(TEST, stderr=True)
self.assertEqual(
'comp:nocomp\n'
'comp:bzip2\n', ret)
TEST = '''
COMP="" COMPS="gzip nocomp" GZIP="/null";set_compressor;echo comp:$COMP;
COMP="gzip" COMPS="gzip" GZIP="" ;set_compressor;echo comp:$COMP;
'''
ret = self.exec_script(TEST, stderr=True)
self.assertEqual(
'comp:nocomp\n'
'comp:gzip\n', ret)
TEST = '''
COMP="xz"
COMPS="gzip bzip2 xz nocomp"
GZIP="/null"; XZ="/null"; BZIP2="/null"
set_compressor;echo comp:$COMP;
'''
ret = self.exec_script(TEST, stderr=True)
self.assertEqual(
'comp:nocomp\n', ret)
def test_Compression(self):
TEST = '''
XZ=/nonexist
BZ2=/nonexist
COMP=gzip
echo "$(get_compressed_name a)"
COMP=bzip2
echo "$(get_compressed_name a)"
COMP=foo
echo "$(get_compressed_name a)"
'''
ret = self.exec_script(TEST)
self.assertEqual(
'a.gz\n'
'a.bz2\n'
'a\n', ret)
TEST = '''
COMP="xz";COMPS="$COMP";set_compressor
echo azerty>thiscompress
do_compression thiscompress
echo $COMPRESSED_NAME
'''
ret = self.exec_script(TEST, stderr=True, no_compress=False)
for i in [
'^thiscompress.xz.*$',
]:
self.assertTrue(re.search(i, ret, re.M | re.U), i)
TEST = '''
COMP="gzip";COMPS="$COMP";set_compressor
echo azerty>thiscompress
do_compression thiscompress
echo $COMPRESSED_NAME
'''
ret = self.exec_script(TEST, stderr=True, no_compress=False)
for i in [
'^thiscompress.gz.*$',
]:
self.assertTrue(re.search(i, ret, re.M | re.U), i)
TEST = '''
COMP="bzip2";COMPS="$COMP";set_compressor
echo azerty>thiscompress
do_compression thiscompress
echo $COMPRESSED_NAME
'''
ret = self.exec_script(TEST, stderr=True, no_compress=False)
for i in [
'^thiscompress.bz2.*$',
]:
self.assertTrue(re.search(i, ret, re.M | re.U), i)
TEST = '''
COMP="nocomp";COMPS="$COMP";set_compressor
echo azerty>thiscompress
do_compression thiscompress
echo $COMPRESSED_NAME
'''
ret = self.exec_script(TEST, stderr=True, no_compress=False)
for i in [
'\[db_smart_backup\] No compressor found, no compression done',
'thiscompress',
]:
self.assertTrue(re.search(i, ret, re.M | re.U), i)
def test_arunas(self):
TEST = '''
outputDir="{dir}/spaces"
export DB_SMART_BACKUP_AS_FUNCS="1" RUNAS="postgres" PSQL="psql"
mkdir -p "$outputDir/WITH QUOTES"
touch "$outputDir/WITH QUOTES/"{{a,b,e}}
touch "$outputDir/WITH QUOTES/"{{cc,dd,ff}}
chown -Rf postgres "$outputDir/WITH QUOTES"
export RUNAS="postgres"
runcmd_as whoami
runcmd_as ls "$outputDir/WITH QUOTES"
export RUNAS="root"
runcmd_as whoami
runcmd_as ls "$outputDir/WITH QUOTES"
'''
ret = self.exec_script(TEST)
self.assertEqual(
'postgres\na\nb\ncc\ndd\ne\nff\n'
'root\na\nb\ncc\ndd\ne\nff\n', ret)
def test_monkeypatch(self):
TEST = '''
export DB_SMART_BACKUP_AS_FUNCS="1" RUNAS="postgres" PSQL="psql"
. "$CWD/db_smart_backup.sh"
dummy_for_tests
dummy_callee_for_tests() {{
echo "foo"
}}
dummy_for_tests
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
'here\nfoo\n'
)
def test_pgbins_run_as(self):
TEST = '''
export DB_SMART_BACKUP_AS_FUNCS="1" RUNAS="postgres" PSQL="psql"
. "$CWD/db_smart_backup.sh"
export RUNAS="postgres"
psql_ -c "select * from pg_roles"
echo $ret
'''
ret = self.exec_script(TEST)
self.assertTrue('rolnam' in ret)
def test_createdirs(self):
TEST = '''
create_db_directories "WITH QUOTES ET utf8 éà"
create_db_directories $GLOBAL_SUBDIR
create_db_directories foo
'''
self.exec_script(TEST)
for i in[
'postgresql/localhost/WITH QUOTES ET utf8 éà/daily',
'postgresql/localhost/WITH QUOTES ET utf8 éà/dumps',
'postgresql/localhost/WITH QUOTES ET utf8 éà/monthly',
'postgresql/localhost/WITH QUOTES ET utf8 éà/weekly',
'postgresql/localhost/foo/daily',
'postgresql/localhost/foo/dumps',
'postgresql/localhost/foo/monthly',
'postgresql/localhost/foo/weekly',
'postgresql/localhost/__GLOBAL__/daily',
'postgresql/localhost/__GLOBAL__/dumps',
'postgresql/localhost/__GLOBAL__/monthly',
'postgresql/localhost/__GLOBAL__/weekly',
'postgresql/localhost/__GLOBAL__',
]:
self.assertTrue(
os.path.isdir(J(self.dir, "pgbackups", i)), i)
def test_backdir(self):
TEST = '''
echo $(TOP_BACKUPDIR="foo" BACKUP_TYPE=bar HOST="" PGHOST="" get_backupdir)
echo $(TOP_BACKUPDIR="foo" BACKUP_TYPE=postgresql PGHOST="" get_backupdir)
echo $(TOP_BACKUPDIR="foo" BACKUP_TYPE=postgresql HOST="bar" PGHOST="foo" get_backupdir)
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
'foo/bar\n'
'foo/postgresql/localhost\n'
'foo/postgresql/bar\n')
def test_link_into_dirs(self):
TEST = '''
DB=foo;YEAR="2002";MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";DOY="0$DOM";W="2" dofic
'''
self.exec_script(TEST)
for i in [
"postgresql/localhost/foo/weekly/foo_2002_2.sql.xz",
"postgresql/localhost/foo/monthly/foo_2002_01.sql.xz",
"postgresql/localhost/foo/daily/foo_2002_008_2002-01-08.sql.xz",
]:
self.assertTrue(os.path.exists(J(self.dir, 'pgbackups', i)), i)
def test_cleanup_orphans_1(self):
common = u'''
BACKUPDIR="$outputDir/pgbackups"
KEEP_MONTHES=1
KEEP_WEEKS=3
KEEP_DAYS=9
DB=foo
YEAR=2002
'''
RTEST1 = common + u'''
DB="WITH QUOTES é utf8"
find "$(get_backupdir)/$DB/"{{daily,monthly,lastsnapshots,weekly}}/ -type f|\
while read fic;do rm -f "${{fic}}";done
do_cleanup_orphans 2>&1
'''
TEST = common + u'''
DB="WITH QUOTES é utf8"
MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";DOY="0$DOM";
DOY="0$DOM";W="2"
FDATE="${{DATE}}_01-02-03";dofic
'''
# removing file from all subdirs except dump place
self.exec_script(TEST)
ret = self.exec_script(RTEST1)
self.assertTrue(
re.search(
'Pruning .*/pgbackups/postgresql/localhost/'
'WITH QUOTES é utf8/dumps/'
'WITH QUOTES é utf8_2002-01-08.sql',
ret
)
)
def test_cleanup_orphans_2(self):
common = u'''
BACKUPDIR="$outputDir/pgbackups"
KEEP_LOGS=1
KEEP_LASTS=24
KEEP_MONTHES=1
KEEP_WEEKS=3
KEEP_DAYS=9
DB=foo
YEAR=2002
'''
RTEST2 = common + u'''
DB="WITH QUOTES é utf8"
find "$(get_backupdir)/$DB/"{{dumps,daily,monthly,weekly}} -type f|\
while read fic;do rm -f "${{fic}}";done
do_cleanup_orphans 2>&1
'''
TEST = common + u'''
DB="WITH QUOTES é utf8"
MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";DOY="0$DOM";
DOY="0$DOM";W="2"
FDATE="${{DATE}}_01-02-03";dofic
'''
# removing file from dumps + all-one dir
# and test than orphan cleanup the last bits
self.exec_script(TEST)
ret = self.exec_script(RTEST2)
import pdb;pdb.set_trace()
self.assertTrue(
re.search(
'Pruning .*WITH QUOTES',
ret
)
)
def test_rotate(self):
rotatec = u'''
BACKUPDIR="$outputDir/pgbackups"
KEEP_MONTHES=1
KEEP_WEEKS=3
KEEP_DAYS=9
DB=foo
YEAR=2002
'''
RTEST = rotatec + u'''
do_rotate 2>&1
'''
TEST = rotatec + u'''
DB="WITH QUOTES é utf8"
MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="09";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="10";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="11";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="12";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="13";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="14";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="15";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="16";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="03";DOM="01";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="60" ;W="8" dofic
DB=__GLOBAL__
MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="09";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="10";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="11";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="12";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="13";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="14";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="15";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="16";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="03";DOM="01";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="60" ;W="8" dofic
DB=bar
MNUM="01";DOM="08";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="09";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="10";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="11";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="12";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="13";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="14";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="2" dofic
MNUM="01";DOM="15";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="16";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="3" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="01";DOM="17";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="0$DOM";W="5" dofic
MNUM="03";DOM="01";DATE="$YEAR-$MNUM-$DOM";FDATE="${{DATE}}_01-01-01";DOY="60" ;W="8" dofic
'''
self.exec_script(TEST)
counters = {
'': 50,
'daily': 11,
'monthly': 2,
'weekly': 4,
'dumps': 22,
'lastsnapshots': 11,
}
for i in ['__GLOBAL__', 'bar', u"WITH QUOTES é utf8"]:
for j in ['', 'daily', 'weekly', 'monthly',
'dumps', 'lastsnapshots']:
cmd = (
u"find '{0}/pgbackups/postgresql/localhost/{1}/{2}' "
u"-type f 2>/dev/null"
u"|wc -l".format(self.dir, i, j))
ret = self.exec_script(cmd)
counter = counters.get(j)
self.assertTrue(int(ret.strip()) == counter,
u"{3} {0}: {2} != {1}".format(
j, ret, counter, i))
ret = self.exec_script(RTEST, no_rotate=False)
counters = {
'': 37,
'daily': 9,
'monthly': 1,
'weekly': 3,
'dumps': 22,
'lastsnapshots': 2,
}
for i in ['__GLOBAL__', 'bar', u"WITH QUOTES é utf8"]:
for j in ['', 'daily', 'weekly', 'monthly',
'dumps', 'lastsnapshots']:
cmd = (
u"find '{0}/pgbackups/postgresql/localhost/{1}/{2}' "
u"-type f 2>/dev/null"
u"|wc -l".format(self.dir, i, j))
ret = self.exec_script(cmd)
counter = counters.get(j)
self.assertTrue(int(ret.strip()) == counter,
u"{3} {0}: {2} != {1}".format(
j, ret, counter, i))
def test_sort1(self):
TEST = u'''
# weekly
cd "{dir}"
rm -rf testtest;mkdir testtest
touch testtest/foo_2001_55.sql
touch testtest/foo_2001_02.sql
touch testtest/foo_2001_3.sql
touch testtest/foo_2001_1.sql
touch testtest/foo_2001_44.sql
get_sorted_files testtest
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
(
'foo_2001_55.sql\n'
'foo_2001_44.sql\n'
'foo_2001_3.sql\n'
'foo_2001_02.sql\n'
'foo_2001_1.sql\n'
)
)
def test_sort2(self):
TEST = u'''
# daily
cd "{dir}"
rm -rf testtest;mkdir testtest
touch testtest/foo_2001_034_2001_02_01.sql
touch testtest/foo_2002_035_2002_02_02.sql
touch testtest/foo_2001_001_2001_01_01.sql
touch testtest/foo_2001_002_2001_01_02.sql
touch testtest/foo_2001_003_2001_1_3.sql
touch testtest/foo_2001_001_2001_01_01.sql
touch testtest/foo_2001_067_2001_04_02.sql
touch testtest/foo_2001_066_2001_04_01.sql
touch testtest/foo_2002_001_2002_1_1.sql
get_sorted_files testtest
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
(
'foo_2002_035_2002_02_02.sql\n'
'foo_2002_001_2002_1_1.sql\n'
'foo_2001_067_2001_04_02.sql\n'
'foo_2001_066_2001_04_01.sql\n'
'foo_2001_034_2001_02_01.sql\n'
'foo_2001_003_2001_1_3.sql\n'
'foo_2001_002_2001_01_02.sql\n'
'foo_2001_001_2001_01_01.sql\n'
)
)
def test_sort3(self):
TEST = u'''
# dumps
cd "{dir}"
rm -rf testtest;mkdir testtest
touch testtest/foo_2001_9_16.sql
touch testtest/foo_2001_02_01.sql
touch testtest/foo_2002_02_02.sql
touch testtest/foo_2001_01_1.sql
touch testtest/foo_2001_01_03.sql
touch testtest/foo_2001_1_2.sql
touch testtest/foo_2001_01_1.sql
touch testtest/foo_2001_04_02.sql
touch testtest/foo_2001_04_01.sql
touch testtest/foo_2003_4_1.sql
get_sorted_files testtest
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
(
'foo_2003_4_1.sql\n'
'foo_2002_02_02.sql\n'
'foo_2001_9_16.sql\n'
'foo_2001_04_02.sql\n'
'foo_2001_04_01.sql\n'
'foo_2001_02_01.sql\n'
'foo_2001_01_03.sql\n'
'foo_2001_1_2.sql\n'
'foo_2001_01_1.sql\n'
)
)
def test_asanitize(self):
TEST = u'''
# monthly
cd "{dir}"
set_vars
activate_IO_redirection
cyan_log "foo"
yellow_log "foo"
log "foo"
deactivate_IO_redirection
sanitize_log
cat -e "$DSB_LOGFILE"
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret.splitlines()[-3:],
['foo$',
'[db_smart_backup] foo$',
'[db_smart_backup] foo$']
)
def test_sort4(self):
TEST = u'''
# monthly
cd "{dir}"
rm -rf testtest;mkdir testtest
touch testtest/foo_2002_02.sql
touch testtest/foo_2001_02.sql
touch testtest/foo_2003_2.sql
touch testtest/foo_2001_01.sql
touch testtest/foo_2001_04.sql
touch testtest/foo_2002_2.sql
get_sorted_files testtest
'''
ret = self.exec_script(TEST)
self.assertEqual(
ret,
(
'foo_2003_2.sql\n'
'foo_2002_2.sql\n'
'foo_2002_02.sql\n'
'foo_2001_04.sql\n'
'foo_2001_02.sql\n'
'foo_2001_01.sql\n'
)
)
if __name__ == '__main__':
unittest.main()
# vim:et:ft=python:sts=4:sw=4