xref: /freebsd/contrib/kyua/engine/plain.cpp (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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/plain.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include <cstdlib>
36 
37 #include "engine/execenv/execenv.hpp"
38 #include "model/test_case.hpp"
39 #include "model/test_program.hpp"
40 #include "model/test_result.hpp"
41 #include "utils/defs.hpp"
42 #include "utils/env.hpp"
43 #include "utils/format/macros.hpp"
44 #include "utils/fs/path.hpp"
45 #include "utils/optional.ipp"
46 #include "utils/process/operations.hpp"
47 #include "utils/process/status.hpp"
48 #include "utils/sanity.hpp"
49 
50 namespace config = utils::config;
51 namespace execenv = engine::execenv;
52 namespace fs = utils::fs;
53 namespace process = utils::process;
54 
55 using utils::optional;
56 
57 
58 /// Executes a test program's list operation.
59 ///
60 /// This method is intended to be called within a subprocess and is expected
61 /// to terminate execution either by exec(2)ing the test program or by
62 /// exiting with a failure.
63 void
64 engine::plain_interface::exec_list(
65     const model::test_program& /* test_program */,
66     const config::properties_map& /* vars */) const
67 {
68     ::_exit(EXIT_SUCCESS);
69 }
70 
71 
72 /// Computes the test cases list of a test program.
73 ///
74 /// \return A list of test cases.
75 model::test_cases_map
76 engine::plain_interface::parse_list(
77     const optional< process::status >& /* status */,
78     const fs::path& /* stdout_path */,
79     const fs::path& /* stderr_path */) const
80 {
81     return model::test_cases_map_builder().add("main").build();
82 }
83 
84 
85 /// Executes a test case of the test program.
86 ///
87 /// This method is intended to be called within a subprocess and is expected
88 /// to terminate execution either by exec(2)ing the test program or by
89 /// exiting with a failure.
90 ///
91 /// \param test_program The test program to execute.
92 /// \param test_case_name Name of the test case to invoke.
93 /// \param vars User-provided variables to pass to the test program.
94 void
95 engine::plain_interface::exec_test(
96     const model::test_program& test_program,
97     const std::string& test_case_name,
98     const config::properties_map& vars,
99     const fs::path& /* control_directory */) const
100 {
101     PRE(test_case_name == "main");
102 
103     for (config::properties_map::const_iterator iter = vars.begin();
104          iter != vars.end(); ++iter) {
105         utils::setenv(F("TEST_ENV_%s") % (*iter).first, (*iter).second);
106     }
107 
108     process::args_vector args;
109 
110     auto e = execenv::get(test_program, test_case_name);
111     e->init();
112     e->exec(args);
113     __builtin_unreachable();
114 }
115 
116 
117 /// Computes the result of a test case based on its termination status.
118 ///
119 /// \param status The termination status of the subprocess used to execute
120 ///     the exec_test() method or none if the test timed out.
121 ///
122 /// \return A test result.
123 model::test_result
124 engine::plain_interface::compute_result(
125     const optional< process::status >& status,
126     const fs::path& /* control_directory */,
127     const fs::path& /* stdout_path */,
128     const fs::path& /* stderr_path */) const
129 {
130     if (!status) {
131         return model::test_result(model::test_result_broken,
132                                   "Test case timed out");
133     }
134 
135     if (status.get().exited()) {
136         const int exitstatus = status.get().exitstatus();
137         if (exitstatus == EXIT_SUCCESS) {
138             return model::test_result(model::test_result_passed);
139         } else {
140             return model::test_result(
141                 model::test_result_failed,
142                 F("Returned non-success exit status %s") % exitstatus);
143         }
144     } else {
145         return model::test_result(
146             model::test_result_broken,
147             F("Received signal %s") % status.get().termsig());
148     }
149 }
150