Salome HOME
f946b4399144d3560760b4bfae9f7eb0ea70470a
[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 #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 SSH. Passwordless SSH authentication" << endl;
46   cout << "must be used for this test to pass (this can be configured with ssh-agent for instance)." << endl;
47   cout << "*******************************************************************************************" << endl;
48
49   // eventually remove any previous result
50   remove("result.txt");
51
52   // Define the job...
53   Job job;
54   // ... and its parameters ...
55   Parametre p;
56   p["EXECUTABLE"]    = "./copied-test-script.sh";
57   p["NAME"]          = "Test_Local_SSH";
58   p["WORKDIR"]       = TEST_LOCAL_SSH_WORK_DIR;
59   p["INFILE"]        = Couple("seta.sh", "copied-seta.sh");
60   p["INFILE"]       += Couple("setb.sh", "copied-setb.sh");
61   p["INFILE"]       += Couple("test-script.sh", "copied-test-script.sh");
62   p["OUTFILE"]       = Couple("result.txt", "orig-result.txt");
63   p["EXECUTIONHOST"] = TEST_LOCAL_SSH_EXECUTION_HOST;
64   job.setParametre(p);
65   // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
66   Environnement e;
67   e["SSH_AUTH_SOCK"] = getenv("SSH_AUTH_SOCK");
68   job.setEnvironnement(e);
69   cout << job << endl;
70
71   // Get the catalog
72   BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
73
74   // Create a BatchManager of type Local_SSH on localhost
75   FactBatchManager * fbm = c("SSH");
76   BatchManager * bm = (*fbm)("localhost");
77
78   // Submit the job to the BatchManager
79   JobId jobid = bm->submitJob(job);
80   cout << jobid.__repr__() << endl;
81
82   // Wait for the end of the job
83   string state = "Unknown";
84   while (state != "Done") {
85     usleep(10000);
86     JobInfo jinfo = jobid.queryJob();
87     state = jinfo.getParametre()["STATE"].str();
88   }
89
90   cout << "Job " << jobid.__repr__() << " is done" << endl;
91
92   // wait for 5 more seconds for the copy of output files and the cleanup
93   // (there's no cleaner way to do that yet)
94   sleep(5);
95
96   // test the result file
97   string exp = "c = 12";
98   string res;
99   ifstream f("result.txt");
100   getline(f, res);
101   f.close();
102
103   cout << "result found : " << res << ", expected : " << exp << endl;
104
105   if (res == exp)
106     return 0;
107   else
108     return 1;
109 }