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

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