1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "BatchTest.hxx"
24 #include "Launcher.hxx"
27 #include <libbatch/MpiImpl.hxx>
30 #include "utilities.h"
38 BatchTest::BatchTest(const Engines::ResourceDefinition& batch_descr)
41 _batch_descr = batch_descr;
44 const size_t BUFSIZE = 32;
46 time_t curtime = time(NULL);
47 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
49 // Creating test temporary file
50 _test_filename = "/tmp/";
51 _test_filename += std::string(date) + "_test_cluster_file_";
52 _test_filename += _batch_descr.hostname.in();
53 _base_filename = std::string(date) + "_test_cluster_file_" + _batch_descr.hostname.in();
57 BatchTest::~BatchTest() {}
64 << "--- Testing batch Machine :" << std::endl
65 << "--- Name : " << _batch_descr.hostname << std::endl
66 << "--- hostname : " << _batch_descr.hostname << std::endl
67 << "--- Protocol : " << _batch_descr.protocol << std::endl
68 << "--- User Name : " << _batch_descr.username << std::endl
69 << "--- Batch Type : " << _batch_descr.batch << std::endl
70 << "--- MPI Impl : " << _batch_descr.mpiImpl << std::endl
71 << "--- Appli Path : " << _batch_descr.applipath << std::endl
74 std::string result_connection("Not Tested");
75 std::string result_filecopy("Not Tested");
76 std::string result_getresult("Not Tested");
77 std::string result_jobsubmit_simple("Not Tested");
78 std::string result_jobsubmit_mpi("Not Tested");
79 std::string result_appli("Not Tested");
81 result_connection = test_connection();
82 result_filecopy = test_filecopy();
83 result_getresult = test_getresult();
84 result_jobsubmit_simple = test_jobsubmit_simple();
85 result_jobsubmit_mpi = test_jobsubmit_mpi();
86 result_appli = test_appli();
89 << "--- Test results" << std::endl
90 << "--- Connection : " << result_connection << std::endl
91 << "--- File copy : " << result_filecopy << std::endl
92 << "--- Get results : " << result_getresult << std::endl
93 << "--- Submit simple job : " << result_jobsubmit_simple << std::endl
94 << "--- Submit mpi job : " << result_jobsubmit_mpi << std::endl
95 << "--- Application : " << result_appli << std::endl
98 if (result_connection == "OK" &&
99 result_filecopy == "OK" &&
100 result_getresult == "OK" &&
101 result_jobsubmit_simple == "OK" &&
102 result_jobsubmit_mpi == "OK" &&
103 result_appli == "OK")
109 // For this test we use : hostname, protocol, username
111 BatchTest::test_connection()
115 std::string result("Failed : ");
116 std::string hostname = _batch_descr.hostname.in();
117 std::string username = _batch_descr.username.in();
118 std::string protocol = _batch_descr.protocol.in();
123 result += "hostname is empty !";
128 result += "username is empty !";
131 if( protocol != "rsh" && protocol != "ssh")
133 result += "protocol unknown ! (" + protocol + ")";
140 + username + "@" + hostname;
143 status = system(command.c_str());
145 std::ostringstream oss;
147 result += "Error of connection on remote host ! status = ";
156 // For this test we use : hostname, protocol, username
158 BatchTest::test_filecopy()
163 std::string result("Failed : ");
164 std::string hostname = _batch_descr.hostname.in();
165 std::string username = _batch_descr.username.in();
166 std::string protocol = _batch_descr.protocol.in();
168 // Getting home directory
169 std::string rst = get_home(&home);
175 // Writing into the tempory file
176 command = "echo Hello > " + _test_filename;
177 status = system(command.c_str());
179 std::ostringstream oss;
181 result += "Error in creating tempory file ! status = ";
188 if(protocol == "rsh")
190 command += " " + _test_filename + " "
191 + username + "@" + hostname + ":" + home;
194 status = system(command.c_str());
196 std::ostringstream oss;
198 result += "Error in copy file on remote host ! status = ";
207 // For this test we use : hostname, protocol, username
209 BatchTest::test_getresult()
214 std::string result("Failed : ");
215 std::string hostname = _batch_descr.hostname.in();
216 std::string username = _batch_descr.username.in();
217 std::string protocol = _batch_descr.protocol.in();
219 // Getting home directory
220 std::string rst = get_home(&home);
228 if(protocol == "rsh")
230 command += " " + username + "@" + hostname + ":" + home
231 + "/" + _base_filename + " " + _test_filename + "_copy";
234 status = system(command.c_str());
236 std::ostringstream oss;
238 result += "Error in copy file from remote host ! status = ";
244 std::ifstream src_file(_test_filename.c_str());
247 result += "Error in reading temporary file ! filename = " + _test_filename;
250 std::string cp_filename = _test_filename + "_copy";
251 std::ifstream cp_file(cp_filename.c_str());
254 result += "Error in reading temporary copy file ! filename = " + cp_filename;
257 std::string src_firstline;
258 std::string cp_firstline;
259 std::getline(src_file, src_firstline);
260 std::getline(cp_file, cp_firstline);
263 if (src_firstline != cp_firstline)
265 result += "Error source file and copy file are not equa ! source = " + src_firstline + " copy = " + cp_firstline;
274 BatchTest::test_jobsubmit_simple()
279 std::string result("Failed : ");
280 std::string hostname = _batch_descr.hostname.in();
281 std::string username = _batch_descr.username.in();
282 std::string protocol = _batch_descr.protocol.in();
283 std::string batch_type = _batch_descr.batch.in();
286 if (batch_type == "lsf")
288 INFOS("test_jobsubmit_simple not yet implemented for lsf... return OK");
292 if (batch_type == "ccc")
294 INFOS("test_jobsubmit_simple not yet implemented for ccc... return OK");
298 if (batch_type == "slurm")
300 INFOS("test_jobsubmit_simple not yet implemented for slurm... return OK");
304 if (batch_type == "sge")
306 INFOS("test_jobsubmit_simple not yet implemented for sge... return OK");
310 if (batch_type != "pbs")
312 result += "Batch type unknown ! : " + batch_type;
316 // Getting home directory
317 std::string rst = get_home(&home);
324 std::string _test_file_simple = _test_filename + "_simple";
326 file.open(_test_file_simple.c_str(), std::ofstream::out);
327 file << "#!/bin/bash\n"
328 << "#PBS -l nodes=1\n"
329 << "#PBS -l walltime=00:01:00\n"
330 << "#PBS -o " + home + "/" + _date + "_simple_output.log\n"
331 << "#PBS -e " + home + "/" + _date + "_simple_error.log\n"
333 << "echo Error >&2\n";
338 // Build command for copy
340 if(protocol == "rsh")
342 command += " " + _test_file_simple + " "
343 + username + "@" + hostname + ":" + home;
344 status = system(command.c_str());
346 std::ostringstream oss;
348 result += "Error in copy job file to remote host ! status = ";
353 // Build command for submit job
354 std::string file_job_name = _test_filename + "_jobid";
355 command = protocol + " " + username + "@" + hostname + " qsub " + _base_filename + "_simple > " + file_job_name;
356 status = system(command.c_str());
358 std::ostringstream oss;
360 result += "Error in sending qsub to remote host ! status = ";
365 std::ifstream file_job(file_job_name.c_str());
368 result += "Error in reading temporary file ! filename = " + file_job_name;
371 std::getline(file_job, jobid);
374 // Wait the end of the job
375 command = protocol + " " + username + "@" + hostname + " qstat -f " + jobid + " > " + file_job_name;
379 status = system(command.c_str());
380 if(status && status != 153 && status != 256*153)
382 std::ostringstream oss;
384 result += "Error in sending qstat to remote host ! status = ";
389 if(status == 153 || status == 256*153 )
398 // Build command for getting results
400 if(protocol == "rsh")
403 + username + "@" + hostname + ":" + home + "/" + _date + "_simple* /tmp";
404 status = system(command.c_str());
406 std::ostringstream oss;
408 result += "error in getting file result of qsub simple to remote host ! status = ";
414 std::string normal_input;
415 std::string file_normal_name = "/tmp/" + _date + "_simple_output.log";
416 std::ifstream file_normal(file_normal_name.c_str());
419 result += "Error in reading temporary file ! filename = " + file_normal_name;
422 std::getline(file_normal, normal_input);
424 if (normal_input != "Bonjour")
426 result += "error from simple ouput file ! waiting for Bonjour and get : " + normal_input;
429 std::string error_input;
430 std::string file_error_name = "/tmp/" + _date + "_simple_error.log";
431 std::ifstream file_error(file_error_name.c_str());
434 result += "Error in reading temporary file ! filename = " + file_error_name;
437 std::getline(file_error, error_input);
439 if (error_input != "Error")
441 result += "error from simple error file ! waiting for Error and get : " + error_input;
449 BatchTest::test_jobsubmit_mpi()
456 std::string result("Failed : ");
457 std::string hostname = _batch_descr.hostname.in();
458 std::string username = _batch_descr.username.in();
459 std::string protocol = _batch_descr.protocol.in();
460 std::string batch_type = _batch_descr.batch.in();
461 std::string mpi_type = _batch_descr.mpiImpl.in();
464 if(mpi_type == "lam")
465 mpiImpl = new MpiImpl_LAM();
466 else if(mpi_type == "mpich1")
467 mpiImpl = new MpiImpl_MPICH1();
468 else if(mpi_type == "mpich2")
469 mpiImpl = new MpiImpl_MPICH2();
470 else if(mpi_type == "openmpi")
471 mpiImpl = new MpiImpl_OPENMPI();
472 else if(mpi_type == "ompi")
473 mpiImpl = new MpiImpl_OMPI();
474 else if(mpi_type == "slurmmpi")
475 mpiImpl = new MpiImpl_SLURM();
478 result += "Error MPI impl not supported : " + mpi_type;
482 // LSF et SGE not yet implemented...
483 if (batch_type == "lsf")
485 INFOS("test_jobsubmit_simple not yet implemented for lsf... return OK");
490 if (batch_type == "ccc")
492 INFOS("test_jobsubmit_simple not yet implemented for ccc... return OK");
497 if (batch_type == "slurm")
499 INFOS("test_jobsubmit_simple not yet implemented for slurm... return OK");
504 if (batch_type == "sge")
506 INFOS("test_jobsubmit_simple not yet implemented for sge... return OK");
511 // Getting home directory
512 std::string rst = get_home(&home);
519 std::string _test_file_script = _test_filename + "_script";
520 std::ofstream file_script;
521 file_script.open(_test_file_script.c_str(), std::ofstream::out);
522 file_script << "#!/bin/bash\n"
523 << "echo HELLO MPI\n";
531 (_test_file_script.c_str(), 0x1ED);
533 std::string _test_file_mpi = _test_filename + "_mpi";
534 std::ofstream file_mpi;
535 file_mpi.open(_test_file_mpi.c_str(), std::ofstream::out);
536 file_mpi << "#!/bin/bash\n"
537 << "#PBS -l nodes=1\n"
538 << "#PBS -l walltime=00:01:00\n"
539 << "#PBS -o " << home << "/" << _date << "_mpi_output.log\n"
540 << "#PBS -e " << home << "/" << _date << "_mpi_error.log\n"
541 << mpiImpl->boot("${PBS_NODEFILE}", 1)
542 << mpiImpl->run("${PBS_NODEFILE}", 1, _base_filename + "_script")
548 // Build command for copy
550 if(protocol == "rsh")
552 command += " " + _test_file_script + " "
553 + username + "@" + hostname + ":" + home;
554 status = system(command.c_str());
556 std::ostringstream oss;
558 result += "Error in copy job file to remote host ! status = ";
563 if(protocol == "rsh")
565 command += " " + _test_file_mpi + " "
566 + username + "@" + hostname + ":" + home;
567 status = system(command.c_str());
569 std::ostringstream oss;
571 result += "Error in copy job file to remote host ! status = ";
576 // Build command for submit job
577 std::string file_job_name = _test_filename + "_jobid";
578 command = protocol + " " + username + "@" + hostname + " qsub " + _base_filename + "_mpi > " + file_job_name;
579 status = system(command.c_str());
581 std::ostringstream oss;
583 result += "Error in sending qsub to remote host ! status = ";
588 std::ifstream file_job(file_job_name.c_str());
591 result += "Error in reading temporary file ! filename = " + file_job_name;
594 std::getline(file_job, jobid);
597 // Wait the end of the job
598 command = protocol + " " + username + "@" + hostname + " qstat -f " + jobid + " > " + file_job_name;
602 status = system(command.c_str());
603 if(status && status != 153 && status != 256*153)
605 std::ostringstream oss;
607 result += "Error in sending qstat to remote host ! status = ";
612 if(status == 153 || status == 256*153 )
621 // Build command for getting results
623 if(protocol == "rsh")
626 + username + "@" + hostname + ":" + home + "/" + _date + "_mpi* /tmp";
627 status = system(command.c_str());
629 std::ostringstream oss;
631 result += "error in getting file result of qsub mpi from remote host ! status = ";
637 std::string normal_input;
638 std::string file_normal_name = "/tmp/" + _date + "_mpi_output.log";
639 std::ifstream file_normal(file_normal_name.c_str());
642 result += "Error in reading temporary file ! filename = " + file_normal_name;
645 bool test_ok = false;
646 while (std::getline(file_normal, normal_input))
648 if (normal_input == "HELLO MPI")
654 result += "error from mpi ouput file ! waiting for HELLO MPI please watch /tmp/" + _date + "_mpi_output.log file";
660 throw LauncherException("Method BatchTest::test_jobsubmit_mpi is not available "
661 "(libBatch was not present at compilation time)");
666 BatchTest::test_appli()
671 std::string result("Failed : ");
672 std::string hostname = _batch_descr.hostname.in();
673 std::string username = _batch_descr.username.in();
674 std::string protocol = _batch_descr.protocol.in();
675 std::string applipath = _batch_descr.applipath.in();
677 // Getting home directory
678 std::string rst = get_home(&home);
684 std::string _test_file_appli = _test_filename + "_appli_test";
685 std::ofstream file_appli;
686 file_appli.open(_test_file_appli.c_str(), std::ofstream::out);
687 file_appli << "#!/bin/bash\n"
688 << "if [ -f " << applipath << "/runAppli ]\n"
697 // Build command for copy
699 if(protocol == "rsh")
701 command += " " + _test_file_appli + " "
702 + username + "@" + hostname + ":" + home;
703 status = system(command.c_str());
705 std::ostringstream oss;
707 result += "Error in copy appli test file to remote host ! status = ";
713 command = protocol + " " + username + "@" + hostname
714 + " sh " + home + "/" + _base_filename + "_appli_test > "
715 + _test_filename + "_appli_test_result";
717 status = system(command.c_str());
719 std::ostringstream oss;
721 result += "Error in launching appli test on remote host ! status = ";
727 std::string rst_appli;
728 std::string file_appli_result_name = _test_filename + "_appli_test_result";
729 std::ifstream file_appli_result(file_appli_result_name.c_str());
730 if (!file_appli_result)
732 result += "Error in reading temporary file ! filename = " + file_appli_result_name;
735 std::getline(file_appli_result, rst_appli);
736 file_appli_result.close();
738 if (rst_appli != "OK")
740 result += "Error checking application on remote host ! result = " + rst;
750 BatchTest::get_home(std::string * home)
753 std::string result = "";
755 std::string hostname = _batch_descr.hostname.in();
756 std::string username = _batch_descr.username.in();
757 std::string protocol = _batch_descr.protocol.in();
758 std::string file_home_name = _test_filename + "_home";
760 command = protocol + " " + username + "@" + hostname + " 'echo $HOME' > " + file_home_name;
761 status = system(command.c_str());
763 std::ostringstream oss;
765 result += "Error in getting home directory ! status = ";
770 std::ifstream file_home(file_home_name.c_str());
773 result += "Error in reading temporary file ! filename = " + file_home_name;
776 std::getline(file_home, *home);