xref: /freebsd/contrib/kyua/engine/atf.cpp (revision 02e9120893770924227138ba49df1edb3896112a)
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 "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"
55 
56 namespace config = utils::config;
57 namespace fs = utils::fs;
58 namespace process = utils::process;
59 
60 using utils::optional;
61 
62 
63 namespace {
64 
65 
66 /// Basename of the file containing the result written by the ATF test case.
67 static const char* result_name = "result.atf";
68 
69 
70 /// Magic numbers returned by exec_list when exec(2) fails.
71 enum list_exit_code {
72     exit_eacces = 90,
73     exit_enoent,
74     exit_enoexec,
75 };
76 
77 
78 }  // anonymous namespace
79 
80 
81 /// Executes a test program's list operation.
82 ///
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.
86 ///
87 /// \param test_program The test program to execute.
88 /// \param vars User-provided variables to pass to the test program.
89 void
90 engine::atf_interface::exec_list(const model::test_program& test_program,
91                                  const config::properties_map& vars) const
92 {
93     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
94 
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);
99     }
100 
101     args.push_back("-l");
102     try {
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);
111         throw;
112     }
113 }
114 
115 
116 /// Computes the test cases list of a test program.
117 ///
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.
122 ///
123 /// \return A list of test cases.
124 ///
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
130 {
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);
134 
135     if (!status)
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");
147         } else {
148             throw engine::error("Test program did not exit cleanly");
149         }
150     } else {
151         throw engine::error("Test program received signal");
152     }
153 
154     std::ifstream input(stdout_path.c_str());
155     if (!input)
156         throw engine::load_error(stdout_path, "Cannot open file for read");
157     const model::test_cases_map test_cases = parse_atf_list(input);
158 
159     if (!stderr_contents.empty())
160         throw engine::error("Test case list wrote to stderr");
161 
162     return test_cases;
163 }
164 
165 
166 /// Executes a test case of the test program.
167 ///
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.
171 ///
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
176 ///     files.
177 void
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
182 {
183     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
184 
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);
189     }
190 
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);
194 }
195 
196 
197 /// Executes a test cleanup routine of the test program.
198 ///
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.
202 ///
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.
206 void
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
212 {
213     utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
214 
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);
219     }
220 
221     args.push_back(F("%s:cleanup") % test_case_name);
222     process::exec(test_program.absolute_path(), args);
223 }
224 
225 
226 /// Computes the result of a test case based on its termination status.
227 ///
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
231 ///     control files.
232 ///
233 /// \return A test result.
234 model::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
240 {
241     return calculate_atf_result(status, control_directory / result_name);
242 }
243