Salome HOME
Fix configuration / compilation problems in KERNEL (wrong #include syntax for local...
[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_Constants.hxx"
34 #include "Batch_Job.hxx"
35 #include "Batch_BatchManagerCatalog.hxx"
36 #include "Batch_FactBatchManager.hxx"
37 #include "Batch_BatchManager.hxx"
38
39 #include "SimpleParser.hxx"
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." << endl;
48   cout << "*******************************************************************************************" << endl;
49
50   // eventually remove any previous result
51   remove("result.txt");
52
53   try {
54     // Parse the test configuration file
55     SimpleParser parser;
56     parser.parseTestConfigFile();
57     const string & host = parser.getValue("TEST_PBS_HOST");
58     const string & user = parser.getValue("TEST_PBS_USER");
59     const string & queue = parser.getValue("TEST_PBS_QUEUE");
60     int timeout = parser.getValueAsInt("TEST_PBS_TIMEOUT");
61
62     char * cwd =
63 #ifdef WIN32
64       _getcwd(NULL, 0);
65 #else
66       new char [PATH_MAX];
67     getcwd(cwd, PATH_MAX);
68 #endif
69     string workdir = cwd;
70     delete [] cwd;
71
72     // Define the job...
73     Job job;
74     // ... and its parameters ...
75     Parametre p;
76     p[EXECUTABLE]    = "test-script.sh";
77     p[NAME]          = "Test_PBS";
78     p[INFILE]        = Couple(workdir + "/seta.sh", "seta.sh");
79     p[INFILE]       += Couple(workdir + "/setb.sh", "setb.sh");
80     p[OUTFILE]       = Couple(workdir + "/result.txt", "result.txt");
81     p[USER]          = user;
82     p[NBPROC]        = 1;
83     p[MAXWALLTIME]   = 1;
84     p[MAXRAMSIZE]    = 4;
85     p[QUEUE]         = queue;
86     job.setParametre(p);
87     // ... and its environment
88     Environnement e;
89     e["MYENVVAR"] = "MYVALUE";
90     job.setEnvironnement(e);
91     cout << job << endl;
92
93     // Get the catalog
94     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
95
96     // Create a BatchManager of type ePBS on localhost
97     FactBatchManager * fbm = c("PBS");
98     BatchManager * bm = (*fbm)(host.c_str());
99
100     // Submit the job to the BatchManager
101     JobId jobid = bm->submitJob(job);
102     cout << jobid.__repr__() << endl;
103
104     // Wait for the end of the job
105     string state = bm->waitForJobEnd(jobid, timeout);
106
107     if (state == FINISHED || state == FAILED) {
108       cout << "Job " << jobid.__repr__() << " is done" << endl;
109     } else {
110       cerr << "Timeout while executing job" << endl;
111       return 1;
112     }
113
114   } catch (GenericException e) {
115     cerr << "Error: " << e << endl;
116     return 1;
117   } catch (ParserException e) {
118     cerr << "Parser error: " << e.what() << endl;
119     return 1;
120   }
121
122   // test the result file
123   try {
124     SimpleParser resultParser;
125     resultParser.parse("result.txt");
126     cout << "Result:" << endl << resultParser;
127     const string & envvar = resultParser.getValue("MYENVVAR");
128     int result = resultParser.getValueAsInt("c");
129     if (envvar == "MYVALUE" && result == 12) {
130       cout << "OK, Expected result found." << endl;
131       return 0;
132     } else {
133       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
134       return 1;
135     }
136   } catch (ParserException e) {
137     cerr << "Parser error on result file: " << e.what() << endl;
138     return 1;
139   }
140 }