Salome HOME
Added method waitForJobEnd in BatchManager class.
[tools/libbatch.git] / src / Local / Test / Test_Local_RSH.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_RSH.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
38 #include <SimpleParser.hxx>
39
40 using namespace std;
41 using namespace Batch;
42
43 int main(int argc, char** argv)
44 {
45   cout << "*******************************************************************************************" << endl;
46   cout << "This program tests the local batch submission based on RSH. Passwordless RSH authentication" << endl;
47   cout << "must be used for this test to pass (this can be configured with the .rhosts file)." << endl;
48   cout << "*******************************************************************************************" << endl;
49
50   // eventually remove any previous result
51   remove("result.txt");
52
53   try {
54     // Parse the test configuration file
55     SimpleParser parser;
56     parser.parseTestConfigFile();
57     const string & workdir = parser.getValue("TEST_LOCAL_RSH_WORK_DIR");
58     const string & exechost = parser.getValue("TEST_LOCAL_RSH_EXECUTION_HOST");
59     const string & user = parser.getValue("TEST_LOCAL_RSH_USER");
60     int timeout = parser.getValueAsInt("TEST_LOCAL_RSH_TIMEOUT");
61
62     // Define the job...
63     Job job;
64     // ... and its parameters ...
65     Parametre p;
66     p[EXECUTABLE]    = "source copied-test-script.sh";
67     p[NAME]          = "Test_Local_RSH";
68     p[WORKDIR]       = workdir;
69     p[INFILE]        = Couple("seta.sh", "copied-seta.sh");
70     p[INFILE]       += Couple("setb.sh", "copied-setb.sh");
71     p[INFILE]       += Couple("test-script.sh", "copied-test-script.sh");
72     p[OUTFILE]       = Couple("result.txt", "orig-result.txt");
73     p[EXECUTIONHOST] = exechost;
74     p[USER]          = user;
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_RSH on localhost
85     FactBatchManager * fbm = c("RSH");
86     if (fbm == NULL) {
87       cerr << "Can't get RSH batch manager factory" << endl;
88       return 1;
89     }
90     BatchManager * bm = (*fbm)("localhost");
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 && state != FAILED) {
100       cerr << "Error: Job not finished after timeout" << endl;
101       return 1;
102     }
103
104     cout << "Job " << jobid.__repr__() << " is done" << endl;
105
106   } catch (GenericException e) {
107     cerr << "Error: " << e << endl;
108     return 1;
109   } catch (ParserException e) {
110     cerr << "Parser error: " << e.what() << endl;
111     return 1;
112   }
113
114   // test the result file
115   string exp = "c = 12";
116   string res;
117   ifstream f("result.txt");
118   getline(f, res);
119   f.close();
120
121   cout << "result found : " << res << ", expected : " << exp << endl;
122
123   if (res == exp)
124     return 0;
125   else
126     return 1;
127 }