Salome HOME
Changed year in copyrights
[tools/libbatch.git] / src / Local / Test / Test_Local_SSH.cxx
1 //  Copyright (C) 2007-2011  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_Constants.hxx>
35 #include <Batch_Job.hxx>
36 #include <Batch_BatchManagerCatalog.hxx>
37 #include <Batch_FactBatchManager.hxx>
38 #include <Batch_BatchManager.hxx>
39
40 #include <SimpleParser.hxx>
41
42 using namespace std;
43 using namespace Batch;
44
45 int main(int argc, char** argv)
46 {
47   cout << "*******************************************************************************************" << endl;
48   cout << "This program tests the local batch submission based on SSH. Passwordless SSH authentication" << endl;
49   cout << "must be used for this test to pass (this can be configured with ssh-agent for instance)." << endl;
50   cout << "*******************************************************************************************" << endl;
51
52   // eventually remove any previous result
53   remove("result.txt");
54
55   try {
56     // Parse the test configuration file
57     SimpleParser parser;
58     parser.parseTestConfigFile();
59     const string & workdir = parser.getValue("TEST_LOCAL_SSH_WORK_DIR");
60     const string & exechost = parser.getValue("TEST_LOCAL_SSH_EXECUTION_HOST");
61     const string & user = parser.getValue("TEST_LOCAL_SSH_USER");
62     int timeout = parser.getValueAsInt("TEST_LOCAL_SSH_TIMEOUT");
63
64     // Define the job...
65     Job job;
66     // ... and its parameters ...
67     Parametre p;
68     p[EXECUTABLE]    = "source copied-test-script.sh";
69     p[NAME]          = "Test_Local_SSH";
70     p[WORKDIR]       = workdir;
71     p[INFILE]        = Couple("seta.sh", "copied-seta.sh");
72     p[INFILE]       += Couple("setb.sh", "copied-setb.sh");
73     p[INFILE]       += Couple("test-script.sh", "copied-test-script.sh");
74     p[OUTFILE]       = Couple("result.txt", "orig-result.txt");
75     p[EXECUTIONHOST] = exechost;
76     p[USER]          = user;
77     job.setParametre(p);
78     // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
79     Environnement e;
80     const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
81     if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
82     job.setEnvironnement(e);
83     cout << job << endl;
84
85     // Get the catalog
86     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
87
88     // Create a BatchManager of type Local_SSH on localhost
89     FactBatchManager * fbm = c("SSH");
90     if (fbm == NULL) {
91       cerr << "Can't get SSH batch manager factory" << endl;
92       return 1;
93     }
94     BatchManager * bm = (*fbm)("localhost");
95
96     // Submit the job to the BatchManager
97     JobId jobid = bm->submitJob(job);
98     cout << jobid.__repr__() << endl;
99
100     // Wait for the end of the job
101     string state = bm->waitForJobEnd(jobid, timeout);
102
103     if (state != FINISHED && state != FAILED) {
104       cerr << "Error: Job not finished after timeout" << endl;
105       return 1;
106     }
107
108     cout << "Job " << jobid.__repr__() << " is done" << endl;
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("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 }