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