Salome HOME
Merge class BatchManager_eClient into class BatchManager
[tools/libbatch.git] / src / Local / Test / Test_Local_RSH.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_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_Constants.hxx>
34 #include <Batch_Job.hxx>
35 #include <Batch_BatchManagerCatalog.hxx>
36 #include <Batch_FactBatchManager.hxx>
37 #include <Batch_BatchManager.hxx>
38
39 #include <SimpleParser.hxx>
40
41 using namespace std;
42 using namespace Batch;
43
44 int main(int argc, char** argv)
45 {
46   cout << "*******************************************************************************************" << endl;
47   cout << "This program tests the local batch submission based on RSH. Passwordless RSH authentication" << endl;
48   cout << "must be used for this test to pass (this can be configured with the .rhosts file)." << endl;
49   cout << "*******************************************************************************************" << endl;
50
51   // eventually remove any previous result
52   remove("result.txt");
53
54   try {
55     // Parse the test configuration file
56     SimpleParser parser;
57     parser.parseTestConfigFile();
58     const string & workdir = parser.getValue("TEST_LOCAL_RSH_WORK_DIR");
59     const string & exechost = parser.getValue("TEST_LOCAL_RSH_EXECUTION_HOST");
60     const string & user = parser.getValue("TEST_LOCAL_RSH_USER");
61     int timeout = parser.getValueAsInt("TEST_LOCAL_RSH_TIMEOUT");
62
63     // Define the job...
64     Job job;
65     // ... and its parameters ...
66     Parametre p;
67     p[EXECUTABLE]    = "test-script.sh";
68     p[NAME]          = "Test_Local_RSH";
69     p[WORKDIR]       = workdir;
70     p[INFILE]        = Couple("seta.sh", workdir + "/copied-seta.sh");
71     p[INFILE]       += Couple("setb.sh", workdir + "/copied-setb.sh");
72     p[OUTFILE]       = Couple("result.txt", workdir + "/orig-result.txt");
73     p[USER]          = user;
74     job.setParametre(p);
75     // ... and its environment
76     Environnement e;
77     job.setEnvironnement(e);
78     cout << job << endl;
79
80     // Get the catalog
81     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
82
83     // Create a BatchManager of type Local_RSH on localhost
84     FactBatchManager * fbm = c("LOCAL");
85     if (fbm == NULL) {
86       cerr << "Can't get RSH batch manager factory" << endl;
87       return 1;
88     }
89     BatchManager * bm = (*fbm)(exechost.c_str(), user.c_str(), RSH);
90
91     // Submit the job to the BatchManager
92     JobId jobid = bm->submitJob(job);
93     cout << jobid.__repr__() << endl;
94
95     // Wait for the end of the job
96     string state = bm->waitForJobEnd(jobid, timeout);
97
98     if (state == FINISHED) {
99       cout << "Job " << jobid.__repr__() << " is done" << endl;
100       bm->importOutputFiles(job, "resultdir/seconddirname");
101     } else if (state == FAILED) {
102       cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
103       bm->importOutputFiles(job, "resultdir/seconddirname");
104       return 1;
105     } else {
106       cerr << "Timeout while executing job" << endl;
107       return 1;
108     }
109
110   } catch (GenericException e) {
111     cerr << "Error: " << e << endl;
112     return 1;
113   } catch (ParserException e) {
114     cerr << "Parser error: " << e.what() << endl;
115     return 1;
116   }
117
118   // test the result file
119   string exp = "c = 12";
120   string res;
121   ifstream f("resultdir/seconddirname/result.txt");
122   getline(f, res);
123   f.close();
124
125   cout << "result found : " << res << ", expected : " << exp << endl;
126
127   if (res == exp)
128     return 0;
129   else
130     return 1;
131 }