1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * Test_BatchManager.cxx :
25 * Author : Renaud BARATE - EDF R&D
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>
40 #include "SimpleParser.hxx"
42 #include <Test_config.h>
45 using namespace Batch;
49 cout << "usage: Test_BatchManager BATCH_MANAGER PROTOCOL" << endl;
50 cout << " BATCH_MANAGER \"CCC\", \"LL\", \"LOCAL\", \"LSF\", \"PBS\", " <<
51 "\"SGE\", \"SLURM\" or \"VISHNU\"" << endl;
52 cout << " PROTOCOL \"SH\", \"SSH\" or \"RSH\"" << endl;
55 int main(int argc, char** argv)
62 const char * bmType = argv[1];
63 CommunicationProtocolType protocol;
64 if (strcmp(argv[2], "SSH") == 0)
66 else if (strcmp(argv[2], "RSH") == 0)
68 else if (strcmp(argv[2], "SH") == 0)
75 cout << "*******************************************************************************************" << endl;
76 cout << "This program tests the batch submission of a job using the batch manager \"" << bmType << "\"" << endl;
77 cout << "and the communication protocol \"" << argv[2] << "\"." << endl;
78 if (protocol == RSH || protocol == SSH) {
79 cout << "Passwordless authentication must be used for this test to pass." << endl;
80 if (protocol == SSH) {
81 cout << "This can be configured with ssh-agent for instance." << endl;
82 } else if (protocol == RSH) {
83 cout << "This can be configured with the .rhosts file." << endl;
86 cout << "*******************************************************************************************" << endl;
88 // eventually remove any previous result
89 remove("resultdir/seconddirname/result.txt");
92 // Get the catalog and the BatchManager factory
93 BatchManagerCatalog& cata = BatchManagerCatalog::getInstance();
94 FactBatchManager * fbm = cata(bmType);
96 // Parse the test configuration file
98 parser.parseTestConfigFile();
99 const string & workdir = parser.getTestValue(bmType, "WORKDIR");
100 const string & host = parser.getTestValue(bmType, "HOST");
101 const string & user = parser.getTestValue(bmType, "USER");
102 int timeout = parser.getTestValueAsInt(bmType, "TIMEOUT");
106 // ... and its parameters ...
108 p[EXECUTABLE] = string(CMAKE_CURRENT_SOURCE_DIR) + "/test-script.sh";
109 p[NAME] = string("Test ") + bmType + " " + argv[2];
110 p[WORKDIR] = workdir;
111 p[INFILE] = Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/seta.sh", "seta.sh");
112 p[INFILE] += Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/setb.sh", "setb.sh");
113 p[OUTFILE] = Couple("result.txt", "result.txt");
118 // ... and its environment
120 e["MYENVVAR"] = "MYVALUE";
121 job.setEnvironnement(e);
124 // Create the BatchManager
125 BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
127 // Submit the job to the BatchManager
128 JobId jobid = bm->submitJob(job);
129 cout << jobid.__repr__() << endl;
131 // Wait for the end of the job
132 string state = bm->waitForJobEnd(jobid, timeout);
134 if (state == FINISHED) {
135 cout << "Job " << jobid.__repr__() << " is done" << endl;
136 bm->importOutputFiles(job, "resultdir/seconddirname");
137 } else if (state == FAILED) {
138 cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
139 bm->importOutputFiles(job, "resultdir/seconddirname");
142 cerr << "Timeout while executing job" << endl;
146 } catch (const GenericException & e) {
147 cerr << "Error: " << e << endl;
149 } catch (const ParserException & e) {
150 cerr << "Parser error: " << e.what() << endl;
154 // test the result file
156 SimpleParser resultParser;
157 resultParser.parse("resultdir/seconddirname/result.txt");
158 cout << "Result:" << endl << resultParser;
159 const string & envvar = resultParser.getValue("MYENVVAR");
160 int result = resultParser.getValueAsInt("c");
161 if (envvar == "MYVALUE" && result == 12) {
162 cout << "OK, Expected result found." << endl;
165 cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
168 } catch (const ParserException & e) {
169 cerr << "Parser error on result file: " << e.what() << endl;