Salome HOME
Removed old constants defined in global namespace. Warning: this change will break...
[tools/libbatch.git] / src / PBS / Test / Test_ePBS.cxx
1 //  Copyright (C) 2007-2008  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_ePBS.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : April 2009
27  *
28  */
29
30 #include <iostream>
31 #include <fstream>
32 #include <cstring>
33
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>
40
41 #include <SimpleParser.hxx>
42
43 #ifdef WIN32
44 #include <Windows.h>
45 #define sleep(seconds) Sleep((seconds)*1000)
46 #define usleep(useconds) Sleep((useconds)/1000)
47 #endif
48
49 using namespace std;
50 using namespace Batch;
51
52 const int MAX_SLEEP_TIME = 600;
53
54 void print_usage()
55 {
56   cout << "usage: Test_ePBS PROTOCOL" << endl;
57   cout << "    PROTOCOL      \"SSH\" or \"RSH\"" << endl;
58 }
59
60 int main(int argc, char** argv)
61 {
62   // Parse argument
63   if (argc != 2) {
64     print_usage();
65     return 1;
66   }
67   CommunicationProtocolType protocol;
68   if (strcmp(argv[1], "SSH") == 0)
69     protocol = SSH;
70   else if (strcmp(argv[1], "RSH") == 0)
71     protocol = RSH;
72   else {
73     print_usage();
74     return 1;
75   }
76
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;
82
83   // eventually remove any previous result
84   remove("result.txt");
85
86   try {
87     // Parse the test configuration file
88     SimpleParser parser;
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");
95
96     // Define the job...
97     Job job;
98     // ... and its parameters ...
99     Parametre p;
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/";
107     p[USER]          = user;
108     p[NBPROC]        = 1;
109     p[MAXWALLTIME]   = 1;
110     p[MAXRAMSIZE]    = 1000;
111     p[HOMEDIR]       = homedir;
112     p[QUEUE]         = queue;
113     job.setParametre(p);
114     // ... and its environment
115     Environnement e;
116     e["MYENVVAR"] = "MYVALUE";
117     job.setEnvironnement(e);
118     cout << job << endl;
119
120     // Get the catalog
121     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
122
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");
126
127     // Submit the job to the BatchManager
128     JobId jobid = bm->submitJob(job);
129     cout << jobid.__repr__() << endl;
130
131     // Wait for the end of the job
132     int time = 0;
133     int sleeptime = 1;
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;
141       sleep(sleeptime);
142       time += sleeptime;
143       timeoutReached = (testTimeout && time >= timeout);
144       sleeptime *= 2;
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 << "\"";
152     }
153     cout << endl;
154
155     if (state == FINISHED || state == FAILED) {
156       cout << "Job " << jobid.__repr__() << " is done" << endl;
157       bm->importOutputFiles(job, ".");
158     } else {
159       cerr << "Timeout while executing job" << endl;
160       return 1;
161     }
162
163   } catch (GenericException e) {
164     cerr << "Error: " << e << endl;
165     return 1;
166   } catch (ParserException e) {
167     cerr << "Parser error: " << e.what() << endl;
168     return 1;
169   }
170
171   // test the result file
172   try {
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;
180       return 0;
181     } else {
182       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
183       return 1;
184     }
185   } catch (ParserException e) {
186     cerr << "Parser error on result file: " << e.what() << endl;
187     return 1;
188   }
189 }