CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TestHelper.cc
Go to the documentation of this file.
1 //------------------------------------------------------------
2 // $Id: TestHelper.cc,v 1.11 2010/09/01 15:37:24 chrjones Exp $
3 //------------------------------------------------------------
4 #include <cerrno>
5 #include <cstdlib>
6 #include <exception>
7 #include <iostream>
8 #include <string>
9 
10 #include <sys/wait.h>
11 #include <unistd.h>
12 #include <cstring>
13 
14 #include "boost/filesystem/convenience.hpp"
15 #include "boost/filesystem/path.hpp"
16 
20 
21 namespace bf=boost::filesystem;
22 
23 int run_script(const std::string& shell, const std::string& script)
24 {
25  pid_t pid;
26  int status=0;
27 
28  if ((pid=fork())<0)
29  {
30  std::cerr << "fork failed, to run " << script << std::endl;;
31  return -1;
32  }
33 
34  if (pid==0) // child
35  {
36  execlp(shell.c_str(), "sh", "-c", script.c_str(), (const char *)(0));
37  std::cerr <<"child failed becuase '"<<strerror(errno)<<"'\n";
38  _exit(127); // signal parent and children processes
39  }
40  else // parent
41  {
42  while(waitpid(pid,&status,0)<0)
43  {
44  if (errno!=EINTR)
45  {
46  std::cerr <<"child process failed "<<strerror(errno)<<"\n";
47  status=-1;
48  break;
49  } else {
50  if( WIFSIGNALED(status) ) {
51  std::cerr << "child existed because of a signal "<<WTERMSIG(status)<<"\n";
52  }
53  }
54  }
55  if( WIFSIGNALED(status) ) {
56  std::cerr << "child existed because of a signal "<<WTERMSIG(status)<<"\n";
57  }
58  if(WIFEXITED(status)) {
59  }
60 
61  }
62  return status;
63 }
64 
65 
66 
67 int do_work(int argc, char* argv[], char** env)
68 {
69  bf::path currentPath(bf::initial_path().string(), bf::no_check);
70 
71  if (argc<4)
72  {
73  std::cout << "Usage: " << argv[0] << " shell subdir script1 script2 ... scriptN\n\n"
74  << "where shell is the path+shell (e.g., /bin/bash) intended to run the scripts\n"
75  << "and subdir is the subsystem/package/subdir in which the scripts are found\n"
76  << "(e.g., FWCore/Utilities/test)\n"
77  << std::endl;
78 
79  std::cout << "Current directory is: " << currentPath.native_directory_string() << '\n';
80  std::cout << "Current environment:\n";
81  std::cout << "---------------------\n";
82  for (int i = 0; env[i] != 0; ++i) std::cout << env[i] << '\n';
83  std::cout << "---------------------\n";
84  std::cout << "Executable name: " << argv[0] << '\n';
85  return -1;
86  }
87 
88  for (int i = 0; i < argc; ++i)
89  {
90  std::cout << "argument " << i << ": " << argv[i] << '\n';
91  }
92 
93 
94  std::string shell(argv[1]);
95  std::cerr << "shell is: " << shell << '\n';
96 
97  std::cout << "Current directory is: " << currentPath.native_directory_string() << '\n';
98  // It is unclear about which of these environment variables should
99  // be used.
100  const char* topdir = getenv("SCRAMRT_LOCALRT");
101  if (!topdir) topdir = getenv("LOCALRT");
102 
103  const char* arch = getenv("SCRAM_ARCH");
104 
105  if ( !arch )
106  {
107  // Try to synthesize SCRAM_ARCH value.
108  bf::path exepath(argv[0], bf::no_check);
109  std::string maybe_arch = exepath.branch_path().leaf();
110  if (setenv("SCRAM_ARCH", maybe_arch.c_str(), 1) != 0)
111  {
112  std::cerr << "SCRAM_ARCH not set and attempt to set it failed\n";
113  return -1;
114  }
115  arch = getenv("SCRAM_ARCH");
116  }
117 
118  int rc=0;
119 
120  if (!topdir)
121  {
122  std::cout << "Neither SCRAMRT_LOCALRT nor LOCALRT is not defined" << std::endl;;
123  return -1;
124  }
125 
126 
127  std::string testdir(topdir); testdir += "/src/"; testdir += argv[2];
128  std::string tmpdir(topdir); tmpdir+="/tmp/"; tmpdir+=arch;
129  std::string testbin(topdir); testbin+="/test/"; testbin+=arch;
130 
131  std::cout << "topdir is: " << topdir << '\n';
132  std::cout << "testdir is: " << testdir << '\n';
133  std::cout << "tmpdir is: " << tmpdir << '\n';
134  std::cout << "testbin is: " << testbin << '\n';
135 
136 
137  if (setenv("LOCAL_TEST_DIR",testdir.c_str(),1)!=0)
138  {
139  std::cerr << "Could not set LOCAL_TEST_DIR to " << testdir << std::endl;;
140  return -1;
141  }
142  if (setenv("LOCAL_TMP_DIR",tmpdir.c_str(),1)!=0)
143  {
144  std::cerr << "Could not set LOCAL_TMP_DIR to " << tmpdir << std::endl;;
145  return -1;
146  }
147  if (setenv("LOCAL_TOP_DIR",topdir,1)!=0)
148  {
149  std::cerr << "Could not set LOCAL_TOP_DIR to " << topdir << std::endl;;
150  return -1;
151  }
152  if (setenv("LOCAL_TEST_BIN",testbin.c_str(),1)!=0)
153  {
154  std::cerr << "Could not set LOCAL_TEST_BIN to " << testbin << std::endl;;
155  return -1;
156  }
157 
158  testdir+="/";
159 
160  for(int i=3; i<argc && rc==0; ++i)
161  {
162  std::string scriptname(testdir);
163  scriptname += argv[i];
164  std::cout << "Running script: " << scriptname << std::endl;
165  rc = run_script(shell, scriptname);
166  }
167 
168  std::cout << "status = " << rc << std::endl;;
169  return rc == 0 ? 0 : -1;
170 }
171 
172 int ptomaine(int argc, char* argv[], char** env)
173 {
174  int rc = 1;
175  try
176  {
177  rc = do_work(argc, argv, env);
178  }
179  catch ( edm::Exception& x )
180  {
181  std::cerr << "Caught an edm::Exception in "
182  << argv[0] << '\n'
183  << x;
184  }
185  catch ( cms::Exception& x )
186  {
187  std::cerr << "Caught a cms::Exception in "
188  << argv[0] << '\n'
189  << x;
190  }
191  catch ( std::exception& x )
192  {
193  std::cerr << "Caught a std::exception in "
194  << argv[0] << '\n'
195  << x.what();
196  }
197  catch (...)
198  {
199  std::cerr << "Caught an unknown exception in "
200  << argv[0];
201  }
202  return rc;
203 }
int i
Definition: DBlmapReader.cc:9
int run_script(const std::string &shell, const std::string &script)
Definition: TestHelper.cc:23
int do_work(int argc, char *argv[], char **env)
Definition: TestHelper.cc:67
int path() const
Definition: HLTadd.h:3
int ptomaine(int argc, char *argv[], char **env)
Definition: TestHelper.cc:172
tuple cout
Definition: gather_cfg.py:41
tuple status
Definition: ntuplemaker.py:245
tuple arch
Definition: __init__.py:4