Salome HOME
8f897cc2fe57c28ff4cbfc1fb6fa6226596ed31a
[tools/libbatch.git] / src / LoadLeveler / Test / Test_eLL.cxx
1 //  Copyright (C) 2007-2012  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_Constants.hxx>
35 #include <Batch_Job.hxx>
36 #include <Batch_BatchManagerCatalog.hxx>
37 #include <Batch_FactBatchManager.hxx>
38 #include <Batch_FactBatchManager_eLL.hxx>
39 #include <Batch_BatchManager.hxx>
40
41 #include <SimpleParser.hxx>
42
43 using namespace std;
44 using namespace Batch;
45
46 void print_usage()
47 {
48   cout << "usage: Test_eLL PROTOCOL" << endl;
49   cout << "    PROTOCOL      \"SSH\" or \"RSH\"" << endl;
50 }
51
52 int main(int argc, char** argv)
53 {
54   // Parse argument
55   if (argc != 2) {
56     print_usage();
57     return 1;
58   }
59   CommunicationProtocolType protocol;
60   if (strcmp(argv[1], "SSH") == 0)
61     protocol = SSH;
62   else if (strcmp(argv[1], "RSH") == 0)
63     protocol = RSH;
64   else {
65     print_usage();
66     return 1;
67   }
68
69   cout << "*******************************************************************************************" << endl;
70   cout << "This program tests the batch submission based on LoadLeveler emulation. Passwordless" << endl;
71   cout << "authentication must be used for this test to pass. For SSH, this can be configured with" << endl;
72   cout << "ssh-agent for instance. For RSH, this can be configured with the .rhosts file." << endl;
73   cout << "*******************************************************************************************" << endl;
74
75   // eventually remove any previous result
76   remove("result.txt");
77
78   try {
79     // Parse the test configuration file
80     SimpleParser parser;
81     parser.parseTestConfigFile();
82     const string & homedir = parser.getValue("TEST_ELL_HOMEDIR");
83     const string & host = parser.getValue("TEST_ELL_HOST");
84     const string & user = parser.getValue("TEST_ELL_USER");
85     const string & queue = parser.getValue("TEST_ELL_QUEUE");
86     const string & jobType = parser.getValue("TEST_ELL_JOBTYPE");
87     int timeout = parser.getValueAsInt("TEST_ELL_TIMEOUT");
88
89     // Define the job...
90     Job job;
91     // ... and its parameters ...
92     Parametre p;
93     p[EXECUTABLE]    = "./test-script.sh";
94     p[NAME]          = string("Test_eLL_") + argv[1];
95     p[WORKDIR]       = homedir + "/tmp/Batch";
96     p[INFILE]        = Couple("seta.sh", "tmp/Batch/seta.sh");
97     p[INFILE]       += Couple("setb.sh", "tmp/Batch/setb.sh");
98     p[OUTFILE]       = Couple("result.txt", "tmp/Batch/result.txt");
99     p[NBPROC]        = 1;
100     p[MAXWALLTIME]   = 1;
101     p[MAXRAMSIZE]    = 50;
102     p[HOMEDIR]       = homedir;
103     p[QUEUE]         = queue;
104     p[LL_JOBTYPE]    = jobType;
105     p[EXCLUSIVE]     = false;
106     job.setParametre(p);
107     // ... and its environment
108     Environnement e;
109     e["MYENVVAR"] = "MYVALUE";
110     job.setEnvironnement(e);
111     cout << job << endl;
112
113     // Get the catalog
114     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
115
116     // Create a BatchManager of type ePBS on localhost
117     FactBatchManager * fbm = c("LL");
118     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
119
120     // Submit the job to the BatchManager
121     JobId jobid = bm->submitJob(job);
122     cout << jobid.__repr__() << endl;
123
124     // Wait for the end of the job
125     string state = bm->waitForJobEnd(jobid, timeout);
126
127     if (state == FINISHED) {
128       cout << "Job " << jobid.__repr__() << " is done" << endl;
129       bm->importOutputFiles(job, "resultdir/seconddirname");
130     } else if (state == FAILED) {
131       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
132       bm->importOutputFiles(job, "resultdir/seconddirname");
133       return 1;
134     } else {
135       cerr << "Timeout while executing job" << endl;
136       return 1;
137     }
138
139   } catch (GenericException e) {
140     cerr << "Error: " << e << endl;
141     return 1;
142   } catch (ParserException e) {
143     cerr << "Parser error: " << e.what() << endl;
144     return 1;
145   }
146
147   // test the result file
148   try {
149     SimpleParser resultParser;
150     resultParser.parse("resultdir/seconddirname/result.txt");
151     cout << "Result:" << endl << resultParser;
152     const string & envvar = resultParser.getValue("MYENVVAR");
153     int result = resultParser.getValueAsInt("c");
154     if (envvar == "MYVALUE" && result == 12) {
155       cout << "OK, Expected result found." << endl;
156       return 0;
157     } else {
158       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
159       return 1;
160     }
161   } catch (ParserException e) {
162     cerr << "Parser error on result file: " << e.what() << endl;
163     return 1;
164   }
165 }