1 | #!/usr/bin/env python3 |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | |
---|
5 | import sys |
---|
6 | import os |
---|
7 | import inspect |
---|
8 | import subprocess |
---|
9 | import tarfile |
---|
10 | import errno |
---|
11 | import shutil |
---|
12 | from genshi.template import TemplateLoader |
---|
13 | from genshi.template.eval import UndefinedError |
---|
14 | |
---|
15 | try: |
---|
16 | import exceptions |
---|
17 | except ImportError: |
---|
18 | import builtins |
---|
19 | import pytest |
---|
20 | from mock import patch |
---|
21 | import mock |
---|
22 | |
---|
23 | sys.path.append('../Python') |
---|
24 | import _config |
---|
25 | from . import _config_test |
---|
26 | from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob, |
---|
27 | mk_job_template) |
---|
28 | |
---|
29 | from Mods.tools import make_dir, silent_remove |
---|
30 | |
---|
31 | # - main |
---|
32 | # - get_install_cmdline_arguments |
---|
33 | # - install_via_gateway |
---|
34 | # - delete_convert_build |
---|
35 | # - make_convert_build |
---|
36 | |
---|
37 | class TestInstall(): |
---|
38 | ''' |
---|
39 | ''' |
---|
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')]) |
---|
49 | |
---|
50 | # un tar the test tarballs from shell script |
---|
51 | # subprocess.check_output([os.path.join(self.testinstalldir, |
---|
52 | # 'un_install_tar.sh')]) |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | @patch('tarfile.open', side_effect=[tarfile.TarError, OSError]) |
---|
57 | def test_fail_mk_tarball_local(self, mock_open): |
---|
58 | import tarfile |
---|
59 | # mock_open.side_effekt = tarfile.TarError |
---|
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 | |
---|
67 | |
---|
68 | def test_success_mk_tarball_local(self): |
---|
69 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
70 | |
---|
71 | # list comparison files for tarball content |
---|
72 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
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() |
---|
77 | |
---|
78 | # create test tarball and list its content files |
---|
79 | tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar' |
---|
80 | mk_tarball(ecd + tarballname, 'local') |
---|
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 |
---|
85 | #os.remove(ecd + tarballname) |
---|
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 | |
---|
91 | def test_success_mk_tarball_ecgate(self): |
---|
92 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
93 | |
---|
94 | # list comparison files for tarball content |
---|
95 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
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() |
---|
100 | |
---|
101 | # create test tarball and list its content files |
---|
102 | tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgatetest.tar' |
---|
103 | mk_tarball(ecd + tarballname, 'ecgate') |
---|
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 | |
---|
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') |
---|
118 | |
---|
119 | def test_success_ecgate_un_tarball(self): |
---|
120 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
121 | |
---|
122 | # list comparison files for tarball content |
---|
123 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
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() |
---|
127 | |
---|
128 | # untar in test directory |
---|
129 | test_dir = os.path.join(tar_test_dir, 'test_ecgate') |
---|
130 | make_dir(test_dir) |
---|
131 | os.chdir(test_dir) |
---|
132 | un_tarball(os.path.join(tar_test_dir, tarballname)) |
---|
133 | tarfiles_list = [] |
---|
134 | for path, subdirs, files in os.walk(test_dir): |
---|
135 | for name in files: |
---|
136 | tarfiles_list.append(os.path.relpath( |
---|
137 | os.path.join(path, name), test_dir)) |
---|
138 | |
---|
139 | # test for equality |
---|
140 | assert sorted(tarfiles_list) == sorted(comparison_list) |
---|
141 | |
---|
142 | def test_success_local_un_tarball(self): |
---|
143 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
144 | |
---|
145 | # list comparison files for tarball content |
---|
146 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
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() |
---|
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)) |
---|
161 | |
---|
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): |
---|
167 | import filecmp |
---|
168 | |
---|
169 | cmp_file = os.path.join(self.testfilesdir, |
---|
170 | 'ECMWF_ENV.test') |
---|
171 | |
---|
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', |
---|
194 | 'testgroup', |
---|
195 | 'gateway.test.ac.at', |
---|
196 | 'user@destination') |
---|
197 | |
---|
198 | @patch('_config.FILE_INSTALL_COMPILEJOB', _config_test.PATH_TESTFILES_DIR+'/compilejob_test.ksh') |
---|
199 | def test_success_mk_compilejob(self): |
---|
200 | import filecmp |
---|
201 | |
---|
202 | testfile = os.path.join(self.testfilesdir, |
---|
203 | 'compilejob.test') |
---|
204 | |
---|
205 | mk_compilejob('Makefile.TEST', |
---|
206 | 'testuser', |
---|
207 | 'testgroup', |
---|
208 | 'fp_root_test_path') |
---|
209 | |
---|
210 | finalfile = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
211 | _config.FILE_INSTALL_COMPILEJOB) |
---|
212 | assert filecmp.cmp(testfile, finalfile, shallow=False) |
---|
213 | |
---|
214 | silent_remove(finalfile) |
---|
215 | |
---|
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 | |
---|
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') |
---|
240 | |
---|
241 | @patch('_config.TEMPFILE_JOB', _config_test.PATH_TESTFILES_DIR+'/submitscript.template.test.comp') |
---|
242 | def test_success_mk_job_template(self): |
---|
243 | import filecmp |
---|
244 | |
---|
245 | testfile = os.path.join(self.testfilesdir, |
---|
246 | 'submitscript.template.test') |
---|
247 | |
---|
248 | mk_job_template('testuser', |
---|
249 | 'testgroup', |
---|
250 | # 'gateway.test.ac.at', |
---|
251 | # 'dest@generic', |
---|
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', |
---|
265 | # 'gateway.test.ac.at', |
---|
266 | # 'dest@generic', |
---|
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', |
---|
274 | 'testgroup', |
---|
275 | # 'gateway.test.ac.at', |
---|
276 | # 'dest@generic', |
---|
277 | 'fp_root_test_path') |
---|
278 | |
---|
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') |
---|
287 | |
---|
288 | @classmethod |
---|
289 | def teardown_class(self): |
---|
290 | |
---|
291 | test_dir = os.path.join(self.testinstalldir, |
---|
292 | _config.FLEXEXTRACT_DIRNAME + '_local') |
---|
293 | # shutil.rmtree(test_dir) |
---|
294 | test_dir = os.path.join(self.testinstalldir, |
---|
295 | _config.FLEXEXTRACT_DIRNAME + '_ecgate') |
---|
296 | # shutil.rmtree(test_dir) |
---|
297 | |
---|
298 | test_dir = os.path.join(self.testinstalldir, |
---|
299 | 'test_local') |
---|
300 | # shutil.rmtree(test_dir) |
---|
301 | test_dir = os.path.join(self.testinstalldir, |
---|
302 | 'test_ecgate') |
---|
303 | # shutil.rmtree(test_dir) |
---|
304 | |
---|
305 | tar_file = os.path.join(self.testinstalldir, |
---|
306 | _config.FLEXEXTRACT_DIRNAME + '_local.tar') |
---|
307 | # os.remove(tar_file) |
---|
308 | tar_file = os.path.join(self.testinstalldir, |
---|
309 | _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar') |
---|
310 | # os.remove(tar_file) |
---|
311 | pass |
---|