1 // Copyright 2015 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "engine/atf_list.hpp"
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"
41 namespace config = utils::config;
42 namespace fs = utils::fs;
48 /// Splits a property line of the form "name: word1 [... wordN]".
50 /// \param line The line to parse.
52 /// \return A (property_name, property_value) pair.
54 /// \throw format_error If the value of line is invalid.
55 static std::pair< std::string, std::string >
56 split_prop_line(const std::string& line)
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));
66 /// Parses a set of consecutive property lines.
68 /// Processing stops when an empty line or the end of file is reached. None of
69 /// these conditions indicate errors.
71 /// \param input The stream to read the lines from.
73 /// \return The parsed property lines.
75 /// throw format_error If the input stream has an invalid format.
76 static model::properties_map
77 parse_properties(std::istream& input)
79 model::properties_map properties;
82 while (std::getline(input, line).good() && !line.empty()) {
83 const std::pair< std::string, std::string > property = split_prop_line(
85 if (properties.find(property.first) != properties.end())
86 throw engine::format_error("Duplicate value for property " +
88 properties.insert(property);
95 } // anonymous namespace
98 /// Parses the metadata of an ATF test case.
100 /// \param props The properties (name/value string pairs) as provided by the
101 /// ATF test program.
103 /// \return A parsed metadata object.
105 /// \throw engine::format_error If the syntax of any of the properties is
108 engine::parse_atf_metadata(const model::properties_map& props)
110 model::metadata_builder mdbuilder;
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;
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 == "require.config") {
125 mdbuilder.set_string("required_configs", value);
126 } else if (name == "require.files") {
127 mdbuilder.set_string("required_files", value);
128 } else if (name == "require.machine") {
129 mdbuilder.set_string("allowed_platforms", value);
130 } else if (name == "require.memory") {
131 mdbuilder.set_string("required_memory", value);
132 } else if (name == "require.progs") {
133 mdbuilder.set_string("required_programs", value);
134 } else if (name == "require.user") {
135 mdbuilder.set_string("required_user", value);
136 } else if (name == "timeout") {
137 mdbuilder.set_string("timeout", value);
138 } else if (name.length() > 2 && name.substr(0, 2) == "X-") {
139 mdbuilder.add_custom(name.substr(2), value);
141 throw engine::format_error(F("Unknown test case metadata "
142 "property '%s'") % name);
145 } catch (const config::error& e) {
146 throw engine::format_error(e.what());
149 return mdbuilder.build();
153 /// Parses the ATF list of test cases from an open stream.
155 /// \param input The stream to read from.
157 /// \return The collection of parsed test cases.
159 /// \throw format_error If there is any problem in the input data.
160 model::test_cases_map
161 engine::parse_atf_list(std::istream& input)
165 std::getline(input, line);
166 if (line != "Content-Type: application/X-atf-tp; version=\"1\""
168 throw format_error(F("Invalid header for test case list; expecting "
169 "Content-Type for application/X-atf-tp version 1, "
172 std::getline(input, line);
173 if (!line.empty() || !input.good())
174 throw format_error(F("Invalid header for test case list; expecting "
175 "a blank line, got '%s'") % line);
177 model::test_cases_map_builder test_cases_builder;
178 while (std::getline(input, line).good()) {
179 const std::pair< std::string, std::string > ident = split_prop_line(
181 if (ident.first != "ident" or ident.second.empty())
182 throw format_error("Invalid test case definition; must be "
183 "preceeded by the identifier");
185 const model::properties_map props = parse_properties(input);
186 test_cases_builder.add(ident.second, parse_atf_metadata(props));
188 const model::test_cases_map test_cases = test_cases_builder.build();
189 if (test_cases.empty()) {
190 // The scheduler interface also checks for the presence of at least one
191 // test case. However, because the atf format itself requires one test
192 // case to be always present, we check for this condition here as well.
193 throw format_error("No test cases");