Salome HOME
Merge class BatchManager_eClient into class BatchManager
[tools/libbatch.git] / src / Vishnu / Test / Test_eVishnu.cxx
1 //  Copyright (C) 2007-2012  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_eVishnu.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : June 2011
27  *
28  */
29
30 #include <iostream>
31 #include <fstream>
32 #include <cstring>
33
34 #include <Batch_Constants.hxx>
35 #include <Batch_Job.hxx>
36 #include <Batch_BatchManagerCatalog.hxx>
37 #include <Batch_FactBatchManager.hxx>
38 #include <Batch_BatchManager.hxx>
39
40 #include <SimpleParser.hxx>
41
42 using namespace std;
43 using namespace Batch;
44
45 int main(int argc, char** argv)
46 {
47   cout << "*******************************************************************************************" << endl;
48   cout << "This program tests the batch submission based on Vishnu commands." << endl;
49   cout << "*******************************************************************************************" << endl;
50
51   // eventually remove any previous result
52   remove("result.txt");
53
54   try {
55     // Parse the test configuration file
56     SimpleParser parser;
57     parser.parseTestConfigFile();
58     const string & homedir = parser.getValue("TEST_EVISHNU_HOMEDIR");
59     const string & host = parser.getValue("TEST_EVISHNU_HOST");
60     const string & user = parser.getValue("TEST_EVISHNU_USER");
61     int timeout = parser.getValueAsInt("TEST_EVISHNU_TIMEOUT");
62
63     // Define the job...
64     Job job;
65     // ... and its parameters ...
66     Parametre p;
67     p[EXECUTABLE]    = "./test-script.sh";
68     p[NAME]          = "Test_eVISHNU";
69     p[WORKDIR]       = homedir + "/tmp/Batch";
70     p[INFILE]        = Couple("seta.sh", "seta.sh");
71     p[INFILE]       += Couple("setb.sh", "setb.sh");
72     p[OUTFILE]       = Couple("result.txt", "result.txt");
73     p[NBPROC]        = 1;
74     p[MAXWALLTIME]   = 1;
75     p[MAXRAMSIZE]    = 50;
76     job.setParametre(p);
77     // ... and its environment
78     Environnement e;
79     e["MYENVVAR"] = "MYVALUE";
80     job.setEnvironnement(e);
81     cout << job << endl;
82
83     // Get the catalog
84     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
85
86     // Create a BatchManager of type ePBS on localhost
87     FactBatchManager * fbm = c("VISHNU");
88     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), SH);
89
90     // Submit the job to the BatchManager
91     JobId jobid = bm->submitJob(job);
92     cout << jobid.__repr__() << endl;
93
94     // Wait for the end of the job
95     string state = bm->waitForJobEnd(jobid, timeout);
96
97     if (state == FINISHED) {
98       cout << "Job " << jobid.__repr__() << " is done" << endl;
99       bm->importOutputFiles(job, "resultdir/seconddirname");
100     } else if (state == FAILED) {
101       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
102       bm->importOutputFiles(job, "resultdir/seconddirname");
103       return 1;
104     } else {
105       cerr << "Timeout while executing job" << endl;
106       return 1;
107     }
108
109   } catch (GenericException e) {
110     cerr << "Error: " << e << endl;
111     return 1;
112   } catch (ParserException e) {
113     cerr << "Parser error: " << e.what() << endl;
114     return 1;
115   }
116
117   // test the result file
118   try {
119     SimpleParser resultParser;
120     resultParser.parse("resultdir/seconddirname/result.txt");
121     cout << "Result:" << endl << resultParser;
122     const string & envvar = resultParser.getValue("MYENVVAR");
123     int result = resultParser.getValueAsInt("c");
124     if (envvar == "MYVALUE" && result == 12) {
125       cout << "OK, Expected result found." << endl;
126       return 0;
127     } else {
128       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
129       return 1;
130     }
131   } catch (ParserException e) {
132     cerr << "Parser error on result file: " << e.what() << endl;
133     return 1;
134   }
135 }