1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * Test_Local_SSH.cxx :
25 * Author : Renaud BARATE - EDF R&D
34 #include <Constants.hxx>
36 #include <BatchManagerCatalog.hxx>
37 #include <FactBatchManager.hxx>
38 #include <BatchManager.hxx>
40 #include <SimpleParser.hxx>
43 using namespace Batch;
45 int main(int argc, char** argv)
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;
52 // eventually remove any previous result
56 // Parse the test configuration file
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");
66 // ... and its parameters ...
68 p[EXECUTABLE] = "test-script.sh";
69 p[NAME] = "Test_Local_SSH";
71 p[INFILE] = Couple("seta.sh", workdir + "/copied-seta.sh");
72 p[INFILE] += Couple("setb.sh", workdir + "/copied-setb.sh");
73 p[OUTFILE] = Couple("result.txt", workdir + "/orig-result.txt");
76 // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
78 const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
79 if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
80 job.setEnvironnement(e);
84 BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
86 // Create a BatchManager of type Local_SSH on localhost
87 FactBatchManager * fbm = c("LOCAL");
89 cerr << "Can't get SSH batch manager factory" << endl;
92 BatchManager * bm = (*fbm)(exechost.c_str(), user.c_str(), SSH);
94 // Submit the job to the BatchManager
95 JobId jobid = bm->submitJob(job);
96 cout << jobid.__repr__() << endl;
98 // Wait for the end of the job
99 string state = bm->waitForJobEnd(jobid, timeout);
101 if (state == FINISHED) {
102 cout << "Job " << jobid.__repr__() << " is done" << endl;
103 bm->importOutputFiles(job, "resultdir/seconddirname");
104 } else if (state == FAILED) {
105 cerr << "Job " << jobid.__repr__() << " finished in error" << endl;
106 bm->importOutputFiles(job, "resultdir/seconddirname");
109 cerr << "Timeout while executing job" << endl;
113 } catch (GenericException e) {
114 cerr << "Error: " << e << endl;
116 } catch (ParserException e) {
117 cerr << "Parser error: " << e.what() << endl;
121 // test the result file
122 string exp = "c = 12";
124 ifstream f("resultdir/seconddirname/result.txt");
128 cout << "result found : " << res << ", expected : " << exp << endl;