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/atf_list.hpp"
30
31 #include <fstream>
32 #include <string>
33 #include <utility>
34
35 #include "engine/exceptions.hpp"
36 #include "model/metadata.hpp"
37 #include "model/test_case.hpp"
38 #include "utils/config/exceptions.hpp"
39 #include "utils/format/macros.hpp"
40
41 namespace config = utils::config;
42 namespace fs = utils::fs;
43
44
45 namespace {
46
47
48 /// Splits a property line of the form "name: word1 [... wordN]".
49 ///
50 /// \param line The line to parse.
51 ///
52 /// \return A (property_name, property_value) pair.
53 ///
54 /// \throw format_error If the value of line is invalid.
55 static std::pair< std::string, std::string >
split_prop_line(const std::string & line)56 split_prop_line(const std::string& line)
57 {
58 const std::string::size_type pos = line.find(": ");
59 if (pos == std::string::npos)
60 throw engine::format_error("Invalid property line; expecting line of "
61 "the form 'name: value'");
62 return std::make_pair(line.substr(0, pos), line.substr(pos + 2));
63 }
64
65
66 /// Parses a set of consecutive property lines.
67 ///
68 /// Processing stops when an empty line or the end of file is reached. None of
69 /// these conditions indicate errors.
70 ///
71 /// \param input The stream to read the lines from.
72 ///
73 /// \return The parsed property lines.
74 ///
75 /// throw format_error If the input stream has an invalid format.
76 static model::properties_map
parse_properties(std::istream & input)77 parse_properties(std::istream& input)
78 {
79 model::properties_map properties;
80
81 std::string line;
82 while (std::getline(input, line).good() && !line.empty()) {
83 const std::pair< std::string, std::string > property = split_prop_line(
84 line);
85 if (properties.find(property.first) != properties.end())
86 throw engine::format_error("Duplicate value for property " +
87 property.first);
88 properties.insert(property);
89 }
90
91 return properties;
92 }
93
94
95 } // anonymous namespace
96
97
98 /// Parses the metadata of an ATF test case.
99 ///
100 /// \param props The properties (name/value string pairs) as provided by the
101 /// ATF test program.
102 ///
103 /// \return A parsed metadata object.
104 ///
105 /// \throw engine::format_error If the syntax of any of the properties is
106 /// invalid.
107 model::metadata
parse_atf_metadata(const model::properties_map & props)108 engine::parse_atf_metadata(const model::properties_map& props)
109 {
110 model::metadata_builder mdbuilder;
111
112 try {
113 for (model::properties_map::const_iterator iter = props.begin();
114 iter != props.end(); iter++) {
115 const std::string& name = (*iter).first;
116 const std::string& value = (*iter).second;
117
118 if (name == "descr") {
119 mdbuilder.set_string("description", value);
120 } else if (name == "has.cleanup") {
121 mdbuilder.set_string("has_cleanup", value);
122 } else if (name == "require.arch") {
123 mdbuilder.set_string("allowed_architectures", value);
124 } else if (name == "execenv") {
125 mdbuilder.set_string("execenv", value);
126 } else if (name == "execenv.jail.params") {
127 mdbuilder.set_string("execenv_jail_params", value);
128 } else if (name == "is.exclusive") {
129 mdbuilder.set_string("is_exclusive", value);
130 } else if (name == "require.config") {
131 mdbuilder.set_string("required_configs", value);
132 } else if (name == "require.diskspace") {
133 mdbuilder.set_string("required_disk_space", value);
134 } else if (name == "require.files") {
135 mdbuilder.set_string("required_files", value);
136 } else if (name == "require.machine") {
137 mdbuilder.set_string("allowed_platforms", value);
138 } else if (name == "require.memory") {
139 mdbuilder.set_string("required_memory", value);
140 } else if (name == "require.progs") {
141 mdbuilder.set_string("required_programs", value);
142 } else if (name == "require.user") {
143 mdbuilder.set_string("required_user", value);
144 } else if (name == "timeout") {
145 mdbuilder.set_string("timeout", value);
146 } else if (name.length() > 2 && name.substr(0, 2) == "X-") {
147 mdbuilder.add_custom(name.substr(2), value);
148 } else {
149 throw engine::format_error(F("Unknown test case metadata "
150 "property '%s'") % name);
151 }
152 }
153 } catch (const config::error& e) {
154 throw engine::format_error(e.what());
155 }
156
157 return mdbuilder.build();
158 }
159
160
161 /// Parses the ATF list of test cases from an open stream.
162 ///
163 /// \param input The stream to read from.
164 ///
165 /// \return The collection of parsed test cases.
166 ///
167 /// \throw format_error If there is any problem in the input data.
168 model::test_cases_map
parse_atf_list(std::istream & input)169 engine::parse_atf_list(std::istream& input)
170 {
171 std::string line;
172
173 std::getline(input, line);
174 if (line != "Content-Type: application/X-atf-tp; version=\"1\""
175 || !input.good())
176 throw format_error(F("Invalid header for test case list; expecting "
177 "Content-Type for application/X-atf-tp version 1, "
178 "got '%s'") % line);
179
180 std::getline(input, line);
181 if (!line.empty() || !input.good())
182 throw format_error(F("Invalid header for test case list; expecting "
183 "a blank line, got '%s'") % line);
184
185 model::test_cases_map_builder test_cases_builder;
186 while (std::getline(input, line).good()) {
187 const std::pair< std::string, std::string > ident = split_prop_line(
188 line);
189 if (ident.first != "ident" or ident.second.empty())
190 throw format_error("Invalid test case definition; must be "
191 "preceeded by the identifier");
192
193 const model::properties_map props = parse_properties(input);
194 test_cases_builder.add(ident.second, parse_atf_metadata(props));
195 }
196 const model::test_cases_map test_cases = test_cases_builder.build();
197 if (test_cases.empty()) {
198 // The scheduler interface also checks for the presence of at least one
199 // test case. However, because the atf format itself requires one test
200 // case to be always present, we check for this condition here as well.
201 throw format_error("No test cases");
202 }
203 return test_cases;
204 }
205