Salome HOME
Major refactoring in classes Versatile, Parametre and Swig wrappings.
[tools/libbatch.git] / src / PBS / Test / Test_ePBS.cxx
index 642cd5eecd6e1fe2f341a33f259d087863893d18..1040faae9374eec5b416cf263acb17e71c30f771 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2011  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
@@ -29,7 +29,9 @@
 
 #include <iostream>
 #include <fstream>
+#include <cstring>
 
+#include <Batch_Constants.hxx>
 #include <Batch_Job.hxx>
 #include <Batch_BatchManagerCatalog.hxx>
 #include <Batch_FactBatchManager.hxx>
 
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
+void print_usage()
+{
+  cout << "usage: Test_ePBS PROTOCOL" << endl;
+  cout << "    PROTOCOL      \"SSH\" or \"RSH\"" << endl;
+}
+
 int main(int argc, char** argv)
 {
+  // Parse argument
+  if (argc != 2) {
+    print_usage();
+    return 1;
+  }
+  CommunicationProtocolType protocol;
+  if (strcmp(argv[1], "SSH") == 0)
+    protocol = SSH;
+  else if (strcmp(argv[1], "RSH") == 0)
+    protocol = RSH;
+  else {
+    print_usage();
+    return 1;
+  }
+
   cout << "*******************************************************************************************" << endl;
-  cout << "This program tests the batch submission based on PBS emulation. Passwordless SSH" << endl;
-  cout << "authentication must be used for this test to pass (this can be configured with ssh-agent" << endl;
-  cout << "for instance). You also need to create a directory \"tmp/Batch\" in your home directory on" << endl;
-  cout << "the PBS server before running this test." << endl;
+  cout << "This program tests the batch submission based on PBS emulation. Passwordless authentication" << endl;
+  cout << "must be used for this test to pass. For SSH, this can be configured with ssh-agent for" << endl;
+  cout << "instance. For RSH, this can be configured with the .rhosts file." << endl;
   cout << "*******************************************************************************************" << endl;
 
   // eventually remove any previous result
@@ -64,34 +80,32 @@ int main(int argc, char** argv)
     // Parse the test configuration file
     SimpleParser parser;
     parser.parseTestConfigFile();
-    const string & homedir = parser.getValue("TEST_PBS_HOMEDIR");
-    const string & host = parser.getValue("TEST_PBS_HOST");
-    const string & user = parser.getValue("TEST_PBS_USER");
-    const string & queue = parser.getValue("TEST_PBS_QUEUE");
-    int timeout = parser.getValueAsInt("TEST_PBS_TIMEOUT");
+    const string & homedir = parser.getValue("TEST_EPBS_HOMEDIR");
+    const string & host = parser.getValue("TEST_EPBS_HOST");
+    const string & user = parser.getValue("TEST_EPBS_USER");
+    const string & queue = parser.getValue("TEST_EPBS_QUEUE");
+    int timeout = parser.getValueAsInt("TEST_EPBS_TIMEOUT");
 
     // Define the job...
     Job job;
     // ... and its parameters ...
     Parametre p;
-    p["EXECUTABLE"]    = "./test-script.sh";
-    p["NAME"]          = "Test_ePBS";
-    p["WORKDIR"]       = homedir + "/tmp/Batch";
-    p["INFILE"]        = Couple("seta.sh", "tmp/Batch/seta.sh");
-    p["INFILE"]       += Couple("setb.sh", "tmp/Batch/setb.sh");
-    p["OUTFILE"]       = Couple("result.txt", "tmp/Batch/result.txt");
-    p["TMPDIR"]        = "tmp/Batch/";
-    p["USER"]          = user;
-    p["NBPROC"]        = 1;
-    p["MAXWALLTIME"]   = 1;
-    p["MAXRAMSIZE"]    = 4;
-    p["HOMEDIR"]       = homedir;
-    p["QUEUE"]         = queue;
+    p[EXECUTABLE]    = "./test-script.sh";
+    p[NAME]          = string("Test_ePBS_") + argv[1];
+    p[WORKDIR]       = homedir + "/tmp/Batch";
+    p[INFILE]        = Couple("seta.sh", "tmp/Batch/seta.sh");
+    p[INFILE]       += Couple("setb.sh", "tmp/Batch/setb.sh");
+    p[OUTFILE]       = Couple("result.txt", "tmp/Batch/result.txt");
+    p[TMPDIR]        = "tmp/Batch/";
+    p[NBPROC]        = 1;
+    p[MAXWALLTIME]   = 1;
+    p[MAXRAMSIZE]    = 1;
+    p[HOMEDIR]       = homedir;
+    p[QUEUE]         = queue;
     job.setParametre(p);
-    // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
+    // ... and its environment
     Environnement e;
-    const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
-    if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
+    e["MYENVVAR"] = "MYVALUE";
     job.setEnvironnement(e);
     cout << job << endl;
 
@@ -100,24 +114,18 @@ int main(int argc, char** argv)
 
     // Create a BatchManager of type ePBS on localhost
     FactBatchManager_eClient * fbm = (FactBatchManager_eClient *)(c("ePBS"));
-    BatchManager_eClient * bm = (*fbm)(host.c_str(), "ssh", "lam");
+    BatchManager_eClient * bm = (*fbm)(host.c_str(), user.c_str(), protocol, "nompi", 8);
 
     // Submit the job to the BatchManager
     JobId jobid = bm->submitJob(job);
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    string state = "Undefined";
-    for (int i=0 ; i<timeout/2 && state != "U"; i++) {
-      sleep(2);
-      JobInfo jinfo = jobid.queryJob();
-      state = jinfo.getParametre()["STATE"].str();
-      cout << "State is \"" << state << "\"" << endl;
-    }
+    string state = bm->waitForJobEnd(jobid, timeout);
 
-    if (state == "U") {
+    if (state == FINISHED || state == FAILED) {
       cout << "Job " << jobid.__repr__() << " is done" << endl;
-      bm->importOutputFiles(job, ".");
+      bm->importOutputFiles(job, "resultdir/seconddirname");
     } else {
       cerr << "Timeout while executing job" << endl;
       return 1;
@@ -132,16 +140,21 @@ int main(int argc, char** argv)
   }
 
   // test the result file
-  string exp = "c = 12";
-  string res;
-  ifstream f("result.txt");
-  getline(f, res);
-  f.close();
-
-  cout << "result found : " << res << ", expected : " << exp << endl;
-
-  if (res == exp)
-    return 0;
-  else
+  try {
+    SimpleParser resultParser;
+    resultParser.parse("resultdir/seconddirname/result.txt");
+    cout << "Result:" << endl << resultParser;
+    const string & envvar = resultParser.getValue("MYENVVAR");
+    int result = resultParser.getValueAsInt("c");
+    if (envvar == "MYVALUE" && result == 12) {
+      cout << "OK, Expected result found." << endl;
+      return 0;
+    } else {
+      cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
+      return 1;
+    }
+  } catch (ParserException e) {
+    cerr << "Parser error on result file: " << e.what() << endl;
     return 1;
+  }
 }