CMS 3D CMS Logo

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