source: trunk/debathena/third/schroot/test/sbuild-chroot-directory.cc @ 24167

Revision 24167, 11.5 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
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#include <config.h>
20
21#include <sbuild/sbuild-chroot-directory.h>
22#include <sbuild/sbuild-i18n.h>
23
24#include "test-helpers.h"
25#include "test-sbuild-chroot.h"
26
27#include <algorithm>
28#include <set>
29
30#include <cppunit/extensions/HelperMacros.h>
31
32using namespace CppUnit;
33
34using sbuild::_;
35
36class chroot_directory : public sbuild::chroot_directory
37{
38public:
39  chroot_directory():
40    sbuild::chroot_directory()
41  {}
42
43  virtual ~chroot_directory()
44  {}
45};
46
47class test_chroot_directory : public test_chroot_base<chroot_directory>
48{
49  CPPUNIT_TEST_SUITE(test_chroot_directory);
50  CPPUNIT_TEST(test_directory);
51  CPPUNIT_TEST(test_chroot_type);
52  CPPUNIT_TEST(test_setup_env);
53  CPPUNIT_TEST(test_setup_env_session);
54#ifdef SBUILD_FEATURE_UNION
55  CPPUNIT_TEST(test_setup_env_union);
56  CPPUNIT_TEST(test_setup_env_session_union);
57  CPPUNIT_TEST(test_setup_env_source_union);
58#endif // SBUILD_FEATURE_UNION
59  CPPUNIT_TEST(test_setup_keyfile);
60  CPPUNIT_TEST(test_setup_keyfile_session);
61  CPPUNIT_TEST(test_setup_keyfile_root_session);
62#ifdef SBUILD_FEATURE_UNION
63  CPPUNIT_TEST(test_setup_keyfile_union);
64  CPPUNIT_TEST(test_setup_keyfile_session_union);
65  CPPUNIT_TEST(test_setup_keyfile_source_union);
66#endif // SBUILD_FEATURE_UNION
67  CPPUNIT_TEST(test_session_flags);
68  CPPUNIT_TEST(test_print_details);
69  CPPUNIT_TEST(test_print_config);
70  CPPUNIT_TEST(test_run_setup_scripts);
71  CPPUNIT_TEST_SUITE_END();
72
73public:
74  test_chroot_directory():
75    test_chroot_base<chroot_directory>()
76  {}
77
78  void setUp()
79  {
80    test_chroot_base<chroot_directory>::setUp();
81    CPPUNIT_ASSERT(session);
82    CPPUNIT_ASSERT(!source);
83    CPPUNIT_ASSERT(chroot_union);
84    CPPUNIT_ASSERT(session_union);
85    CPPUNIT_ASSERT(source_union);
86  }
87
88  virtual void setup_chroot_props (sbuild::chroot::ptr& chroot)
89  {
90    test_chroot_base<chroot_directory>::setup_chroot_props(chroot);
91
92    std::tr1::shared_ptr<sbuild::chroot_directory> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_directory>(chroot);
93
94    c->set_directory("/srv/chroot/example-chroot");
95  }
96
97  void
98  test_directory()
99  {
100    std::tr1::shared_ptr<sbuild::chroot_directory> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_directory>(chroot);
101    CPPUNIT_ASSERT(c);
102    c->set_directory("/mnt/chroot/example");
103    CPPUNIT_ASSERT(chroot->get_mount_location() == "/mnt/mount-location");
104    CPPUNIT_ASSERT(c->get_directory() == "/mnt/chroot/example");
105    CPPUNIT_ASSERT(chroot->get_path() == "/mnt/mount-location");
106  }
107
108  void test_chroot_type()
109  {
110    CPPUNIT_ASSERT(chroot->get_chroot_type() == "directory");
111  }
112
113  void setup_env_gen(sbuild::environment& expected)
114  {
115    setup_env_chroot(expected);
116
117    expected.add("CHROOT_TYPE",           "directory");
118    expected.add("CHROOT_MOUNT_LOCATION", "/mnt/mount-location");
119    expected.add("CHROOT_DIRECTORY",      "/srv/chroot/example-chroot");
120    expected.add("CHROOT_PATH",           "/mnt/mount-location");
121  }
122
123  void test_setup_env()
124  {
125    sbuild::environment expected;
126    setup_env_gen(expected);
127
128    expected.add("CHROOT_SESSION_CLONE",  "false");
129    expected.add("CHROOT_SESSION_CREATE", "true");
130    expected.add("CHROOT_SESSION_PURGE",  "false");
131#ifdef SBUILD_FEATURE_UNION
132    expected.add("CHROOT_UNION_TYPE",     "none");
133#endif // SBUILD_FEATURE_UNION
134
135    test_chroot_base<chroot_directory>::test_setup_env(chroot, expected);
136  }
137
138  void test_setup_env_session()
139  {
140    sbuild::environment expected;
141    setup_env_gen(expected);
142
143    expected.add("CHROOT_NAME",           "test-session-name");
144    expected.add("CHROOT_DESCRIPTION",     chroot->get_description() + ' ' + _("(session chroot)"));
145    expected.add("CHROOT_SESSION_CLONE",  "false");
146    expected.add("CHROOT_SESSION_CREATE", "false");
147    expected.add("CHROOT_SESSION_PURGE",  "false");
148    expected.add("CHROOT_UNION_TYPE",     "none");
149
150    test_chroot_base<chroot_directory>::test_setup_env(session, expected);
151  }
152
153#ifdef SBUILD_FEATURE_UNION
154  void test_setup_env_union()
155  {
156    sbuild::environment expected;
157    setup_env_gen(expected);
158
159    expected.add("CHROOT_SESSION_CLONE",  "true");
160    expected.add("CHROOT_SESSION_CREATE", "true");
161    expected.add("CHROOT_SESSION_PURGE",  "false");
162    expected.add("CHROOT_UNION_TYPE",     "aufs");
163    expected.add("CHROOT_UNION_MOUNT_OPTIONS",      "union-mount-options");
164    expected.add("CHROOT_UNION_OVERLAY_DIRECTORY",  "/overlay");
165    expected.add("CHROOT_UNION_UNDERLAY_DIRECTORY", "/underlay");
166
167    test_chroot_base<chroot_directory>::test_setup_env(chroot_union, expected);
168  }
169
170  void test_setup_env_session_union()
171  {
172    sbuild::environment expected;
173    setup_env_gen(expected);
174
175    expected.add("CHROOT_NAME",           "test-union-session-name");
176    expected.add("CHROOT_DESCRIPTION",     chroot->get_description() + ' ' + _("(session chroot)"));
177    expected.add("CHROOT_SESSION_CLONE",  "false");
178    expected.add("CHROOT_SESSION_CREATE", "false");
179    expected.add("CHROOT_SESSION_PURGE",  "true");
180    expected.add("CHROOT_UNION_TYPE",     "aufs");
181    expected.add("CHROOT_UNION_MOUNT_OPTIONS",      "union-mount-options");
182    expected.add("CHROOT_UNION_OVERLAY_DIRECTORY",  "/overlay/test-union-session-name");
183    expected.add("CHROOT_UNION_UNDERLAY_DIRECTORY", "/underlay/test-union-session-name");
184
185    test_chroot_base<chroot_directory>::test_setup_env(session_union, expected);
186  }
187
188  void test_setup_env_source_union()
189  {
190    sbuild::environment expected;
191    setup_env_gen(expected);
192
193    expected.add("CHROOT_NAME",           "test-name-source");
194    expected.add("CHROOT_DESCRIPTION",     chroot->get_description() + ' ' + _("(source chroot)"));
195    expected.add("CHROOT_SESSION_CLONE",  "false");
196    expected.add("CHROOT_SESSION_CREATE", "true");
197    expected.add("CHROOT_SESSION_PURGE",  "false");
198    expected.add("CHROOT_UNION_TYPE",     "none");
199
200    test_chroot_base<chroot_directory>::test_setup_env(source_union, expected);
201  }
202#endif // SBUILD_FEATURE_UNION
203
204  void test_setup_keyfile()
205  {
206    sbuild::keyfile expected;
207    const std::string group(chroot->get_name());
208    setup_keyfile_chroot(expected, group);
209    expected.set_value(group, "type", "directory");
210    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
211    setup_keyfile_union_unconfigured(expected, group);
212
213    test_chroot_base<chroot_directory>::test_setup_keyfile
214      (chroot, expected, group);
215  }
216
217  void test_setup_keyfile_session()
218  {
219    sbuild::keyfile expected;
220    const std::string group(session->get_name());
221    setup_keyfile_session(expected, group);
222    expected.set_value(group, "type", "directory");
223    expected.set_value(group, "name", "test-session-name");
224    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
225    expected.set_value(group, "mount-location", "/mnt/mount-location");
226    setup_keyfile_session_clone(expected, group);
227    setup_keyfile_union_unconfigured(expected, group);
228
229    test_chroot_base<chroot_directory>::test_setup_keyfile
230      (session, expected, group);
231  }
232
233  // Note, this test is unique to the directory chroot but is
234  // applicable to all.
235  void test_setup_keyfile_root_session()
236  {
237    // Create session owned by user in root-users.
238    this->session = this->chroot->clone_session("test-session-name",
239                                                "user3",
240                                                true);
241    if (this->session)
242      {
243        CPPUNIT_ASSERT(this->session->get_active() == true);
244      }
245
246    sbuild::keyfile expected;
247    const std::string group(session->get_name());
248    setup_keyfile_session(expected, group);
249    expected.set_value(group, "users", "");
250    expected.set_value(group, "root-users", "user3");
251    expected.set_value(group, "type", "directory");
252    expected.set_value(group, "name", "test-session-name");
253    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
254    expected.set_value(group, "mount-location", "/mnt/mount-location");
255    setup_keyfile_session_clone(expected, group);
256    setup_keyfile_union_unconfigured(expected, group);
257
258    test_chroot_base<chroot_directory>::test_setup_keyfile
259      (session, expected, group);
260  }
261
262#ifdef SBUILD_FEATURE_UNION
263  void test_setup_keyfile_union()
264  {
265    sbuild::keyfile expected;
266    const std::string group(chroot_union->get_name());
267    setup_keyfile_chroot(expected, group);
268    setup_keyfile_source(expected, group);
269    expected.set_value(group, "type", "directory");
270    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
271    setup_keyfile_union_configured(expected, group);
272
273    test_chroot_base<chroot_directory>::test_setup_keyfile
274      (chroot_union, expected, group);
275  }
276
277  void test_setup_keyfile_session_union()
278  {
279    sbuild::keyfile expected;
280    const std::string group(session_union->get_name());
281    setup_keyfile_session(expected, group);
282    expected.set_value(group, "type", "directory");
283    expected.set_value(group, "name", "test-union-session-name");
284    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
285    expected.set_value(group, "mount-location", "/mnt/mount-location");
286    setup_keyfile_session_clone(expected, group);
287    setup_keyfile_union_session(expected, group);
288
289    test_chroot_base<chroot_directory>::test_setup_keyfile
290      (session_union, expected, group);
291  }
292
293  void test_setup_keyfile_source_union()
294  {
295    sbuild::keyfile expected;
296    const std::string group(source_union->get_name());
297    setup_keyfile_chroot(expected, group);
298    setup_keyfile_source_clone(expected, group);
299    expected.set_value(group, "type", "directory");
300    expected.set_value(group, "description", chroot->get_description() + ' ' + _("(source chroot)"));
301    expected.set_value(group, "directory", "/srv/chroot/example-chroot");
302    setup_keyfile_union_unconfigured(expected, group);
303
304    test_chroot_base<chroot_directory>::test_setup_keyfile
305      (source_union, expected, group);
306  }
307#endif // SBUILD_FEATURE_UNION
308
309void test_session_flags()
310  {
311    CPPUNIT_ASSERT(chroot->get_session_flags() ==
312                   sbuild::chroot::SESSION_CREATE);
313
314    CPPUNIT_ASSERT(session->get_session_flags() ==
315                   sbuild::chroot::SESSION_NOFLAGS);
316
317#ifdef SBUILD_FEATURE_UNION
318    CPPUNIT_ASSERT(chroot_union->get_session_flags() ==
319                   (sbuild::chroot::SESSION_CREATE |
320                    sbuild::chroot::SESSION_CLONE));
321
322    CPPUNIT_ASSERT(session_union->get_session_flags() ==
323                   sbuild::chroot::SESSION_PURGE);
324
325    CPPUNIT_ASSERT(source_union->get_session_flags() ==
326                   sbuild::chroot::SESSION_CREATE);
327#endif // SBUILD_FEATURE_UNION
328  }
329
330  void test_print_details()
331  {
332    std::ostringstream os;
333    os << chroot;
334    // TODO: Compare output.
335    CPPUNIT_ASSERT(!os.str().empty());
336  }
337
338  void test_print_config()
339  {
340    std::ostringstream os;
341    sbuild::keyfile config;
342    config << chroot;
343    os << config;
344    // TODO: Compare output.
345    CPPUNIT_ASSERT(!os.str().empty());
346  }
347
348  void test_run_setup_scripts()
349  {
350    CPPUNIT_ASSERT(chroot->get_run_setup_scripts());
351  }
352
353};
354
355CPPUNIT_TEST_SUITE_REGISTRATION(test_chroot_directory);
Note: See TracBrowser for help on using the repository browser.