Salome HOME
Added Windows implementation for local submission based on SSH and PBS emulation...
[tools/libbatch.git] / src / PBS / Test / Test_ePBS.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * Test_ePBS.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : April 2009
27  *
28  */
29
30 #include <iostream>
31 #include <fstream>
32
33 #include <Batch_Job.hxx>
34 #include <Batch_BatchManagerCatalog.hxx>
35 #include <Batch_FactBatchManager.hxx>
36 #include <Batch_FactBatchManager_eClient.hxx>
37 #include <Batch_BatchManager.hxx>
38 #include <Batch_BatchManager_eClient.hxx>
39 #include <Test_PBS_config.h>
40
41 using namespace std;
42 using namespace Batch;
43
44 int main(int argc, char** argv)
45 {
46   cout << "*******************************************************************************************" << endl;
47   cout << "This program tests the batch submission based on PBS emulation. Passwordless SSH" << endl;
48   cout << "authentication must be used for this test to pass (this can be configured with ssh-agent" << endl;
49   cout << "for instance). You also need to create a directory \"tmp/Batch\" in your home directory on" << endl;
50   cout << "the PBS server before running this test." << endl;
51   cout << "*******************************************************************************************" << endl;
52
53   // eventually remove any previous result
54   remove("result.txt");
55
56   try {
57     // Define the job...
58     Job job;
59     // ... and its parameters ...
60     Parametre p;
61     p["EXECUTABLE"]    = "./test-script.sh";
62     p["NAME"]          = "Test_ePBS";
63     p["WORKDIR"]       = string(TEST_PBS_HOMEDIR) + "/tmp/Batch";
64     p["INFILE"]        = Couple("seta.sh", "tmp/Batch/seta.sh");
65     p["INFILE"]       += Couple("setb.sh", "tmp/Batch/setb.sh");
66     p["OUTFILE"]       = Couple("result.txt", "tmp/Batch/result.txt");
67     p["TMPDIR"]        = "tmp/Batch/";
68     p["USER"]          = TEST_PBS_USER;
69     p["NBPROC"]        = 1;
70     p["MAXWALLTIME"]   = 1;
71     p["MAXRAMSIZE"]    = 4;
72     p["HOMEDIR"]       = TEST_PBS_HOMEDIR;
73     p["QUEUE"]         = TEST_PBS_QUEUE;
74     job.setParametre(p);
75     // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
76     Environnement e;
77     const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
78     if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
79     job.setEnvironnement(e);
80     cout << job << endl;
81
82     // Get the catalog
83     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
84
85     // Create a BatchManager of type ePBS on localhost
86     FactBatchManager_eClient * fbm = (FactBatchManager_eClient *)(c("ePBS"));
87     BatchManager_eClient * bm = (*fbm)(TEST_PBS_HOST, "ssh", "lam");
88
89     // Submit the job to the BatchManager
90     JobId jobid = bm->submitJob(job);
91     cout << jobid.__repr__() << endl;
92
93     // Wait for the end of the job
94     string state = "Undefined";
95     for (int i=0 ; i<60 && state != "U"; i++) {
96       sleep(2);
97       JobInfo jinfo = jobid.queryJob();
98       state = jinfo.getParametre()["STATE"].str();
99       cout << "State is \"" << state << "\"" << endl;
100     }
101
102     if (state == "U") {
103       cout << "Job " << jobid.__repr__() << " is done" << endl;
104       bm->importOutputFiles(job, ".");
105     } else {
106       cerr << "Timeout while executing job" << endl;
107       return 1;
108     }
109
110   } catch (GenericException e) {
111     cerr << "Error: " << e << endl;
112     return 1;
113   }
114
115   // test the result file
116   string exp = "c = 12";
117   string res;
118   ifstream f("result.txt");
119   getline(f, res);
120   f.close();
121
122   cout << "result found : " << res << ", expected : " << exp << endl;
123
124   if (res == exp)
125     return 0;
126   else
127     return 1;
128 }