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/atf.hpp"
39 #include "engine/atf_list.hpp"
40 #include "engine/atf_result.hpp"
41 #include "engine/exceptions.hpp"
42 #include "model/test_case.hpp"
43 #include "model/test_program.hpp"
44 #include "model/test_result.hpp"
45 #include "utils/defs.hpp"
46 #include "utils/env.hpp"
47 #include "utils/format/macros.hpp"
48 #include "utils/fs/path.hpp"
49 #include "utils/logging/macros.hpp"
50 #include "utils/optional.ipp"
51 #include "utils/process/exceptions.hpp"
52 #include "utils/process/operations.hpp"
53 #include "utils/process/status.hpp"
54 #include "utils/stream.hpp"
56 namespace config = utils::config;
57 namespace fs = utils::fs;
58 namespace process = utils::process;
60 using utils::optional;
66 /// Basename of the file containing the result written by the ATF test case.
67 static const char* result_name = "result.atf";
70 /// Magic numbers returned by exec_list when exec(2) fails.
78 } // anonymous namespace
81 /// Executes a test program's list operation.
83 /// This method is intended to be called within a subprocess and is expected
84 /// to terminate execution either by exec(2)ing the test program or by
85 /// exiting with a failure.
87 /// \param test_program The test program to execute.
88 /// \param vars User-provided variables to pass to the test program.
90 engine::atf_interface::exec_list(const model::test_program& test_program,
91 const config::properties_map& vars) const
93 utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
95 process::args_vector args;
96 for (config::properties_map::const_iterator iter = vars.begin();
97 iter != vars.end(); ++iter) {
98 args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
101 args.push_back("-l");
103 process::exec_unsafe(test_program.absolute_path(), args);
104 } catch (const process::system_error& e) {
105 if (e.original_errno() == EACCES)
106 ::_exit(exit_eacces);
107 else if (e.original_errno() == ENOENT)
108 ::_exit(exit_enoent);
109 else if (e.original_errno() == ENOEXEC)
110 ::_exit(exit_enoexec);
116 /// Computes the test cases list of a test program.
118 /// \param status The termination status of the subprocess used to execute
119 /// the exec_test() method or none if the test timed out.
120 /// \param stdout_path Path to the file containing the stdout of the test.
121 /// \param stderr_path Path to the file containing the stderr of the test.
123 /// \return A list of test cases.
125 /// \throw error If there is a problem parsing the test case list.
126 model::test_cases_map
127 engine::atf_interface::parse_list(const optional< process::status >& status,
128 const fs::path& stdout_path,
129 const fs::path& stderr_path) const
131 const std::string stderr_contents = utils::read_file(stderr_path);
132 if (!stderr_contents.empty())
133 LW("Test case list wrote to stderr: " + stderr_contents);
136 throw engine::error("Test case list timed out");
137 if (status.get().exited()) {
138 const int exitstatus = status.get().exitstatus();
139 if (exitstatus == EXIT_SUCCESS) {
140 // Nothing to do; fall through.
141 } else if (exitstatus == exit_eacces) {
142 throw engine::error("Permission denied to run test program");
143 } else if (exitstatus == exit_enoent) {
144 throw engine::error("Cannot find test program");
145 } else if (exitstatus == exit_enoexec) {
146 throw engine::error("Invalid test program format");
148 throw engine::error("Test program did not exit cleanly");
151 throw engine::error("Test program received signal");
154 std::ifstream input(stdout_path.c_str());
156 throw engine::load_error(stdout_path, "Cannot open file for read");
157 const model::test_cases_map test_cases = parse_atf_list(input);
159 if (!stderr_contents.empty())
160 throw engine::error("Test case list wrote to stderr");
166 /// Executes a test case of the test program.
168 /// This method is intended to be called within a subprocess and is expected
169 /// to terminate execution either by exec(2)ing the test program or by
170 /// exiting with a failure.
172 /// \param test_program The test program to execute.
173 /// \param test_case_name Name of the test case to invoke.
174 /// \param vars User-provided variables to pass to the test program.
175 /// \param control_directory Directory where the interface may place control
178 engine::atf_interface::exec_test(const model::test_program& test_program,
179 const std::string& test_case_name,
180 const config::properties_map& vars,
181 const fs::path& control_directory) const
183 utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
185 process::args_vector args;
186 for (config::properties_map::const_iterator iter = vars.begin();
187 iter != vars.end(); ++iter) {
188 args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
191 args.push_back(F("-r%s") % (control_directory / result_name));
192 args.push_back(test_case_name);
193 process::exec(test_program.absolute_path(), args);
197 /// Executes a test cleanup routine of the test program.
199 /// This method is intended to be called within a subprocess and is expected
200 /// to terminate execution either by exec(2)ing the test program or by
201 /// exiting with a failure.
203 /// \param test_program The test program to execute.
204 /// \param test_case_name Name of the test case to invoke.
205 /// \param vars User-provided variables to pass to the test program.
207 engine::atf_interface::exec_cleanup(
208 const model::test_program& test_program,
209 const std::string& test_case_name,
210 const config::properties_map& vars,
211 const fs::path& /* control_directory */) const
213 utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
215 process::args_vector args;
216 for (config::properties_map::const_iterator iter = vars.begin();
217 iter != vars.end(); ++iter) {
218 args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
221 args.push_back(F("%s:cleanup") % test_case_name);
222 process::exec(test_program.absolute_path(), args);
226 /// Computes the result of a test case based on its termination status.
228 /// \param status The termination status of the subprocess used to execute
229 /// the exec_test() method or none if the test timed out.
230 /// \param control_directory Directory where the interface may have placed
233 /// \return A test result.
235 engine::atf_interface::compute_result(
236 const optional< process::status >& status,
237 const fs::path& control_directory,
238 const fs::path& /* stdout_path */,
239 const fs::path& /* stderr_path */) const
241 return calculate_atf_result(status, control_directory / result_name);