1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import sys
5import os
6import os.path as osp
7import re
8import unittest
9import time
10
11from common import dict_conf, execcmd, tmpdir, aster_version
12from data   import study_export, available_hosts
13
14import asrun
15from asrun.run import AsRunFactory
16from asrun.profil import AsterProfil, ExportEntry
17from asrun.calcul import parse_submission_result
18from asrun.client import MULTIDIR
19from asrun.job import parse_actu_result
20from asrun.common.sysutils import short_hostname
21
22
23def build_prof(nomjob, resoncli):
24    prof = AsterProfil()
25    prof.parse(study_export % dict_conf)
26    prof['nomjob'] = nomjob
27    prof['tpsjob'] = 1
28    prof['multiple'] = 'yes'
29    prof['multiple_result_on_client'] = resoncli
30    lhosts = [dict_conf['localhost'],] + list(available_hosts.keys())
31    #lhosts = available_hosts.keys()
32    prof['multiple_server_list'] = " ".join(lhosts)
33    prof.add(ExportEntry(osp.join(tmpdir, nomjob + '.repe'),
34                         type='repe', isrep=True, result=True))
35    return prof
36
37def study(resoncli):
38    assert len(available_hosts) > 0, "no remote host available."
39    nomjob = 'multi_study_' + resoncli
40    run = AsRunFactory()
41    prof = build_prof(nomjob, resoncli)
42
43    export = osp.join(tmpdir, "%s.export" % nomjob)
44    prof.WriteExportTo(export)
45    cmd = dict_conf["as_run"] + ["--serv", export]
46    iret, out = execcmd(cmd, "%s.1" % nomjob, return_output=True)
47    assert iret == 0
48    jobid, queue, studyid = parse_submission_result(out)
49    assert jobid.strip() != ""
50    assert studyid.strip() != "" and studyid == jobid
51    etat = 'RUN'
52    while etat != 'ENDED':
53        cmd = dict_conf["as_run"] + ["--actu", jobid, nomjob, "interactif"]
54        iret, out = execcmd(cmd, "%s.2" % nomjob, return_output=True)
55        etat, diag, node, tcpu, wrk, queue = parse_actu_result(out)
56        if etat != "ENDED":
57            time.sleep(0.5)
58    assert run.GetGrav(diag) <= 1, "diag : %s, more details in ~/flasheur/%s.o%s" \
59        % (diag, nomjob, jobid)
60    # check that $HOME/MULTI/multi_study_`host` exists
61    multidir = osp.expandvars(MULTIDIR)
62    resdir = osp.join(multidir, '%s_%s' % (nomjob, dict_conf['localhost']))
63    assert osp.isfile(osp.join(resdir, nomjob + '.repe', 'parameter'))
64    for host in list(available_hosts.keys()):
65        host = short_hostname(host)
66        if resoncli == "yes":
67            resdir = osp.join(multidir, '%s_%s' % (nomjob, host))
68            flashdir = osp.join(multidir, '%s_%s' % (nomjob, host), 'flash')
69        else:
70            # this will fail if MULTIDIR change !
71            resdir = '%s:%s' % (host, osp.join('MULTI', '%s_%s' % (nomjob, host)))
72            flashdir = '%s:%s' % (host, osp.join('MULTI', '%s_%s' % (nomjob, host), 'flash'))
73        assert run.IsDir(resdir), "results on found at %s" % resdir
74        assert run.IsDir(flashdir), "flash directory on found at %s" % flashdir
75    cmd = dict_conf["as_run"] + ["--del", jobid, nomjob, "interactif"]
76    iret, out = execcmd(cmd, "%s.3" % nomjob, return_output=True)
77    assert iret == 0
78
79def astout():
80    assert len(available_hosts) > 0, "no remote host available."
81    run = AsRunFactory()
82    prof = AsterProfil()
83    nomjob = 'multi_astout'
84    resoncli = "yes"
85    prof['nomjob'] = nomjob
86    prof['actions'] = 'astout'
87    prof['version'] = aster_version
88    prof['tpsjob'] = 1
89    prof['memjob'] = 1024*16
90    prof['multiple'] = 'yes'
91    prof['multiple_result_on_client'] = resoncli
92    lhosts = [dict_conf['localhost'],] + list(available_hosts.keys())
93    #lhosts = available_hosts.keys()
94    prof['multiple_server_list'] = " ".join(lhosts)
95    flist = osp.join(tmpdir, nomjob + '.list')
96    with open(flist, 'w') as f:
97        f.write('ttnl02b\nssls122c')
98    prof.add(ExportEntry(flist, type='list', data=True))
99    prof.add(ExportEntry(osp.join(tmpdir, nomjob + '.resu_test'),
100                         type='resu_test', isrep=True, result=True))
101    prof.add(ExportEntry(osp.join(tmpdir, nomjob + '.flash'),
102                         type='flash', isrep=True, result=True))
103
104    export = osp.join(tmpdir, "multi_astout.export")
105    prof.WriteExportTo(export)
106    cmd = dict_conf["as_run"] + ["--serv", export]
107    iret, out = execcmd(cmd, "multi_astout.1", return_output=True)
108    assert iret == 0
109    jobid, queue, studyid = parse_submission_result(out)
110    assert jobid.strip() != ""
111    assert studyid.strip() != "" and studyid == jobid
112    etat = 'RUN'
113    while etat != 'ENDED':
114        cmd = dict_conf["as_run"] + ["--actu", jobid, nomjob, "interactif"]
115        iret, out = execcmd(cmd, "multi_astout.2", return_output=True)
116        etat, diag, node, tcpu, wrk, queue = parse_actu_result(out)
117        if etat != "ENDED":
118            time.sleep(0.5)
119    assert run.GetGrav(diag) <= 1, "diag : %s, more details in ~/flasheur/%s.o%s" \
120        % (diag, nomjob, jobid)
121    # check results
122    multidir = osp.expandvars(MULTIDIR)
123    for host in lhosts:
124        host = short_hostname(host)
125        resdir = osp.join(multidir, '%s_%s' % (nomjob, host), nomjob + '.resu_test')
126        assert osp.isdir(resdir), "results directory not found: %s" % resdir
127        nook = osp.join(resdir, "NOOK")
128        assert not osp.isfile(nook), "test(s) failed, see %s" % nook
129        res = osp.join(resdir, "RESULTAT")
130        assert osp.isfile(res), "result file not found: %s" % res
131        # check flash
132        flashdir = osp.join(multidir, '%s_%s' % (nomjob, host), nomjob + '.flash')
133        assert osp.isdir(flashdir), "flash directory not found: %s" % flashdir
134    cmd = dict_conf["as_run"] + ["--del", jobid, nomjob, "interactif"]
135    iret, out = execcmd(cmd, "multi_astout.3", return_output=True)
136    assert iret == 0
137
138
139class TestMultiple(unittest.TestCase):
140
141    def test01a_study(self):
142        study("yes")
143
144    def test01b_study(self):
145        study("no")
146
147    def test02_astout(self):
148        astout()
149
150
151if __name__ == "__main__":
152    unittest.main()
153