Salome HOME
Added Windows implementation for local submission based on sh and rsh (partial and...
[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 #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 RSH. Passwordless RSH authentication" << endl;
46   cout << "must be used for this test to pass (this can be configured with the .rhosts file)." << 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"]    = "source copied-test-script.sh";
58     p["NAME"]          = "Test_Local_RSH";
59     p["WORKDIR"]       = TEST_LOCAL_RSH_WORK_DIR;
60     p["INFILE"]        = Couple("seta.sh", "copied-seta.sh");
61     p["INFILE"]       += Couple("setb.sh", "copied-setb.sh");
62     p["INFILE"]       += Couple("test-script.sh", "copied-test-script.sh");
63     p["OUTFILE"]       = Couple("result.txt", "orig-result.txt");
64     p["EXECUTIONHOST"] = TEST_LOCAL_RSH_EXECUTION_HOST;
65     p["USER"]          = TEST_LOCAL_RSH_USER;
66     job.setParametre(p);
67     // ... and its environment
68     Environnement e;
69     job.setEnvironnement(e);
70     cout << job << endl;
71
72     // Get the catalog
73     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
74
75     // Create a BatchManager of type Local_RSH on localhost
76     FactBatchManager * fbm = c("RSH");
77     BatchManager * bm = (*fbm)("localhost");
78
79     // Submit the job to the BatchManager
80     JobId jobid = bm->submitJob(job);
81     cout << jobid.__repr__() << endl;
82
83     // Wait for the end of the job
84     string state = "Unknown";
85     for (int i=0 ; i<100 && state != "Done" ; i++) {
86       usleep(100000);
87       Versatile paramState = jobid.queryJob().getParametre()["STATE"];
88       state = (paramState.size() > 0) ? paramState.str() : "Unknown";
89       cout << "Job state is: " << state << endl;
90     }
91
92     if (state != "Done") {
93       cerr << "Error: Job not finished after timeout" << endl;
94       return 1;
95     }
96
97     cout << "Job " << jobid.__repr__() << " is done" << endl;
98
99   } catch (GenericException e) {
100     cerr << "Error: " << e << endl;
101     return 1;
102   }
103
104   // wait for 5 more seconds for the copy of output files and the cleanup
105   // (there's no cleaner way to do that yet)
106   sleep(5);
107
108   // test the result file
109   string exp = "c = 12";
110   string res;
111   ifstream f("result.txt");
112   getline(f, res);
113   f.close();
114
115   cout << "result found : " << res << ", expected : " << exp << endl;
116
117   if (res == exp)
118     return 0;
119   else
120     return 1;
121 }