Salome HOME
2bac0b3d36cc0915b0177c38abacaff390531eee
[tools/libbatch.git] / src / Core / Test / Test_BatchManager.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_BatchManager.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : Jan 2013
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_BatchManager.hxx>
39
40 #include "SimpleParser.hxx"
41
42 #include <Test_config.h>
43
44 using namespace std;
45 using namespace Batch;
46
47 void print_usage()
48 {
49   cout << "usage: Test_BatchManager BATCH_MANAGER PROTOCOL" << endl;
50   cout << "    BATCH_MANAGER \"CCC\", \"LL\", \"LOCAL\", \"LSF\", \"PBS\", " <<
51           "\"SGE\", \"SLURM\" or \"VISHNU\"" << endl;
52   cout << "    PROTOCOL      \"SH\", \"SSH\" or \"RSH\"" << endl;
53 }
54
55 int main(int argc, char** argv)
56 {
57   // Parse argument
58   if (argc != 3) {
59     print_usage();
60     return 1;
61   }
62   const char * bmType = argv[1];
63   CommunicationProtocolType protocol;
64   if (strcmp(argv[2], "SSH") == 0)
65     protocol = SSH;
66   else if (strcmp(argv[2], "RSH") == 0)
67     protocol = RSH;
68   else if (strcmp(argv[2], "SH") == 0)
69     protocol = SH;
70   else {
71     print_usage();
72     return 1;
73   }
74
75   cout << "*******************************************************************************************" << endl;
76   cout << "This program tests the batch submission of a job using the batch manager \"" << bmType << "\"" << endl;
77   cout << "and the communication protocol \"" << argv[2] << "\"." << endl;
78   if (protocol == RSH || protocol == SSH) {
79     cout << "Passwordless authentication must be used for this test to pass." << endl;
80     if (protocol == SSH) {
81       cout << "This can be configured with ssh-agent for instance." << endl;
82     } else if (protocol == RSH) {
83       cout << "This can be configured with the .rhosts file." << endl;
84     }
85   }
86   cout << "*******************************************************************************************" << endl;
87
88   // eventually remove any previous result
89   remove("resultdir/seconddirname/result.txt");
90
91   try {
92     // Get the catalog and the BatchManager factory
93     BatchManagerCatalog& cata = BatchManagerCatalog::getInstance();
94     FactBatchManager * fbm = cata(bmType);
95
96     // Parse the test configuration file
97     SimpleParser parser;
98     parser.parseTestConfigFile();
99     const string & workdir = parser.getTestValue(bmType, "WORKDIR");
100     const string & host = parser.getTestValue(bmType, "HOST");
101     const string & user = parser.getTestValue(bmType, "USER");
102     int timeout = parser.getTestValueAsInt(bmType, "TIMEOUT");
103
104     // Define the job...
105     Job job;
106     // ... and its parameters ...
107     Parametre p;
108     p[EXECUTABLE]    = string(CMAKE_CURRENT_SOURCE_DIR) + "/test-script.sh";
109     p[NAME]          = string("Test ") + bmType + " " + argv[2];
110     p[WORKDIR]       = workdir;
111     p[INFILE]        = Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/seta.sh", "seta.sh");
112     p[INFILE]       += Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/setb.sh", "setb.sh");
113     p[OUTFILE]       = Couple("result.txt", "result.txt");
114     p[NBPROC]        = 1;
115     p[MAXWALLTIME]   = 1;
116     p[MAXRAMSIZE]    = 50;
117     job.setParametre(p);
118     // ... and its environment
119     Environnement e;
120     e["MYENVVAR"] = "MYVALUE";
121     job.setEnvironnement(e);
122     cout << job << endl;
123
124     // Create the BatchManager
125     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
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     string state = bm->waitForJobEnd(jobid, timeout);
133
134     if (state == FINISHED) {
135       cout << "Job " << jobid.__repr__() << " is done" << endl;
136       bm->importOutputFiles(job, "resultdir/seconddirname");
137     } else if (state == FAILED) {
138       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
139       bm->importOutputFiles(job, "resultdir/seconddirname");
140       return 1;
141     } else {
142       cerr << "Timeout while executing job" << endl;
143       return 1;
144     }
145
146   } catch (const GenericException & e) {
147     cerr << "Error: " << e << endl;
148     return 1;
149   } catch (const ParserException & e) {
150     cerr << "Parser error: " << e.what() << endl;
151     return 1;
152   }
153
154   // test the result file
155   try {
156     SimpleParser resultParser;
157     resultParser.parse("resultdir/seconddirname/result.txt");
158     cout << "Result:" << endl << resultParser;
159     const string & envvar = resultParser.getValue("MYENVVAR");
160     int result = resultParser.getValueAsInt("c");
161     if (envvar == "MYVALUE" && result == 12) {
162       cout << "OK, Expected result found." << endl;
163       return 0;
164     } else {
165       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
166       return 1;
167     }
168   } catch (const ParserException & e) {
169     cerr << "Parser error on result file: " << e.what() << endl;
170     return 1;
171   }
172 }