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 == "require.config") {
129 mdbuilder.set_string("required_configs", value);
130 } else if (name == "require.files") {
131 mdbuilder.set_string("required_files", value);
132 } else if (name == "require.machine") {
133 mdbuilder.set_string("allowed_platforms", value);
134 } else if (name == "require.memory") {
135 mdbuilder.set_string("required_memory", value);
136 } else if (name == "require.progs") {
137 mdbuilder.set_string("required_programs", value);
138 } else if (name == "require.user") {
139 mdbuilder.set_string("required_user", value);
140 } else if (name == "timeout") {
141 mdbuilder.set_string("timeout", value);
142 } else if (name.length() > 2 && name.substr(0, 2) == "X-") {
143 mdbuilder.add_custom(name.substr(2), value);
144 } else {
145 throw engine::format_error(F("Unknown test case metadata "
146 "property '%s'") % name);
147 }
148 }
149 } catch (const config::error& e) {
150 throw engine::format_error(e.what());
151 }
152
153 return mdbuilder.build();
154 }
155
156
157 /// Parses the ATF list of test cases from an open stream.
158 ///
159 /// \param input The stream to read from.
160 ///
161 /// \return The collection of parsed test cases.
162 ///
163 /// \throw format_error If there is any problem in the input data.
164 model::test_cases_map
parse_atf_list(std::istream & input)165 engine::parse_atf_list(std::istream& input)
166 {
167 std::string line;
168
169 std::getline(input, line);
170 if (line != "Content-Type: application/X-atf-tp; version=\"1\""
171 || !input.good())
172 throw format_error(F("Invalid header for test case list; expecting "
173 "Content-Type for application/X-atf-tp version 1, "
174 "got '%s'") % line);
175
176 std::getline(input, line);
177 if (!line.empty() || !input.good())
178 throw format_error(F("Invalid header for test case list; expecting "
179 "a blank line, got '%s'") % line);
180
181 model::test_cases_map_builder test_cases_builder;
182 while (std::getline(input, line).good()) {
183 const std::pair< std::string, std::string > ident = split_prop_line(
184 line);
185 if (ident.first != "ident" or ident.second.empty())
186 throw format_error("Invalid test case definition; must be "
187 "preceeded by the identifier");
188
189 const model::properties_map props = parse_properties(input);
190 test_cases_builder.add(ident.second, parse_atf_metadata(props));
191 }
192 const model::test_cases_map test_cases = test_cases_builder.build();
193 if (test_cases.empty()) {
194 // The scheduler interface also checks for the presence of at least one
195 // test case. However, because the atf format itself requires one test
196 // case to be always present, we check for this condition here as well.
197 throw format_error("No test cases");
198 }
199 return test_cases;
200 }
201