1 // Copyright 2012 Google Inc.
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/metadata.hpp"
33 #include "engine/exceptions.hpp"
34 #include "utils/config/exceptions.hpp"
35 #include "utils/config/nodes.ipp"
36 #include "utils/config/tree.ipp"
37 #include "utils/datetime.hpp"
38 #include "utils/defs.hpp"
39 #include "utils/format/macros.hpp"
40 #include "utils/fs/exceptions.hpp"
41 #include "utils/fs/operations.hpp"
42 #include "utils/fs/path.hpp"
43 #include "utils/memory.hpp"
44 #include "utils/passwd.hpp"
45 #include "utils/sanity.hpp"
46 #include "utils/text/exceptions.hpp"
47 #include "utils/text/operations.hpp"
48 #include "utils/units.hpp"
50 namespace config
= utils::config
;
51 namespace datetime
= utils::datetime
;
52 namespace fs
= utils::fs
;
53 namespace passwd
= utils::passwd
;
54 namespace text
= utils::text
;
55 namespace units
= utils::units
;
58 /// The default timeout value for test cases that do not provide one.
59 /// TODO(jmmv): We should not be doing this; see issue 5 for details.
60 datetime::delta
engine::default_timeout(300, 0);
66 /// A leaf node that holds a bytes quantity.
67 class bytes_node
: public config::native_leaf_node
< units::bytes
> {
71 /// \return A dynamically-allocated node.
75 std::auto_ptr
< bytes_node
> new_node(new bytes_node());
76 new_node
->_value
= _value
;
77 return new_node
.release();
80 /// Pushes the node's value onto the Lua stack.
82 /// \param unused_state The Lua state onto which to push the value.
84 push_lua(lutok::state
& UTILS_UNUSED_PARAM(state
)) const
89 /// Sets the value of the node from an entry in the Lua stack.
91 /// \param unused_state The Lua state from which to get the value.
92 /// \param unused_index The stack index in which the value resides.
94 set_lua(lutok::state
& UTILS_UNUSED_PARAM(state
),
95 const int UTILS_UNUSED_PARAM(index
))
102 /// A leaf node that holds a time delta.
103 class delta_node
: public config::typed_leaf_node
< datetime::delta
> {
107 /// \return A dynamically-allocated node.
109 deep_copy(void) const
111 std::auto_ptr
< delta_node
> new_node(new delta_node());
112 new_node
->_value
= _value
;
113 return new_node
.release();
116 /// Sets the value of the node from a raw string representation.
118 /// \param raw_value The value to set the node to.
120 /// \throw value_error If the value is invalid.
122 set_string(const std::string
& raw_value
)
124 unsigned int seconds
;
126 seconds
= text::to_type
< unsigned int >(raw_value
);
127 } catch (const text::error
& e
) {
128 throw config::value_error(F("Invalid time delta %s") % raw_value
);
130 set(datetime::delta(seconds
, 0));
133 /// Converts the contents of the node to a string.
135 /// \pre The node must have a value.
137 /// \return A string representation of the value held by the node.
139 to_string(void) const
141 return F("%s") % value().seconds
;
144 /// Pushes the node's value onto the Lua stack.
146 /// \param unused_state The Lua state onto which to push the value.
148 push_lua(lutok::state
& UTILS_UNUSED_PARAM(state
)) const
153 /// Sets the value of the node from an entry in the Lua stack.
155 /// \param unused_state The Lua state from which to get the value.
156 /// \param unused_index The stack index in which the value resides.
158 set_lua(lutok::state
& UTILS_UNUSED_PARAM(state
),
159 const int UTILS_UNUSED_PARAM(index
))
166 /// A leaf node that holds a "required user" property.
168 /// This node is just a string, but it provides validation of the only allowed
170 class user_node
: public config::string_node
{
173 /// \return A dynamically-allocated node.
175 deep_copy(void) const
177 std::auto_ptr
< user_node
> new_node(new user_node());
178 new_node
->_value
= _value
;
179 return new_node
.release();
182 /// Checks a given user textual representation for validity.
184 /// \param user The value to validate.
186 /// \throw config::value_error If the value is not valid.
188 validate(const value_type
& user
) const
190 if (!user
.empty() && user
!= "root" && user
!= "unprivileged")
191 throw config::value_error("Invalid required user value");
196 /// A leaf node that holds a set of paths.
198 /// This node type is used to represent the value of the required files and
199 /// required programs, for example, and these do not allow relative paths. We
201 class paths_set_node
: public config::base_set_node
< fs::path
> {
204 /// \return A dynamically-allocated node.
206 deep_copy(void) const
208 std::auto_ptr
< paths_set_node
> new_node(new paths_set_node());
209 new_node
->_value
= _value
;
210 return new_node
.release();
213 /// Converts a single path to the native type.
215 /// \param raw_value The value to parse.
217 /// \return The parsed value.
219 /// \throw config::value_error If the value is invalid.
221 parse_one(const std::string
& raw_value
) const
224 return fs::path(raw_value
);
225 } catch (const fs::error
& e
) {
226 throw config::value_error(e
.what());
230 /// Checks a collection of paths for validity.
232 /// \param paths The value to validate.
234 /// \throw config::value_error If the value is not valid.
236 validate(const value_type
& paths
) const
238 for (value_type::const_iterator iter
= paths
.begin();
239 iter
!= paths
.end(); ++iter
) {
240 const fs::path
& path
= *iter
;
241 if (!path
.is_absolute() && path
.ncomponents() > 1)
242 throw config::value_error(F("Relative path '%s' not allowed") %
249 /// Initializes a tree to hold test case requirements.
251 /// \param [in,out] tree The tree to initialize.
253 init_tree(config::tree
& tree
)
255 tree
.define
< config::strings_set_node
>("allowed_architectures");
256 tree
.set
< config::strings_set_node
>("allowed_architectures",
257 engine::strings_set());
259 tree
.define
< config::strings_set_node
>("allowed_platforms");
260 tree
.set
< config::strings_set_node
>("allowed_platforms",
261 engine::strings_set());
263 tree
.define_dynamic("custom");
265 tree
.define
< config::string_node
>("description");
266 tree
.set
< config::string_node
>("description", "");
268 tree
.define
< config::bool_node
>("has_cleanup");
269 tree
.set
< config::bool_node
>("has_cleanup", false);
271 tree
.define
< config::strings_set_node
>("required_configs");
272 tree
.set
< config::strings_set_node
>("required_configs",
273 engine::strings_set());
275 tree
.define
< paths_set_node
>("required_files");
276 tree
.set
< paths_set_node
>("required_files", engine::paths_set());
278 tree
.define
< bytes_node
>("required_memory");
279 tree
.set
< bytes_node
>("required_memory", units::bytes(0));
281 tree
.define
< paths_set_node
>("required_programs");
282 tree
.set
< paths_set_node
>("required_programs", engine::paths_set());
284 tree
.define
< user_node
>("required_user");
285 tree
.set
< user_node
>("required_user", "");
287 tree
.define
< delta_node
>("timeout");
288 tree
.set
< delta_node
>("timeout", engine::default_timeout
);
292 /// Looks up a value in a tree with error rewriting.
294 /// \tparam NodeType The type of the node.
295 /// \param tree The tree in which to insert the value.
296 /// \param key The key to set.
298 /// \return A read-write reference to the value in the node.
300 /// \throw engine::error If the key is not known or if the value is not valid.
301 template< class NodeType
>
302 typename
NodeType::value_type
&
303 lookup_rw(config::tree
& tree
, const std::string
& key
)
306 return tree
.lookup_rw
< NodeType
>(key
);
307 } catch (const config::unknown_key_error
& e
) {
308 throw engine::error(F("Unknown metadata property %s") % key
);
309 } catch (const config::value_error
& e
) {
310 throw engine::error(F("Invalid value for metadata property %s: %s") %
316 /// Sets a value in a tree with error rewriting.
318 /// \tparam NodeType The type of the node.
319 /// \param tree The tree in which to insert the value.
320 /// \param key The key to set.
321 /// \param value The value to set the node to.
323 /// \throw engine::error If the key is not known or if the value is not valid.
324 template< class NodeType
>
326 set(config::tree
& tree
, const std::string
& key
,
327 const typename
NodeType::value_type
& value
)
330 tree
.set
< NodeType
>(key
, value
);
331 } catch (const config::unknown_key_error
& e
) {
332 throw engine::error(F("Unknown metadata property %s") % key
);
333 } catch (const config::value_error
& e
) {
334 throw engine::error(F("Invalid value for metadata property %s: %s") %
340 /// Checks if all required configuration variables are present.
342 /// \param required_configs Set of required variable names.
343 /// \param user_config Runtime user configuration.
344 /// \param test_suite_name Name of the test suite the test belongs to.
346 /// \return Empty if all variables are present or an error message otherwise.
348 check_required_configs(const engine::strings_set
& required_configs
,
349 const config::tree
& user_config
,
350 const std::string
& test_suite_name
)
352 for (engine::strings_set::const_iterator iter
= required_configs
.begin();
353 iter
!= required_configs
.end(); iter
++) {
354 std::string property
;
355 // TODO(jmmv): All this rewrite logic belongs in the ATF interface.
356 if ((*iter
) == "unprivileged-user" || (*iter
) == "unprivileged_user")
357 property
= "unprivileged_user";
359 property
= F("test_suites.%s.%s") % test_suite_name
% (*iter
);
361 if (!user_config
.is_set(property
))
362 return F("Required configuration property '%s' not defined") %
369 /// Checks if the allowed architectures match the current architecture.
371 /// \param allowed_architectures Set of allowed architectures.
372 /// \param user_config Runtime user configuration.
374 /// \return Empty if the current architecture is in the list or an error
375 /// message otherwise.
377 check_allowed_architectures(const engine::strings_set
& allowed_architectures
,
378 const config::tree
& user_config
)
380 if (!allowed_architectures
.empty()) {
381 const std::string architecture
=
382 user_config
.lookup
< config::string_node
>("architecture");
383 if (allowed_architectures
.find(architecture
) ==
384 allowed_architectures
.end())
385 return F("Current architecture '%s' not supported") % architecture
;
391 /// Checks if the allowed platforms match the current architecture.
393 /// \param allowed_platforms Set of allowed platforms.
394 /// \param user_config Runtime user configuration.
396 /// \return Empty if the current platform is in the list or an error message
399 check_allowed_platforms(const engine::strings_set
& allowed_platforms
,
400 const config::tree
& user_config
)
402 if (!allowed_platforms
.empty()) {
403 const std::string platform
=
404 user_config
.lookup
< config::string_node
>("platform");
405 if (allowed_platforms
.find(platform
) == allowed_platforms
.end())
406 return F("Current platform '%s' not supported") % platform
;
412 /// Checks if the current user matches the required user.
414 /// \param required_user Name of the required user category.
415 /// \param user_config Runtime user configuration.
417 /// \return Empty if the current user fits the required user characteristics or
418 /// an error message otherwise.
420 check_required_user(const std::string
& required_user
,
421 const config::tree
& user_config
)
423 if (!required_user
.empty()) {
424 const passwd::user user
= passwd::current_user();
425 if (required_user
== "root") {
427 return "Requires root privileges";
428 } else if (required_user
== "unprivileged") {
430 if (!user_config
.is_set("unprivileged_user"))
431 return "Requires an unprivileged user but the "
432 "unprivileged-user configuration variable is not "
435 UNREACHABLE_MSG("Value of require.user not properly validated");
441 /// Checks if all required files exist.
443 /// \param required_files Set of paths.
445 /// \return Empty if the required files all exist or an error message otherwise.
447 check_required_files(const engine::paths_set
& required_files
)
449 for (engine::paths_set::const_iterator iter
= required_files
.begin();
450 iter
!= required_files
.end(); iter
++) {
451 INV((*iter
).is_absolute());
452 if (!fs::exists(*iter
))
453 return F("Required file '%s' not found") % *iter
;
459 /// Checks if all required programs exist.
461 /// \param required_programs Set of paths.
463 /// \return Empty if the required programs all exist or an error message
466 check_required_programs(const engine::paths_set
& required_programs
)
468 for (engine::paths_set::const_iterator iter
= required_programs
.begin();
469 iter
!= required_programs
.end(); iter
++) {
470 if ((*iter
).is_absolute()) {
471 if (!fs::exists(*iter
))
472 return F("Required program '%s' not found") % *iter
;
474 if (!fs::find_in_path((*iter
).c_str()))
475 return F("Required program '%s' not found in PATH") % *iter
;
482 /// Checks if the current system has the specified amount of memory.
484 /// \param required_memory Amount of required physical memory, or zero if not
487 /// \return Empty if the current system has the required amount of memory or an
488 /// error message otherwise.
490 check_required_memory(const units::bytes
& required_memory
)
492 if (required_memory
> 0) {
493 const units::bytes physical_memory
= utils::physical_memory();
494 if (physical_memory
> 0 && physical_memory
< required_memory
)
495 return F("Requires %s bytes of physical memory but only %s "
497 required_memory
.format() % physical_memory
.format();
503 } // anonymous namespace
506 /// Internal implementation of the metadata class.
507 struct engine::metadata::impl
{
508 /// Metadata properties.
513 /// \param props_ Metadata properties of the test.
514 impl(const utils::config::tree
& props_
) :
519 /// Equality comparator.
521 /// \param other The other object to compare this one to.
523 /// \return True if this object and other are equal; false otherwise.
525 operator==(const impl
& other
) const
527 return props
== other
.props
;
534 /// \param props Metadata properties of the test.
535 engine::metadata::metadata(const utils::config::tree
& props
) :
536 _pimpl(new impl(props
))
542 engine::metadata::~metadata(void)
547 /// Returns the architectures allowed by the test.
549 /// \return Set of architectures, or empty if this does not apply.
550 const engine::strings_set
&
551 engine::metadata::allowed_architectures(void) const
553 return _pimpl
->props
.lookup
< config::strings_set_node
>(
554 "allowed_architectures");
558 /// Returns the platforms allowed by the test.
560 /// \return Set of platforms, or empty if this does not apply.
561 const engine::strings_set
&
562 engine::metadata::allowed_platforms(void) const
564 return _pimpl
->props
.lookup
< config::strings_set_node
>("allowed_platforms");
568 /// Returns all the user-defined metadata properties.
570 /// \return A key/value map of properties.
571 engine::properties_map
572 engine::metadata::custom(void) const
574 return _pimpl
->props
.all_properties("custom", true);
578 /// Returns the description of the test.
580 /// \return Textual description; may be empty.
582 engine::metadata::description(void) const
584 return _pimpl
->props
.lookup
< config::string_node
>("description");
588 /// Returns whether the test has a cleanup part or not.
590 /// \return True if there is a cleanup part; false otherwise.
592 engine::metadata::has_cleanup(void) const
594 return _pimpl
->props
.lookup
< config::bool_node
>("has_cleanup");
598 /// Returns the list of configuration variables needed by the test.
600 /// \return Set of configuration variables.
601 const engine::strings_set
&
602 engine::metadata::required_configs(void) const
604 return _pimpl
->props
.lookup
< config::strings_set_node
>("required_configs");
608 /// Returns the list of files needed by the test.
610 /// \return Set of paths.
611 const engine::paths_set
&
612 engine::metadata::required_files(void) const
614 return _pimpl
->props
.lookup
< paths_set_node
>("required_files");
618 /// Returns the amount of memory required by the test.
620 /// \return Number of bytes, or 0 if this does not apply.
622 engine::metadata::required_memory(void) const
624 return _pimpl
->props
.lookup
< bytes_node
>("required_memory");
628 /// Returns the list of programs needed by the test.
630 /// \return Set of paths.
631 const engine::paths_set
&
632 engine::metadata::required_programs(void) const
634 return _pimpl
->props
.lookup
< paths_set_node
>("required_programs");
638 /// Returns the user required by the test.
640 /// \return One of unprivileged, root or empty.
642 engine::metadata::required_user(void) const
644 return _pimpl
->props
.lookup
< user_node
>("required_user");
648 /// Returns the timeout of the test.
650 /// \return A time delta; should be compared to default_timeout to see if it has
652 const datetime::delta
&
653 engine::metadata::timeout(void) const
655 return _pimpl
->props
.lookup
< delta_node
>("timeout");
659 /// Externalizes the metadata to a set of key/value textual pairs.
661 /// \return A key/value representation of the metadata.
662 engine::properties_map
663 engine::metadata::to_properties(void) const
665 return _pimpl
->props
.all_properties();
669 /// Equality comparator.
671 /// \param other The other object to compare this one to.
673 /// \return True if this object and other are equal; false otherwise.
675 engine::metadata::operator==(const metadata
& other
) const
677 return _pimpl
== other
._pimpl
|| *_pimpl
== *other
._pimpl
;
681 /// Inequality comparator.
683 /// \param other The other object to compare this one to.
685 /// \return True if this object and other are different; false otherwise.
687 engine::metadata::operator!=(const metadata
& other
) const
689 return !(*this == other
);
693 /// Injects the object into a stream.
695 /// \param output The stream into which to inject the object.
696 /// \param object The object to format.
698 /// \return The output stream.
700 engine::operator<<(std::ostream
& output
, const metadata
& object
)
702 output
<< "metadata{";
705 const engine::properties_map props
= object
.to_properties();
706 for (engine::properties_map::const_iterator iter
= props
.begin();
707 iter
!= props
.end(); ++iter
) {
710 output
<< F("%s=%s") % (*iter
).first
%
711 text::quote((*iter
).second
, '\'');
720 /// Internal implementation of the metadata_builder class.
721 struct engine::metadata_builder::impl
{
722 /// Collection of requirements.
725 /// Whether we have created a metadata object or not.
736 impl(const engine::metadata
& base
) :
737 props(base
._pimpl
->props
.deep_copy()),
745 engine::metadata_builder::metadata_builder(void) :
752 engine::metadata_builder::metadata_builder(const engine::metadata
& base
) :
753 _pimpl(new impl(base
))
759 engine::metadata_builder::~metadata_builder(void)
764 /// Accumulates an additional allowed architecture.
766 /// \param arch The architecture.
768 /// \return A reference to this builder.
770 /// \throw engine::error If the value is invalid.
771 engine::metadata_builder
&
772 engine::metadata_builder::add_allowed_architecture(const std::string
& arch
)
774 lookup_rw
< config::strings_set_node
>(
775 _pimpl
->props
, "allowed_architectures").insert(arch
);
780 /// Accumulates an additional allowed platform.
782 /// \param platform The platform.
784 /// \return A reference to this builder.
786 /// \throw engine::error If the value is invalid.
787 engine::metadata_builder
&
788 engine::metadata_builder::add_allowed_platform(const std::string
& platform
)
790 lookup_rw
< config::strings_set_node
>(
791 _pimpl
->props
, "allowed_platforms").insert(platform
);
796 /// Accumulates a single user-defined property.
798 /// \param key Name of the property to define.
799 /// \param value Value of the property.
801 /// \return A reference to this builder.
803 /// \throw engine::error If the value is invalid.
804 engine::metadata_builder
&
805 engine::metadata_builder::add_custom(const std::string
& key
,
806 const std::string
& value
)
808 _pimpl
->props
.set_string(F("custom.%s") % key
, value
);
813 /// Accumulates an additional required configuration variable.
815 /// \param var The name of the configuration variable.
817 /// \return A reference to this builder.
819 /// \throw engine::error If the value is invalid.
820 engine::metadata_builder
&
821 engine::metadata_builder::add_required_config(const std::string
& var
)
823 lookup_rw
< config::strings_set_node
>(
824 _pimpl
->props
, "required_configs").insert(var
);
829 /// Accumulates an additional required file.
831 /// \param path The path to the file.
833 /// \return A reference to this builder.
835 /// \throw engine::error If the value is invalid.
836 engine::metadata_builder
&
837 engine::metadata_builder::add_required_file(const fs::path
& path
)
839 lookup_rw
< paths_set_node
>(_pimpl
->props
, "required_files").insert(path
);
844 /// Accumulates an additional required program.
846 /// \param path The path to the program.
848 /// \return A reference to this builder.
850 /// \throw engine::error If the value is invalid.
851 engine::metadata_builder
&
852 engine::metadata_builder::add_required_program(const fs::path
& path
)
854 lookup_rw
< paths_set_node
>(_pimpl
->props
,
855 "required_programs").insert(path
);
860 /// Sets the architectures allowed by the test.
862 /// \param as Set of architectures.
864 /// \return A reference to this builder.
866 /// \throw engine::error If the value is invalid.
867 engine::metadata_builder
&
868 engine::metadata_builder::set_allowed_architectures(const strings_set
& as
)
870 set
< config::strings_set_node
>(_pimpl
->props
, "allowed_architectures", as
);
875 /// Sets the platforms allowed by the test.
877 /// \return ps Set of platforms.
879 /// \return A reference to this builder.
881 /// \throw engine::error If the value is invalid.
882 engine::metadata_builder
&
883 engine::metadata_builder::set_allowed_platforms(const strings_set
& ps
)
885 set
< config::strings_set_node
>(_pimpl
->props
, "allowed_platforms", ps
);
890 /// Sets the user-defined properties.
892 /// \param props The custom properties to set.
894 /// \return A reference to this builder.
896 /// \throw engine::error If the value is invalid.
897 engine::metadata_builder
&
898 engine::metadata_builder::set_custom(const properties_map
& props
)
900 for (properties_map::const_iterator iter
= props
.begin();
901 iter
!= props
.end(); ++iter
)
902 _pimpl
->props
.set_string(F("custom.%s") % (*iter
).first
,
908 /// Sets the description of the test.
910 /// \param description Textual description of the test.
912 /// \return A reference to this builder.
914 /// \throw engine::error If the value is invalid.
915 engine::metadata_builder
&
916 engine::metadata_builder::set_description(const std::string
& description
)
918 set
< config::string_node
>(_pimpl
->props
, "description", description
);
923 /// Sets whether the test has a cleanup part or not.
925 /// \param cleanup True if the test has a cleanup part; false otherwise.
927 /// \return A reference to this builder.
929 /// \throw engine::error If the value is invalid.
930 engine::metadata_builder
&
931 engine::metadata_builder::set_has_cleanup(const bool cleanup
)
933 set
< config::bool_node
>(_pimpl
->props
, "has_cleanup", cleanup
);
938 /// Sets the list of configuration variables needed by the test.
940 /// \param vars Set of configuration variables.
942 /// \return A reference to this builder.
944 /// \throw engine::error If the value is invalid.
945 engine::metadata_builder
&
946 engine::metadata_builder::set_required_configs(const strings_set
& vars
)
948 set
< config::strings_set_node
>(_pimpl
->props
, "required_configs", vars
);
953 /// Sets the list of files needed by the test.
955 /// \param files Set of paths.
957 /// \return A reference to this builder.
959 /// \throw engine::error If the value is invalid.
960 engine::metadata_builder
&
961 engine::metadata_builder::set_required_files(const paths_set
& files
)
963 set
< paths_set_node
>(_pimpl
->props
, "required_files", files
);
968 /// Sets the amount of memory required by the test.
970 /// \param bytes Number of bytes.
972 /// \return A reference to this builder.
974 /// \throw engine::error If the value is invalid.
975 engine::metadata_builder
&
976 engine::metadata_builder::set_required_memory(const units::bytes
& bytes
)
978 set
< bytes_node
>(_pimpl
->props
, "required_memory", bytes
);
983 /// Sets the list of programs needed by the test.
985 /// \param progs Set of paths.
987 /// \return A reference to this builder.
989 /// \throw engine::error If the value is invalid.
990 engine::metadata_builder
&
991 engine::metadata_builder::set_required_programs(const paths_set
& progs
)
993 set
< paths_set_node
>(_pimpl
->props
, "required_programs", progs
);
998 /// Sets the user required by the test.
1000 /// \param user One of unprivileged, root or empty.
1002 /// \return A reference to this builder.
1004 /// \throw engine::error If the value is invalid.
1005 engine::metadata_builder
&
1006 engine::metadata_builder::set_required_user(const std::string
& user
)
1008 set
< user_node
>(_pimpl
->props
, "required_user", user
);
1013 /// Sets a metadata property by name from its textual representation.
1015 /// \param key The property to set.
1016 /// \param value The value to set the property to.
1018 /// \return A reference to this builder.
1020 /// \throw engine::error If the value is invalid or the key does not exist.
1021 engine::metadata_builder
&
1022 engine::metadata_builder::set_string(const std::string
& key
,
1023 const std::string
& value
)
1026 _pimpl
->props
.set_string(key
, value
);
1027 } catch (const config::unknown_key_error
& e
) {
1028 throw engine::format_error(F("Unknown metadata property %s") % key
);
1029 } catch (const config::value_error
& e
) {
1030 throw engine::format_error(
1031 F("Invalid value for metadata property %s: %s") % key
% e
.what());
1037 /// Sets the timeout of the test.
1039 /// \param timeout The timeout to set.
1041 /// \return A reference to this builder.
1043 /// \throw engine::error If the value is invalid.
1044 engine::metadata_builder
&
1045 engine::metadata_builder::set_timeout(const datetime::delta
& timeout
)
1047 set
< delta_node
>(_pimpl
->props
, "timeout", timeout
);
1052 /// Creates a new metadata object.
1054 /// \pre This has not yet been called. We only support calling this function
1055 /// once due to the way the internal tree works: we pass around references, not
1056 /// deep copies, so if we allowed a second build, we'd encourage reusing the
1057 /// same builder to construct different metadata objects, and this could have
1058 /// unintended consequences.
1060 /// \return The constructed metadata object.
1062 engine::metadata_builder::build(void) const
1064 PRE(!_pimpl
->built
);
1065 _pimpl
->built
= true;
1067 return metadata(_pimpl
->props
);
1071 /// Checks if all the requirements specified by the test case are met.
1073 /// \param md The test metadata.
1074 /// \param cfg The engine configuration.
1075 /// \param test_suite Name of the test suite the test belongs to.
1077 /// \return A string describing the reason for skipping the test, or empty if
1078 /// the test should be executed.
1080 engine::check_reqs(const engine::metadata
& md
, const config::tree
& cfg
,
1081 const std::string
& test_suite
)
1085 reason
= check_required_configs(md
.required_configs(), cfg
, test_suite
);
1086 if (!reason
.empty())
1089 reason
= check_allowed_architectures(md
.allowed_architectures(), cfg
);
1090 if (!reason
.empty())
1093 reason
= check_allowed_platforms(md
.allowed_platforms(), cfg
);
1094 if (!reason
.empty())
1097 reason
= check_required_user(md
.required_user(), cfg
);
1098 if (!reason
.empty())
1101 reason
= check_required_files(md
.required_files());
1102 if (!reason
.empty())
1105 reason
= check_required_programs(md
.required_programs());
1106 if (!reason
.empty())
1109 reason
= check_required_memory(md
.required_memory());
1110 if (!reason
.empty())
1113 INV(reason
.empty());