Salome HOME
Rename files:
[tools/libbatch.git] / src / Local / Test / Test_Local_SH.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_Local_SH.cxx :
24  *
25  * Author : Renaud BARATE - EDF R&D
26  * Date   : April 2009
27  *
28  */
29
30 #include <iostream>
31 #include <fstream>
32
33 #include <Constants.hxx>
34 #include <Job.hxx>
35 #include <BatchManagerCatalog.hxx>
36 #include <FactBatchManager.hxx>
37 #include <BatchManager.hxx>
38
39 #include <Test_Local_config.h>
40 #include <SimpleParser.hxx>
41
42 using namespace std;
43 using namespace Batch;
44
45 int main(int argc, char** argv)
46 {
47   cout << "*******************************************************************************************" << endl;
48   cout << "This program tests the local batch submission based on SH. No specific configuration is" << endl;
49   cout << "needed for this test." << endl;
50   cout << "*******************************************************************************************" << endl;
51
52   // eventually remove any previous result
53   remove("result.txt");
54
55   try {
56     // Parse the test configuration file
57     SimpleParser parser;
58     parser.parseTestConfigFile();
59     const string & workdir = parser.getValue("TEST_LOCAL_SH_WORK_DIR");
60     int timeout = parser.getValueAsInt("TEST_LOCAL_SH_TIMEOUT");
61
62     // Define the job...
63     Job job;
64     // ... and its parameters ...
65     Parametre p;
66     p[EXECUTABLE] = EXEC_TEST_NAME;
67     p[ARGUMENTS]  = "copied-seta.sh";
68     p[ARGUMENTS] += "copied-setb.sh";
69     p[ARGUMENTS] += "orig-result.txt";
70     p[NAME]       = "Test_Local_SH";
71     p[WORKDIR]    = workdir;
72     p[INFILE]     = Couple("seta.sh", workdir + "/copied-seta.sh");
73     p[INFILE]    += Couple("setb.sh", workdir + "/copied-setb.sh");
74     p[OUTFILE]    = Couple("result.txt", workdir + "/orig-result.txt");
75     job.setParametre(p);
76     // ... and its environment
77     Environnement e;
78     job.setEnvironnement(e);
79     cout << job << endl;
80
81     // Get the catalog
82     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
83
84     // Create a BatchManager of type Local_SH on localhost
85     FactBatchManager * fbm = c("LOCAL");
86     if (fbm == NULL) {
87       cerr << "Can't get SH batch manager factory" << endl;
88       return 1;
89     }
90     BatchManager * bm = (*fbm)("localhost", "", SH);
91
92     // Submit the job to the BatchManager
93     JobId jobid = bm->submitJob(job);
94     cout << jobid.__repr__() << endl;
95
96     // Wait for the end of the job
97     string state = bm->waitForJobEnd(jobid, timeout);
98
99     if (state == FINISHED) {
100       cout << "Job " << jobid.__repr__() << " is done" << endl;
101       bm->importOutputFiles(job, "resultdir/seconddirname");
102     } else if (state == FAILED) {
103       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
104       bm->importOutputFiles(job, "resultdir/seconddirname");
105       return 1;
106     } else {
107       cerr << "Timeout while executing job" << endl;
108       return 1;
109     }
110
111   } catch (GenericException e) {
112     cerr << "Error: " << e << endl;
113     return 1;
114   } catch (ParserException e) {
115     cerr << "Parser error: " << e.what() << endl;
116     return 1;
117   }
118
119   // test the result file
120   string exp = "c = 12";
121   string res;
122   ifstream f("resultdir/seconddirname/result.txt");
123   getline(f, res);
124   f.close();
125
126   cout << "result found : " << res << ", expected : " << exp << endl;
127
128   if (res == exp)
129     return 0;
130   else
131     return 1;
132 }