Salome HOME
8e714bce7b5eee4fd94ba5edfc9311dc4a2431f9
[tools/libbatch.git] / src / Local / Test / Test_Local_SH.cxx
1 //  Copyright (C) 2007-2008  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 <Batch_Job.hxx>
34 #include <Batch_BatchManagerCatalog.hxx>
35 #include <Batch_FactBatchManager.hxx>
36 #include <Batch_BatchManager.hxx>
37 #include <Test_Local_config.h>
38
39 using namespace std;
40 using namespace Batch;
41
42 int main(int argc, char** argv)
43 {
44   cout << "*******************************************************************************************" << endl;
45   cout << "This program tests the local batch submission based on SH. No specific configuration is" << endl;
46   cout << "needed for this test." << endl;
47   cout << "*******************************************************************************************" << endl;
48
49   // eventually remove any previous result
50   remove("result.txt");
51
52   try {
53     // Define the job...
54     Job job;
55     // ... and its parameters ...
56     Parametre p;
57     p["EXECUTABLE"] = string("./copied-") + EXEC_TEST_NAME;
58     p["ARGUMENTS"]  = "copied-seta.sh";
59     p["ARGUMENTS"] += "copied-setb.sh";
60     p["ARGUMENTS"] += "orig-result.txt";
61     p["NAME"]       = "Test_Local_SH";
62     p["WORKDIR"]    = TEST_LOCAL_SH_WORK_DIR;
63     p["INFILE"]     = Couple("seta.sh", "copied-seta.sh");
64     p["INFILE"]    += Couple("setb.sh", "copied-setb.sh");
65     p["INFILE"]    += Couple(EXEC_TEST_NAME, string("copied-") + EXEC_TEST_NAME);
66     p["OUTFILE"]    = Couple("result.txt", "orig-result.txt");
67     job.setParametre(p);
68     // ... and its environment
69     Environnement e;
70     job.setEnvironnement(e);
71     cout << job << endl;
72
73     // Get the catalog
74     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
75
76     // Create a BatchManager of type Local_SH on localhost
77     FactBatchManager * fbm = c("SH");
78     BatchManager * bm = (*fbm)("localhost");
79
80     // Submit the job to the BatchManager
81     JobId jobid = bm->submitJob(job);
82     cout << jobid.__repr__() << endl;
83
84     // Wait for the end of the job
85     string state = "Unknown";
86     for (int i=0 ; i<20 && state != "Done" ; i++) {
87       usleep(100000);
88       Versatile paramState = jobid.queryJob().getParametre()["STATE"];
89       state = (paramState.size() > 0) ? paramState.str() : "Unknown";
90       cout << "Job state is: " << state << endl;
91     }
92
93     if (state != "Done") {
94       cerr << "Error: Job not finished after timeout" << endl;
95       return 1;
96     }
97
98     cout << "Job " << jobid.__repr__() << " is done" << endl;
99
100   } catch (GenericException e) {
101     cerr << "Error: " << e << endl;
102     return 1;
103   }
104
105   // wait for 2 more seconds for the copy of output files and the cleanup
106   // (there's no cleaner way to do that yet)
107   sleep(2);
108
109   // test the result file
110   string exp = "c = 12";
111   string res;
112   ifstream f("result.txt");
113   getline(f, res);
114   f.close();
115
116   cout << "result found : " << res << ", expected : " << exp << endl;
117
118   if (res == exp)
119     return 0;
120   else
121     return 1;
122 }