Salome HOME
Fixed nodes file when nb procs > 128
[tools/libbatch.git] / src / PBS / Test / Test_PBS.cxx
1 //  Copyright (C) 2007-2010  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_PBS.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : September 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_BatchManager.hxx>
37
38 #include <SimpleParser.hxx>
39
40 using namespace std;
41 using namespace Batch;
42
43 int main(int argc, char** argv)
44 {
45   cout << "*******************************************************************************************" << endl;
46   cout << "This program tests the batch submission based on PBS." << endl;
47   cout << "*******************************************************************************************" << endl;
48
49   // eventually remove any previous result
50   remove("result.txt");
51
52   try {
53     // Parse the test configuration file
54     SimpleParser parser;
55     parser.parseTestConfigFile();
56     const string & host = parser.getValue("TEST_PBS_HOST");
57     const string & user = parser.getValue("TEST_PBS_USER");
58     const string & queue = parser.getValue("TEST_PBS_QUEUE");
59     int timeout = parser.getValueAsInt("TEST_PBS_TIMEOUT");
60
61     char * cwd =
62 #ifdef WIN32
63       _getcwd(NULL, 0);
64 #else
65       new char [PATH_MAX];
66     getcwd(cwd, PATH_MAX);
67 #endif
68     string workdir = cwd;
69     delete [] cwd;
70
71     // Define the job...
72     Job job;
73     // ... and its parameters ...
74     Parametre p;
75     p[EXECUTABLE]    = "test-script.sh";
76     p[NAME]          = "Test_PBS";
77     p[INFILE]        = Couple(workdir + "/seta.sh", "seta.sh");
78     p[INFILE]       += Couple(workdir + "/setb.sh", "setb.sh");
79     p[OUTFILE]       = Couple(workdir + "/result.txt", "result.txt");
80     p[USER]          = user;
81     p[NBPROC]        = 1;
82     p[MAXWALLTIME]   = 1;
83     p[MAXRAMSIZE]    = 4;
84     p[QUEUE]         = queue;
85     job.setParametre(p);
86     // ... and its environment
87     Environnement e;
88     e["MYENVVAR"] = "MYVALUE";
89     job.setEnvironnement(e);
90     cout << job << endl;
91
92     // Get the catalog
93     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
94
95     // Create a BatchManager of type ePBS on localhost
96     FactBatchManager * fbm = c("PBS");
97     BatchManager * bm = (*fbm)(host.c_str());
98
99     // Submit the job to the BatchManager
100     JobId jobid = bm->submitJob(job);
101     cout << jobid.__repr__() << endl;
102
103     // Wait for the end of the job
104     string state = bm->waitForJobEnd(jobid, timeout);
105
106     if (state == FINISHED || state == FAILED) {
107       cout << "Job " << jobid.__repr__() << " is done" << endl;
108     } else {
109       cerr << "Timeout while executing job" << endl;
110       return 1;
111     }
112
113   } catch (GenericException e) {
114     cerr << "Error: " << e << endl;
115     return 1;
116   } catch (ParserException e) {
117     cerr << "Parser error: " << e.what() << endl;
118     return 1;
119   }
120
121   // test the result file
122   try {
123     SimpleParser resultParser;
124     resultParser.parse("result.txt");
125     cout << "Result:" << endl << resultParser;
126     const string & envvar = resultParser.getValue("MYENVVAR");
127     int result = resultParser.getValueAsInt("c");
128     if (envvar == "MYVALUE" && result == 12) {
129       cout << "OK, Expected result found." << endl;
130       return 0;
131     } else {
132       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
133       return 1;
134     }
135   } catch (ParserException e) {
136     cerr << "Parser error on result file: " << e.what() << endl;
137     return 1;
138   }
139 }