1 // Copyright 2014 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "engine/plain.hpp"
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"
49 namespace config = utils::config;
50 namespace fs = utils::fs;
51 namespace process = utils::process;
53 using utils::optional;
56 /// Executes a test program's list operation.
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.
62 engine::plain_interface::exec_list(
63 const model::test_program& /* test_program */,
64 const config::properties_map& /* vars */) const
66 ::_exit(EXIT_SUCCESS);
70 /// Computes the test cases list of a test program.
72 /// \return A list of test cases.
74 engine::plain_interface::parse_list(
75 const optional< process::status >& /* status */,
76 const fs::path& /* stdout_path */,
77 const fs::path& /* stderr_path */) const
79 return model::test_cases_map_builder().add("main").build();
83 /// Executes a test case of the test program.
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.
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.
93 engine::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
99 PRE(test_case_name == "main");
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);
106 process::args_vector args;
107 process::exec(test_program.absolute_path(), args);
111 /// Computes the result of a test case based on its termination status.
113 /// \param status The termination status of the subprocess used to execute
114 /// the exec_test() method or none if the test timed out.
116 /// \return A test result.
118 engine::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
125 return model::test_result(model::test_result_broken,
126 "Test case timed out");
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);
134 return model::test_result(
135 model::test_result_failed,
136 F("Returned non-success exit status %s") % exitstatus);
139 return model::test_result(
140 model::test_result_broken,
141 F("Received signal %s") % status.get().termsig());