xref: /freebsd/contrib/kyua/engine/atf.cpp (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
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/atf.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include <cerrno>
36 #include <cstdlib>
37 #include <fstream>
38 
39 #include "engine/atf_list.hpp"
40 #include "engine/atf_result.hpp"
41 #include "engine/exceptions.hpp"
42 #include "engine/execenv/execenv.hpp"
43 #include "model/test_case.hpp"
44 #include "model/test_program.hpp"
45 #include "model/test_result.hpp"
46 #include "utils/defs.hpp"
47 #include "utils/env.hpp"
48 #include "utils/format/macros.hpp"
49 #include "utils/fs/path.hpp"
50 #include "utils/logging/macros.hpp"
51 #include "utils/optional.ipp"
52 #include "utils/process/exceptions.hpp"
53 #include "utils/process/operations.hpp"
54 #include "utils/process/status.hpp"
55 #include "utils/stream.hpp"
56 
57 namespace config = utils::config;
58 namespace execenv = engine::execenv;
59 namespace fs = utils::fs;
60 namespace process = utils::process;
61 
62 using utils::optional;
63 
64 
65 namespace {
66 
67 
68 /// Basename of the file containing the result written by the ATF test case.
69 static const char* result_name = "result.atf";
70 
71 
72 /// Magic numbers returned by exec_list when exec(2) fails.
73 enum list_exit_code {
74     exit_eacces = 90,
75     exit_enoent,
76     exit_enoexec,
77 };
78 
79 
80 }  // anonymous namespace
81 
82 
83 /// Executes a test program's list operation.
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 vars User-provided variables to pass to the test program.
91 void
92 engine::atf_interface::exec_list(const model::test_program& test_program,
93                                  const config::properties_map& vars) const
94 {
95     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
96 
97     process::args_vector args;
98     for (config::properties_map::const_iterator iter = vars.begin();
99          iter != vars.end(); ++iter) {
100         args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
101     }
102 
103     args.push_back("-l");
104     try {
105         process::exec_unsafe(test_program.absolute_path(), args);
106     } catch (const process::system_error& e) {
107         if (e.original_errno() == EACCES)
108             ::_exit(exit_eacces);
109         else if (e.original_errno() == ENOENT)
110             ::_exit(exit_enoent);
111         else if (e.original_errno() == ENOEXEC)
112             ::_exit(exit_enoexec);
113         throw;
114     }
115 }
116 
117 
118 /// Computes the test cases list of a test program.
119 ///
120 /// \param status The termination status of the subprocess used to execute
121 ///     the exec_test() method or none if the test timed out.
122 /// \param stdout_path Path to the file containing the stdout of the test.
123 /// \param stderr_path Path to the file containing the stderr of the test.
124 ///
125 /// \return A list of test cases.
126 ///
127 /// \throw error If there is a problem parsing the test case list.
128 model::test_cases_map
129 engine::atf_interface::parse_list(const optional< process::status >& status,
130                                   const fs::path& stdout_path,
131                                   const fs::path& stderr_path) const
132 {
133     const std::string stderr_contents = utils::read_file(stderr_path);
134     if (!stderr_contents.empty())
135         LW("Test case list wrote to stderr: " + stderr_contents);
136 
137     if (!status)
138         throw engine::error("Test case list timed out");
139     if (status.get().exited()) {
140         const int exitstatus = status.get().exitstatus();
141         if (exitstatus == EXIT_SUCCESS) {
142             // Nothing to do; fall through.
143         } else if (exitstatus == exit_eacces) {
144             throw engine::error("Permission denied to run test program");
145         } else if (exitstatus == exit_enoent) {
146             throw engine::error("Cannot find test program");
147         } else if (exitstatus == exit_enoexec) {
148             throw engine::error("Invalid test program format");
149         } else {
150             throw engine::error("Test program did not exit cleanly");
151         }
152     } else {
153         throw engine::error("Test program received signal");
154     }
155 
156     std::ifstream input(stdout_path.c_str());
157     if (!input)
158         throw engine::load_error(stdout_path, "Cannot open file for read");
159     const model::test_cases_map test_cases = parse_atf_list(input);
160 
161     if (!stderr_contents.empty())
162         throw engine::error("Test case list wrote to stderr");
163 
164     return test_cases;
165 }
166 
167 
168 /// Executes a test case of the test program.
169 ///
170 /// This method is intended to be called within a subprocess and is expected
171 /// to terminate execution either by exec(2)ing the test program or by
172 /// exiting with a failure.
173 ///
174 /// \param test_program The test program to execute.
175 /// \param test_case_name Name of the test case to invoke.
176 /// \param vars User-provided variables to pass to the test program.
177 /// \param control_directory Directory where the interface may place control
178 ///     files.
179 void
180 engine::atf_interface::exec_test(const model::test_program& test_program,
181                                  const std::string& test_case_name,
182                                  const config::properties_map& vars,
183                                  const fs::path& control_directory) const
184 {
185     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
186 
187     process::args_vector args;
188     for (config::properties_map::const_iterator iter = vars.begin();
189          iter != vars.end(); ++iter) {
190         args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
191     }
192 
193     args.push_back(F("-r%s") % (control_directory / result_name));
194     args.push_back(test_case_name);
195 
196     auto e = execenv::get(test_program, test_case_name);
197     e->init();
198     e->exec(args);
199     __builtin_unreachable();
200 }
201 
202 
203 /// Executes a test cleanup routine of the test program.
204 ///
205 /// This method is intended to be called within a subprocess and is expected
206 /// to terminate execution either by exec(2)ing the test program or by
207 /// exiting with a failure.
208 ///
209 /// \param test_program The test program to execute.
210 /// \param test_case_name Name of the test case to invoke.
211 /// \param vars User-provided variables to pass to the test program.
212 void
213 engine::atf_interface::exec_cleanup(
214     const model::test_program& test_program,
215     const std::string& test_case_name,
216     const config::properties_map& vars,
217     const fs::path& /* control_directory */) const
218 {
219     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
220 
221     process::args_vector args;
222     for (config::properties_map::const_iterator iter = vars.begin();
223          iter != vars.end(); ++iter) {
224         args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
225     }
226 
227     args.push_back(F("%s:cleanup") % test_case_name);
228 
229     auto e = execenv::get(test_program, test_case_name);
230     e->exec(args);
231     __builtin_unreachable();
232 }
233 
234 
235 /// Computes the result of a test case based on its termination status.
236 ///
237 /// \param status The termination status of the subprocess used to execute
238 ///     the exec_test() method or none if the test timed out.
239 /// \param control_directory Directory where the interface may have placed
240 ///     control files.
241 ///
242 /// \return A test result.
243 model::test_result
244 engine::atf_interface::compute_result(
245     const optional< process::status >& status,
246     const fs::path& control_directory,
247     const fs::path& /* stdout_path */,
248     const fs::path& /* stderr_path */) const
249 {
250     return calculate_atf_result(status, control_directory / result_name);
251 }
252