IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 8, 2019, 3:17:21 PM (7 years ago)
Author:
fairlamb
Message:

Fleshed out basic copy functionality; based off of atlassian backups

File:
1 edited

Legend:

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

    r40920 r40923  
    33import socket
    44from configparser import ConfigParser
    5 
     5from pathlib import Path
     6
     7import backups.utils.errors as errs
    68from backups.backup import Backup
    7 from backups.utils.errors import ConfigError
    89
    910
     
    1718    config = ConfigParser()
    1819
     20    # make the paths:
     21    for b in backup_targs:
     22        os.makedirs(os.path.join(tmpdir, b))
     23
     24    # Get the correct formats for the config
    1925    backup_paths = ','.join([os.path.join(tmpdir, b) for b in backup_targs])
    2026    source_machine = socket.gethostname()
    21 
    2227    config['DEFAULT'] = \
    2328    {
     
    98103    def test_failure_if_no_defaults_provided(self):
    99104        config = ConfigParser()
    100         with pytest.raises(ConfigError):
     105        with pytest.raises(errs.ConfigError):
    101106            Backup(config_file=config)
    102107
    103         with pytest.raises(ConfigError):
     108        with pytest.raises(errs.ConfigError):
    104109            Backup(config_file=None)
    105110
    106         with pytest.raises(ConfigError):
     111        with pytest.raises(errs.ConfigError):
    107112            Backup(config_file="/not/a/real/file.config")
    108113
     
    130135        backy.default_dict['source_machine'] = actual_hostname
    131136        assert backy.hostname_is_valid() is True
     137
     138
     139class TestRunningCopy():
     140
     141    def test_simple_copy(self, tmpdir):
     142        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     143        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     144        config['COPY'] = {'sources' : f"{source}"}
     145
     146        # make file to be copied
     147        Path(source).touch()
     148
     149        backy = Backup(config)
     150
     151        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     152        expected2file = os.path.join(tmpdir, 'bck2', 'and_specific_file.jpeg')
     153        assert not os.path.exists(expected1file)
     154        assert not os.path.exists(expected2file)
     155        backy._run_copy()
     156        assert os.path.exists(expected1file)
     157        assert os.path.exists(expected2file)
     158
     159    def test_copy_with_additional_args(self, tmpdir):
     160        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     161        source1 = os.path.join(tmpdir, "some_dir")
     162        source2 = os.path.join(tmpdir, "and_specific_file.jpeg")
     163        config['COPY'] = \
     164        { 'sources' : f"{source1}, {source2}"
     165        , 'additional_args' : "-v --preserve -r"
     166        }
     167
     168        os.makedirs(source1)
     169        Path(source2).touch()
     170
     171        backy = Backup(config)
     172
     173        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     174        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     175        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     176        expected2file = os.path.join(tmpdir, 'bck2', 'and_specific_file.jpeg')
     177        assert not os.path.exists(expected1dir)
     178        assert not os.path.exists(expected1file)
     179        assert not os.path.exists(expected2dir)
     180        assert not os.path.exists(expected2file)
     181        backy._run_copy()
     182        assert os.path.exists(expected1dir)
     183        assert os.path.exists(expected1file)
     184        assert os.path.exists(expected2dir)
     185        assert os.path.exists(expected2file)
     186
     187    def copy_fails_if_source_file_does_not_exist(self, tmpdir):
     188        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     189        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     190        config['COPY'] = \
     191        { 'sources' : f"{source}"
     192        , 'additional_args' : '-r'
     193        }
     194
     195        assert not os.path.exists(source)
     196        backy = Backup(config)
     197        with pytest.raises(errs.SubprocessError):
     198            backy._run_copy()
     199
     200    def copy_fails_if_destination_does_not_exist(self, tmpdir):
     201        config = ConfigParser()
     202
     203        backup_does_not_exist = os.path.join(tmpdir, 'does_not_exist')
     204
     205        config['DEFAULT'] = \
     206        { 'backup_paths' : backup_does_not_exist
     207        , 'source_machine': socket.gethostname()
     208        }
     209        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     210        Path(source).touch()
     211        config['COPY'] = {'sources' : f"{source}"}
     212
     213        assert not os.path.exists(backup_does_not_exist)
     214        backy = Backup(config)
     215        with pytest.raises(errs.SubprocessError):
     216            backy._run_copy()
     217
     218    def test_copy_requires_dash_r_arg_for_copying_dirs(self, tmpdir):
     219        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     220        source = os.path.join(tmpdir, "some_dir")
     221        config['COPY'] = \
     222        { 'sources' : f"{source}"
     223        }
     224        os.makedirs(source)
     225
     226        with pytest.raises(errs.SubprocessError):
     227            backy = Backup(config)
     228            backy._run_copy()
     229
     230        config['COPY'] = \
     231        { 'sources' : f"{source}"
     232        , 'additional_args' : '-r'
     233        }
     234        backy = Backup(config)
     235
     236        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     237        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     238        assert not os.path.exists(expected1dir)
     239        assert not os.path.exists(expected2dir)
     240        backy._run_copy()
     241        assert os.path.exists(expected1dir)
     242        assert os.path.exists(expected2dir)
     243
     244
     245# class TestRunningTar():
     246
     247#     def test_simply_tar(self):
     248#         assert True is False
     249
     250#     def test_tar_with_additional_args(self):
     251#         assert True is False
     252
     253
     254# class TestRunningRsync():
     255
     256#     def test_simply_rsync(self):
     257#         assert True is False
     258
     259#     def test_rsync_with_additional_args(self):
     260#         assert True is False
     261
     262
     263# class TestRunningMysqldump():
     264
     265#     def test_simply_mysqldump(self):
     266#         assert True is False
     267
     268#     def test_mysqldump_with_additional_args(self):
     269#         assert True is False
Note: See TracChangeset for help on using the changeset viewer.