Salome HOME
98aa060c8b47b67faff58ac5bfeb6b3f81f43f39
[tools/libbatch.git] / src / LSF / Test / Test_eLSF.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_eLSF.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : September 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 void print_usage()
46 {
47   cout << "usage: Test_eLSF PROTOCOL" << endl;
48   cout << "    PROTOCOL      \"SSH\" or \"RSH\"" << endl;
49 }
50
51 int main(int argc, char** argv)
52 {
53   // Parse argument
54   if (argc != 2) {
55     print_usage();
56     return 1;
57   }
58   CommunicationProtocolType protocol;
59   if (strcmp(argv[1], "SSH") == 0)
60     protocol = SSH;
61   else if (strcmp(argv[1], "RSH") == 0)
62     protocol = RSH;
63   else {
64     print_usage();
65     return 1;
66   }
67
68   cout << "*******************************************************************************************" << endl;
69   cout << "This program tests the batch submission based on LSF emulation. Passwordless" << endl;
70   cout << "authentication must be used for this test to pass. For SSH, this can be configured with" << endl;
71   cout << "ssh-agent for instance. For RSH, this can be configured with the .rhosts file." << endl;
72   cout << "*******************************************************************************************" << endl;
73
74   // eventually remove any previous result
75   remove("result.txt");
76
77   try {
78     // Parse the test configuration file
79     SimpleParser parser;
80     parser.parseTestConfigFile();
81     const string & homedir = parser.getValue("TEST_ELSF_HOMEDIR");
82     const string & host = parser.getValue("TEST_ELSF_HOST");
83     const string & user = parser.getValue("TEST_ELSF_USER");
84     int timeout = parser.getValueAsInt("TEST_ELSF_TIMEOUT");
85
86     // Define the job...
87     Job job;
88     // ... and its parameters ...
89     Parametre p;
90     p[EXECUTABLE]    = "./test-script.sh";
91     p[NAME]          = string("Test_eLSF_") + argv[1];
92     p[WORKDIR]       = homedir + "/tmp/Batch";
93     p[INFILE]        = Couple("seta.sh", "tmp/Batch/seta.sh");
94     p[INFILE]       += Couple("setb.sh", "tmp/Batch/setb.sh");
95     p[OUTFILE]       = Couple("result.txt", "tmp/Batch/result.txt");
96     p[NBPROC]        = 1;
97     p[MAXWALLTIME]   = 1;
98     p[MAXRAMSIZE]    = 128;
99     p[HOMEDIR]       = homedir;
100     p[EXCLUSIVE]     = true;
101     job.setParametre(p);
102     // ... and its environment
103     Environnement e;
104     e["MYENVVAR"] = "MYVALUE";
105     job.setEnvironnement(e);
106     cout << job << endl;
107
108     // Get the catalog
109     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
110
111     // Create a BatchManager of type ePBS on localhost
112     FactBatchManager * fbm = c("LSF");
113     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
114
115     // Submit the job to the BatchManager
116     JobId jobid = bm->submitJob(job);
117     cout << jobid.__repr__() << endl;
118
119     // Wait for the end of the job
120     string state = bm->waitForJobEnd(jobid, timeout);
121
122     if (state == FINISHED) {
123       cout << "Job " << jobid.__repr__() << " is done" << endl;
124       bm->importOutputFiles(job, "resultdir/seconddirname");
125     } else if (state == FAILED) {
126       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
127       bm->importOutputFiles(job, "resultdir/seconddirname");
128       return 1;
129     } else {
130       cerr << "Timeout while executing job" << endl;
131       return 1;
132     }
133
134   } catch (GenericException e) {
135     cerr << "Error: " << e << endl;
136     return 1;
137   } catch (ParserException e) {
138     cerr << "Parser error: " << e.what() << endl;
139     return 1;
140   }
141
142   // test the result file
143   try {
144     SimpleParser resultParser;
145     resultParser.parse("resultdir/seconddirname/result.txt");
146     cout << "Result:" << endl << resultParser;
147     const string & envvar = resultParser.getValue("MYENVVAR");
148     int result = resultParser.getValueAsInt("c");
149     if (envvar == "MYVALUE" && result == 12) {
150       cout << "OK, Expected result found." << endl;
151       return 0;
152     } else {
153       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
154       return 1;
155     }
156   } catch (ParserException e) {
157     cerr << "Parser error on result file: " << e.what() << endl;
158     return 1;
159   }
160 }