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

Revision 24167, 10.9 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2008-2009  Jan-Marek Glogowski <glogow@fbihome.de>
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-loopback.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_loopback : public sbuild::chroot_loopback
38{
39public:
40  chroot_loopback():
41    sbuild::chroot_loopback()
42  {}
43
44  virtual ~chroot_loopback()
45  {}
46};
47
48class test_chroot_loopback : public test_chroot_base<chroot_loopback>
49{
50  CPPUNIT_TEST_SUITE(test_chroot_loopback);
51  CPPUNIT_TEST(test_file);
52  CPPUNIT_TEST(test_mount_options);
53  CPPUNIT_TEST(test_setup_env);
54  CPPUNIT_TEST(test_setup_env_session);
55#ifdef SBUILD_FEATURE_UNION
56  CPPUNIT_TEST(test_setup_env_union);
57  CPPUNIT_TEST(test_setup_env_session_union);
58  CPPUNIT_TEST(test_setup_env_source_union);
59#endif // SBUILD_FEATURE_UNION
60  CPPUNIT_TEST(test_setup_keyfile);
61  CPPUNIT_TEST(test_setup_keyfile_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
73protected:
74  std::string loopback_file;
75
76public:
77  test_chroot_loopback():
78    test_chroot_base<chroot_loopback>(),
79    loopback_file()
80  {
81    loopback_file = abs_testdata_dir;
82    loopback_file.append("/loopback-file");
83  }
84
85  void setUp()
86  {
87    test_chroot_base<chroot_loopback>::setUp();
88    CPPUNIT_ASSERT(session);
89    CPPUNIT_ASSERT(!source);
90    CPPUNIT_ASSERT(chroot_union);
91    CPPUNIT_ASSERT(session_union);
92    CPPUNIT_ASSERT(source_union);
93  }
94
95  virtual void setup_chroot_props (sbuild::chroot::ptr& chroot)
96  {
97    test_chroot_base<chroot_loopback>::setup_chroot_props(chroot);
98
99    std::tr1::shared_ptr<sbuild::chroot_loopback> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_loopback>(chroot);
100    c->set_file(loopback_file);
101
102    sbuild::chroot_facet_mountable::ptr pmnt(chroot->get_facet<sbuild::chroot_facet_mountable>());
103    CPPUNIT_ASSERT(pmnt);
104
105    pmnt->set_mount_options("-t jfs -o quota,rw");
106    pmnt->set_location("/squeeze");
107  }
108
109  void
110  test_file()
111  {
112    std::tr1::shared_ptr<sbuild::chroot_loopback> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_loopback>(chroot);
113    CPPUNIT_ASSERT(c);
114    c->set_file("/dev/some/file");
115    CPPUNIT_ASSERT(c->get_file() == "/dev/some/file");
116  }
117
118  void
119  test_mount_options()
120  {
121    sbuild::chroot_facet_mountable::ptr pmnt(chroot->get_facet<sbuild::chroot_facet_mountable>());
122    CPPUNIT_ASSERT(pmnt);
123    pmnt->set_mount_options("-o opt1,opt2");
124    CPPUNIT_ASSERT(pmnt->get_mount_options() == "-o opt1,opt2");
125  }
126
127  void setup_env_gen(sbuild::environment& expected)
128  {
129    setup_env_chroot(expected);
130
131    expected.add("CHROOT_TYPE",           "loopback");
132    expected.add("CHROOT_MOUNT_LOCATION", "/mnt/mount-location");
133    expected.add("CHROOT_LOCATION",       "/squeeze");
134    expected.add("CHROOT_PATH",           "/mnt/mount-location/squeeze");
135    expected.add("CHROOT_FILE",           loopback_file);
136    expected.add("CHROOT_MOUNT_DEVICE",   loopback_file);
137    expected.add("CHROOT_MOUNT_OPTIONS",  "-t jfs -o quota,rw");
138
139#ifdef SBUILD_FEATURE_UNION
140    expected.add("CHROOT_UNION_TYPE",     "none");
141#endif // SBUILD_FEATURE_UNION
142  }
143
144  void test_setup_env()
145  {
146    sbuild::environment expected;
147    setup_env_gen(expected);
148
149    expected.add("CHROOT_SESSION_CLONE",  "false");
150    expected.add("CHROOT_SESSION_CREATE", "true");
151    expected.add("CHROOT_SESSION_PURGE",  "false");
152
153    test_chroot_base<chroot_loopback>::test_setup_env(chroot, expected);
154  }
155
156  void test_setup_env_session()
157  {
158    sbuild::environment expected;
159    setup_env_gen(expected);
160
161    expected.add("CHROOT_NAME",           "test-session-name");
162    expected.add("CHROOT_DESCRIPTION",     chroot->get_description() + ' ' + _("(session chroot)"));
163    expected.add("CHROOT_SESSION_CLONE",  "false");
164    expected.add("CHROOT_SESSION_CREATE", "false");
165    expected.add("CHROOT_SESSION_PURGE",  "false");
166    expected.add("CHROOT_UNION_TYPE",     "none");
167    test_chroot_base<chroot_loopback>::test_setup_env(session, expected);
168  }
169
170#ifdef SBUILD_FEATURE_UNION
171  void test_setup_env_union()
172  {
173    sbuild::environment expected;
174    setup_env_gen(expected);
175
176    expected.add("CHROOT_SESSION_CLONE",  "true");
177    expected.add("CHROOT_SESSION_CREATE", "true");
178    expected.add("CHROOT_SESSION_PURGE",  "false");
179    expected.add("CHROOT_UNION_TYPE",     "aufs");
180    expected.add("CHROOT_UNION_MOUNT_OPTIONS",      "union-mount-options");
181    expected.add("CHROOT_UNION_OVERLAY_DIRECTORY",  "/overlay");
182    expected.add("CHROOT_UNION_UNDERLAY_DIRECTORY", "/underlay");
183
184    test_chroot_base<chroot_loopback>::test_setup_env(chroot_union, expected);
185  }
186
187  void test_setup_env_session_union()
188  {
189    sbuild::environment expected;
190    setup_env_gen(expected);
191
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_loopback>::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_loopback>::test_setup_env(source_union, expected);
216  }
217#endif // SBUILD_FEATURE_UNION
218
219  void setup_keyfile_loop(sbuild::keyfile &expected, std::string group)
220  {
221    expected.set_value(group, "type", "loopback");
222    expected.set_value(group, "file", loopback_file);
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    const std::string group(chroot->get_name());
231    setup_keyfile_chroot(expected, group);
232    setup_keyfile_loop(expected, group);
233    setup_keyfile_union_unconfigured(expected, group);
234
235    test_chroot_base<chroot_loopback>::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_loop(expected, group);
245    expected.set_value(group, "name", "test-session-name");
246    expected.set_value(group, "mount-device", loopback_file);
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_loopback>::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_chroot(expected, group);
261    setup_keyfile_source(expected, group);
262    setup_keyfile_loop(expected, group);
263    setup_keyfile_union_configured(expected, group);
264
265    test_chroot_base<chroot_loopback>::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_loop(expected, group);
275    expected.set_value(group, "name", "test-union-session-name");
276    expected.set_value(group, "mount-device", loopback_file);
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_loopback>::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_loop(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_loopback>::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_loopback);
Note: See TracBrowser for help on using the repository browser.