File: cmdlinetest.py

package info (click to toggle)
rdiff-backup 2.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: python: 24,129; javascript: 9,512; sh: 1,230; ansic: 580; makefile: 36
file content (853 lines) | stat: -rw-r--r-- 33,077 bytes parent folder | download
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
import unittest
import os
import re
import time
import pathlib
from commontest import (
    rdiff_backup, Myrm, compare_recursive,
    old_test_dir, abs_test_dir, get_increment_rp, xcopytree
)
import commontest as comtst
import fileset
from rdiff_backup import Globals, log, rpath, robust, FilenameMapping, Time, selection
from rdiffbackup.locations.map import filenames as map_filenames

"""Regression tests"""

Globals.exclude_mirror_regexps = [re.compile(b".*/rdiff-backup-data")]

lc = Globals.local_connection


class Local:
    """This is just a place to put increments relative to the local
    connection"""

    def get_src_local_rp(extension):
        return rpath.RPath(Globals.local_connection,
                           os.path.join(old_test_dir, os.fsencode(extension)))

    def get_tgt_local_rp(extension):
        return rpath.RPath(Globals.local_connection,
                           os.path.join(abs_test_dir, os.fsencode(extension)))

    vftrp = get_src_local_rp('various_file_types')
    emptyrp = get_src_local_rp('empty')
    inc1rp = get_src_local_rp('increment1')
    inc2rp = get_src_local_rp('increment2')
    inc3rp = get_src_local_rp('increment3')
    inc4rp = get_src_local_rp('increment4')
    backup1rp = get_src_local_rp('restoretest')
    backup2rp = get_src_local_rp('restoretest2')
    backup3rp = get_src_local_rp('restoretest3')
    backup4rp = get_src_local_rp('restoretest4')
    backup5rp = get_src_local_rp('restoretest5')

    rpout = get_tgt_local_rp('output')
    rpout_inc = get_tgt_local_rp('output_inc')
    rpout1 = get_tgt_local_rp('restoretarget1')
    rpout2 = get_tgt_local_rp('restoretarget2')
    rpout3 = get_tgt_local_rp('restoretarget3')
    rpout4 = get_tgt_local_rp('restoretarget4')

    vft_in = get_src_local_rp('increment2/various_file_types')
    vft_out = get_tgt_local_rp('vft_out')
    vft_recover = get_tgt_local_rp('vft2_out')

    timbar_in = get_src_local_rp('increment1/timbar.pyc')
    timbar_out = get_tgt_local_rp('timbar.pyc')  # in cur directory

    # these directories are actually source directories but will be created
    # on the fly from the incrementX directories (without win-)
    wininc2 = get_tgt_local_rp('win-increment2')
    wininc3 = get_tgt_local_rp('win-increment3')


class PathSetter(unittest.TestCase):
    def refresh(self, *rp_list):
        """Reread data for the given rps"""
        for rp in rp_list:
            rp.setdata()

    def delete_tmpdirs(self):
        """Remove any temp directories created by previous tests"""
        self.refresh(Local.rpout, Local.rpout1, Local.rpout2, Local.rpout3,
                     Local.rpout4, Local.vft_recover, Local.timbar_out,
                     Local.vft_out)
        if Local.rpout.lstat():
            Local.rpout.delete()
        if Local.rpout1.lstat():
            Local.rpout1.delete()
        if Local.rpout2.lstat():
            Local.rpout2.delete()
        if Local.rpout3.lstat():
            Local.rpout3.delete()
        if Local.rpout4.lstat():
            Local.rpout4.delete()
        if Local.vft_recover.lstat():
            Local.vft_recover.delete()
        if Local.timbar_out.lstat():
            Local.timbar_out.delete()
        if Local.vft_out.lstat():
            Local.vft_out.delete()

    def runtest(self, from_local, to_local):
        self.delete_tmpdirs()

        # Backing up increment1
        rdiff_backup(from_local,
                     to_local,
                     Local.inc1rp.path,
                     Local.rpout.path,
                     current_time=10000)
        self.assertTrue(compare_recursive(Local.inc1rp, Local.rpout))
        time.sleep(1)

        # Backing up increment2
        rdiff_backup(from_local,
                     to_local,
                     Local.inc2rp.path,
                     Local.rpout.path,
                     current_time=20000)
        self.assertTrue(compare_recursive(Local.inc2rp, Local.rpout))
        time.sleep(1)

        # Backing up increment3
        rdiff_backup(from_local,
                     to_local,
                     Local.inc3rp.path,
                     Local.rpout.path,
                     current_time=30000)
        self.assertTrue(compare_recursive(Local.inc3rp, Local.rpout))
        time.sleep(1)

        # Backing up increment4
        rdiff_backup(from_local,
                     to_local,
                     Local.inc4rp.path,
                     Local.rpout.path,
                     current_time=40000)
        self.assertTrue(compare_recursive(Local.inc4rp, Local.rpout))

        # Getting restore rps
        inc_paths = self.getinc_paths(
            b"increments.", os.path.join(Local.rpout.path,
                                         b"rdiff-backup-data"))
        self.assertEqual(len(inc_paths), 3)

        # Restoring increment1
        rdiff_backup(from_local, to_local, inc_paths[0], Local.rpout1.path,
                     extra_options=b"--restore")
        self.assertTrue(compare_recursive(Local.inc1rp, Local.rpout1))

        # Restoring increment2
        rdiff_backup(from_local, to_local, inc_paths[1], Local.rpout2.path,
                     extra_options=b"--restore")
        self.assertTrue(compare_recursive(Local.inc2rp, Local.rpout2))

        # Restoring increment3
        rdiff_backup(from_local, to_local, inc_paths[2], Local.rpout3.path,
                     extra_options=b"--restore")
        self.assertTrue(compare_recursive(Local.inc3rp, Local.rpout3))

        # Test restoration of a few random files
        vft_paths = self.getinc_paths(
            b"various_file_types.",
            os.path.join(Local.rpout.path, b"rdiff-backup-data",
                         b"increments"))
        rdiff_backup(from_local, to_local, vft_paths[1], Local.vft_out.path,
                     extra_options=b"--restore")
        self.refresh(Local.vft_in, Local.vft_out)
        self.assertTrue(compare_recursive(Local.vft_in, Local.vft_out))

        timbar_paths = self.getinc_paths(
            b"timbar.pyc.",
            os.path.join(Local.rpout.path, b"rdiff-backup-data",
                         b"increments"))
        rdiff_backup(from_local, to_local, timbar_paths[0],
                     Local.timbar_out.path,
                     extra_options=b"--restore")
        self.refresh(Local.timbar_in, Local.timbar_out)
        self.assertTrue(Local.timbar_in.equal_loose(Local.timbar_out))

        rdiff_backup(from_local,
                     to_local,
                     Local.rpout.append('various_file_types').path,
                     Local.vft_recover.path,
                     extra_options=b"--restore-as-of 25000")
        self.refresh(Local.vft_recover, Local.vft_in)
        self.assertTrue(compare_recursive(Local.vft_recover, Local.vft_in))

        # Make sure too many increment files not created
        self.assertEqual(len(self.getinc_paths(
            b"nochange.", os.path.join(Local.rpout.path, b"rdiff-backup-data",
                                       b"increments"))), 0)
        nochange_incs = len(self.getinc_paths(
            b"", os.path.join(Local.rpout.path, b"rdiff-backup-data",
                              b"increments", b"nochange")))
        self.assertIn(nochange_incs, (0, 1))

    def getinc_paths(self, basename, directory, quoted=0):
        """Returns a sorted list of files which starts with basename
        within a given directory."""

        if quoted:
            if Globals.get_api_version() < 201:  # compat200
                FilenameMapping.set_init_quote_vals()
                dirrp = FilenameMapping.QuotedRPath(Globals.local_connection,
                                                    directory)
            else:
                dirrp = map_filenames.QuotedRPath(Globals.local_connection,
                                                  directory)
        else:
            dirrp = rpath.RPath(Globals.local_connection, directory)
        incbasenames = [
            filename for filename in robust.listrp(dirrp)
            if filename.startswith(basename)
        ]
        incbasenames.sort()
        incrps = list(map(dirrp.append, incbasenames))
        return [
            x.path for x in [incrp for incrp in incrps if incrp.isincfile()]
        ]


class Final(PathSetter):
    @unittest.skipIf(Globals.get_api_version() > 200,  # compat200
                     "Higher API version than 200 not supported")
    def testLocal(self):
        """Run test sequence everything local"""
        self.runtest(True, True)

    @unittest.skipIf(Globals.get_api_version() > 200,  # compat200
                     "Higher API version than 200 not supported")
    def testRemoteAll(self):
        """Run test sequence everything remote"""
        self.runtest(False, False)

    @unittest.skipIf(Globals.get_api_version() > 200,  # compat200
                     "Higher API version than 200 not supported")
    def testRemoteSource(self):
        """Run test sequence when remote side is source"""
        self.runtest(False, True)

    @unittest.skipIf(Globals.get_api_version() > 200,  # compat200
                     "Higher API version than 200 not supported")
    def testRemoteDest(self):
        """Run test sequence when remote side is destination"""
        self.runtest(True, False)

    # FIXME see issue #35 for backup of proc filesystems
    # https://github.com/ericzolf/rdiff-backup/issues/35
    @unittest.skip("Not sure it makes any sense to backup proc FIXME")
    def testProcLocal(self):
        """Test initial backup of /proc locally"""
        procout_dir = os.path.join(abs_test_dir, b"procoutput")
        Myrm(procout_dir)
        procout = rpath.RPath(Globals.local_connection, procout_dir)
        rdiff_backup(True, True, '/proc', procout.path, current_time=10000)
        time.sleep(1)
        rdiff_backup(True, True, '/proc', procout.path, current_time=20000)
        time.sleep(1)
        rdiff_backup(True,
                     True,
                     Local.inc1rp.path,
                     procout.path,
                     current_time=30000)
        self.assertTrue(compare_recursive(Local.inc1rp, procout))
        time.sleep(1)
        rdiff_backup(True, True, '/proc', procout.path, current_time=40000)

    @unittest.skip("Not sure it makes any sense to backup proc FIXME")
    def testProcLocalToRemote(self):
        """Test mirroring proc remote"""
        procout_dir = os.path.join(abs_test_dir, b"procoutput")
        Myrm(procout_dir)
        procout = rpath.RPath(Globals.local_connection, procout_dir)
        rdiff_backup(True, False, '/proc', procout.path, current_time=10000)
        time.sleep(1)
        rdiff_backup(True, False, '/proc', procout.path, current_time=20000)
        time.sleep(1)
        rdiff_backup(True,
                     False,
                     Local.inc1rp.path,
                     procout.path,
                     current_time=30000)
        self.assertTrue(compare_recursive(Local.inc1rp, procout))
        time.sleep(1)
        rdiff_backup(True, False, '/proc', procout.path, current_time=40000)

    @unittest.skip("Not sure it makes any sense to backup proc FIXME")
    def testProcRemoteToLocal(self):
        """Test mirroring proc, this time when proc is remote, dest local"""
        procout_dir = os.path.join(abs_test_dir, b"procoutput")
        Myrm(procout_dir)
        procout = rpath.RPath(Globals.local_connection, procout_dir)
        rdiff_backup(False, True, '/proc', procout.path)

    # FIXME: to be done later, Windows isn't yet priority
    @unittest.skipUnless(os.name == "nt", "Requires Windows support")
    def testWindowsMode(self):
        """Test backup with quoting enabled

        We need to delete from the increment? directories long file
        names, because quoting adds too many extra letters.

        """

        def delete_long(base_rp, length=100):
            """Delete filenames longer than length given"""
            for rp in selection.Select(base_rp).get_select_iter():
                if len(rp.dirsplit()[1]) > length:
                    rp.delete()

        if not Local.wininc2.lstat() or not Local.wininc3.lstat():
            xcopytree(b"testfiles/increment2", b"testfiles/win-increment2")
            xcopytree(b"testfiles/increment3", b"testfiles/win-increment3")
            delete_long(Local.wininc2)
            delete_long(Local.wininc3)

        old_schema = self.rb_schema
        self.rb_schema = old_schema + b" --override-chars-to-quote '^a-z0-9_ -.' "
        self.set_connections(None, None, None, None)

        self.delete_tmpdirs()
        # Back up increment2, this contains a file with colons
        self.exec_rb(20000, b'testfiles/win-increment2', b'testfiles/output')
        self.rb_schema = old_schema  # Quoting setting should now be saved
        time.sleep(1)

        # Back up increment3
        self.exec_rb(30000, b'testfiles/win-increment3', b'testfiles/output')

        # Now check to make sure no ":" in output directory
        popen_fp = os.popen(b"find testfiles/output -name '*:*' | wc")
        wc_output = popen_fp.read()
        popen_fp.close()
        self.assertEqual(wc_output.split(), [b"0", b"0", b"0"])

        # Start restore of increment 2
        Globals.chars_to_quote = b'^a-z0-9_ -.'
        inc_paths = self.getinc_paths(b"increments.",
                                      b"testfiles/output/rdiff-backup-data", 1)
        Globals.chars_to_quote = None
        self.assertEqual(len(inc_paths), 1)
        self.exec_rb(None, inc_paths[0], b'testfiles/restoretarget2')
        self.assertTrue(
            compare_recursive(Local.wininc2, Local.rpout2, compare_hardlinks=0))

        # Restore increment 3 again, using different syntax
        self.rb_schema = old_schema + b'-r 30000 '
        self.exec_rb(None, b'testfiles/output', b'testfiles/restoretarget3')
        self.assertTrue(
            compare_recursive(Local.wininc3, Local.rpout3, compare_hardlinks=0))
        self.rb_schema = old_schema

    @unittest.skipIf(os.name == "nt",
                     "Windows doesn't recognize case insensitivity")
    def testLegacy(self):
        """Test restoring directory with no mirror_metadata file"""
        self.delete_tmpdirs()
        rdiff_backup(True,
                     True,
                     Local.vftrp.path,
                     Local.rpout.path,
                     current_time=10000)
        rdiff_backup(True,
                     True,
                     Local.emptyrp.path,
                     Local.rpout.path,
                     current_time=20000)
        # remove mirror_metadata files to simulate old style backups
        # pathlib.Path doesn't work with bytes hence need to work with str path
        for mirror_file in pathlib.Path(
                os.fsdecode(Local.rpout.append(
                    b'rdiff-backup-data').path)).glob('mirror_metadata*'):
            mirror_file.unlink()
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     Local.rpout1.path,
                     extra_options=b'-r0')
        self.assertTrue(
            compare_recursive(Local.vftrp, Local.rpout1, compare_hardlinks=0))


class FinalMisc(PathSetter):
    """Test miscellaneous operations like list-increments, etc.

    Many of these just run and make sure there were no errors; they
    don't verify the output.

    """

    def testListIncrementsLocal(self):
        """Test --list-increments switch.  Assumes restoretest3 valid rd dir"""
        rdiff_backup(True,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-increments')

    def testListIncrementsRemote(self):
        """Test --list-increments mode remotely.  Uses restoretest3"""
        rdiff_backup(False,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-increments')

    def testListChangeSinceLocal(self):
        """Test --list-changed-since mode locally.  Uses restoretest3"""
        rdiff_backup(True,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-changed-since 10000')
        rdiff_backup(True,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-changed-since 2B')

    def testListChangeSinceRemote(self):
        """Test --list-changed-since mode remotely.  Uses restoretest3"""
        rdiff_backup(False,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-changed-since 10000')

    def testListAtTimeLocal(self):
        """Test --list-at-time mode locally.  Uses restoretest3"""
        rdiff_backup(True,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-at-time 20000')

    def testListAtTimeRemote(self):
        """Test --list-at-time mode locally.  Uses restoretest3"""
        rdiff_backup(False,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-at-time 20000')

    def testListIncrementSizesLocal(self):
        """Test --list-increment-sizes switch.  Uses restoretest3"""
        rdiff_backup(True,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-increment-sizes')

    def testListIncrementSizesRemote(self):
        """Test --list-increment-sizes switch.  Uses restoretest3"""
        rdiff_backup(False,
                     True,
                     Local.backup3rp.path,
                     None,
                     extra_options=b'--list-increment-sizes')

    def get_all_increments(self, rp):
        """Iterate all increments at or below given directory"""
        self.assertTrue(rp.isdir())
        dirlist = rp.listdir()
        dirlist.sort()
        for filename in dirlist:
            subrp = rp.append(filename)
            if subrp.isincfile():
                yield subrp
            elif subrp.isdir():
                for subsubrp in self.get_all_increments(subrp):
                    yield subsubrp

    def testRemoveOlderThan(self):
        """Test --remove-older-than.  Uses restoretest3"""
        Myrm(Local.rpout.path)
        xcopytree(Local.backup3rp.path, Local.rpout.path)
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     None,
                     extra_options=b"--remove-older-than 20000")
        rbdir = Local.rpout.append("rdiff-backup-data")
        for inc in self.get_all_increments(rbdir):
            self.assertGreaterEqual(inc.getinctime(), 20000)

    def testRemoveOlderThan2(self):
        """Test --remove-older-than, but '1B'.  Uses restoretest3"""
        Myrm(Local.rpout.path)
        xcopytree(Local.backup3rp.path, Local.rpout.path)
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     None,
                     extra_options=b"--remove-older-than 1B --force")
        rbdir = Local.rpout.append("rdiff-backup-data")
        for inc in self.get_all_increments(rbdir):
            self.assertGreaterEqual(inc.getinctime(), 30000)

    def testRemoveOlderThanCurrent(self):
        """Make sure --remove-older-than doesn't delete current incs"""
        Myrm(Local.rpout.path)
        xcopytree(Local.backup3rp.path, Local.rpout.path)
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     None,
                     extra_options=b"--remove-older-than now --force")
        rbdir = Local.rpout.append("rdiff-backup-data")

        has_cur_mirror, has_metadata = 0, 0
        for inc in self.get_all_increments(rbdir):
            if inc.getincbase().index[-1] == b'current_mirror':
                has_cur_mirror = 1
            elif inc.getincbase().index[-1] == b'mirror_metadata':
                has_metadata = 1
        self.assertTrue(has_cur_mirror)
        self.assertTrue(has_metadata)

    def testRemoveOlderThanQuoting(self):
        """Test --remove-older-than when dest directory is quoted"""
        Myrm(Local.rpout.path)
        rdiff_backup(True,
                     True,
                     Local.inc1rp.path,
                     Local.rpout.path,
                     extra_options=b"--override-chars-to-quote '^a-z0-9_ -.'"
                     b" --current-time 10000")
        rdiff_backup(True,
                     True,
                     Local.inc2rp.path,
                     Local.rpout.path,
                     extra_options=b"--override-chars-to-quote '^a-z0-9_ -.'"
                     b" --current-time 20000")
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     None,
                     extra_options=b"--remove-older-than now")

    def testRemoveOlderThanRemote(self):
        """Test --remove-older-than remotely"""
        Myrm(Local.rpout.path)
        xcopytree(Local.backup3rp.path, Local.rpout.path)
        rdiff_backup(False,
                     True,
                     Local.rpout.path,
                     None,
                     extra_options=b"--remove-older-than 20000")
        rbdir = Local.rpout.append("rdiff-backup-data")
        for inc in self.get_all_increments(rbdir):
            self.assertGreaterEqual(inc.getinctime(), 20000)

    def testNonExistingTempDir(self):
        """Test that a missing tempdir is properly catched as an error"""
        Myrm(Local.rpout.path)
        rdiff_backup(True,
                     True,
                     Local.vftrp.path,
                     Local.rpout.path,
                     extra_options=b"--tempdir DoesSurelyNotExist",
                     expected_ret_code=Globals.RET_CODE_ERR)


class FinalSelection(PathSetter):
    """Test selection options"""

    def testSelLocal(self):
        """Quick backup testing a few selection options"""
        self.delete_tmpdirs()

        # create a few relative paths to check a different approach than absolute
        inc2_rel = os.path.relpath(Local.inc2rp.path)
        out_rel = os.path.relpath(Local.rpout.path)
        rest1_rel = os.path.relpath(Local.rpout1.path)

        # Test --include option
        rdiff_backup(
            True,
            True,
            inc2_rel,
            out_rel,
            extra_options=b"--current-time 10000 --include %b --exclude '**' " %
            os.path.join(inc2_rel, b"various_file_types"))

        # check that one included file exists and one excluded doesn't
        self.assertTrue(os.lstat(
            os.path.join(out_rel, b"various_file_types", b"regular_file")))
        self.assertRaises(OSError, os.lstat, os.path.join(out_rel, b"test.py"))

        # Now try reading list of files
        rdiff_backup(True,
                     True,
                     inc2_rel,
                     out_rel,
                     extra_options=b"--current-time 20000 --include-filelist-stdin --exclude '**' ",
                     input=b"\n%b\n%b" % (os.path.join(inc2_rel, b"test.py"),
                                          os.path.join(inc2_rel, b"changed_dir")))

        # check that two included files exist and two excluded don't
        self.assertTrue(os.lstat(os.path.join(out_rel, b"changed_dir")))
        self.assertTrue(os.lstat(os.path.join(out_rel, b"test.py")))
        self.assertRaises(OSError, os.lstat,
                          os.path.join(out_rel, b"various_file_types"))
        self.assertRaises(OSError, os.lstat,
                          os.path.join(out_rel, b"changed_dir", b"foo"))

        # Test selective restoring
        mirror_rp = rpath.RPath(Globals.local_connection, out_rel)
        restore_filename = get_increment_rp(mirror_rp, 10000).path

        rdiff_backup(
            True,
            True,
            restore_filename,
            rest1_rel,
            extra_options=b"--include-filelist-stdin --exclude '**' --restore",
            input=b"\n%b" % os.path.join(rest1_rel, b"various_file_types", b"regular_file"))

        self.assertTrue(os.lstat(
            os.path.join(rest1_rel, b"various_file_types", b"regular_file")))
        self.assertRaises(OSError, os.lstat,
                          os.path.join(rest1_rel, b"tester"))
        self.assertRaises(
            OSError, os.lstat,
            os.path.join(rest1_rel, b"various_file_types", b"executable"))

    def testSelFilesRemote(self):
        """Test for bug found in 0.7.[34] - filelist where source remote"""
        self.delete_tmpdirs()

        Local.vft_out.mkdir()
        # Make an exclude list
        excluderp = Local.vft_out.append("exclude")
        with excluderp.open("wb") as fp:
            fp.write(Local.vftrp.append('regular_file\n').path)
            fp.write(Local.vftrp.append('test\n').path)

        # Make an include list
        includerp = Local.vft_out.append("include")
        with includerp.open("wb") as fp:
            fp.write(Local.vftrp.append('executable\n').path)
            fp.write(Local.vftrp.append('symbolic_link\n').path)
            fp.write(Local.vftrp.append('regular_file\n').path)
            fp.write(Local.vftrp.append('test\n').path)

        rdiff_backup(False,
                     False,
                     Local.vftrp.path,
                     Local.rpout.path,
                     extra_options=b"--exclude-filelist %b --include-filelist %b"
                     b" --exclude '**'" % (excluderp.path, includerp.path))

        rdiff_backup(False,
                     False,
                     Local.rpout.path,
                     Local.rpout1.path,
                     extra_options=b"--restore-as-of now")
        self.assertTrue(os.lstat(Local.rpout1.append('executable').path))
        self.assertTrue(os.lstat(Local.rpout1.append('symbolic_link').path))
        self.assertRaises(OSError, os.lstat,
                          Local.rpout1.append('regular_file').path)
        self.assertRaises(OSError, os.lstat,
                          Local.rpout1.append('executable2').path)

    def testSelRestoreLocal(self):
        """Test selection options when restoring locally"""
        self.run_sel_restore_test(True, True)

    def testSelRestoreRemote(self):
        """Test selection options when both sides are remote"""
        self.run_sel_restore_test(False, False)

    def run_sel_restore_test(self, source_local, dest_local):
        """Test selection options with restore"""
        self.make_restore_sel_dir(source_local, dest_local)
        existing_file = self.make_restore_existing_target()
        file1_target = Local.rpout1.append("file1")
        file2_target = Local.rpout1.append("file2")
        excludes = (b"--exclude %b --exclude %b --force" %
                    (file1_target.path, existing_file.path))
        rdiff_backup(source_local,
                     dest_local,
                     Local.rpout.path,
                     Local.rpout1.path,
                     extra_options=b"--restore-as-of now " + excludes,
                     expected_ret_code=Globals.RET_CODE_WARN)
        for rp in (file1_target, file2_target, existing_file):
            rp.setdata()
        self.assertFalse(file1_target.lstat())
        self.assertTrue(file2_target.lstat())
        # excluded file shouldn't be deleted:
        self.assertTrue(existing_file.lstat())

    def make_restore_sel_dir(self, source_local, dest_local):
        """Create rdiff-backup repository at Local.rpout"""
        self.delete_tmpdirs()
        Local.vft_out.mkdir()
        rp1 = Local.vft_out.append("file1")
        rp2 = Local.vft_out.append("file2")
        rp1.touch()
        rp2.touch()
        rdiff_backup(source_local, dest_local, Local.vft_out.path,
                     Local.rpout.path)
        Myrm(Local.vft_out.path)

    def make_restore_existing_target(self):
        """Create an existing file in the restore target directory"""
        Local.rpout1.mkdir()
        existing_file = Local.rpout1.append("existing_file")
        existing_file.touch()
        return existing_file


class FinalCorrupt(PathSetter):
    """Test messing with things a bit and making sure they still work"""

    def make_dir(self):
        self.delete_tmpdirs()
        rp1 = Local.get_tgt_local_rp('final_deleted1')
        if rp1.lstat():
            Myrm(rp1.path)
        rp1.mkdir()
        rp1_1 = rp1.append('regfile')
        rp1_1.touch()
        rp1_2 = rp1.append('dir')
        rp1_2.mkdir()
        rp1_2_1 = rp1_2.append('regfile2')
        rp1_2_1.write_string('foo')

        rp2 = Local.get_tgt_local_rp('final_deleted2')
        if rp2.lstat():
            Myrm(rp2.path)
        xcopytree(rp1.path, rp2.path)
        rp2_2_1 = rp2.append('dir', 'regfile2')
        self.assertTrue(rp2_2_1.lstat())
        rp2_2_1.delete()
        rp2_2_1.touch()
        return rp1, rp1_2, rp2

    def test_dest_delete(self):
        """Test deleting a directory from the destination dir

        Obviously that directory can no longer be restored, but the
        rest of the files should be OK.  Just runs locally for now.

        """
        in_dir1, in_subdir, in_dir2 = self.make_dir()
        rdiff_backup(True,
                     True,
                     in_dir1.path,
                     Local.rpout.path,
                     current_time=10000)

        out_subdir = Local.rpout.append(in_subdir.index[-1])
        log.Log("Deleting {rp}".format(rp=out_subdir), 3)
        out_subdir.delete()
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     Local.rpout1.path,
                     extra_options=b"--restore-as-of 10000")


class FinalBugs(PathSetter):
    """Test for specific bugs that have been reported"""

    def test_symlink_popple(self):
        """Test for Popple's symlink bug

        Earlier, certain symlinks could cause data loss in _source_
        directory when regressing.  See mailing lists around 4/2/05
        for more info.

        """
        self.delete_tmpdirs()

        # Make directories
        rp1 = Local.get_tgt_local_rp('sym_in1')
        if rp1.lstat():
            rp1.delete()
        rp1.mkdir()
        rp1_d = rp1.append('subdir')
        rp1_d.mkdir()
        rp1_d_f = rp1_d.append('file')
        rp1_d_f.touch()

        rp2 = Local.get_tgt_local_rp('sym_in2')
        if rp2.lstat():
            rp2.delete()
        rp2.mkdir()
        rp2_s = rp2.append('subdir')
        rp2_s.symlink("%s/%s" % (abs_test_dir, rp1_d.path))

        # Backup
        rdiff_backup(True,
                     True,
                     rp1.path,
                     Local.rpout.path,
                     current_time=10000)
        rdiff_backup(True,
                     True,
                     rp2.path,
                     Local.rpout.path,
                     current_time=20000)

        # Make failed backup
        rbdir = Local.rpout.append('rdiff-backup-data')
        curmir = rbdir.append('current_mirror.%s.data' %
                              (Time.timetostring(30000), ))
        curmir.touch()

        # Regress
        rdiff_backup(True,
                     True,
                     Local.rpout.path,
                     None,
                     current_time=30000,
                     extra_options=b'--check-destination-dir',
                     expected_ret_code=Globals.RET_CODE_WARN)

        # Check to see if file still there
        rp1_d_f.setdata()
        self.assertTrue(rp1_d_f.isreg())

    def test_CCPP_keyerror(self):
        """Test when no change until middle of a directory

        This tests CCPP, to make sure it isn't asked to provide rorps
        for indices that are out of the cache.

        """
        self.delete_tmpdirs()

        # create the bigdir on the fly
        bigdir_path = os.path.join(comtst.abs_test_dir, b"cmd_bigdir")
        bigrp = rpath.RPath(Globals.local_connection, bigdir_path)
        bigdir_struct = {
            "subdir{}": {
                "range": 4, "contents": {
                    "subdir{}": {
                        "range": 50, "contents": {
                            "file{}": {"range": 50, "size": 1024}
                        }
                    }
                }
            }
        }
        fileset.create_fileset(bigdir_path, bigdir_struct)

        rdiff_backup(True, True, bigrp.path, Local.rpout.path)
        rp = bigrp.append('subdir3', 'subdir49', 'file49')
        self.assertTrue(rp.isreg())
        rp.touch()
        rdiff_backup(True, True, bigrp.path, Local.rpout.path)

        fileset.remove_fileset(bigdir_path, bigdir_struct)


if __name__ == "__main__":
    unittest.main()