Salome HOME
Upgrade version to 2.5.0
[tools/libbatch.git] / src / Core / Test / Test_BatchManager.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 <Constants.hxx>
35 #include <Job.hxx>
36 #include <BatchManagerCatalog.hxx>
37 #include <FactBatchManager.hxx>
38 #include <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   const char * protocolStr = argv[2];
64   CommunicationProtocolType protocol;
65   if (strcmp(protocolStr, "SSH") == 0)
66     protocol = SSH;
67   else if (strcmp(protocolStr, "RSH") == 0)
68     protocol = RSH;
69   else if (strcmp(protocolStr, "SH") == 0)
70     protocol = SH;
71   else {
72     print_usage();
73     return 1;
74   }
75
76   cout << "*******************************************************************************************" << endl;
77   cout << "This program tests the batch submission of a job using the batch manager \"" << bmType << "\"" << endl;
78   cout << "and the communication protocol \"" << protocolStr << "\"." << endl;
79   if (protocol == RSH || protocol == SSH) {
80     cout << "Passwordless authentication must be used for this test to pass." << endl;
81     if (protocol == SSH) {
82       cout << "This can be configured with ssh-agent for instance." << endl;
83     } else if (protocol == RSH) {
84       cout << "This can be configured with the .rhosts file." << endl;
85     }
86   }
87   cout << "*******************************************************************************************" << endl;
88
89   // eventually remove any previous result
90   remove("resultdir/seconddirname/result.txt");
91
92   try {
93     // Get the catalog and the BatchManager factory
94     BatchManagerCatalog& cata = BatchManagerCatalog::getInstance();
95     FactBatchManager * fbm = cata(bmType);
96
97     // Parse the test configuration file
98     SimpleParser parser;
99     parser.parseTestConfigFile();
100     const string & workdir = parser.getTestValue(bmType, protocolStr, "WORKDIR");
101     const string & host = parser.getTestValue(bmType, protocolStr, "HOST");
102     const string & user = parser.getTestValue(bmType, protocolStr, "USER");
103     int timeout = parser.getTestValueAsInt(bmType, protocolStr, "TIMEOUT");
104
105 #ifdef WIN32
106     const char separator = '\\';
107 #else
108     const char separator = '/';
109 #endif
110
111     // Define the job...
112     Job job;
113     // ... and its parameters ...
114     Parametre p;
115     p[EXECUTABLE]    = string(CMAKE_CURRENT_SOURCE_DIR) + separator + "test_script.py";
116     p[ARGUMENTS]     = "copied_seta.py";
117     p[ARGUMENTS]    += "copied_setb.py";
118     p[ARGUMENTS]    += "orig_result.txt";
119     p[NAME]          = string("Test ") + bmType + " " + argv[2];
120     p[WORKDIR]       = workdir;
121     p[INFILE]        = Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/seta.py", "copied_seta.py");
122     p[INFILE]       += Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/setb.py", "copied_setb.py");
123     p[OUTFILE]       = Couple("result.txt", "orig_result.txt");
124     p[NBPROC]        = 1;
125     p[MAXWALLTIME]   = 1;
126     p[MAXRAMSIZE]    = 50;
127     job.setParametre(p);
128     // ... and its environment
129     Environnement e;
130     e["MYENVVAR"] = "MYVALUE";
131     job.setEnvironnement(e);
132     cout << job << endl;
133
134     // Create the BatchManager
135     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
136
137     // Submit the job to the BatchManager
138     JobId jobid = bm->submitJob(job);
139     cout << jobid.__repr__() << endl;
140
141     // Wait for the end of the job
142     string state = bm->waitForJobEnd(jobid, timeout);
143
144     if (state == FINISHED) {
145       cout << "Job " << jobid.__repr__() << " is done" << endl;
146       bm->importOutputFiles(job, "resultdir/seconddirname");
147     } else if (state == FAILED) {
148       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
149       bm->importOutputFiles(job, "resultdir/seconddirname");
150       return 1;
151     } else {
152       cerr << "Timeout while executing job" << endl;
153       return 1;
154     }
155
156   } catch (const GenericException & e) {
157     cerr << "Error: " << e << endl;
158     return 1;
159   } catch (const ParserException & e) {
160     cerr << "Parser error: " << e.what() << endl;
161     return 1;
162   }
163
164   // test the result file
165   try {
166     SimpleParser resultParser;
167     resultParser.parse("resultdir/seconddirname/result.txt");
168     cout << "Result:" << endl << resultParser;
169     const string & envvar = resultParser.getValue("MYENVVAR");
170     int result = resultParser.getValueAsInt("c");
171     if (envvar == "MYVALUE" && result == 12) {
172       cout << "OK, Expected result found." << endl;
173       return 0;
174     } else {
175       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
176       return 1;
177     }
178   } catch (const ParserException & e) {
179     cerr << "Parser error on result file: " << e.what() << endl;
180     return 1;
181   }
182 }