Salome HOME
82e85f9d6ba35afedb238624dd3df284e9fb5a1e
[tools/libbatch.git] / src / Local / Test / Test_Local_SSH.cxx
1 //  Copyright (C) 2007-2010  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 #include <cstdlib>
33
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 SSH. Passwordless SSH authentication" << endl;
48   cout << "must be used for this test to pass (this can be configured with ssh-agent for instance)." << 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_SSH_WORK_DIR");
59     const string & exechost = parser.getValue("TEST_LOCAL_SSH_EXECUTION_HOST");
60     const string & user = parser.getValue("TEST_LOCAL_SSH_USER");
61     int timeout = parser.getValueAsInt("TEST_LOCAL_SSH_TIMEOUT");
62
63     // Define the job...
64     Job job;
65     // ... and its parameters ...
66     Parametre p;
67     p[EXECUTABLE]    = "source copied-test-script.sh";
68     p[NAME]          = "Test_Local_SSH";
69     p[WORKDIR]       = workdir;
70     p[INFILE]        = Couple("seta.sh", "copied-seta.sh");
71     p[INFILE]       += Couple("setb.sh", "copied-setb.sh");
72     p[INFILE]       += Couple("test-script.sh", "copied-test-script.sh");
73     p[OUTFILE]       = Couple("result.txt", "orig-result.txt");
74     p[EXECUTIONHOST] = exechost;
75     p[USER]          = user;
76     job.setParametre(p);
77     // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
78     Environnement e;
79     const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
80     if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
81     job.setEnvironnement(e);
82     cout << job << endl;
83
84     // Get the catalog
85     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
86
87     // Create a BatchManager of type Local_SSH on localhost
88     FactBatchManager * fbm = c("SSH");
89     if (fbm == NULL) {
90       cerr << "Can't get SSH batch manager factory" << endl;
91       return 1;
92     }
93     BatchManager * bm = (*fbm)("localhost");
94
95     // Submit the job to the BatchManager
96     JobId jobid = bm->submitJob(job);
97     cout << jobid.__repr__() << endl;
98
99     // Wait for the end of the job
100     string state = bm->waitForJobEnd(jobid, timeout);
101
102     if (state != FINISHED && state != FAILED) {
103       cerr << "Error: Job not finished after timeout" << endl;
104       return 1;
105     }
106
107     cout << "Job " << jobid.__repr__() << " is done" << endl;
108
109   } catch (GenericException e) {
110     cerr << "Error: " << e << endl;
111     return 1;
112   } catch (ParserException e) {
113     cerr << "Parser error: " << e.what() << endl;
114     return 1;
115   }
116
117   // test the result file
118   string exp = "c = 12";
119   string res;
120   ifstream f("result.txt");
121   getline(f, res);
122   f.close();
123
124   cout << "result found : " << res << ", expected : " << exp << endl;
125
126   if (res == exp)
127     return 0;
128   else
129     return 1;
130 }