1 | #!/usr/bin/env python |
---|
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 | from exceptions import OSError |
---|
15 | import pytest |
---|
16 | from mock import patch |
---|
17 | |
---|
18 | sys.path.append('../python') |
---|
19 | import _config |
---|
20 | import _config_test |
---|
21 | from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob, |
---|
22 | mk_job_template) |
---|
23 | |
---|
24 | from mods.tools import make_dir, silent_remove |
---|
25 | |
---|
26 | # - main |
---|
27 | # - get_install_cmdline_arguments |
---|
28 | # - install_via_gateway |
---|
29 | # - delete_convert_build |
---|
30 | # - make_convert_build |
---|
31 | |
---|
32 | class TestInstall(): |
---|
33 | ''' |
---|
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): |
---|
64 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
65 | |
---|
66 | # list comparison files for tarball content |
---|
67 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
68 | tar_test_file = os.path.join(tar_test_dir, |
---|
69 | 'flex_extract_v7.1_local.tar') |
---|
70 | with tarfile.open(tar_test_file, 'r') as tar_handle: |
---|
71 | comparison_list = tar_handle.getnames() |
---|
72 | |
---|
73 | # create test tarball and list its content files |
---|
74 | tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar' |
---|
75 | mk_tarball(ecd + tarballname, 'local') |
---|
76 | with tarfile.open(ecd + tarballname, 'r') as tar_handle: |
---|
77 | tar_content_list = tar_handle.getnames() |
---|
78 | |
---|
79 | # remove test tar file from flex_extract directory |
---|
80 | os.remove(ecd + tarballname) |
---|
81 | |
---|
82 | # test if comparison filelist is equal to the |
---|
83 | # filelist of tarball content |
---|
84 | assert sorted(comparison_list) == sorted(tar_content_list) |
---|
85 | |
---|
86 | def test_success_mk_tarball_ecgate(self): |
---|
87 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
88 | |
---|
89 | # list comparison files for tarball content |
---|
90 | tar_test_dir = os.path.join(self.testdir, 'InstallTar') |
---|
91 | tar_test_file = os.path.join(tar_test_dir, |
---|
92 | 'flex_extract_v7.1_ecgate.tar') |
---|
93 | with tarfile.open(tar_test_file, 'r') as tar_handle: |
---|
94 | comparison_list = tar_handle.getnames() |
---|
95 | |
---|
96 | # create test tarball and list its content files |
---|
97 | tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgatetest.tar' |
---|
98 | mk_tarball(ecd + tarballname, 'ecgate') |
---|
99 | with tarfile.open(ecd + tarballname, 'r') as tar_handle: |
---|
100 | tar_content_list = tar_handle.getnames() |
---|
101 | |
---|
102 | # remove test tar file from flex_extract directory |
---|
103 | os.remove(ecd + tarballname) |
---|
104 | |
---|
105 | # test if comparison filelist is equal to the |
---|
106 | # filelist of tarball content |
---|
107 | assert sorted(comparison_list) == sorted(tar_content_list) |
---|
108 | |
---|
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): |
---|
115 | ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep |
---|
116 | |
---|
117 | # list comparison files for tarball content |
---|
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) |
---|
121 | comparison_list = [] |
---|
122 | for path, subdirs, files in os.walk(tar_test_fedir): |
---|
123 | for name in files: |
---|
124 | if 'tar' not in name: |
---|
125 | comparison_list.append(os.path.relpath( |
---|
126 | os.path.join(path, name), tar_test_fedir)) |
---|
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 | tarballname = _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar' |
---|
133 | un_tarball(os.path.join(tar_test_dir, tarballname)) |
---|
134 | tarfiles_list = [] |
---|
135 | for path, subdirs, files in os.walk(test_dir): |
---|
136 | for name in files: |
---|
137 | tarfiles_list.append(os.path.relpath( |
---|
138 | os.path.join(path, name), test_dir)) |
---|
139 | |
---|
140 | # test for equality |
---|
141 | assert sorted(tarfiles_list) == sorted(comparison_list) |
---|
142 | |
---|
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): |
---|
174 | import filecmp |
---|
175 | |
---|
176 | cmp_file = os.path.join(self.testfilesdir, |
---|
177 | 'ECMWF_ENV.test') |
---|
178 | |
---|
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', |
---|
201 | 'testgroup', |
---|
202 | 'gateway.test.ac.at', |
---|
203 | 'user@destination') |
---|
204 | |
---|
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): |
---|
215 | import filecmp |
---|
216 | |
---|
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', |
---|
246 | '', |
---|
247 | 'testuser', |
---|
248 | 'testgroup', |
---|
249 | 'fp_root_test_path') |
---|
250 | |
---|
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) |
---|
275 | assert filecmp.cmp(testfile, finalfile, shallow=False) |
---|
276 | |
---|
277 | silent_remove(finalfile) |
---|
278 | |
---|
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', |
---|
293 | 'testgroup', |
---|
294 | 'gateway.test.ac.at', |
---|
295 | 'dest@generic', |
---|
296 | 'fp_root_test_path') |
---|
297 | |
---|
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 |
---|