Salome HOME
Refactored submission based on SH, RSH and SSH by grouping related commands in Commun...
[tools/libbatch.git] / src / PBS / Test / Test_ePBS_RSH.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_ePBS.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_FactBatchManager_eClient.hxx>
37 #include <Batch_BatchManager.hxx>
38 #include <Batch_BatchManager_eClient.hxx>
39
40 #include <SimpleParser.hxx>
41
42 #ifdef WIN32
43 #include <Windows.h>
44 #define sleep(seconds) Sleep((seconds)*1000)
45 #define usleep(useconds) Sleep((useconds)/1000)
46 #endif
47
48 using namespace std;
49 using namespace Batch;
50
51 int main(int argc, char** argv)
52 {
53   cout << "*******************************************************************************************" << endl;
54   cout << "This program tests the batch submission based on PBS emulation with RSH. Passwordless RSH" << endl;
55   cout << "authentication must be used for this test to pass (this can be configured with the .rhosts" << endl;
56   cout << "file). You also need to create a directory \"tmp/Batch\" in your home directory on the PBS" << endl;
57   cout << "server before running this test." << endl;
58   cout << "*******************************************************************************************" << endl;
59
60   // eventually remove any previous result
61   remove("result.txt");
62
63   try {
64     // Parse the test configuration file
65     SimpleParser parser;
66     parser.parseTestConfigFile();
67     const string & homedir = parser.getValue("TEST_EPBS_HOMEDIR");
68     const string & host = parser.getValue("TEST_EPBS_HOST");
69     const string & user = parser.getValue("TEST_EPBS_USER");
70     const string & queue = parser.getValue("TEST_EPBS_QUEUE");
71     int timeout = parser.getValueAsInt("TEST_EPBS_TIMEOUT");
72
73     // Define the job...
74     Job job;
75     // ... and its parameters ...
76     Parametre p;
77     p["EXECUTABLE"]    = "./test-script.sh";
78     p["NAME"]          = "Test_ePBS_RSH";
79     p["WORKDIR"]       = homedir + "/tmp/Batch";
80     p["INFILE"]        = Couple("seta.sh", "tmp/Batch/seta.sh");
81     p["INFILE"]       += Couple("setb.sh", "tmp/Batch/setb.sh");
82     p["OUTFILE"]       = Couple("result.txt", "tmp/Batch/result.txt");
83     p["TMPDIR"]        = "tmp/Batch/";
84     p["USER"]          = user;
85     p["NBPROC"]        = 1;
86     p["MAXWALLTIME"]   = 1;
87     p["MAXRAMSIZE"]    = 4;
88     p["HOMEDIR"]       = homedir;
89     p["QUEUE"]         = queue;
90     job.setParametre(p);
91     // ... and its environment (SSH_AUTH_SOCK env var is important for ssh agent authentication)
92     Environnement e;
93     const char * sshAuthSock = getenv("SSH_AUTH_SOCK");
94     if (sshAuthSock != NULL) e["SSH_AUTH_SOCK"] = sshAuthSock;
95     job.setEnvironnement(e);
96     cout << job << endl;
97
98     // Get the catalog
99     BatchManagerCatalog& c = BatchManagerCatalog::getInstance();
100
101     // Create a BatchManager of type ePBS on localhost
102     FactBatchManager_eClient * fbm = (FactBatchManager_eClient *)(c("ePBS"));
103     BatchManager_eClient * bm = (*fbm)(host.c_str(), RSH, "lam");
104
105     // Submit the job to the BatchManager
106     JobId jobid = bm->submitJob(job);
107     cout << jobid.__repr__() << endl;
108
109     // Wait for the end of the job
110     string state = "Undefined";
111     for (int i=0 ; i<timeout/2 && state != "U"; i++) {
112       sleep(2);
113       JobInfo jinfo = jobid.queryJob();
114       state = jinfo.getParametre()["STATE"].str();
115       cout << "State is \"" << state << "\"" << endl;
116     }
117
118     if (state == "U") {
119       cout << "Job " << jobid.__repr__() << " is done" << endl;
120       bm->importOutputFiles(job, ".");
121     } else {
122       cerr << "Timeout while executing job" << endl;
123       return 1;
124     }
125
126   } catch (GenericException e) {
127     cerr << "Error: " << e << endl;
128     return 1;
129   } catch (ParserException e) {
130     cerr << "Parser error: " << e.what() << endl;
131     return 1;
132   }
133
134   // test the result file
135   string exp = "c = 12";
136   string res;
137   ifstream f("result.txt");
138   getline(f, res);
139   f.close();
140
141   cout << "result found : " << res << ", expected : " << exp << endl;
142
143   if (res == exp)
144     return 0;
145   else
146     return 1;
147 }