source: flex_extract.git/Source/Pythontest/TestInstall.py @ 76c37a9

ctbtodev
Last change on this file since 76c37a9 was 76c37a9, checked in by Anne Philipp <anne.philipp@…>, 4 years ago

updated unit tests to work with updated code

  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[76c37a9]1#!/usr/bin/env python3
[067a0c3]2# -*- coding: utf-8 -*-
3
[25b14be]4
[067a0c3]5import sys
6import os
7import inspect
[3946de5]8import subprocess
9import tarfile
10import errno
11import shutil
12from genshi.template import TemplateLoader
[76c37a9]13from genshi.template.eval import UndefinedError
14
15try:
16    import exceptions
17except ImportError:
18    import builtins
[25b14be]19import pytest
[3946de5]20from mock import patch
[25b14be]21
[76c37a9]22sys.path.append('../Python')
[2fb99de]23import _config
[76c37a9]24from . import _config_test
[3946de5]25from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob,
26                     mk_job_template)
27
[76c37a9]28from Mods.tools import make_dir, silent_remove
[067a0c3]29
[3946de5]30#    - main
31#    - get_install_cmdline_arguments
32#    - install_via_gateway
33#    - delete_convert_build
34#    - make_convert_build
[067a0c3]35
[3946de5]36class TestInstall():
[067a0c3]37    '''
38    '''
[3946de5]39    @classmethod
40    def setup_class(self):
41        self.testdir = _config_test.PATH_TEST_DIR
42        self.testfilesdir = _config_test.PATH_TESTFILES_DIR
43        self.testinstalldir = _config_test.PATH_TESTINSTALL_DIR
44
45        # make test tarballs from shell script
46        subprocess.check_output([os.path.join(self.testinstalldir,
47                                              'mk_install_tar.sh')])
[067a0c3]48
[3946de5]49        # un tar the test tarballs from shell script
[76c37a9]50#        subprocess.check_output([os.path.join(self.testinstalldir,
51#                                            'un_install_tar.sh')])
[067a0c3]52
53
54
[3946de5]55    @patch('tarfile.open', side_effect=[subprocess.CalledProcessError(1,'test'),
56                                        OSError(errno.EEXIST)])
57    def test_fail_mk_tarball_local(self, mock_open):
58        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
59        # create test tarball and list its content files
60        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
61
62        with pytest.raises(SystemExit):
63            mk_tarball(ecd + tarballname, 'local')
64
65    def test_success_mk_tarball_local(self):
[2fb99de]66        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
67
68        # list comparison files for tarball content
[3946de5]69        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
[25b14be]70        tar_test_file = os.path.join(tar_test_dir,
71                                     'flex_extract_v7.1_local.tar')
72        with tarfile.open(tar_test_file, 'r') as tar_handle:
73            comparison_list = tar_handle.getnames()
[2fb99de]74
[25b14be]75        # create test tarball and list its content files
76        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
[3946de5]77        mk_tarball(ecd + tarballname, 'local')
[25b14be]78        with tarfile.open(ecd + tarballname, 'r') as tar_handle:
79            tar_content_list = tar_handle.getnames()
80
81        # remove test tar file from flex_extract directory
[76c37a9]82        #os.remove(ecd + tarballname)
[25b14be]83
84        # test if comparison filelist is equal to the
85        # filelist of tarball content
86        assert sorted(comparison_list) == sorted(tar_content_list)
87
[3946de5]88    def test_success_mk_tarball_ecgate(self):
[25b14be]89        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
90
91        # list comparison files for tarball content
[3946de5]92        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
[25b14be]93        tar_test_file = os.path.join(tar_test_dir,
94                                     'flex_extract_v7.1_ecgate.tar')
95        with tarfile.open(tar_test_file, 'r') as tar_handle:
96            comparison_list = tar_handle.getnames()
[2fb99de]97
98        # create test tarball and list its content files
[25b14be]99        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgatetest.tar'
[3946de5]100        mk_tarball(ecd + tarballname, 'ecgate')
[2fb99de]101        with tarfile.open(ecd + tarballname, 'r') as tar_handle:
102            tar_content_list = tar_handle.getnames()
103
104        # remove test tar file from flex_extract directory
105        os.remove(ecd + tarballname)
106
107        # test if comparison filelist is equal to the
108        # filelist of tarball content
109        assert sorted(comparison_list) == sorted(tar_content_list)
110
[3946de5]111    @patch('tarfile.open', side_effect=[tarfile.TarError, OSError])
112    def test_fail_un_tarball(self, mock_open):
113        with pytest.raises(SystemExit):
114            un_tarball('testpath')
[2fb99de]115
[3946de5]116    def test_success_ecgate_un_tarball(self):
[2fb99de]117        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
118
119        # list comparison files for tarball content
[3946de5]120        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
121        cmp_dir = _config.FLEXEXTRACT_DIRNAME + '_ecgate'
122        tar_test_fedir = os.path.join(tar_test_dir, cmp_dir)
[2fb99de]123        comparison_list = []
124        for path, subdirs, files in os.walk(tar_test_fedir):
125            for name in files:
126                if 'tar' not in name:
[25b14be]127                    comparison_list.append(os.path.relpath(
128                        os.path.join(path, name), tar_test_fedir))
[2fb99de]129
130        # untar in test directory
[3946de5]131        test_dir = os.path.join(tar_test_dir, 'test_ecgate')
[2fb99de]132        make_dir(test_dir)
133        os.chdir(test_dir)
[25b14be]134        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar'
[3946de5]135        un_tarball(os.path.join(tar_test_dir, tarballname))
[2fb99de]136        tarfiles_list = []
137        for path, subdirs, files in os.walk(test_dir):
138            for name in files:
[25b14be]139                tarfiles_list.append(os.path.relpath(
140                    os.path.join(path, name), test_dir))
[2fb99de]141
142        # test for equality
143        assert sorted(tarfiles_list) == sorted(comparison_list)
144
[3946de5]145    def test_success_local_un_tarball(self):
146        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
[0aaeb04]147
[3946de5]148        # list comparison files for tarball content
149        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
150        cmp_dir = _config.FLEXEXTRACT_DIRNAME + '_local'
151        tar_test_fedir = os.path.join(tar_test_dir, cmp_dir)
152        comparison_list = []
153        for path, subdirs, files in os.walk(tar_test_fedir):
154            for name in files:
155                if 'tar' not in name:
156                    comparison_list.append(os.path.relpath(
157                        os.path.join(path, name), tar_test_fedir))
158
159        # untar in test directory
160        test_dir = os.path.join(tar_test_dir, 'test_local')
161        make_dir(test_dir)
162        os.chdir(test_dir)
163        tarballname = _config.FLEXEXTRACT_DIRNAME + '_local.tar'
164        un_tarball(os.path.join(tar_test_dir, tarballname))
165        tarfiles_list = []
166        for path, subdirs, files in os.walk(test_dir):
167            for name in files:
168                tarfiles_list.append(os.path.relpath(
169                    os.path.join(path, name), test_dir))
170
171        # test for equality
172        assert sorted(tarfiles_list) == sorted(comparison_list)
173
174    @patch('_config.PATH_ECMWF_ENV', _config_test.PATH_TESTFILES_DIR+'/ecmwf_test')
175    def test_success_mk_env_vars(self):
[0aaeb04]176        import filecmp
177
[3946de5]178        cmp_file = os.path.join(self.testfilesdir,
[0aaeb04]179                                'ECMWF_ENV.test')
180
[3946de5]181        mk_env_vars('testuser',
182                    'testgroup',
183                    'gateway.test.ac.at',
184                    'user@destination')
185
186        assert filecmp.cmp(cmp_file, _config.PATH_ECMWF_ENV, shallow=False)
187
188        silent_remove(_config.PATH_ECMWF_ENV)
189
190    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
191    def test_fail_load_mk_env_vars(self, mock_generate):
192        with pytest.raises(SystemExit):
193            mk_env_vars('testuser',
194                        'testgroup',
195                        'gateway.test.ac.at',
196                        'user@destination')
197
198    def test_fail_generate_mk_env_vars(self):
199        with patch('genshi.template.TemplateLoader.load') as MockHelper:
200            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
201            with pytest.raises(SystemExit):
202                mk_env_vars('testuser',
[0aaeb04]203                            'testgroup',
204                            'gateway.test.ac.at',
205                            'user@destination')
206
[3946de5]207    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
208    def test_fail_open_mk_env_vars(self, mock_open):
209        with pytest.raises(SystemExit):
210            mk_env_vars('testuser',
211                        'testgroup',
212                        'gateway.test.ac.at',
213                        'user@destination')
[0aaeb04]214
[3946de5]215    @patch('_config.FILE_INSTALL_COMPILEJOB', _config_test.PATH_TESTFILES_DIR+'/compilejob_test.ksh')
216    def test_success_mk_compilejob(self):
[0aaeb04]217        import filecmp
218
[3946de5]219        testfile = os.path.join(self.testfilesdir,
220                                'compilejob.test')
[0aaeb04]221
[3946de5]222        mk_compilejob('Makefile.TEST',
223                      '',
224                      'testuser',
225                      'testgroup',
226                      'fp_root_test_path')
[0aaeb04]227
228        finalfile = os.path.join(_config.PATH_JOBSCRIPTS,
[3946de5]229                                 _config.FILE_INSTALL_COMPILEJOB)
[0aaeb04]230        assert filecmp.cmp(testfile, finalfile, shallow=False)
231
232        silent_remove(finalfile)
[c5074d2]233
[3946de5]234    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
235    def test_fail_load_mk_compilejob(self, mock_generate):
236        with pytest.raises(SystemExit):
237            mk_compilejob('Makefile.TEST',
238                          '',
239                          'testuser',
240                          'testgroup',
241                          'fp_root_test_path')
242
243    def test_fail_generate_mk_compilejob(self):
244        with patch('genshi.template.TemplateLoader.load') as MockHelper:
245            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
246            with pytest.raises(SystemExit):
247                mk_compilejob('Makefile.TEST',
248                              '',
249                              'testuser',
250                              'testgroup',
251                              'fp_root_test_path')
252
253    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
254    def test_fail_open_mk_compilejob(self, mock_open):
255        with pytest.raises(SystemExit):
256            mk_compilejob('Makefile.TEST',
257                          '',
258                          'testuser',
259                          'testgroup',
260                          'fp_root_test_path')
261
262    @patch('_config.TEMPFILE_JOB', _config_test.PATH_TESTFILES_DIR+'/job_temp.test_test')
263    def test_success_mk_job_template(self):
[c5074d2]264        import filecmp
265
[3946de5]266        testfile = os.path.join(self.testfilesdir,
[c5074d2]267                                'job.temp.test')
268
[3946de5]269        mk_job_template('testuser',
270                        'testgroup',
271                        'gateway.test.ac.at',
272                        'dest@generic',
273                        'fp_root_test_path')
274
275        finalfile = os.path.join(_config.PATH_TEMPLATES,
276                                 _config.TEMPFILE_JOB)
277        assert filecmp.cmp(testfile, finalfile, shallow=False)
278
279        silent_remove(finalfile)
280
281    @patch('genshi.template.TemplateLoader', side_effect=[OSError])
282    def test_fail_load_mk_job_template(self, mock_generate):
283        with pytest.raises(SystemExit):
284            mk_job_template('testuser',
285                            'testgroup',
286                            'gateway.test.ac.at',
287                            'dest@generic',
288                            'fp_root_test_path')
289
290    def test_fail_generate_mk_job_template(self):
291        with patch('genshi.template.TemplateLoader.load') as MockHelper:
292            MockHelper.return_value.generate.side_effect = UndefinedError('undefined')
293            with pytest.raises(SystemExit):
294                mk_job_template('testuser',
[c5074d2]295                                'testgroup',
296                                'gateway.test.ac.at',
297                                'dest@generic',
298                                'fp_root_test_path')
299
[3946de5]300    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
301    def test_fail_open_mk_job_template(self, mock_open):
302        with pytest.raises(SystemExit):
303            mk_job_template('testuser',
304                            'testgroup',
305                            'gateway.test.ac.at',
306                            'dest@generic',
307                            'fp_root_test_path')
308
309    @classmethod
310    def teardown_class(self):
311
312        test_dir = os.path.join(self.testinstalldir,
313                                _config.FLEXEXTRACT_DIRNAME + '_local')
[76c37a9]314#        shutil.rmtree(test_dir)
[3946de5]315        test_dir = os.path.join(self.testinstalldir,
316                                _config.FLEXEXTRACT_DIRNAME + '_ecgate')
[76c37a9]317#        shutil.rmtree(test_dir)
[3946de5]318
319        test_dir = os.path.join(self.testinstalldir,
320                                'test_local')
[76c37a9]321#        shutil.rmtree(test_dir)
[3946de5]322        test_dir = os.path.join(self.testinstalldir,
323                                'test_ecgate')
[76c37a9]324#        shutil.rmtree(test_dir)
[c5074d2]325
[3946de5]326        tar_file = os.path.join(self.testinstalldir,
327                     _config.FLEXEXTRACT_DIRNAME + '_local.tar')
[76c37a9]328#        os.remove(tar_file)
[3946de5]329        tar_file = os.path.join(self.testinstalldir,
330                                _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar')
[76c37a9]331#        os.remove(tar_file)
[3946de5]332        pass
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG