IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40923


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

Location:
branches/ipp-259_genericise_backups/tools/backups
Files:
2 edited

Legend:

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

    r40920 r40923  
    44
    55import backups.utils.config_parse_helper as cfg_help
    6 from backups.utils.errors import ConfigError
     6import backups.utils.errors as errs
     7import backups.utils.subprocess_utils as sub_utils
    78from configparser import ConfigParser
    89
     
    2425        config = ConfigParser()
    2526        if config_class_or_filepath is None:
    26             raise ConfigError(config_class_or_filepath, "Neither config settings or filepath of config given")
     27            raise errs.ConfigError(config_class_or_filepath, "Neither config settings or filepath of config given")
    2728        elif isinstance(config_class_or_filepath, configparser.ConfigParser):
    2829            config = config_class_or_filepath
     
    119120        actual_hostname = socket.gethostname()
    120121        return actual_hostname == self.default_dict['source_machine']
     122
     123    def run_backup(self):
     124            self._run_copy()
     125            self._run_tar()
     126            self._run_rsync()
     127            self._run_mysqldump()
     128
     129    def _run_copy(self):
     130        if self.run_copy is None or False:
     131            return 0
     132
     133        for bp in self.default_dict['backup_paths']:
     134            if not path.exists(bp):
     135                    raise errs.ValidationError(f"ValidationError: target path does not exist: {bp}")
     136
     137            for s in self.copy_dict['sources']:
     138                if not path.exists(s):
     139                    raise errs.ValidationError(f"ValidationError: target path does not exist: {s}")
     140                try:
     141                    args = [s, bp]
     142                    if 'additional_args' in self.copy_dict.keys():
     143                        print("hiya")
     144                        args = args + self.copy_dict['additional_args']
     145                    sub_utils.cp_wrapper(args)
     146                except errs.SubprocessError as e:
     147                    raise errs.SubprocessError( f"Error: copying\n"
     148                                                f"    from: {s}\n"
     149                                                f"    to: {bp}\n"
     150                                              ) from e
     151        return 0
     152
     153    def _run_tar(self):
     154        if self.run_tar is None or False:
     155            return False
     156        return True
     157
     158    def _run_rsync(self):
     159        if self.run_rsync is None or False:
     160            return False
     161        return True
     162
     163    def _run_mysqldump(self):
     164        if self.run_mysqldump is None or False:
     165            return False
     166        return True
  • 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.