1 // Copyright (C) 2007-2014 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 <Constants.hxx>
36 #include <BatchManagerCatalog.hxx>
37 #include <FactBatchManager.hxx>
38 #include <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 const char * protocolStr = argv[2];
64 CommunicationProtocolType protocol;
65 if (strcmp(protocolStr, "SSH") == 0)
67 else if (strcmp(protocolStr, "RSH") == 0)
69 else if (strcmp(protocolStr, "SH") == 0)
76 cout << "*******************************************************************************************" << endl;
77 cout << "This program tests the batch submission of a job using the batch manager \"" << bmType << "\"" << endl;
78 cout << "and the communication protocol \"" << protocolStr << "\"." << endl;
79 if (protocol == RSH || protocol == SSH) {
80 cout << "Passwordless authentication must be used for this test to pass." << endl;
81 if (protocol == SSH) {
82 cout << "This can be configured with ssh-agent for instance." << endl;
83 } else if (protocol == RSH) {
84 cout << "This can be configured with the .rhosts file." << endl;
87 cout << "*******************************************************************************************" << endl;
89 // eventually remove any previous result
90 remove("resultdir/seconddirname/result.txt");
93 // Get the catalog and the BatchManager factory
94 BatchManagerCatalog& cata = BatchManagerCatalog::getInstance();
95 FactBatchManager * fbm = cata(bmType);
97 // Parse the test configuration file
99 parser.parseTestConfigFile();
100 const string & workdir = parser.getTestValue(bmType, protocolStr, "WORKDIR");
101 const string & host = parser.getTestValue(bmType, protocolStr, "HOST");
102 const string & user = parser.getTestValue(bmType, protocolStr, "USER");
103 int timeout = parser.getTestValueAsInt(bmType, protocolStr, "TIMEOUT");
107 // ... and its parameters ...
109 p[EXECUTABLE] = string(CMAKE_CURRENT_SOURCE_DIR) + "/test_script.py";
110 p[ARGUMENTS] = "copied_seta.py";
111 p[ARGUMENTS] += "copied_setb.py";
112 p[ARGUMENTS] += "orig_result.txt";
113 p[NAME] = string("Test ") + bmType + " " + argv[2];
114 p[WORKDIR] = workdir;
115 p[INFILE] = Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/seta.py", "copied_seta.py");
116 p[INFILE] += Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/setb.py", "copied_setb.py");
117 p[OUTFILE] = Couple("result.txt", "orig_result.txt");
122 // ... and its environment
124 e["MYENVVAR"] = "MYVALUE";
125 job.setEnvironnement(e);
128 // Create the BatchManager
129 BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
131 // Submit the job to the BatchManager
132 JobId jobid = bm->submitJob(job);
133 cout << jobid.__repr__() << endl;
135 // Wait for the end of the job
136 string state = bm->waitForJobEnd(jobid, timeout);
138 if (state == FINISHED) {
139 cout << "Job " << jobid.__repr__() << " is done" << endl;
140 bm->importOutputFiles(job, "resultdir/seconddirname");
141 } else if (state == FAILED) {
142 cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
143 bm->importOutputFiles(job, "resultdir/seconddirname");
146 cerr << "Timeout while executing job" << endl;
150 } catch (const GenericException & e) {
151 cerr << "Error: " << e << endl;
153 } catch (const ParserException & e) {
154 cerr << "Parser error: " << e.what() << endl;
158 // test the result file
160 SimpleParser resultParser;
161 resultParser.parse("resultdir/seconddirname/result.txt");
162 cout << "Result:" << endl << resultParser;
163 const string & envvar = resultParser.getValue("MYENVVAR");
164 int result = resultParser.getValueAsInt("c");
165 if (envvar == "MYVALUE" && result == 12) {
166 cout << "OK, Expected result found." << endl;
169 cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
172 } catch (const ParserException & e) {
173 cerr << "Parser error on result file: " << e.what() << endl;