source: flex_extract.git/python/pythontest/TestTools.py @ 308e144

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

minor bugfix

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[067a0c3]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import os
[2f4cc73]5import sys
[70a0bec]6import subprocess
7import pipes
[2f4cc73]8import pytest
9
10sys.path.append('../')
11import _config
[efa05d7]12from tools import (get_cmdline_arguments, read_ecenv, clean_up, my_error,
13                   normal_exit, product, silent_remove, init128, to_param_id,
14                   get_list_as_string, make_dir, put_file_to_ecserver,
15                   submit_job_to_ecserver)
[067a0c3]16
17
[2f4cc73]18class TestTools():
[067a0c3]19    '''
20    '''
21
22    def setUp(self):
23        pass
24
[2f4cc73]25    def test_get_cmdline_arguments(self):
[2ad8ea5]26        '''
27        '''
28        cmd_dict_control = {'start_date':'20180101',
29                            'end_date':'20180101',
30                            'date_chunk':'3',
31                            'basetime':'12',
32                            'step':'1',
33                            'levelist':'1/to/10',
34                            'area':'50/10/60/20',
35                            'inputdir':'../work',
36                            'outputdir':'../work',
37                            'flexpart_root_scripts':'../',
38                            'ppid':'1234',
39                            'job_template':'job.sh',
40                            'queue':'ecgate',
41                            'controlfile':'CONTROL.WORK',
42                            'debug':'1'}
43
44        sys.argv = ['dummy.py',
45                    '--start_date=20180101',
46                    '--end_date=20180101',
47                    '--date_chunk=3',
48                    '--basetime=12',
49                    '--step=1',
50                    '--levelist=1/to/10',
51                    '--area=50/10/60/20',
52                    '--inputdir=../work',
53                    '--outputdir=../work',
54                    '--flexpart_root_scripts=../',
55                    '--ppid=1234',
56                    '--job_template=job.sh',
57                    '--queue=ecgate',
58                    '--controlfile=CONTROL.WORK',
59                    '--debug=1']
60
61        results = get_cmdline_arguments()
62
63        assert cmd_dict_control == vars(results)
[2f4cc73]64
[067a0c3]65    def test_init128(self):
66        '''
67        '''
[2f4cc73]68        table128 = init128(_config.PATH_GRIBTABLE)
[067a0c3]69        expected = {'078': 'TCLW', '130': 'T', '034': 'SST'}
70        # check a sample of parameters which must have been read in
71        result = all((k in table128 and table128[k]==v) for k,v in expected.iteritems())
[2f4cc73]72        assert result == True
[067a0c3]73
74    def test_to_param_id(self):
75        '''
76        '''
[2f4cc73]77        table128 = init128(_config.PATH_GRIBTABLE)
[067a0c3]78        pars = to_param_id("T/SP/LSP/SSHF", table128)
79        for par in pars:
[2f4cc73]80            assert par in [130, 134, 142, 146]
[067a0c3]81
[451bb19]82    def test_my_error(self):
[067a0c3]83        '''
84        '''
[2f4cc73]85        with pytest.raises(SystemExit) as pytest_wrapped_e:
[067a0c3]86            my_error(['${USER}', 'anne.philipp@univie.ac.at'], 'Failed!')
[2f4cc73]87        assert pytest_wrapped_e.type == SystemExit
88        assert pytest_wrapped_e.value.code == 1
[067a0c3]89
90    def test_read_ecenv(self):
[2f4cc73]91        '''
92        '''
[067a0c3]93        envs_ref = {'ECUID': 'km4a',
94                    'ECGID': 'at',
95                    'GATEWAY': 'srvx8.img.univie.ac.at',
96                    'DESTINATION': 'annep@genericSftp'
97                   }
98        envs = read_ecenv(os.getcwd() + '/TestData/ECMWF_ENV')
99
[2f4cc73]100        assert envs_ref == envs
101
102    def test_clean_up(self):
103        assert True
104
105    def test_normal_exit(self):
106        assert True
107
108    def test_product(self):
109        assert True
110
[efa05d7]111    def test_success_silent_remove(self, capfd):
112        testfile = 'testfile.test'
113        open(testfile, 'w').close()
114        silent_remove(testfile)
115        out, err = capfd.readouterr()
116        assert os.path.isfile(testfile) == False
117        assert out == ''
118
119    def test_failnotexist_silent_remove(self, capfd):
120        testfile = 'testfile.test'
121        silent_remove(testfile)
122        out, err = capfd.readouterr()
123        assert os.path.isfile(testfile) == False
124        assert out == ''
[308e144]125#
126#    def test_failany_silent_remove(self):
127#        testfile = 'testfileany.test'
128#        with pytest.raises(OSError) as pytest_wrapped_e:
129#            silent_remove(testfile)
130#        #out, err = capfd.readouterr()
131#        #assert os.path.isfile(testfile) == False
132#        #assert out == ''
[2f4cc73]133
134    def test_get_list_as_string(self):
135        assert True
[067a0c3]136
[efa05d7]137    def test_warningexist_make_dir(self, capfd):
138        testdir = 'TestData'
139        make_dir(testdir)
140        out, err = capfd.readouterr()
141        assert out.strip() == 'WARNING: Directory {0} already exists!'.format(testdir)
142
143    def test_failany_make_dir(self):
144        testdir = '/test' # force a permission denied error
145        with pytest.raises(OSError) as pytest_wrapped_e:
146            make_dir(testdir)
147        assert pytest_wrapped_e.type == OSError
148
149    def test_success_make_dir(self):
150        testdir = 'testing_mkdir'
151        make_dir(testdir)
152        assert os.path.exists(testdir) == True
153        os.rmdir(testdir)
[067a0c3]154
[97e09f4]155    def test_fail_put_file_to_ecserver(self):
156        ecuid=os.environ['ECUID']
157        ecgid=os.environ['ECGID']
158        with pytest.raises(SystemExit) as pytest_wrapped_e:
159            put_file_to_ecserver('TestData/', 'testfil.txt',
160                                 'ecgate', ecuid, ecgid)
161        assert pytest_wrapped_e.type == SystemExit
162        assert pytest_wrapped_e.value.code == '... ECACCESS-FILE-PUT FAILED!'
163
[70a0bec]164    def test_success_put_file_to_ecserver(self):
[f9f7b3f]165        ecuid=os.environ['ECUID']
166        ecgid=os.environ['ECGID']
[97e09f4]167        result = put_file_to_ecserver('TestData/', 'testfile.txt',
168                                      'ecgate', ecuid, ecgid)
169        assert result == ''
170
171    @pytest.mark.msuser_pw
172    def test_fullsuccess_put_file_to_ecserver(self):
173        ecuid=os.environ['ECUID']
174        ecgid=os.environ['ECGID']
[70a0bec]175        put_file_to_ecserver('TestData/', 'testfile.txt', 'ecgate', ecuid, ecgid)
[f9f7b3f]176        assert subprocess.call(['ssh', ecuid+'@ecaccess.ecmwf.int' ,
[70a0bec]177                                'test -e ' +
178                                pipes.quote('/home/ms/'+ecgid+'/'+ecuid)]) == 0
[451bb19]179
180    def test_fail_submit_job_to_ecserver(self):
181        with pytest.raises(SystemExit) as pytest_wrapped_e:
[763fcf6]182            submit_job_to_ecserver('ecgate', 'job.ksh')
[451bb19]183        assert pytest_wrapped_e.type == SystemExit
184        assert pytest_wrapped_e.value.code == '... ECACCESS-JOB-SUBMIT FAILED!'
185
186    def test_success_submit_job_to_ecserver(self):
[763fcf6]187        result = submit_job_to_ecserver('ecgate', 'TestData/testfile.txt')
[97e09f4]188        assert result.strip().isdigit() == True
[067a0c3]189
190
191if __name__ == "__main__":
[2f4cc73]192    unittest.main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG