1 | /* Copyright © 2006-2008 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #ifndef TEST_SBUILD_CHROOT_H |
---|
20 | #define TEST_SBUILD_CHROOT_H |
---|
21 | |
---|
22 | #include <sbuild/sbuild-config.h> |
---|
23 | #include <sbuild/sbuild-chroot.h> |
---|
24 | #include <sbuild/sbuild-chroot-facet-personality.h> |
---|
25 | #include <sbuild/sbuild-chroot-facet-session.h> |
---|
26 | #include <sbuild/sbuild-chroot-facet-session-clonable.h> |
---|
27 | #include <sbuild/sbuild-chroot-facet-source.h> |
---|
28 | #include <sbuild/sbuild-chroot-facet-source-clonable.h> |
---|
29 | #include <sbuild/sbuild-chroot-facet-union.h> |
---|
30 | #include <sbuild/sbuild-i18n.h> |
---|
31 | #include <sbuild/sbuild-util.h> |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <iostream> |
---|
35 | #include <set> |
---|
36 | |
---|
37 | #include <cppunit/extensions/HelperMacros.h> |
---|
38 | |
---|
39 | using namespace CppUnit; |
---|
40 | using sbuild::_; |
---|
41 | |
---|
42 | template <class T> |
---|
43 | class test_chroot_base : public TestFixture |
---|
44 | { |
---|
45 | protected: |
---|
46 | sbuild::chroot::ptr chroot; |
---|
47 | sbuild::chroot::ptr session; |
---|
48 | sbuild::chroot::ptr source; |
---|
49 | sbuild::chroot::ptr chroot_union; |
---|
50 | sbuild::chroot::ptr session_union; |
---|
51 | sbuild::chroot::ptr source_union; |
---|
52 | std::string abs_testdata_dir; |
---|
53 | |
---|
54 | public: |
---|
55 | test_chroot_base(): |
---|
56 | TestFixture(), |
---|
57 | chroot(), |
---|
58 | abs_testdata_dir() |
---|
59 | { |
---|
60 | abs_testdata_dir = sbuild::getcwd(); |
---|
61 | abs_testdata_dir.append("/" TESTDATADIR); |
---|
62 | } |
---|
63 | |
---|
64 | virtual ~test_chroot_base() |
---|
65 | {} |
---|
66 | |
---|
67 | void setUp() |
---|
68 | { |
---|
69 | // Create new chroot |
---|
70 | this->chroot = sbuild::chroot::ptr(new T); |
---|
71 | CPPUNIT_ASSERT(this->chroot); |
---|
72 | CPPUNIT_ASSERT(this->chroot->get_active() == false); |
---|
73 | |
---|
74 | setup_chroot_props(this->chroot); |
---|
75 | |
---|
76 | CPPUNIT_ASSERT(this->chroot->get_name().length()); |
---|
77 | |
---|
78 | // Create new source chroot. |
---|
79 | sbuild::chroot_facet_session_clonable::const_ptr psess |
---|
80 | (this->chroot->template get_facet<sbuild::chroot_facet_session_clonable>()); |
---|
81 | if (psess) |
---|
82 | { |
---|
83 | this->session = this->chroot->clone_session("test-session-name", |
---|
84 | "user1", |
---|
85 | false); |
---|
86 | if (this->session) |
---|
87 | { |
---|
88 | CPPUNIT_ASSERT(this->session->get_active() == true); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | sbuild::chroot_facet_source_clonable::const_ptr psrc |
---|
93 | (this->chroot-> |
---|
94 | template get_facet<sbuild::chroot_facet_source_clonable>()); |
---|
95 | if (psrc) |
---|
96 | this->source = this->chroot->clone_source(); |
---|
97 | if (this->source) |
---|
98 | { |
---|
99 | sbuild::chroot_facet_source_clonable::const_ptr pfsrcc |
---|
100 | (this->source-> |
---|
101 | template get_facet<sbuild::chroot_facet_source_clonable>()); |
---|
102 | CPPUNIT_ASSERT(!pfsrcc); |
---|
103 | sbuild::chroot_facet_source::const_ptr pfsrc |
---|
104 | (this->source-> |
---|
105 | template get_facet<sbuild::chroot_facet_source>()); |
---|
106 | CPPUNIT_ASSERT(pfsrc); |
---|
107 | } |
---|
108 | |
---|
109 | this->chroot_union = sbuild::chroot::ptr(new T); |
---|
110 | sbuild::chroot_facet_union::ptr un = |
---|
111 | this->chroot_union->template get_facet<sbuild::chroot_facet_union>(); |
---|
112 | if (!un) |
---|
113 | { |
---|
114 | this->chroot_union.reset(); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | un->set_union_type("aufs"); |
---|
119 | |
---|
120 | setup_chroot_props(this->chroot_union); |
---|
121 | CPPUNIT_ASSERT(this->chroot_union->get_active() == false); |
---|
122 | CPPUNIT_ASSERT(this->chroot_union->get_name().length()); |
---|
123 | |
---|
124 | un->set_union_overlay_directory("/overlay"); |
---|
125 | un->set_union_underlay_directory("/underlay"); |
---|
126 | un->set_union_mount_options("union-mount-options"); |
---|
127 | |
---|
128 | this->session_union = |
---|
129 | this->chroot_union->clone_session("test-union-session-name", |
---|
130 | "user1", |
---|
131 | false); |
---|
132 | this->source_union = chroot_union->clone_source(); |
---|
133 | |
---|
134 | CPPUNIT_ASSERT(this->session_union); |
---|
135 | CPPUNIT_ASSERT(this->session_union->get_active() == true); |
---|
136 | CPPUNIT_ASSERT(this->source_union); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | virtual void setup_chroot_props (sbuild::chroot::ptr& chroot) |
---|
141 | { |
---|
142 | chroot->set_name("test-name"); |
---|
143 | chroot->set_description("test-description"); |
---|
144 | chroot->set_aliases(sbuild::split_string("test-alias-1,test-alias-2", ",")); |
---|
145 | chroot->set_description("test-description"); |
---|
146 | chroot->set_mount_location("/mnt/mount-location"); |
---|
147 | chroot->set_environment_filter(SBUILD_DEFAULT_ENVIRONMENT_FILTER); |
---|
148 | chroot->set_users(sbuild::split_string("user1,user2", ",")); |
---|
149 | chroot->set_root_users(sbuild::split_string("user3,user4", ",")); |
---|
150 | chroot->set_groups(sbuild::split_string("group1,group2", ",")); |
---|
151 | chroot->set_root_groups(sbuild::split_string("group3,group4", ",")); |
---|
152 | |
---|
153 | sbuild::chroot_facet_personality::ptr pfac |
---|
154 | (chroot->get_facet<sbuild::chroot_facet_personality>()); |
---|
155 | if (pfac) |
---|
156 | pfac->set_persona(sbuild::personality("undefined")); |
---|
157 | chroot->set_priority(3); |
---|
158 | |
---|
159 | sbuild::chroot_facet_source_clonable::ptr usrc |
---|
160 | (chroot->template get_facet<sbuild::chroot_facet_source_clonable>()); |
---|
161 | if (usrc) |
---|
162 | { |
---|
163 | usrc->set_source_users(sbuild::split_string("suser1,suser2", ",")); |
---|
164 | usrc->set_source_root_users(sbuild::split_string("suser3,suser4", ",")); |
---|
165 | usrc->set_source_groups(sbuild::split_string("sgroup1,sgroup2", ",")); |
---|
166 | usrc->set_source_root_groups(sbuild::split_string("sgroup3,sgroup4", ",")); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void tearDown() |
---|
171 | { |
---|
172 | this->chroot = sbuild::chroot::ptr(); |
---|
173 | } |
---|
174 | |
---|
175 | void setup_env_chroot (sbuild::environment& env) |
---|
176 | { |
---|
177 | env.add("CHROOT_NAME", "test-name"); |
---|
178 | env.add("CHROOT_DESCRIPTION", "test-description"); |
---|
179 | env.add("CHROOT_SCRIPT_CONFIG", sbuild::normalname(std::string(PACKAGE_SYSCONF_DIR) + "/script-defaults")); |
---|
180 | } |
---|
181 | |
---|
182 | void setup_keyfile_chroot (sbuild::keyfile& keyfile, |
---|
183 | std::string const& group) |
---|
184 | { |
---|
185 | keyfile.set_value(group, "description", "test-description"); |
---|
186 | keyfile.set_value(group, "priority", "3"); |
---|
187 | keyfile.set_value(group, "aliases", "test-alias-1,test-alias-2"); |
---|
188 | keyfile.set_value(group, "users", "user1,user2"); |
---|
189 | keyfile.set_value(group, "root-users", "user3,user4"); |
---|
190 | keyfile.set_value(group, "groups", "group1,group2"); |
---|
191 | keyfile.set_value(group, "root-groups", "group3,group4"); |
---|
192 | keyfile.set_value(group, "environment-filter", |
---|
193 | SBUILD_DEFAULT_ENVIRONMENT_FILTER); |
---|
194 | keyfile.set_value(group, "personality", "undefined"); |
---|
195 | keyfile.set_value(group, "command-prefix", ""); |
---|
196 | keyfile.set_value(group, "script-config", "script-defaults"); |
---|
197 | } |
---|
198 | |
---|
199 | void setup_keyfile_session (sbuild::keyfile& keyfile, |
---|
200 | std::string const& group) |
---|
201 | { |
---|
202 | setup_keyfile_chroot(keyfile, group); |
---|
203 | keyfile.set_value(group, "users", "user1"); |
---|
204 | keyfile.set_value(group, "root-users", ""); |
---|
205 | keyfile.set_value(group, "groups", ""); |
---|
206 | keyfile.set_value(group, "root-groups", ""); |
---|
207 | } |
---|
208 | |
---|
209 | void setup_keyfile_union_unconfigured (sbuild::keyfile& keyfile, |
---|
210 | std::string const& group) |
---|
211 | { |
---|
212 | #ifdef SBUILD_FEATURE_UNION |
---|
213 | keyfile.set_value(group, "union-type", "none"); |
---|
214 | #endif // SBUILD_FEATURE_UNION |
---|
215 | } |
---|
216 | |
---|
217 | void setup_keyfile_union_configured (sbuild::keyfile& keyfile, |
---|
218 | std::string const& group) |
---|
219 | { |
---|
220 | #ifdef SBUILD_FEATURE_UNION |
---|
221 | keyfile.set_value(group, "union-type", "aufs"); |
---|
222 | keyfile.set_value(group, "union-mount-options", "union-mount-options"); |
---|
223 | keyfile.set_value(group, "union-overlay-directory", "/overlay"); |
---|
224 | keyfile.set_value(group, "union-underlay-directory", "/underlay"); |
---|
225 | #endif // SBUILD_FEATURE_UNION |
---|
226 | } |
---|
227 | |
---|
228 | void setup_keyfile_union_session (sbuild::keyfile& keyfile, |
---|
229 | std::string const& group) |
---|
230 | { |
---|
231 | #ifdef SBUILD_FEATURE_UNION |
---|
232 | keyfile.set_value(group, "union-type", "aufs"); |
---|
233 | keyfile.set_value(group, "union-mount-options", "union-mount-options"); |
---|
234 | keyfile.set_value(group, "union-overlay-directory", "/overlay/test-union-session-name"); |
---|
235 | keyfile.set_value(group, "union-underlay-directory", "/underlay/test-union-session-name"); |
---|
236 | #endif // SBUILD_FEATURE_UNION |
---|
237 | } |
---|
238 | |
---|
239 | void setup_keyfile_session_clone (sbuild::keyfile& keyfile, |
---|
240 | std::string const& group) |
---|
241 | { |
---|
242 | keyfile.set_value(group, "description", chroot->get_description() + ' ' + _("(session chroot)")); |
---|
243 | keyfile.set_value(group, "aliases", ""); |
---|
244 | } |
---|
245 | |
---|
246 | void setup_keyfile_source (sbuild::keyfile& keyfile, |
---|
247 | std::string const& group) |
---|
248 | { |
---|
249 | keyfile.set_value(group, "source-users", "suser1,suser2"); |
---|
250 | keyfile.set_value(group, "source-root-users", "suser3,suser4"); |
---|
251 | keyfile.set_value(group, "source-groups", "sgroup1,sgroup2"); |
---|
252 | keyfile.set_value(group, "source-root-groups", "sgroup3,sgroup4"); |
---|
253 | } |
---|
254 | |
---|
255 | void setup_keyfile_source_clone (sbuild::keyfile& keyfile, |
---|
256 | std::string const& group) |
---|
257 | { |
---|
258 | keyfile.set_value(group, "description", chroot->get_description() + ' ' + _("(source chroot)")); |
---|
259 | keyfile.set_value(group, "users", "suser1,suser2"); |
---|
260 | keyfile.set_value(group, "root-users", "suser3,suser4"); |
---|
261 | keyfile.set_value(group, "groups", "sgroup1,sgroup2"); |
---|
262 | keyfile.set_value(group, "root-groups", "sgroup3,sgroup4"); |
---|
263 | keyfile.set_value(group, "aliases", "test-alias-1-source,test-alias-2-source"); |
---|
264 | } |
---|
265 | |
---|
266 | void setup_keyfile_source_clone (sbuild::keyfile& keyfile) |
---|
267 | { |
---|
268 | setup_keyfile_source_clone(keyfile, chroot->get_name()); |
---|
269 | } |
---|
270 | |
---|
271 | void test_setup_env(const sbuild::environment& observed_environment, |
---|
272 | const sbuild::environment& expected_environment) |
---|
273 | { |
---|
274 | CPPUNIT_ASSERT(observed_environment.size() != 0); |
---|
275 | CPPUNIT_ASSERT(expected_environment.size() != 0); |
---|
276 | |
---|
277 | std::set<std::string> expected; |
---|
278 | for (sbuild::environment::const_iterator pos = expected_environment.begin(); |
---|
279 | pos != expected_environment.end(); |
---|
280 | ++pos) |
---|
281 | expected.insert(pos->first); |
---|
282 | |
---|
283 | std::set<std::string> found; |
---|
284 | for (sbuild::environment::const_iterator pos = observed_environment.begin(); |
---|
285 | pos != observed_environment.end(); |
---|
286 | ++pos) |
---|
287 | found.insert(pos->first); |
---|
288 | |
---|
289 | sbuild::string_list missing; |
---|
290 | set_difference(expected.begin(), expected.end(), |
---|
291 | found.begin(), found.end(), |
---|
292 | std::back_inserter(missing)); |
---|
293 | if (!missing.empty()) |
---|
294 | { |
---|
295 | std::string value; |
---|
296 | for (sbuild::string_list::const_iterator pos = missing.begin(); |
---|
297 | pos != missing.end(); |
---|
298 | ++pos) |
---|
299 | { |
---|
300 | expected_environment.get(*pos, value); |
---|
301 | std::cout << "Missing environment: " |
---|
302 | << *pos << "=" << value << std::endl; |
---|
303 | } |
---|
304 | } |
---|
305 | CPPUNIT_ASSERT(missing.empty()); |
---|
306 | |
---|
307 | sbuild::string_list extra; |
---|
308 | set_difference(found.begin(), found.end(), |
---|
309 | expected.begin(), expected.end(), |
---|
310 | std::back_inserter(extra)); |
---|
311 | if (!extra.empty()) |
---|
312 | { |
---|
313 | std::string value; |
---|
314 | for (sbuild::string_list::const_iterator pos = extra.begin(); |
---|
315 | pos != extra.end(); |
---|
316 | ++pos) |
---|
317 | { |
---|
318 | observed_environment.get(*pos, value); |
---|
319 | std::cout << "Additional environment: " |
---|
320 | << *pos << "=" << value << std::endl; |
---|
321 | } |
---|
322 | } |
---|
323 | CPPUNIT_ASSERT(extra.empty()); |
---|
324 | |
---|
325 | for (sbuild::environment::const_iterator pos = expected_environment.begin(); |
---|
326 | pos != expected_environment.end(); |
---|
327 | ++pos) |
---|
328 | { |
---|
329 | std::string checkval; |
---|
330 | CPPUNIT_ASSERT(observed_environment.get(pos->first, checkval) == true); |
---|
331 | |
---|
332 | if (checkval != pos->second) |
---|
333 | std::cout << "Environment error (" << pos->first << "): " |
---|
334 | << checkval << " [observed] != " |
---|
335 | << pos->second << " [expected]" |
---|
336 | << std::endl; |
---|
337 | CPPUNIT_ASSERT(checkval == pos->second); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | void test_setup_env(sbuild::chroot::ptr& chroot, |
---|
342 | const sbuild::environment& expected_environment) |
---|
343 | { |
---|
344 | sbuild::environment observed_environment; |
---|
345 | chroot->setup_env(observed_environment); |
---|
346 | |
---|
347 | CPPUNIT_ASSERT(observed_environment.size() != 0); |
---|
348 | |
---|
349 | test_setup_env(observed_environment, expected_environment); |
---|
350 | } |
---|
351 | |
---|
352 | void test_setup_keyfile(const sbuild::keyfile& observed_keyfile, |
---|
353 | const std::string& observed_group, |
---|
354 | const sbuild::keyfile& expected_keyfile, |
---|
355 | const std::string& expected_group) |
---|
356 | { |
---|
357 | CPPUNIT_ASSERT(observed_keyfile.get_keys(observed_group).size() != 0); |
---|
358 | CPPUNIT_ASSERT(expected_keyfile.get_keys(expected_group).size() != 0); |
---|
359 | |
---|
360 | |
---|
361 | sbuild::string_list expected_keys = |
---|
362 | expected_keyfile.get_keys(expected_group); |
---|
363 | std::set<std::string> expected(expected_keys.begin(), expected_keys.end()); |
---|
364 | |
---|
365 | sbuild::string_list observed_keys = |
---|
366 | observed_keyfile.get_keys(observed_group); |
---|
367 | std::set<std::string> observed(observed_keys.begin(), observed_keys.end()); |
---|
368 | |
---|
369 | sbuild::string_list missing; |
---|
370 | set_difference(expected.begin(), expected.end(), |
---|
371 | observed.begin(), observed.end(), |
---|
372 | std::back_inserter(missing)); |
---|
373 | if (!missing.empty()) |
---|
374 | { |
---|
375 | std::string value; |
---|
376 | for (sbuild::string_list::const_iterator pos = missing.begin(); |
---|
377 | pos != missing.end(); |
---|
378 | ++pos) |
---|
379 | { |
---|
380 | expected_keyfile.get_value(expected_group, *pos, value); |
---|
381 | std::cout << "Missing keys: " |
---|
382 | << *pos << "=" << value << std::endl; |
---|
383 | } |
---|
384 | } |
---|
385 | CPPUNIT_ASSERT(missing.empty()); |
---|
386 | |
---|
387 | sbuild::string_list extra; |
---|
388 | set_difference(observed.begin(), observed.end(), |
---|
389 | expected.begin(), expected.end(), |
---|
390 | std::back_inserter(extra)); |
---|
391 | if (!extra.empty()) |
---|
392 | { |
---|
393 | std::string value; |
---|
394 | for (sbuild::string_list::const_iterator pos = extra.begin(); |
---|
395 | pos != extra.end(); |
---|
396 | ++pos) |
---|
397 | { |
---|
398 | observed_keyfile.get_value(observed_group, *pos, value); |
---|
399 | std::cout << "Additional keys: " |
---|
400 | << *pos << "=" << value << std::endl; |
---|
401 | } |
---|
402 | } |
---|
403 | CPPUNIT_ASSERT(extra.empty()); |
---|
404 | |
---|
405 | for (sbuild::string_list::const_iterator pos = expected_keys.begin(); |
---|
406 | pos != expected_keys.end(); |
---|
407 | ++pos) |
---|
408 | { |
---|
409 | std::string expected_val; |
---|
410 | CPPUNIT_ASSERT(expected_keyfile.get_value(expected_group, |
---|
411 | *pos, expected_val) == true); |
---|
412 | |
---|
413 | std::string observed_val; |
---|
414 | CPPUNIT_ASSERT(observed_keyfile.get_value(observed_group, |
---|
415 | *pos, observed_val) == true); |
---|
416 | |
---|
417 | if (expected_val != observed_val) |
---|
418 | std::cout << "Keyfile error (" << *pos << "): " |
---|
419 | << observed_val << " [observed] != " |
---|
420 | << expected_val << " [expected]" |
---|
421 | << std::endl; |
---|
422 | CPPUNIT_ASSERT(expected_val == observed_val); |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | void test_setup_keyfile(sbuild::chroot::ptr& chroot, |
---|
427 | const sbuild::keyfile& expected_keyfile, |
---|
428 | const std::string& group) |
---|
429 | { |
---|
430 | sbuild::keyfile keys; |
---|
431 | chroot->get_keyfile(keys); |
---|
432 | |
---|
433 | test_setup_keyfile(keys, chroot->get_name(), |
---|
434 | expected_keyfile, group); |
---|
435 | } |
---|
436 | |
---|
437 | // TODO: All chroot types should check text output matches. If |
---|
438 | // possible, test chroot locking functions, but this is going to be |
---|
439 | // tricky without having root in many cases. |
---|
440 | |
---|
441 | }; |
---|
442 | |
---|
443 | #endif /* TEST_SBUILD_CHROOT_H */ |
---|
444 | |
---|
445 | /* |
---|
446 | * Local Variables: |
---|
447 | * mode:C++ |
---|
448 | * End: |
---|
449 | */ |
---|