Salome HOME
Copyright update 2021
[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     // Define the job...
106     Job job;
107     // ... and its parameters ...
108     Parametre p;
109     p[EXECUTABLE]    = string(CMAKE_CURRENT_SOURCE_DIR) + "/test_script.py";
110     p[ARGUMENTS]     = "copied_seta.py";
111     p[ARGUMENTS]    += "copied_setb.py";
112     p[ARGUMENTS]    += "orig_result.txt";
113     p[NAME]          = string("Test ") + bmType + " " + argv[2];
114     p[WORKDIR]       = workdir;
115     p[INFILE]        = Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/seta.py", "copied_seta.py");
116     p[INFILE]       += Couple(string(CMAKE_CURRENT_SOURCE_DIR) + "/setb.py", "copied_setb.py");
117     p[OUTFILE]       = Couple("result.txt", "orig_result.txt");
118     p[NBPROC]        = 1;
119     p[MAXWALLTIME]   = 1;
120     p[MAXRAMSIZE]    = 50;
121     job.setParametre(p);
122     // ... and its environment
123     Environnement e;
124     e["MYENVVAR"] = "MYVALUE";
125     job.setEnvironnement(e);
126     cout << job << endl;
127
128     // Create the BatchManager
129     BatchManager * bm = (*fbm)(host.c_str(), user.c_str(), protocol);
130
131     // Submit the job to the BatchManager
132     JobId jobid = bm->submitJob(job);
133     cout << jobid.__repr__() << endl;
134
135     // Wait for the end of the job
136     string state = bm->waitForJobEnd(jobid, timeout);
137
138     if (state == FINISHED) {
139       cout << "Job " << jobid.__repr__() << " is done" << endl;
140       bm->importOutputFiles(job, "resultdir/seconddirname");
141     } else if (state == FAILED) {
142       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
143       bm->importOutputFiles(job, "resultdir/seconddirname");
144       return 1;
145     } else {
146       cerr << "Timeout while executing job" << endl;
147       return 1;
148     }
149
150   } catch (const GenericException & e) {
151     cerr << "Error: " << e << endl;
152     return 1;
153   } catch (const ParserException & e) {
154     cerr << "Parser error: " << e.what() << endl;
155     return 1;
156   }
157
158   // test the result file
159   try {
160     SimpleParser resultParser;
161     resultParser.parse("resultdir/seconddirname/result.txt");
162     cout << "Result:" << endl << resultParser;
163     const string & envvar = resultParser.getValue("MYENVVAR");
164     int result = resultParser.getValueAsInt("c");
165     if (envvar == "MYVALUE" && result == 12) {
166       cout << "OK, Expected result found." << endl;
167       return 0;
168     } else {
169       cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
170       return 1;
171     }
172   } catch (const ParserException & e) {
173     cerr << "Parser error on result file: " << e.what() << endl;
174     return 1;
175   }
176 }