1// Copyright 2014 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 "engine/plain.hpp" 30 31extern "C" { 32#include <unistd.h> 33} 34 35#include <cstdlib> 36 37#include "model/test_case.hpp" 38#include "model/test_program.hpp" 39#include "model/test_result.hpp" 40#include "utils/defs.hpp" 41#include "utils/env.hpp" 42#include "utils/format/macros.hpp" 43#include "utils/fs/path.hpp" 44#include "utils/optional.ipp" 45#include "utils/process/operations.hpp" 46#include "utils/process/status.hpp" 47#include "utils/sanity.hpp" 48 49namespace config = utils::config; 50namespace fs = utils::fs; 51namespace process = utils::process; 52 53using utils::optional; 54 55 56/// Executes a test program's list operation. 57/// 58/// This method is intended to be called within a subprocess and is expected 59/// to terminate execution either by exec(2)ing the test program or by 60/// exiting with a failure. 61void 62engine::plain_interface::exec_list( 63 const model::test_program& /* test_program */, 64 const config::properties_map& /* vars */) const 65{ 66 ::_exit(EXIT_SUCCESS); 67} 68 69 70/// Computes the test cases list of a test program. 71/// 72/// \return A list of test cases. 73model::test_cases_map 74engine::plain_interface::parse_list( 75 const optional< process::status >& /* status */, 76 const fs::path& /* stdout_path */, 77 const fs::path& /* stderr_path */) const 78{ 79 return model::test_cases_map_builder().add("main").build(); 80} 81 82 83/// Executes a test case of the test program. 84/// 85/// This method is intended to be called within a subprocess and is expected 86/// to terminate execution either by exec(2)ing the test program or by 87/// exiting with a failure. 88/// 89/// \param test_program The test program to execute. 90/// \param test_case_name Name of the test case to invoke. 91/// \param vars User-provided variables to pass to the test program. 92void 93engine::plain_interface::exec_test( 94 const model::test_program& test_program, 95 const std::string& test_case_name, 96 const config::properties_map& vars, 97 const fs::path& /* control_directory */) const 98{ 99 PRE(test_case_name == "main"); 100 101 for (config::properties_map::const_iterator iter = vars.begin(); 102 iter != vars.end(); ++iter) { 103 utils::setenv(F("TEST_ENV_%s") % (*iter).first, (*iter).second); 104 } 105 106 process::args_vector args; 107 process::exec(test_program.absolute_path(), args); 108} 109 110 111/// Computes the result of a test case based on its termination status. 112/// 113/// \param status The termination status of the subprocess used to execute 114/// the exec_test() method or none if the test timed out. 115/// 116/// \return A test result. 117model::test_result 118engine::plain_interface::compute_result( 119 const optional< process::status >& status, 120 const fs::path& /* control_directory */, 121 const fs::path& /* stdout_path */, 122 const fs::path& /* stderr_path */) const 123{ 124 if (!status) { 125 return model::test_result(model::test_result_broken, 126 "Test case timed out"); 127 } 128 129 if (status.get().exited()) { 130 const int exitstatus = status.get().exitstatus(); 131 if (exitstatus == EXIT_SUCCESS) { 132 return model::test_result(model::test_result_passed); 133 } else { 134 return model::test_result( 135 model::test_result_failed, 136 F("Returned non-success exit status %s") % exitstatus); 137 } 138 } else { 139 return model::test_result( 140 model::test_result_broken, 141 F("Received signal %s") % status.get().termsig()); 142 } 143} 144