Salome HOME
Refactored submission based on SH, RSH and SSH by grouping related commands in Commun...
[tools/libbatch.git] / src / Local / Test / Test_Local_SSH.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_SSH.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 #ifdef WIN32
41 #include <Windows.h>
42 #define sleep(seconds) Sleep((seconds)*1000)
43 #define usleep(useconds) Sleep((useconds)/1000)
44 #endif
45
46 using namespace std;
47 using namespace Batch;
48
49 int main(int argc, char** argv)
50 {
51   cout << "*******************************************************************************************" << endl;
52   cout << "This program tests the local batch submission based on SSH. Passwordless SSH authentication" << endl;
53   cout << "must be used for this test to pass (this can be configured with ssh-agent for instance)." << endl;
54   cout << "*******************************************************************************************" << endl;
55
56   // eventually remove any previous result
57   remove("result.txt");
58
59   try {
60     // Parse the test configuration file
61     SimpleParser parser;
62     parser.parseTestConfigFile();
63     const string & workdir = parser.getValue("TEST_LOCAL_SSH_WORK_DIR");
64     const string & exechost = parser.getValue("TEST_LOCAL_SSH_EXECUTION_HOST");
65     const string & user = parser.getValue("TEST_LOCAL_SSH_USER");
66     int timeout = parser.getValueAsInt("TEST_LOCAL_SSH_TIMEOUT");
67     int finalizationTime = parser.getValueAsInt("TEST_LOCAL_SSH_FINALIZATION_TIME");
68
69     // Define the job...
70     Job job;
71     // ... and its parameters ...
72     Parametre p;
73     p["EXECUTABLE"]    = "source copied-test-script.sh";
74     p["NAME"]          = "Test_Local_SSH";
75     p["WORKDIR"]       = workdir;
76     p["INFILE"]        = Couple("seta.sh", "copied-seta.sh");
77     p["INFILE"]       += Couple("setb.sh", "copied-setb.sh");
78     p["INFILE"]       += Couple("test-script.sh", "copied-test-script.sh");
79     p["OUTFILE"]       = Couple("result.txt", "orig-result.txt");
80     p["EXECUTIONHOST"] = exechost;
81     p["USER"]          = user;
82     job.setParametre(p);
83     // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
84     Environnement e;
85     const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
86     if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
87     job.setEnvironnement(e);
88     cout << job << endl;
89
90     // Get the catalog
91     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
92
93     // Create a BatchManager of type Local_SSH on localhost
94     FactBatchManager * fbm = c("SSH");
95     if (fbm == NULL) {
96       cerr << "Can't get SSH batch manager factory" << endl;
97       return 1;
98     }
99     BatchManager * bm = (*fbm)("localhost");
100
101     // Submit the job to the BatchManager
102     JobId jobid = bm->submitJob(job);
103     cout << jobid.__repr__() << endl;
104
105     // Wait for the end of the job
106     string state = "Unknown";
107     for (int i=0 ; i<timeout*10 && state != "Done" ; i++) {
108       usleep(100000);
109       Versatile paramState = jobid.queryJob().getParametre()["STATE"];
110       state = (paramState.size() > 0) ? paramState.str() : "Unknown";
111       cout << "Job state is: " << state << endl;
112     }
113
114     if (state != "Done") {
115       cerr << "Error: Job not finished after timeout" << endl;
116       return 1;
117     }
118
119     cout << "Job " << jobid.__repr__() << " is done" << endl;
120
121     // wait for the copy of output files and the cleanup
122     // (there's no cleaner way to do that yet)
123     sleep(finalizationTime);
124
125   } catch (GenericException e) {
126     cerr << "Error: " << e << endl;
127     return 1;
128   } catch (ParserException e) {
129     cerr << "Parser error: " << e.what() << endl;
130     return 1;
131   }
132
133   // test the result file
134   string exp = "c = 12";
135   string res;
136   ifstream f("result.txt");
137   getline(f, res);
138   f.close();
139
140   cout << "result found : " << res << ", expected : " << exp << endl;
141
142   if (res == exp)
143     return 0;
144   else
145     return 1;
146 }