1 // Copyright 2010 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/config.hpp"
31 #if defined(HAVE_CONFIG_H)
38 #include <atf-c++.hpp>
40 #include "engine/exceptions.hpp"
41 #include "utils/cmdline/exceptions.hpp"
42 #include "utils/cmdline/parser.hpp"
43 #include "utils/config/tree.ipp"
44 #include "utils/passwd.hpp"
46 namespace config = utils::config;
47 namespace fs = utils::fs;
48 namespace passwd = utils::passwd;
51 using utils::optional;
57 /// Replaces the system user database with a fake one for testing purposes.
61 std::vector< passwd::user > users;
62 users.push_back(passwd::user("user1", 100, 150));
63 users.push_back(passwd::user("user2", 200, 250));
64 passwd::set_mock_users_for_testing(users);
68 /// Checks that the default values of a config object match our expectations.
70 /// This fails the test case if any field of the input config object is not
73 /// \param config The configuration to validate.
75 validate_defaults(const config::tree& config)
79 config.lookup< config::string_node >("architecture"));
83 config.lookup< config::positive_int_node >("parallelism"));
87 config.lookup< config::string_node >("platform"));
89 ATF_REQUIRE(!config.is_set("unprivileged_user"));
91 ATF_REQUIRE(config.all_properties("test_suites").empty());
95 } // anonymous namespace
98 ATF_TEST_CASE_WITHOUT_HEAD(config__defaults);
99 ATF_TEST_CASE_BODY(config__defaults)
101 const config::tree user_config = engine::default_config();
102 validate_defaults(user_config);
106 ATF_TEST_CASE_WITHOUT_HEAD(config__set__parallelism);
107 ATF_TEST_CASE_BODY(config__set__parallelism)
109 config::tree user_config = engine::default_config();
110 user_config.set_string("parallelism", "8");
111 ATF_REQUIRE_THROW_RE(
112 config::error, "parallelism.*Must be a positive integer",
113 user_config.set_string("parallelism", "0"));
114 ATF_REQUIRE_THROW_RE(
115 config::error, "parallelism.*Must be a positive integer",
116 user_config.set_string("parallelism", "-1"));
120 ATF_TEST_CASE_WITHOUT_HEAD(config__load__defaults);
121 ATF_TEST_CASE_BODY(config__load__defaults)
123 atf::utils::create_file("config", "syntax(2)\n");
125 const config::tree user_config = engine::load_config(fs::path("config"));
126 validate_defaults(user_config);
130 ATF_TEST_CASE_WITHOUT_HEAD(config__load__overrides);
131 ATF_TEST_CASE_BODY(config__load__overrides)
135 atf::utils::create_file(
138 "architecture = 'test-architecture'\n"
140 "platform = 'test-platform'\n"
141 "unprivileged_user = 'user2'\n"
142 "test_suites.mysuite.myvar = 'myvalue'\n");
144 const config::tree user_config = engine::load_config(fs::path("config"));
146 ATF_REQUIRE_EQ("test-architecture",
147 user_config.lookup_string("architecture"));
149 user_config.lookup_string("parallelism"));
150 ATF_REQUIRE_EQ("test-platform",
151 user_config.lookup_string("platform"));
153 const passwd::user& user = user_config.lookup< engine::user_node >(
154 "unprivileged_user");
155 ATF_REQUIRE_EQ("user2", user.name);
156 ATF_REQUIRE_EQ(200, user.uid);
158 config::properties_map exp_test_suites;
159 exp_test_suites["test_suites.mysuite.myvar"] = "myvalue";
161 ATF_REQUIRE(exp_test_suites == user_config.all_properties("test_suites"));
165 ATF_TEST_CASE_WITHOUT_HEAD(config__load__lua_error);
166 ATF_TEST_CASE_BODY(config__load__lua_error)
168 atf::utils::create_file("config", "this syntax is invalid\n");
170 ATF_REQUIRE_THROW(engine::load_error, engine::load_config(
171 fs::path("config")));
175 ATF_TEST_CASE_WITHOUT_HEAD(config__load__bad_syntax__version);
176 ATF_TEST_CASE_BODY(config__load__bad_syntax__version)
178 atf::utils::create_file("config", "syntax(123)\n");
180 ATF_REQUIRE_THROW_RE(engine::load_error,
181 "Unsupported config version 123",
182 engine::load_config(fs::path("config")));
186 ATF_TEST_CASE_WITHOUT_HEAD(config__load__missing_file);
187 ATF_TEST_CASE_BODY(config__load__missing_file)
189 ATF_REQUIRE_THROW_RE(engine::load_error, "Load of 'missing' failed",
190 engine::load_config(fs::path("missing")));
194 ATF_INIT_TEST_CASES(tcs)
196 ATF_ADD_TEST_CASE(tcs, config__defaults);
197 ATF_ADD_TEST_CASE(tcs, config__set__parallelism);
198 ATF_ADD_TEST_CASE(tcs, config__load__defaults);
199 ATF_ADD_TEST_CASE(tcs, config__load__overrides);
200 ATF_ADD_TEST_CASE(tcs, config__load__lua_error);
201 ATF_ADD_TEST_CASE(tcs, config__load__bad_syntax__version);
202 ATF_ADD_TEST_CASE(tcs, config__load__missing_file);