Changeset 3946de5 in flex_extract.git for source/pythontest/TestInstall.py


Ignore:
Timestamp:
Nov 16, 2018, 8:07:45 PM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
5e7c588
Parents:
1abf820
Message:

added testing for most of the install functions; improved install functions with exception handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/pythontest/TestInstall.py

    rc5074d2 r3946de5  
    66import os
    77import inspect
     8import subprocess
     9import tarfile
     10import errno
     11import shutil
     12from genshi.template import TemplateLoader
     13from genshi.template.eval import  UndefinedError
     14from exceptions import OSError
    815import pytest
     16from mock import patch
    917
    1018sys.path.append('../python')
    1119import _config
    12 import install
     20import _config_test
     21from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob,
     22                     mk_job_template)
     23
    1324from mods.tools import make_dir, silent_remove
    1425
    15 
    16 class TestTools():
     26#    - main
     27#    - get_install_cmdline_arguments
     28#    - install_via_gateway
     29#    - delete_convert_build
     30#    - make_convert_build
     31
     32class TestInstall():
    1733    '''
    1834    '''
    19 
    20 
    21     #    - main
    22     #    - get_install_cmdline_arguments
    23     #    - install_via_gateway
    24     #!    - mk_tarball
    25     #!    - un_tarball
    26     #!    - mk_env_vars
    27     #!    - mk_compilejob
    28     #!    - mk_job_template
    29     #    - delete_convert_build
    30     #    - make_convert_build
    31 
    32     def test_mk_tarball_local(self):
    33         import tarfile
    34 
     35    @classmethod
     36    def setup_class(self):
     37        self.testdir = _config_test.PATH_TEST_DIR
     38        self.testfilesdir = _config_test.PATH_TESTFILES_DIR
     39        self.testinstalldir = _config_test.PATH_TESTINSTALL_DIR
     40
     41        # make test tarballs from shell script
     42        subprocess.check_output([os.path.join(self.testinstalldir,
     43                                              'mk_install_tar.sh')])
     44
     45        # un tar the test tarballs from shell script
     46        subprocess.check_output([os.path.join(self.testinstalldir,
     47                                              'un_install_tar.sh')])
     48
     49
     50
     51    @patch('tarfile.open', side_effect=[subprocess.CalledProcessError(1,'test'),
     52                                        OSError(errno.EEXIST)])
     53    def test_fail_mk_tarball_local(self, mock_open):
     54        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
     55        # create test tarball and list its content files
     56        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
     57
     58        with pytest.raises(SystemExit):
     59            mk_tarball(ecd + tarballname, 'local')
     60        with pytest.raises(SystemExit):
     61            mk_tarball(ecd + tarballname, 'local')
     62
     63    def test_success_mk_tarball_local(self):
    3564        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
    3665
    3766        # list comparison files for tarball content
    38         tar_test_dir = os.path.join(_config.PATH_TEST_DIR +
    39                                     os.path.sep + 'TestInstallTar')
     67        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
    4068        tar_test_file = os.path.join(tar_test_dir,
    4169                                     'flex_extract_v7.1_local.tar')
     
    4573        # create test tarball and list its content files
    4674        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
    47         install.mk_tarball(ecd + tarballname, 'local')
     75        mk_tarball(ecd + tarballname, 'local')
    4876        with tarfile.open(ecd + tarballname, 'r') as tar_handle:
    4977            tar_content_list = tar_handle.getnames()
     
    5684        assert sorted(comparison_list) == sorted(tar_content_list)
    5785
    58     def test_mk_tarball_ecgate(self):
    59         import tarfile
    60 
     86    def test_success_mk_tarball_ecgate(self):
    6187        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
    6288
    6389        # list comparison files for tarball content
    64         tar_test_dir = os.path.join(_config.PATH_TEST_DIR +
    65                                     os.path.sep + 'TestInstallTar')
     90        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
    6691        tar_test_file = os.path.join(tar_test_dir,
    6792                                     'flex_extract_v7.1_ecgate.tar')
     
    7196        # create test tarball and list its content files
    7297        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgatetest.tar'
    73         install.mk_tarball(ecd + tarballname, 'ecgate')
     98        mk_tarball(ecd + tarballname, 'ecgate')
    7499        with tarfile.open(ecd + tarballname, 'r') as tar_handle:
    75100            tar_content_list = tar_handle.getnames()
     
    82107        assert sorted(comparison_list) == sorted(tar_content_list)
    83108
    84     def test_un_tarball(self):
    85         import tarfile
    86         import shutil
    87 
     109    @patch('tarfile.open', side_effect=[tarfile.TarError, OSError])
     110    def test_fail_un_tarball(self, mock_open):
     111        with pytest.raises(SystemExit):
     112            un_tarball('testpath')
     113
     114    def test_success_ecgate_un_tarball(self):
    88115        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
    89116
    90117        # list comparison files for tarball content
    91         tar_test_dir = os.path.join(_config.PATH_TEST_DIR +
    92                                         os.path.sep + 'TestInstallTar')
    93         tar_test_fedir = os.path.join(tar_test_dir, 'flex_extract_v7.1_ecgate')
     118        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
     119        cmp_dir = _config.FLEXEXTRACT_DIRNAME + '_ecgate'
     120        tar_test_fedir = os.path.join(tar_test_dir, cmp_dir)
    94121        comparison_list = []
    95122        for path, subdirs, files in os.walk(tar_test_fedir):
     
    100127
    101128        # untar in test directory
    102         test_dir = os.path.join(tar_test_dir, 'test_untar')
     129        test_dir = os.path.join(tar_test_dir, 'test_ecgate')
    103130        make_dir(test_dir)
    104131        os.chdir(test_dir)
    105132        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar'
    106         install.un_tarball(os.path.join(tar_test_dir, tarballname))
     133        un_tarball(os.path.join(tar_test_dir, tarballname))
    107134        tarfiles_list = []
    108135        for path, subdirs, files in os.walk(test_dir):
     
    114141        assert sorted(tarfiles_list) == sorted(comparison_list)
    115142
    116         # clean up temp test dir
    117         shutil.rmtree(test_dir)
    118 
    119     def test_mk_env_vars(self):
     143    def test_success_local_un_tarball(self):
     144        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
     145
     146        # list comparison files for tarball content
     147        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
     148        cmp_dir = _config.FLEXEXTRACT_DIRNAME + '_local'
     149        tar_test_fedir = os.path.join(tar_test_dir, cmp_dir)
     150        comparison_list = []
     151        for path, subdirs, files in os.walk(tar_test_fedir):
     152            for name in files:
     153                if 'tar' not in name:
     154                    comparison_list.append(os.path.relpath(
     155                        os.path.join(path, name), tar_test_fedir))
     156
     157        # untar in test directory
     158        test_dir = os.path.join(tar_test_dir, 'test_local')
     159        make_dir(test_dir)
     160        os.chdir(test_dir)
     161        tarballname = _config.FLEXEXTRACT_DIRNAME + '_local.tar'
     162        un_tarball(os.path.join(tar_test_dir, tarballname))
     163        tarfiles_list = []
     164        for path, subdirs, files in os.walk(test_dir):
     165            for name in files:
     166                tarfiles_list.append(os.path.relpath(
     167                    os.path.join(path, name), test_dir))
     168
     169        # test for equality
     170        assert sorted(tarfiles_list) == sorted(comparison_list)
     171
     172    @patch('_config.PATH_ECMWF_ENV', _config_test.PATH_TESTFILES_DIR+'/ecmwf_test')
     173    def test_success_mk_env_vars(self):
    120174        import filecmp
    121175
    122         # comparison file
    123         testfile = os.path.join(_config.PATH_TEST_DIR,'TestData',
     176        cmp_file = os.path.join(self.testfilesdir,
    124177                                'ECMWF_ENV.test')
    125178
    126         # create test ECMWF_ENV file
    127         install.mk_env_vars('testuser',
     179        mk_env_vars('testuser',
     180                    'testgroup',
     181                    'gateway.test.ac.at',
     182                    'user@destination')
     183
     184        assert filecmp.cmp(cmp_file, _config.PATH_ECMWF_ENV, shallow=False)
     185
     186        silent_remove(_config.PATH_ECMWF_ENV)
     187
     188    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
     189    def test_fail_load_mk_env_vars(self, mock_generate):
     190        with pytest.raises(SystemExit):
     191            mk_env_vars('testuser',
     192                        'testgroup',
     193                        'gateway.test.ac.at',
     194                        'user@destination')
     195
     196    def test_fail_generate_mk_env_vars(self):
     197        with patch('genshi.template.TemplateLoader.load') as MockHelper:
     198            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
     199            with pytest.raises(SystemExit):
     200                mk_env_vars('testuser',
    128201                            'testgroup',
    129202                            'gateway.test.ac.at',
    130203                            'user@destination')
    131204
    132         assert filecmp.cmp(testfile, _config.PATH_ECMWF_ENV, shallow=False)
    133 
    134         # delte test file
    135         silent_remove(_config.PATH_ECMWF_ENV)
    136 
    137     def test_mk_compilejob(self):
     205    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
     206    def test_fail_open_mk_env_vars(self, mock_open):
     207        with pytest.raises(SystemExit):
     208            mk_env_vars('testuser',
     209                        'testgroup',
     210                        'gateway.test.ac.at',
     211                        'user@destination')
     212
     213    @patch('_config.FILE_INSTALL_COMPILEJOB', _config_test.PATH_TESTFILES_DIR+'/compilejob_test.ksh')
     214    def test_success_mk_compilejob(self):
    138215        import filecmp
    139216
    140         # comparison file
    141         testfile = os.path.join(_config.PATH_TEST_DIR,'TestData',
    142                                     'compilejob.test')
    143 
    144         # create
    145         install.mk_compilejob('Makefile.TEST',
     217        testfile = os.path.join(self.testfilesdir,
     218                                'compilejob.test')
     219
     220        mk_compilejob('Makefile.TEST',
     221                      '',
     222                      'testuser',
     223                      'testgroup',
     224                      'fp_root_test_path')
     225
     226        finalfile = os.path.join(_config.PATH_JOBSCRIPTS,
     227                                 _config.FILE_INSTALL_COMPILEJOB)
     228        assert filecmp.cmp(testfile, finalfile, shallow=False)
     229
     230        silent_remove(finalfile)
     231
     232    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
     233    def test_fail_load_mk_compilejob(self, mock_generate):
     234        with pytest.raises(SystemExit):
     235            mk_compilejob('Makefile.TEST',
     236                          '',
     237                          'testuser',
     238                          'testgroup',
     239                          'fp_root_test_path')
     240
     241    def test_fail_generate_mk_compilejob(self):
     242        with patch('genshi.template.TemplateLoader.load') as MockHelper:
     243            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
     244            with pytest.raises(SystemExit):
     245                mk_compilejob('Makefile.TEST',
    146246                              '',
    147247                              'testuser',
     
    149249                              'fp_root_test_path')
    150250
    151         finalfile = os.path.join(_config.PATH_JOBSCRIPTS,
    152                               _config.FILE_INSTALL_COMPILEJOB)
     251    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
     252    def test_fail_open_mk_compilejob(self, mock_open):
     253        with pytest.raises(SystemExit):
     254            mk_compilejob('Makefile.TEST',
     255                          '',
     256                          'testuser',
     257                          'testgroup',
     258                          'fp_root_test_path')
     259
     260    @patch('_config.TEMPFILE_JOB', _config_test.PATH_TESTFILES_DIR+'/job_temp.test_test')
     261    def test_success_mk_job_template(self):
     262        import filecmp
     263
     264        testfile = os.path.join(self.testfilesdir,
     265                                'job.temp.test')
     266
     267        mk_job_template('testuser',
     268                        'testgroup',
     269                        'gateway.test.ac.at',
     270                        'dest@generic',
     271                        'fp_root_test_path')
     272
     273        finalfile = os.path.join(_config.PATH_TEMPLATES,
     274                                 _config.TEMPFILE_JOB)
    153275        assert filecmp.cmp(testfile, finalfile, shallow=False)
    154276
    155         # delete test file
    156277        silent_remove(finalfile)
    157278
    158     def test_mk_job_template(self):
    159         import filecmp
    160 
    161         # comparison file
    162         testfile = os.path.join(_config.PATH_TEST_DIR,
    163                                 'TestData',
    164                                 'job.temp.test')
    165 
    166         # create
    167         install.mk_job_template('testuser',
     279    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
     280    def test_fail_load_mk_job_template(self, mock_generate):
     281        with pytest.raises(SystemExit):
     282            mk_job_template('testuser',
     283                            'testgroup',
     284                            'gateway.test.ac.at',
     285                            'dest@generic',
     286                            'fp_root_test_path')
     287
     288    def test_fail_generate_mk_job_template(self):
     289        with patch('genshi.template.TemplateLoader.load') as MockHelper:
     290            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
     291            with pytest.raises(SystemExit):
     292                mk_job_template('testuser',
    168293                                'testgroup',
    169294                                'gateway.test.ac.at',
     
    171296                                'fp_root_test_path')
    172297
    173         finalfile = os.path.join(_config.PATH_TEMPLATES,
    174                                  _config.TEMPFILE_JOB)
    175         assert filecmp.cmp(testfile, finalfile, shallow=False)
    176 
    177         # delete test file
    178         silent_remove(finalfile)
     298    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
     299    def test_fail_open_mk_job_template(self, mock_open):
     300        with pytest.raises(SystemExit):
     301            mk_job_template('testuser',
     302                            'testgroup',
     303                            'gateway.test.ac.at',
     304                            'dest@generic',
     305                            'fp_root_test_path')
     306
     307    @classmethod
     308    def teardown_class(self):
     309
     310        test_dir = os.path.join(self.testinstalldir,
     311                                _config.FLEXEXTRACT_DIRNAME + '_local')
     312        shutil.rmtree(test_dir)
     313        test_dir = os.path.join(self.testinstalldir,
     314                                _config.FLEXEXTRACT_DIRNAME + '_ecgate')
     315        shutil.rmtree(test_dir)
     316
     317        test_dir = os.path.join(self.testinstalldir,
     318                                'test_local')
     319        shutil.rmtree(test_dir)
     320        test_dir = os.path.join(self.testinstalldir,
     321                                'test_ecgate')
     322        shutil.rmtree(test_dir)
     323
     324        tar_file = os.path.join(self.testinstalldir,
     325                     _config.FLEXEXTRACT_DIRNAME + '_local.tar')
     326        os.remove(tar_file)
     327        tar_file = os.path.join(self.testinstalldir,
     328                                _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar')
     329        os.remove(tar_file)
     330        pass
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG