[067a0c3] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import os |
---|
[2f4cc73] | 5 | import sys |
---|
| 6 | #import unittest |
---|
[70a0bec] | 7 | import subprocess |
---|
| 8 | import pipes |
---|
[2f4cc73] | 9 | import pytest |
---|
| 10 | |
---|
| 11 | sys.path.append('../') |
---|
| 12 | import _config |
---|
[2ad8ea5] | 13 | from tools import (init128, to_param_id, my_error, read_ecenv, |
---|
[70a0bec] | 14 | get_cmdline_arguments, submit_job_to_ecserver, |
---|
| 15 | put_file_to_ecserver) |
---|
[067a0c3] | 16 | |
---|
| 17 | |
---|
[2f4cc73] | 18 | class 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 | |
---|
| 111 | def test_silent_remove(self): |
---|
| 112 | assert True |
---|
| 113 | |
---|
| 114 | def test_get_list_as_string(self): |
---|
| 115 | assert True |
---|
[067a0c3] | 116 | |
---|
[2f4cc73] | 117 | def test_make_dir(self): |
---|
| 118 | assert True |
---|
[067a0c3] | 119 | |
---|
[70a0bec] | 120 | def test_success_put_file_to_ecserver(self): |
---|
| 121 | ecuid=os.environ['ECUID']#'km4a' |
---|
| 122 | ecgid=os.environ['ECGID']#'at' |
---|
| 123 | put_file_to_ecserver('TestData/', 'testfile.txt', 'ecgate', ecuid, ecgid) |
---|
| 124 | assert subprocess.call(['ssh', 'km4a@ecaccess.ecmwf.int' , |
---|
| 125 | 'test -e ' + |
---|
| 126 | pipes.quote('/home/ms/'+ecgid+'/'+ecuid)]) == 0 |
---|
[451bb19] | 127 | |
---|
| 128 | def test_fail_submit_job_to_ecserver(self): |
---|
| 129 | with pytest.raises(SystemExit) as pytest_wrapped_e: |
---|
[763fcf6] | 130 | submit_job_to_ecserver('ecgate', 'job.ksh') |
---|
[451bb19] | 131 | assert pytest_wrapped_e.type == SystemExit |
---|
| 132 | assert pytest_wrapped_e.value.code == '... ECACCESS-JOB-SUBMIT FAILED!' |
---|
| 133 | |
---|
| 134 | def test_success_submit_job_to_ecserver(self): |
---|
[763fcf6] | 135 | result = submit_job_to_ecserver('ecgate', 'TestData/testfile.txt') |
---|
[451bb19] | 136 | assert result == 0 |
---|
| 137 | |
---|
[067a0c3] | 138 | |
---|
| 139 | |
---|
| 140 | if __name__ == "__main__": |
---|
[2f4cc73] | 141 | unittest.main() |
---|