IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 9, 2019, 2:15:34 PM (7 years ago)
Author:
fairlamb
Message:

Added local rsync to subprocess and how to handle it in backups (functionally the same as cp)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-259_genericise_backups/tools/backups/backup_test.py

    r40931 r40932  
    186186        assert os.path.exists(expected2file)
    187187
    188     def copy_fails_if_source_file_does_not_exist(self, tmpdir):
     188    def test_copy_fails_if_source_file_does_not_exist(self, tmpdir):
    189189        config = make_default_config(tmpdir, ['bck1', 'bck2'])
    190190        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     
    196196        assert not os.path.exists(source)
    197197        backy = Backup(config)
    198         with pytest.raises(errs.SubprocessError):
     198        with pytest.raises(errs.ValidationError):
    199199            backy._run_copy()
    200200
    201     def copy_fails_if_destination_does_not_exist(self, tmpdir):
     201    def test_copy_fails_if_destination_does_not_exist(self, tmpdir):
    202202        config = ConfigParser()
    203203
     
    214214        assert not os.path.exists(backup_does_not_exist)
    215215        backy = Backup(config)
    216         with pytest.raises(errs.SubprocessError):
     216        with pytest.raises(errs.ValidationError):
    217217            backy._run_copy()
    218218
     
    298298
    299299
    300 # class TestRunningRsync():
    301 
    302 #     def test_simply_rsync(self):
    303 #         assert True is False
    304 
    305 #     def test_rsync_with_additional_args(self):
    306 #         assert True is False
     300class TestRunningRsync():
     301
     302    def test_simple_rsync(self, tmpdir):
     303        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     304        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     305        config['RSYNC'] = {'sources' : f"{source}"}
     306
     307        # make file to be copied
     308        Path(source).touch()
     309
     310        backy = Backup(config)
     311
     312        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     313        expected2file = os.path.join(tmpdir, 'bck2', 'and_specific_file.jpeg')
     314        assert not os.path.exists(expected1file)
     315        assert not os.path.exists(expected2file)
     316        backy._run_rsync()
     317        assert os.path.exists(expected1file)
     318        assert os.path.exists(expected2file)
     319
     320    def test_rsync_with_additional_args(self, tmpdir):
     321        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     322        source1 = os.path.join(tmpdir, "some_dir")
     323        source2 = os.path.join(tmpdir, "and_specific_file.jpeg")
     324        config['RSYNC'] = \
     325        { 'sources' : f"{source1}, {source2}"
     326        , 'additional_args' : "-v -z -r --progress"
     327        }
     328
     329        os.makedirs(source1)
     330        Path(source2).touch()
     331
     332        backy = Backup(config)
     333
     334        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     335        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     336        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     337        expected2file = os.path.join(tmpdir, 'bck2', 'and_specific_file.jpeg')
     338        assert not os.path.exists(expected1dir)
     339        assert not os.path.exists(expected1file)
     340        assert not os.path.exists(expected2dir)
     341        assert not os.path.exists(expected2file)
     342        backy._run_rsync()
     343        assert os.path.exists(expected1dir)
     344        assert os.path.exists(expected1file)
     345        assert os.path.exists(expected2dir)
     346        assert os.path.exists(expected2file)
     347
     348    def test_rsync_fails_if_source_file_does_not_exist(self, tmpdir):
     349        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     350        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     351        config['RSYNC'] = \
     352        { 'sources' : f"{source}"
     353        , 'additional_args' : '-r'
     354        }
     355
     356        assert not os.path.exists(source)
     357        backy = Backup(config)
     358        with pytest.raises(errs.ValidationError):
     359            backy._run_rsync()
     360
     361    def test_rsync_fails_if_destination_does_not_exist(self, tmpdir):
     362        config = ConfigParser()
     363
     364        backup_does_not_exist = os.path.join(tmpdir, 'does_not_exist')
     365
     366        config['DEFAULT'] = \
     367        { 'backup_paths' : backup_does_not_exist
     368        , 'source_machine': socket.gethostname()
     369        }
     370        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     371        Path(source).touch()
     372        config['RSYNC'] = {'sources' : f"{source}"}
     373
     374        assert not os.path.exists(backup_does_not_exist)
     375        backy = Backup(config)
     376        with pytest.raises(errs.ValidationError):
     377            backy._run_rsync()
     378
     379    def test_rsync_requires_dash_r_arg_for_copying_dirs(self, tmpdir):
     380        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     381        source = os.path.join(tmpdir, "some_dir")
     382        config['RSYNC'] = \
     383        { 'sources' : f"{source}"
     384        }
     385        os.makedirs(source)
     386
     387        backy = Backup(config)
     388        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     389        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     390        assert not os.path.exists(expected1dir)
     391        assert not os.path.exists(expected2dir)
     392        backy._run_rsync()
     393        assert not os.path.exists(expected1dir)
     394        assert not os.path.exists(expected2dir)
     395
     396        config['RSYNC'] = \
     397        { 'sources' : f"{source}"
     398        , 'additional_args' : '-r'
     399        }
     400
     401        backy = Backup(config)
     402        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     403        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     404        assert not os.path.exists(expected1dir)
     405        assert not os.path.exists(expected2dir)
     406        backy._run_rsync()
     407        assert os.path.exists(expected1dir)
     408        assert os.path.exists(expected2dir)
    307409
    308410
Note: See TracChangeset for help on using the changeset viewer.