source: flex_extract.git/Source/Pythontest/TestInstall.py @ 8028176

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

updated unit tests

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