Salome HOME
Merged from BR_AR
[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
33 #include <Batch_Job.hxx>
34 #include <Batch_BatchManagerCatalog.hxx>
35 #include <Batch_FactBatchManager.hxx>
36 #include <Batch_FactBatchManager_eClient.hxx>
37 #include <Batch_BatchManager.hxx>
38 #include <Batch_BatchManager_eClient.hxx>
39
40 #include <SimpleParser.hxx>
41
42 #ifdef WIN32
43 #include <Windows.h>
44 #define sleep(seconds) Sleep((seconds)*1000)
45 #define usleep(useconds) Sleep((useconds)/1000)
46 #endif
47
48 using namespace std;
49 using namespace Batch;
50
51 const int MAX_SLEEP_TIME = 600;
52
53 void print_usage()
54 {
55   cout << "usage: Test_ePBS PROTOCOL" << endl;
56   cout << "    PROTOCOL      \"SSH\" or \"RSH\"" << endl;
57 }
58
59 int main(int argc, char** argv)
60 {
61   // Parse argument
62   if (argc != 2) {
63     print_usage();
64     return 1;
65   }
66   CommunicationProtocolType protocol;
67   if (strcmp(argv[1], "SSH") == 0)
68     protocol = SSH;
69   else if (strcmp(argv[1], "RSH") == 0)
70     protocol = RSH;
71   else {
72     print_usage();
73     return 1;
74   }
75
76   cout << "*******************************************************************************************" << endl;
77   cout << "This program tests the batch submission based on PBS emulation. Passwordless authentication" << endl;
78   cout << "must be used for this test to pass. For SSH, this can be configured with ssh-agent for" << endl;
79   cout << "instance. For RSH, this can be configured with the .rhosts file." << endl;
80   cout << "*******************************************************************************************" << endl;
81
82   // eventually remove any previous result
83   remove("result.txt");
84
85   try {
86     // Parse the test configuration file
87     SimpleParser parser;
88     parser.parseTestConfigFile();
89     const string & homedir = parser.getValue("TEST_EPBS_HOMEDIR");
90     const string & host = parser.getValue("TEST_EPBS_HOST");
91     const string & user = parser.getValue("TEST_EPBS_USER");
92     const string & queue = parser.getValue("TEST_EPBS_QUEUE");
93     int timeout = parser.getValueAsInt("TEST_EPBS_TIMEOUT");
94
95     // Define the job...
96     Job job;
97     // ... and its parameters ...
98     Parametre p;
99     p["EXECUTABLE"]    = "./test-script.sh";
100     p["NAME"]          = string("Test_ePBS_") + argv[1];
101     p["WORKDIR"]       = homedir + "/tmp/Batch";
102     p["INFILE"]        = Couple("seta.sh", "tmp/Batch/seta.sh");
103     p["INFILE"]       += Couple("setb.sh", "tmp/Batch/setb.sh");
104     p["OUTFILE"]       = Couple("result.txt", "tmp/Batch/result.txt");
105     p["TMPDIR"]        = "tmp/Batch/";
106     p["USER"]          = user;
107     p["NBPROC"]        = 1;
108     p["MAXWALLTIME"]   = 1;
109     p["MAXRAMSIZE"]    = 1000;
110     p["HOMEDIR"]       = homedir;
111     p["QUEUE"]         = queue;
112     job.setParametre(p);
113     // ... and its environment
114     Environnement e;
115     e["MYENVVAR"] = "MYVALUE";
116     job.setEnvironnement(e);
117     cout << job << endl;
118
119     // Get the catalog
120     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
121
122     // Create a BatchManager of type ePBS on localhost
123     FactBatchManager_eClient * fbm = (FactBatchManager_eClient *)(c("ePBS"));
124     BatchManager_eClient * bm = (*fbm)(host.c_str(), protocol, "lam");
125
126     // Submit the job to the BatchManager
127     JobId jobid = bm->submitJob(job);
128     cout << jobid.__repr__() << endl;
129
130     // Wait for the end of the job
131     int time = 0;
132     int sleeptime = 1;
133     bool testTimeout = (timeout > -1);
134     bool timeoutReached = (testTimeout && time >= timeout);
135     JobInfo jinfo = jobid.queryJob();
136     string state = jinfo.getParametre()["STATE"].str();
137     cout << "State is \"" << state << "\"";
138     while (!timeoutReached && state != "U" && state != "C") {
139       cout << ", sleeping " << sleeptime << "s..." << endl;
140       sleep(sleeptime);
141       time += sleeptime;
142       timeoutReached = (testTimeout && time >= timeout);
143       sleeptime *= 2;
144       if (testTimeout && sleeptime > timeout - time)
145         sleeptime = timeout - time;
146       if (sleeptime > MAX_SLEEP_TIME)
147         sleeptime = MAX_SLEEP_TIME;
148       jinfo = jobid.queryJob();
149       state = jinfo.getParametre()["STATE"].str();
150       cout << "State is \"" << state << "\"";
151     }
152     cout << endl;
153
154     if (state == "U" || state == "C") {
155       cout << "Job " << jobid.__repr__() << " is done" << endl;
156       bm->importOutputFiles(job, ".");
157     } else {
158       cerr << "Timeout while executing job" << endl;
159       return 1;
160     }
161
162   } catch (GenericException e) {
163     cerr << "Error: " << e << endl;
164     return 1;
165   } catch (ParserException e) {
166     cerr << "Parser error: " << e.what() << endl;
167     return 1;
168   }
169
170   // test the result file
171   try {
172     SimpleParser resultParser;
173     resultParser.parse("result.txt");
174     cout << "Result:" << endl << resultParser;
175     const string & envvar = resultParser.getValue("MYENVVAR");
176     int result = resultParser.getValueAsInt("c");
177     if (envvar == "MYVALUE" && result == 12) {
178       cout << "OK, Expected result found." << endl;
179       return 0;
180     } else {
181       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
182       return 1;
183     }
184   } catch (ParserException e) {
185     cerr << "Parser error on result file: " << e.what() << endl;
186     return 1;
187   }
188 }