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