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
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4
5import sys
6import os
7import inspect
8import subprocess
9import tarfile
10import errno
11import shutil
12from genshi.template import TemplateLoader
13from genshi.template.eval import UndefinedError
14
15try:
16    import exceptions
17except ImportError:
18    import builtins
19import pytest
20from mock import patch
21
22sys.path.append('../Python')
23import _config
24from . import _config_test
25from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob,
26                     mk_job_template)
27
28from Mods.tools import make_dir, silent_remove
29
30#    - main
31#    - get_install_cmdline_arguments
32#    - install_via_gateway
33#    - delete_convert_build
34#    - make_convert_build
35
36class TestInstall():
37    '''
38    '''
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')])
48
49        # un tar the test tarballs from shell script
50#        subprocess.check_output([os.path.join(self.testinstalldir,
51#                                            'un_install_tar.sh')])
52
53
54
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):
66        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
67
68        # list comparison files for tarball content
69        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
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()
74
75        # create test tarball and list its content files
76        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
77        mk_tarball(ecd + tarballname, 'local')
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
82        #os.remove(ecd + tarballname)
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
88    def test_success_mk_tarball_ecgate(self):
89        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
90
91        # list comparison files for tarball content
92        tar_test_dir = os.path.join(self.testdir, 'InstallTar')
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()
97
98        # create test tarball and list its content files
99        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgatetest.tar'
100        mk_tarball(ecd + tarballname, 'ecgate')
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
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')
115
116    def test_success_ecgate_un_tarball(self):
117        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
118
119        # list comparison files for tarball content
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)
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:
127                    comparison_list.append(os.path.relpath(
128                        os.path.join(path, name), tar_test_fedir))
129
130        # untar in test directory
131        test_dir = os.path.join(tar_test_dir, 'test_ecgate')
132        make_dir(test_dir)
133        os.chdir(test_dir)
134        tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar'
135        un_tarball(os.path.join(tar_test_dir, tarballname))
136        tarfiles_list = []
137        for path, subdirs, files in os.walk(test_dir):
138            for name in files:
139                tarfiles_list.append(os.path.relpath(
140                    os.path.join(path, name), test_dir))
141
142        # test for equality
143        assert sorted(tarfiles_list) == sorted(comparison_list)
144
145    def test_success_local_un_tarball(self):
146        ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
147
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):
176        import filecmp
177
178        cmp_file = os.path.join(self.testfilesdir,
179                                'ECMWF_ENV.test')
180
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',
203                            'testgroup',
204                            'gateway.test.ac.at',
205                            'user@destination')
206
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')
214
215    @patch('_config.FILE_INSTALL_COMPILEJOB', _config_test.PATH_TESTFILES_DIR+'/compilejob_test.ksh')
216    def test_success_mk_compilejob(self):
217        import filecmp
218
219        testfile = os.path.join(self.testfilesdir,
220                                'compilejob.test')
221
222        mk_compilejob('Makefile.TEST',
223                      '',
224                      'testuser',
225                      'testgroup',
226                      'fp_root_test_path')
227
228        finalfile = os.path.join(_config.PATH_JOBSCRIPTS,
229                                 _config.FILE_INSTALL_COMPILEJOB)
230        assert filecmp.cmp(testfile, finalfile, shallow=False)
231
232        silent_remove(finalfile)
233
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):
264        import filecmp
265
266        testfile = os.path.join(self.testfilesdir,
267                                'job.temp.test')
268
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',
295                                'testgroup',
296                                'gateway.test.ac.at',
297                                'dest@generic',
298                                'fp_root_test_path')
299
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')
314#        shutil.rmtree(test_dir)
315        test_dir = os.path.join(self.testinstalldir,
316                                _config.FLEXEXTRACT_DIRNAME + '_ecgate')
317#        shutil.rmtree(test_dir)
318
319        test_dir = os.path.join(self.testinstalldir,
320                                'test_local')
321#        shutil.rmtree(test_dir)
322        test_dir = os.path.join(self.testinstalldir,
323                                'test_ecgate')
324#        shutil.rmtree(test_dir)
325
326        tar_file = os.path.join(self.testinstalldir,
327                     _config.FLEXEXTRACT_DIRNAME + '_local.tar')
328#        os.remove(tar_file)
329        tar_file = os.path.join(self.testinstalldir,
330                                _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar')
331#        os.remove(tar_file)
332        pass
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG