summaryrefslogtreecommitdiff
path: root/engine/atf_list.cpp
blob: a16b889c74f09ae1961b2a9ac517b5658ca20d94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015 The Kyua Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "engine/atf_list.hpp"

#include <fstream>
#include <string>
#include <utility>

#include "engine/exceptions.hpp"
#include "model/metadata.hpp"
#include "model/test_case.hpp"
#include "utils/config/exceptions.hpp"
#include "utils/format/macros.hpp"

namespace config = utils::config;
namespace fs = utils::fs;


namespace {


/// Splits a property line of the form "name: word1 [... wordN]".
///
/// \param line The line to parse.
///
/// \return A (property_name, property_value) pair.
///
/// \throw format_error If the value of line is invalid.
static std::pair< std::string, std::string >
split_prop_line(const std::string& line)
{
    const std::string::size_type pos = line.find(": ");
    if (pos == std::string::npos)
        throw engine::format_error("Invalid property line; expecting line of "
                                   "the form 'name: value'");
    return std::make_pair(line.substr(0, pos), line.substr(pos + 2));
}


/// Parses a set of consecutive property lines.
///
/// Processing stops when an empty line or the end of file is reached.  None of
/// these conditions indicate errors.
///
/// \param input The stream to read the lines from.
///
/// \return The parsed property lines.
///
/// throw format_error If the input stream has an invalid format.
static model::properties_map
parse_properties(std::istream& input)
{
    model::properties_map properties;

    std::string line;
    while (std::getline(input, line).good() && !line.empty()) {
        const std::pair< std::string, std::string > property = split_prop_line(
            line);
        if (properties.find(property.first) != properties.end())
            throw engine::format_error("Duplicate value for property " +
                                       property.first);
        properties.insert(property);
    }

    return properties;
}


}  // anonymous namespace


/// Parses the metadata of an ATF test case.
///
/// \param props The properties (name/value string pairs) as provided by the
///     ATF test program.
///
/// \return A parsed metadata object.
///
/// \throw engine::format_error If the syntax of any of the properties is
///     invalid.
model::metadata
engine::parse_atf_metadata(const model::properties_map& props)
{
    model::metadata_builder mdbuilder;

    try {
        for (model::properties_map::const_iterator iter = props.begin();
             iter != props.end(); iter++) {
            const std::string& name = (*iter).first;
            const std::string& value = (*iter).second;

            if (name == "descr") {
                mdbuilder.set_string("description", value);
            } else if (name == "has.cleanup") {
                mdbuilder.set_string("has_cleanup", value);
            } else if (name == "require.arch") {
                mdbuilder.set_string("allowed_architectures", value);
            } else if (name == "require.config") {
                mdbuilder.set_string("required_configs", value);
            } else if (name == "require.files") {
                mdbuilder.set_string("required_files", value);
            } else if (name == "require.machine") {
                mdbuilder.set_string("allowed_platforms", value);
            } else if (name == "require.memory") {
                mdbuilder.set_string("required_memory", value);
            } else if (name == "require.progs") {
                mdbuilder.set_string("required_programs", value);
            } else if (name == "require.user") {
                mdbuilder.set_string("required_user", value);
            } else if (name == "timeout") {
                mdbuilder.set_string("timeout", value);
            } else if (name.length() > 2 && name.substr(0, 2) == "X-") {
                mdbuilder.add_custom(name.substr(2), value);
            } else {
                throw engine::format_error(F("Unknown test case metadata "
                                             "property '%s'") % name);
            }
        }
    } catch (const config::error& e) {
        throw engine::format_error(e.what());
    }

    return mdbuilder.build();
}


/// Parses the ATF list of test cases from an open stream.
///
/// \param input The stream to read from.
///
/// \return The collection of parsed test cases.
///
/// \throw format_error If there is any problem in the input data.
model::test_cases_map
engine::parse_atf_list(std::istream& input)
{
    std::string line;

    std::getline(input, line);
    if (line != "Content-Type: application/X-atf-tp; version=\"1\""
        || !input.good())
        throw format_error(F("Invalid header for test case list; expecting "
                             "Content-Type for application/X-atf-tp version 1, "
                             "got '%s'") % line);

    std::getline(input, line);
    if (!line.empty() || !input.good())
        throw format_error(F("Invalid header for test case list; expecting "
                             "a blank line, got '%s'") % line);

    model::test_cases_map_builder test_cases_builder;
    while (std::getline(input, line).good()) {
        const std::pair< std::string, std::string > ident = split_prop_line(
            line);
        if (ident.first != "ident" or ident.second.empty())
            throw format_error("Invalid test case definition; must be "
                               "preceeded by the identifier");

        const model::properties_map props = parse_properties(input);
        test_cases_builder.add(ident.second, parse_atf_metadata(props));
    }
    const model::test_cases_map test_cases = test_cases_builder.build();
    if (test_cases.empty()) {
        // The scheduler interface also checks for the presence of at least one
        // test case.  However, because the atf format itself requires one test
        // case to be always present, we check for this condition here as well.
        throw format_error("No test cases");
    }
    return test_cases;
}