1 // Copyright (C) 2007-2008 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
25 * Author : Renaud BARATE - EDF R&D
34 #include <Batch_Job.hxx>
35 #include <Batch_BatchManagerCatalog.hxx>
36 #include <Batch_FactBatchManager.hxx>
37 #include <Batch_FactBatchManager_eClient.hxx>
38 #include <Batch_BatchManager.hxx>
39 #include <Batch_BatchManager_eClient.hxx>
41 #include <SimpleParser.hxx>
45 #define sleep(seconds) Sleep((seconds)*1000)
46 #define usleep(useconds) Sleep((useconds)/1000)
50 using namespace Batch;
52 const int MAX_SLEEP_TIME = 600;
56 cout << "usage: Test_ePBS PROTOCOL" << endl;
57 cout << " PROTOCOL \"SSH\" or \"RSH\"" << endl;
60 int main(int argc, char** argv)
67 CommunicationProtocolType protocol;
68 if (strcmp(argv[1], "SSH") == 0)
70 else if (strcmp(argv[1], "RSH") == 0)
77 cout << "*******************************************************************************************" << endl;
78 cout << "This program tests the batch submission based on PBS emulation. Passwordless authentication" << endl;
79 cout << "must be used for this test to pass. For SSH, this can be configured with ssh-agent for" << endl;
80 cout << "instance. For RSH, this can be configured with the .rhosts file." << endl;
81 cout << "*******************************************************************************************" << endl;
83 // eventually remove any previous result
87 // Parse the test configuration file
89 parser.parseTestConfigFile();
90 const string & homedir = parser.getValue("TEST_EPBS_HOMEDIR");
91 const string & host = parser.getValue("TEST_EPBS_HOST");
92 const string & user = parser.getValue("TEST_EPBS_USER");
93 const string & queue = parser.getValue("TEST_EPBS_QUEUE");
94 int timeout = parser.getValueAsInt("TEST_EPBS_TIMEOUT");
98 // ... and its parameters ...
100 p[EXECUTABLE] = "./test-script.sh";
101 p[NAME] = string("Test_ePBS_") + argv[1];
102 p[WORKDIR] = homedir + "/tmp/Batch";
103 p[INFILE] = Couple("seta.sh", "tmp/Batch/seta.sh");
104 p[INFILE] += Couple("setb.sh", "tmp/Batch/setb.sh");
105 p[OUTFILE] = Couple("result.txt", "tmp/Batch/result.txt");
106 p[TMPDIR] = "tmp/Batch/";
110 p[MAXRAMSIZE] = 1000;
111 p[HOMEDIR] = homedir;
114 // ... and its environment
116 e["MYENVVAR"] = "MYVALUE";
117 job.setEnvironnement(e);
121 BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
123 // Create a BatchManager of type ePBS on localhost
124 FactBatchManager_eClient * fbm = (FactBatchManager_eClient *)(c("ePBS"));
125 BatchManager_eClient * bm = (*fbm)(host.c_str(), protocol, "lam");
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
134 bool testTimeout = (timeout > -1);
135 bool timeoutReached = (testTimeout && time >= timeout);
136 JobInfo jinfo = jobid.queryJob();
137 string state = jinfo.getParametre()[STATE].str();
138 cout << "State is \"" << state << "\"";
139 while (!timeoutReached && state != FINISHED && state != FAILED) {
140 cout << ", sleeping " << sleeptime << "s..." << endl;
143 timeoutReached = (testTimeout && time >= timeout);
145 if (testTimeout && sleeptime > timeout - time)
146 sleeptime = timeout - time;
147 if (sleeptime > MAX_SLEEP_TIME)
148 sleeptime = MAX_SLEEP_TIME;
149 jinfo = jobid.queryJob();
150 state = jinfo.getParametre()[STATE].str();
151 cout << "State is \"" << state << "\"";
155 if (state == FINISHED || state == FAILED) {
156 cout << "Job " << jobid.__repr__() << " is done" << endl;
157 bm->importOutputFiles(job, ".");
159 cerr << "Timeout while executing job" << endl;
163 } catch (GenericException e) {
164 cerr << "Error: " << e << endl;
166 } catch (ParserException e) {
167 cerr << "Parser error: " << e.what() << endl;
171 // test the result file
173 SimpleParser resultParser;
174 resultParser.parse("result.txt");
175 cout << "Result:" << endl << resultParser;
176 const string & envvar = resultParser.getValue("MYENVVAR");
177 int result = resultParser.getValueAsInt("c");
178 if (envvar == "MYVALUE" && result == 12) {
179 cout << "OK, Expected result found." << endl;
182 cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
185 } catch (ParserException e) {
186 cerr << "Parser error on result file: " << e.what() << endl;