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