1 // Copyright 2011 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "cli/cmd_debug.hpp" 30 31 extern "C" { 32 #include <unistd.h> 33 } 34 35 #include <cstdlib> 36 #include <iostream> 37 38 #include "cli/common.ipp" 39 #include "drivers/debug_test.hpp" 40 #include "engine/filters.hpp" 41 #include "utils/cmdline/exceptions.hpp" 42 #include "utils/cmdline/options.hpp" 43 #include "utils/cmdline/parser.ipp" 44 #include "utils/cmdline/ui.hpp" 45 #include "utils/format/macros.hpp" 46 #include "utils/fs/path.hpp" 47 #include "utils/process/child.ipp" 48 #include "utils/process/executor.hpp" 49 #include "utils/process/operations.hpp" 50 #include "utils/process/status.hpp" 51 52 namespace cmdline = utils::cmdline; 53 namespace config = utils::config; 54 namespace executor = utils::process::executor; 55 namespace process = utils::process; 56 57 using cli::cmd_debug; 58 using utils::process::args_vector; 59 using utils::process::child; 60 61 62 namespace { 63 64 65 const cmdline::bool_option pause_before_cleanup_upon_fail_option( 66 'p', 67 "pause-before-cleanup-upon-fail", 68 "Pauses right before the test cleanup upon fail"); 69 70 71 const cmdline::bool_option pause_before_cleanup_option( 72 'P', 73 "pause-before-cleanup", 74 "Pauses right before the test cleanup"); 75 76 77 static const char* DEFAULT_CMD = "$SHELL"; 78 const cmdline::string_option execute_option( 79 'x', "execute", 80 "A command to run within the given execenv upon test failure", 81 "cmd", DEFAULT_CMD, true); 82 83 84 /// Functor to execute a program. 85 class execute { 86 const std::string& _cmd; 87 executor::exit_handle& _eh; 88 89 public: 90 /// Constructor. 91 /// 92 /// \param program Program binary absolute path. 93 /// \param args Program arguments. 94 execute( 95 const std::string& cmd_, 96 executor::exit_handle& eh_) : 97 _cmd(cmd_), 98 _eh(eh_) 99 { 100 } 101 102 /// Body of the subprocess. 103 void 104 operator()(void) 105 { 106 if (::chdir(_eh.work_directory().c_str()) == -1) { 107 std::cerr << "execute: chdir() errors: " 108 << strerror(errno) << ".\n"; 109 std::exit(EXIT_FAILURE); 110 } 111 112 std::string program_path = "/bin/sh"; 113 const char* shell = std::getenv("SHELL"); 114 if (shell) 115 program_path = shell; 116 117 args_vector av; 118 if (!(_cmd.empty() || _cmd == DEFAULT_CMD)) { 119 av.push_back("-c"); 120 av.push_back(_cmd); 121 } 122 123 process::exec(utils::fs::path(program_path), av); 124 } 125 }; 126 127 128 /// The debugger interface implementation. 129 class dbg : public engine::debugger { 130 /// Object to interact with the I/O of the program. 131 cmdline::ui* _ui; 132 133 /// Representation of the command line to the subcommand. 134 const cmdline::parsed_cmdline& _cmdline; 135 136 public: 137 /// Constructor. 138 /// 139 /// \param ui_ Object to interact with the I/O of the program. 140 /// \param cmdline Representation of the command line to the subcommand. 141 dbg(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline) : 142 _ui(ui), _cmdline(cmdline) 143 {} 144 145 void before_cleanup( 146 const model::test_program_ptr&, 147 const model::test_case&, 148 optional< model::test_result >& result, 149 executor::exit_handle& eh) const 150 { 151 if (_cmdline.has_option(pause_before_cleanup_upon_fail_option 152 .long_name())) { 153 if (result && !result.get().good()) { 154 _ui->out("The test failed and paused right before its cleanup " 155 "routine."); 156 _ui->out(F("Test work dir: %s") % eh.work_directory().str()); 157 _ui->out("Press <Enter> to continue..."); 158 (void) std::cin.get(); 159 } 160 } else if (_cmdline.has_option(pause_before_cleanup_option 161 .long_name())) { 162 _ui->out("The test paused right before its cleanup routine."); 163 _ui->out(F("Test work dir: %s") % eh.work_directory().str()); 164 _ui->out("Press <Enter> to continue..."); 165 (void) std::cin.get(); 166 } 167 }; 168 169 void upon_test_failure( 170 const model::test_program_ptr&, 171 const model::test_case&, 172 optional< model::test_result >&, 173 executor::exit_handle& eh) const 174 { 175 if (!_cmdline.has_option(execute_option.long_name())) 176 return; 177 const std::string& cmd = _cmdline.get_option<cmdline::string_option>( 178 execute_option.long_name()); 179 std::unique_ptr< process::child > child = child::fork_interactive( 180 execute(cmd, eh)); 181 (void) child->wait(); 182 }; 183 184 }; 185 186 187 } // anonymous namespace 188 189 190 /// Default constructor for cmd_debug. 191 cmd_debug::cmd_debug(void) : cli_command( 192 "debug", "test_case", 1, 1, 193 "Executes a single test case providing facilities for debugging") 194 { 195 add_option(build_root_option); 196 add_option(kyuafile_option); 197 198 add_option(pause_before_cleanup_upon_fail_option); 199 add_option(pause_before_cleanup_option); 200 201 add_option(cmdline::path_option( 202 "stdout", "Where to direct the standard output of the test case", 203 "path", "/dev/stdout")); 204 205 add_option(cmdline::path_option( 206 "stderr", "Where to direct the standard error of the test case", 207 "path", "/dev/stderr")); 208 209 add_option(execute_option); 210 } 211 212 213 /// Entry point for the "debug" subcommand. 214 /// 215 /// \param ui Object to interact with the I/O of the program. 216 /// \param cmdline Representation of the command line to the subcommand. 217 /// \param user_config The runtime debuguration of the program. 218 /// 219 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be 220 /// opened. 221 int 222 cmd_debug::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline, 223 const config::tree& user_config) 224 { 225 const std::string& test_case_name = cmdline.arguments()[0]; 226 if (test_case_name.find(':') == std::string::npos) 227 throw cmdline::usage_error(F("'%s' is not a test case identifier " 228 "(missing ':'?)") % test_case_name); 229 const engine::test_filter filter = engine::test_filter::parse( 230 test_case_name); 231 232 engine::debugger_ptr debugger = nullptr; 233 if (cmdline.has_option(pause_before_cleanup_upon_fail_option.long_name()) 234 || cmdline.has_option(pause_before_cleanup_option.long_name()) 235 || cmdline.has_option(execute_option.long_name())) { 236 debugger = std::shared_ptr< engine::debugger >(new dbg(ui, cmdline)); 237 } 238 239 const drivers::debug_test::result result = drivers::debug_test::drive( 240 debugger, 241 kyuafile_path(cmdline), build_root_path(cmdline), filter, user_config, 242 cmdline.get_option< cmdline::path_option >("stdout"), 243 cmdline.get_option< cmdline::path_option >("stderr")); 244 245 ui->out(F("%s -> %s") % cli::format_test_case_id(result.test_case) % 246 cli::format_result(result.test_result)); 247 248 return result.test_result.good() ? EXIT_SUCCESS : EXIT_FAILURE; 249 } 250