Salome HOME
Merge branch 'gdd_env_modules_in_config_appli'
[modules/kernel.git] / src / Launcher / BatchTest.cxx
index a320ecedc6b75b03475ca1bbc91402a0c66c8236..d81ef8a04b07510118e60e94c5636ec773ea1bd0 100644 (file)
@@ -1,28 +1,34 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2017  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "BatchTest.hxx"
+#include "Launcher.hxx"
+#include "Basics_Utils.hxx"
+#include "Basics_DirUtils.hxx"
+
+#ifdef WITH_LIBBATCH
+#include <libbatch/MpiImpl.hxx>
+#endif
 
-#include "Batch_Date.hxx"
-#include "MpiImpl.hxx"
 #include "utilities.h"
 
 #include <sys/stat.h>
 #ifdef WIN32
 # include <io.h>
 #endif
-BatchTest::BatchTest(const Engines::MachineParameters& batch_descr) 
+BatchTest::BatchTest(const Engines::ResourceDefinition& batch_descr)
 {
+#ifdef WITH_LIBBATCH
   _batch_descr = batch_descr;
 
   // Getting date
-  Batch::Date date = Batch::Date(time(0));
-  _date = date.str();
-  int lend = _date.size() ;
-  int i = 0 ;
-  while (i < lend) 
-  {
-    if (_date[i] == '/' || _date[i] == '-' || _date[i] == ':' ) 
-    {
-      _date[i] = '_' ;
-    }
-    i++ ;
-  }
-  
+  const size_t BUFSIZE = 32;
+  char date[BUFSIZE];
+  time_t curtime = time(NULL);
+  strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
+
   // Creating test temporary file
-  _test_filename =  "/tmp/";
-  _test_filename +=  _date + "_test_cluster_file_";
-  _test_filename += _batch_descr.alias.in();
-  _base_filename = _date + "_test_cluster_file_" + _batch_descr.alias.in();
+  _base_filename = std::string(date) + "_test_cluster_file_" + _batch_descr.hostname.in();
+  _test_filename = Kernel_Utils::GetTmpDir() + _base_filename;
+#endif
 }
 
 BatchTest::~BatchTest() {}
@@ -62,15 +60,15 @@ bool
 BatchTest::test()
 {
   bool rtn = false;
-  INFOS(std::endl 
-       << "--- Testing batch Machine :" << std::endl
-       << "--- Name       : " << _batch_descr.hostname << std::endl
-       << "--- Alias      : " << _batch_descr.alias << std::endl
-       << "--- Protocol   : " << _batch_descr.protocol << std::endl
-       << "--- User Name  : " << _batch_descr.username << std::endl
-       << "--- Batch Type : " << _batch_descr.batch << std::endl
-       << "--- MPI Impl   : " << _batch_descr.mpiImpl << std::endl
-       << "--- Appli Path : " << _batch_descr.applipath << std::endl
+  INFOS(std::endl
+        << "--- Testing batch Machine :" << std::endl
+        << "--- Name       : " << _batch_descr.hostname << std::endl
+        << "--- hostname      : " << _batch_descr.hostname << std::endl
+        << "--- Protocol   : " << _batch_descr.protocol << std::endl
+        << "--- User Name  : " << _batch_descr.username << std::endl
+        << "--- Batch Type : " << _batch_descr.batch << std::endl
+        << "--- MPI Impl   : " << _batch_descr.mpiImpl << std::endl
+        << "--- Appli Path : " << _batch_descr.applipath << std::endl
        );
 
   std::string result_connection("Not Tested");
@@ -88,41 +86,41 @@ BatchTest::test()
   result_appli = test_appli();
 
   INFOS(std::endl
-       << "--- Test results" << std::endl
-       << "--- Connection          : " << result_connection << std::endl
-       << "--- File copy           : " << result_filecopy << std::endl
-       << "--- Get results         : " << result_getresult << std::endl
-       << "--- Submit simple job   : " << result_jobsubmit_simple << std::endl
-       << "--- Submit mpi job      : " << result_jobsubmit_mpi << std::endl
-       << "--- Application         : " << result_appli << std::endl
+        << "--- Test results" << std::endl
+        << "--- Connection          : " << result_connection << std::endl
+        << "--- File copy           : " << result_filecopy << std::endl
+        << "--- Get results         : " << result_getresult << std::endl
+        << "--- Submit simple job   : " << result_jobsubmit_simple << std::endl
+        << "--- Submit mpi job      : " << result_jobsubmit_mpi << std::endl
+        << "--- Application         : " << result_appli << std::endl
        );
-  
-  if (result_connection == "OK"       && 
+
+  if (result_connection == "OK"       &&
       result_filecopy == "OK"         &&
       result_getresult == "OK"        &&
       result_jobsubmit_simple == "OK" &&
       result_jobsubmit_mpi == "OK"    &&
       result_appli == "OK")
     rtn = true;
-      
+
   return rtn;
 }
 
-// For this test we use : alias, protocol, username
+// For this test we use : hostname, protocol, username
 std::string
 BatchTest::test_connection()
 {
   int status;
   std::string command;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
 
   // Basic tests
-  if(alias == "")
+  if(hostname == "")
   {
-    result += "alias is empty !";
+    result += "hostname is empty !";
     return result;
   }
   if(username == "")
@@ -138,8 +136,8 @@ BatchTest::test_connection()
 
   // Build command
   command += protocol
-         + " "
-         + username + "@" + alias;
+          + " "
+          + username + "@" + hostname;
 
   // Test
   status = system(command.c_str());
@@ -155,7 +153,7 @@ BatchTest::test_connection()
   return result;
 }
 
-// For this test we use : alias, protocol, username
+// For this test we use : hostname, protocol, username
 std::string
 BatchTest::test_filecopy()
 {
@@ -163,7 +161,7 @@ BatchTest::test_filecopy()
   std::string home;
   std::string command;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
 
@@ -190,7 +188,7 @@ BatchTest::test_filecopy()
   if(protocol == "rsh")
     command = "rcp";
   command += " " + _test_filename + " "
-         + username + "@" + alias + ":" + home;
+          + username + "@" + hostname + ":" + home;
 
   // Test
   status = system(command.c_str());
@@ -206,7 +204,7 @@ BatchTest::test_filecopy()
   return result;
 }
 
-// For this test we use : alias, protocol, username
+// For this test we use : hostname, protocol, username
 std::string
 BatchTest::test_getresult()
 {
@@ -214,7 +212,7 @@ BatchTest::test_getresult()
   std::string home;
   std::string command;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
 
@@ -229,8 +227,8 @@ BatchTest::test_getresult()
   command = "scp";
   if(protocol == "rsh")
     command = "rcp";
-  command += " " + username + "@" + alias + ":" + home 
-         + "/" + _base_filename + " " + _test_filename + "_copy";
+  command += " " + username + "@" + hostname + ":" + home
+          + "/" + _base_filename + " " + _test_filename + "_copy";
 
   // Test
   status = system(command.c_str());
@@ -241,7 +239,7 @@ BatchTest::test_getresult()
     result += oss.str();
     return result;
   }
-  
+
   // Compare files
   std::ifstream src_file(_test_filename.c_str());
   if (!src_file)
@@ -272,14 +270,14 @@ BatchTest::test_getresult()
   return result;
 }
 
-std::string 
-BatchTest::test_jobsubmit_simple() 
+std::string
+BatchTest::test_jobsubmit_simple()
 {
   int status;
   std::string home;
   std::string command;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
   std::string batch_type = _batch_descr.batch.in();
@@ -291,12 +289,36 @@ BatchTest::test_jobsubmit_simple()
     result = "OK";
     return result;
   }
+  if (batch_type == "ccc")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for ccc... return OK");
+    result = "OK";
+    return result;
+  }
+  if (batch_type == "slurm")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for slurm... return OK");
+    result = "OK";
+    return result;
+  }
   if (batch_type == "sge")
   {
     INFOS("test_jobsubmit_simple not yet implemented for sge... return OK");
     result = "OK";
     return result;
   }
+  if (batch_type == "oar")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for oar... return OK");
+    result = "OK";
+    return result;
+  }
+  if (batch_type == "coorm")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for coorm... return OK");
+    result = "OK";
+    return result;
+  }
   if (batch_type != "pbs")
   {
     result += "Batch type unknown ! : " + batch_type;
@@ -330,7 +352,7 @@ BatchTest::test_jobsubmit_simple()
   if(protocol == "rsh")
     command = "rcp";
   command += " " + _test_file_simple + " "
-         + username + "@" + alias + ":" + home;
+          + username + "@" + hostname + ":" + home;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -342,7 +364,7 @@ BatchTest::test_jobsubmit_simple()
 
   // Build command for submit job
   std::string file_job_name = _test_filename + "_jobid";
-  command = protocol + " " + username + "@" + alias + " qsub " + _base_filename + "_simple > " + file_job_name;
+  command = protocol + " " + username + "@" + hostname + " qsub " + _base_filename + "_simple > " + file_job_name;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -360,11 +382,11 @@ BatchTest::test_jobsubmit_simple()
   }
   std::getline(file_job, jobid);
   file_job.close();
+
   // Wait the end of the job
-  command = protocol + " " + username + "@" + alias + " qstat -f " + jobid + " > " + file_job_name;
+  command = protocol + " " + username + "@" + hostname + " qstat -f " + jobid + " > " + file_job_name;
   bool stop = false;
-  while (!stop) 
+  while (!stop)
   {
     status = system(command.c_str());
     if(status && status != 153 && status != 256*153)
@@ -389,8 +411,8 @@ BatchTest::test_jobsubmit_simple()
   command = "scp";
   if(protocol == "rsh")
     command = "rcp";
-  command += " " 
-         + username + "@" + alias + ":" + home + "/" + _date + "_simple* /tmp";
+  command += " "
+          + username + "@" + hostname + ":" + home + "/" + _date + "_simple* /tmp";
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -399,7 +421,7 @@ BatchTest::test_jobsubmit_simple()
     result += oss.str();
     return result;
   }
-  
+
   // Test results
   std::string normal_input;
   std::string file_normal_name = "/tmp/" + _date + "_simple_output.log";
@@ -435,15 +457,16 @@ BatchTest::test_jobsubmit_simple()
   return result;
 }
 
-std::string 
-BatchTest::test_jobsubmit_mpi() 
+std::string
+BatchTest::test_jobsubmit_mpi()
 {
+#ifdef WITH_LIBBATCH
   int status;
   std::string home;
   std::string command;
   MpiImpl * mpiImpl;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
   std::string batch_type = _batch_descr.batch.in();
@@ -458,7 +481,9 @@ BatchTest::test_jobsubmit_mpi()
     mpiImpl = new MpiImpl_MPICH2();
   else if(mpi_type == "openmpi")
     mpiImpl = new MpiImpl_OPENMPI();
-  else if(mpi_type == "slurm")
+  else if(mpi_type == "ompi")
+    mpiImpl = new MpiImpl_OMPI();
+  else if(mpi_type == "slurmmpi")
     mpiImpl = new MpiImpl_SLURM();
   else
   {
@@ -474,6 +499,20 @@ BatchTest::test_jobsubmit_mpi()
     return result;
   }
 
+  if (batch_type == "ccc")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for ccc... return OK");
+    result = "OK";
+    return result;
+  }
+
+  if (batch_type == "slurm")
+  {
+    INFOS("test_jobsubmit_simple not yet implemented for slurm... return OK");
+    result = "OK";
+    return result;
+  }
+
   if (batch_type == "sge")
   {
     INFOS("test_jobsubmit_simple not yet implemented for sge... return OK");
@@ -493,7 +532,7 @@ BatchTest::test_jobsubmit_mpi()
   std::ofstream file_script;
   file_script.open(_test_file_script.c_str(), std::ofstream::out);
   file_script << "#!/bin/bash\n"
-             << "echo HELLO MPI\n";
+              << "echo HELLO MPI\n";
   file_script.flush();
   file_script.close();
 #ifdef WIN32
@@ -507,13 +546,13 @@ BatchTest::test_jobsubmit_mpi()
   std::ofstream file_mpi;
   file_mpi.open(_test_file_mpi.c_str(), std::ofstream::out);
   file_mpi << "#!/bin/bash\n"
-          << "#PBS -l nodes=1\n"
-          << "#PBS -l walltime=00:01:00\n"
-          << "#PBS -o " << home << "/" << _date << "_mpi_output.log\n"
-          << "#PBS -e " << home << "/" << _date << "_mpi_error.log\n"
-          << mpiImpl->boot("${PBS_NODEFILE}", 1)
-          << mpiImpl->run("${PBS_NODEFILE}", 1, _base_filename + "_script")
-          << mpiImpl->halt();
+           << "#PBS -l nodes=1\n"
+           << "#PBS -l walltime=00:01:00\n"
+           << "#PBS -o " << home << "/" << _date << "_mpi_output.log\n"
+           << "#PBS -e " << home << "/" << _date << "_mpi_error.log\n"
+           << mpiImpl->boot("${PBS_NODEFILE}", 1)
+           << mpiImpl->run("${PBS_NODEFILE}", 1, _base_filename + "_script")
+           << mpiImpl->halt();
   file_mpi.flush();
   file_mpi.close();
 
@@ -523,7 +562,7 @@ BatchTest::test_jobsubmit_mpi()
   if(protocol == "rsh")
     command = "rcp";
   command += " " + _test_file_script + " "
-         + username + "@" + alias + ":" + home;
+          + username + "@" + hostname + ":" + home;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -536,7 +575,7 @@ BatchTest::test_jobsubmit_mpi()
   if(protocol == "rsh")
     command = "rcp";
   command += " " + _test_file_mpi + " "
-         + username + "@" + alias + ":" + home;
+          + username + "@" + hostname + ":" + home;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -548,7 +587,7 @@ BatchTest::test_jobsubmit_mpi()
 
   // Build command for submit job
   std::string file_job_name = _test_filename + "_jobid";
-  command = protocol + " " + username + "@" + alias + " qsub " + _base_filename + "_mpi > " + file_job_name;
+  command = protocol + " " + username + "@" + hostname + " qsub " + _base_filename + "_mpi > " + file_job_name;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -566,11 +605,11 @@ BatchTest::test_jobsubmit_mpi()
   }
   std::getline(file_job, jobid);
   file_job.close();
+
   // Wait the end of the job
-  command = protocol + " " + username + "@" + alias + " qstat -f " + jobid + " > " + file_job_name;
+  command = protocol + " " + username + "@" + hostname + " qstat -f " + jobid + " > " + file_job_name;
   bool stop = false;
-  while (!stop) 
+  while (!stop)
   {
     status = system(command.c_str());
     if(status && status != 153 && status != 256*153)
@@ -595,8 +634,8 @@ BatchTest::test_jobsubmit_mpi()
   command = "scp";
   if(protocol == "rsh")
     command = "rcp";
-  command += " " 
-         + username + "@" + alias + ":" + home + "/" + _date + "_mpi* /tmp";
+  command += " "
+          + username + "@" + hostname + ":" + home + "/" + _date + "_mpi* /tmp";
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -605,7 +644,7 @@ BatchTest::test_jobsubmit_mpi()
     result += oss.str();
     return result;
   }
-  
+
   // Test results
   std::string normal_input;
   std::string file_normal_name = "/tmp/" + _date + "_mpi_output.log";
@@ -628,21 +667,25 @@ BatchTest::test_jobsubmit_mpi()
     return result;
   }
   result = "OK";
-  return result;  
+  return result;
+#else
+  throw LauncherException("Method BatchTest::test_jobsubmit_mpi is not available "
+                          "(libBatch was not present at compilation time)");
+#endif
 }
 
-std::string 
+std::string
 BatchTest::test_appli()
 {
   int status;
   std::string home;
   std::string command;
   std::string result("Failed : ");
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
   std::string applipath = _batch_descr.applipath.in();
-  
+
   // Getting home directory
   std::string rst = get_home(&home);
   if(rst != "") {
@@ -654,12 +697,12 @@ BatchTest::test_appli()
   std::ofstream file_appli;
   file_appli.open(_test_file_appli.c_str(), std::ofstream::out);
   file_appli << "#!/bin/bash\n"
-            << "if [ -f " << applipath << "/runAppli ]\n"
-            << "then\n"
-            << "  echo OK\n"
-            << "else\n"
-            << "  echo NOK\n"
-            << "fi\n";
+             << "if [ -f " << applipath << "/salome ]\n"
+             << "then\n"
+             << "  echo OK\n"
+             << "else\n"
+             << "  echo NOK\n"
+             << "fi\n";
   file_appli.flush();
   file_appli.close();
 
@@ -668,7 +711,7 @@ BatchTest::test_appli()
   if(protocol == "rsh")
     command = "rcp";
   command += " " + _test_file_appli + " "
-         + username + "@" + alias + ":" + home;
+          + username + "@" + hostname + ":" + home;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;
@@ -679,9 +722,9 @@ BatchTest::test_appli()
   }
 
   // Launch test
-  command = protocol + " " + username + "@" + alias 
-         + " sh " + home + "/" + _base_filename + "_appli_test > " 
-         + _test_filename + "_appli_test_result";
+  command = protocol + " " + username + "@" + hostname
+          + " sh " + home + "/" + _base_filename + "_appli_test > "
+          + _test_filename + "_appli_test_result";
 
   status = system(command.c_str());
   if(status) {
@@ -703,7 +746,7 @@ BatchTest::test_appli()
   }
   std::getline(file_appli_result, rst_appli);
   file_appli_result.close();
-  
+
   if (rst_appli != "OK")
   {
     result += "Error checking application on remote host ! result = " + rst;
@@ -721,12 +764,12 @@ BatchTest::get_home(std::string * home)
   int status;
   std::string result = "";
   std::string command;
-  std::string alias = _batch_descr.alias.in();
+  std::string hostname = _batch_descr.hostname.in();
   std::string username = _batch_descr.username.in();
   std::string protocol = _batch_descr.protocol.in();
   std::string file_home_name = _test_filename + "_home";
 
-  command = protocol + " " + username + "@" + alias + " 'echo $HOME' > " + file_home_name; 
+  command = protocol + " " + username + "@" + hostname + " 'echo $HOME' > " + file_home_name;
   status = system(command.c_str());
   if(status) {
     std::ostringstream oss;