xref: /freebsd/contrib/kyua/engine/tap.cpp (revision 4e859e67dde2cb4a583d340b27793a255f62f53e)
1 // Copyright 2015 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/tap.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include <cstdlib>
36 
37 #include "engine/exceptions.hpp"
38 #include "engine/execenv/execenv.hpp"
39 #include "engine/tap_parser.hpp"
40 #include "model/test_case.hpp"
41 #include "model/test_program.hpp"
42 #include "model/test_result.hpp"
43 #include "utils/defs.hpp"
44 #include "utils/env.hpp"
45 #include "utils/format/macros.hpp"
46 #include "utils/optional.ipp"
47 #include "utils/process/operations.hpp"
48 #include "utils/process/status.hpp"
49 #include "utils/sanity.hpp"
50 
51 namespace config = utils::config;
52 namespace execenv = engine::execenv;
53 namespace fs = utils::fs;
54 namespace process = utils::process;
55 
56 using utils::optional;
57 
58 
59 namespace {
60 
61 
62 /// Computes the result of a TAP test program termination.
63 ///
64 /// Timeouts and bad TAP data must be handled by the caller.  Here we assume
65 /// that we have been able to successfully parse the TAP output.
66 ///
67 /// \param summary Parsed TAP data for the test program.
68 /// \param status Exit status of the test program.
69 ///
70 /// \return A test result.
71 static model::test_result
72 tap_to_result(const engine::tap_summary& summary,
73               const process::status& status)
74 {
75     if (summary.bailed_out()) {
76         return model::test_result(model::test_result_failed, "Bailed out");
77     }
78 
79     if (summary.plan() == engine::all_skipped_plan) {
80         return model::test_result(model::test_result_skipped,
81                                   summary.all_skipped_reason());
82     }
83 
84     if (summary.not_ok_count() == 0) {
85         if (status.exitstatus() == EXIT_SUCCESS) {
86             return model::test_result(model::test_result_passed);
87         } else {
88             return model::test_result(
89                 model::test_result_broken,
90                 F("Dubious test program: reported all tests as passed "
91                   "but returned exit code %s") % status.exitstatus());
92         }
93     } else {
94         const std::size_t total = summary.ok_count() + summary.not_ok_count();
95         return model::test_result(model::test_result_failed,
96                                   F("%s of %s tests failed") %
97                                   summary.not_ok_count() % total);
98     }
99 }
100 
101 
102 }  // anonymous namespace
103 
104 
105 /// Executes a test program's list operation.
106 ///
107 /// This method is intended to be called within a subprocess and is expected
108 /// to terminate execution either by exec(2)ing the test program or by
109 /// exiting with a failure.
110 void
111 engine::tap_interface::exec_list(
112     const model::test_program& /* test_program */,
113     const config::properties_map& /* vars */) const
114 {
115     ::_exit(EXIT_SUCCESS);
116 }
117 
118 
119 /// Computes the test cases list of a test program.
120 ///
121 /// \return A list of test cases.
122 model::test_cases_map
123 engine::tap_interface::parse_list(
124     const optional< process::status >& /* status */,
125     const fs::path& /* stdout_path */,
126     const fs::path& /* stderr_path */) const
127 {
128     return model::test_cases_map_builder().add("main").build();
129 }
130 
131 
132 /// Executes a test case of the test program.
133 ///
134 /// This method is intended to be called within a subprocess and is expected
135 /// to terminate execution either by exec(2)ing the test program or by
136 /// exiting with a failure.
137 ///
138 /// \param test_program The test program to execute.
139 /// \param test_case_name Name of the test case to invoke.
140 /// \param vars User-provided variables to pass to the test program.
141 void
142 engine::tap_interface::exec_test(
143     const model::test_program& test_program,
144     const std::string& test_case_name,
145     const config::properties_map& vars,
146     const fs::path& /* control_directory */) const
147 {
148     PRE(test_case_name == "main");
149 
150     for (config::properties_map::const_iterator iter = vars.begin();
151          iter != vars.end(); ++iter) {
152         utils::setenv(F("TEST_ENV_%s") % (*iter).first, (*iter).second);
153     }
154 
155     process::args_vector args;
156 
157     auto e = execenv::get(test_program, test_case_name);
158     e->init();
159     e->exec(args);
160     __builtin_unreachable();
161 }
162 
163 
164 /// Computes the result of a test case based on its termination status.
165 ///
166 /// \param status The termination status of the subprocess used to execute
167 ///     the exec_test() method or none if the test timed out.
168 /// \param stdout_path Path to the file containing the stdout of the test.
169 ///
170 /// \return A test result.
171 model::test_result
172 engine::tap_interface::compute_result(
173     const optional< process::status >& status,
174     const fs::path& /* control_directory */,
175     const fs::path& stdout_path,
176     const fs::path& /* stderr_path */) const
177 {
178     if (!status) {
179         return model::test_result(model::test_result_broken,
180                                   "Test case timed out");
181     } else {
182         if (status.get().signaled()) {
183             return model::test_result(
184                 model::test_result_broken,
185                 F("Received signal %s") % status.get().termsig());
186         } else {
187             try {
188                 const tap_summary summary = parse_tap_output(stdout_path);
189                 return tap_to_result(summary, status.get());
190             } catch (const load_error& e) {
191                 return model::test_result(
192                     model::test_result_broken,
193                     F("TAP test program yielded invalid data: %s") % e.what());
194             }
195         }
196     }
197 }
198