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

Revision 24167, 6.8 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.h>
22
23#include "test-helpers.h"
24#include "test-sbuild-chroot.h"
25
26#include <algorithm>
27#include <set>
28
29#include <cppunit/extensions/HelperMacros.h>
30
31using namespace CppUnit;
32
33class basic_chroot : public sbuild::chroot
34{
35public:
36  basic_chroot ():
37    sbuild::chroot()
38  {}
39
40  virtual ~basic_chroot()
41  {}
42
43  virtual ptr
44  clone () const
45  { return ptr(new basic_chroot(*this)); }
46
47  virtual ptr
48  clone_session (std::string const& session_id,
49                 std::string const& user,
50                 bool               root) const
51  { return ptr(); }
52
53
54  chroot::ptr
55  clone_source () const
56  { return ptr(); }
57
58  virtual std::string const&
59  get_chroot_type () const
60  { static const std::string type("test"); return type; }
61
62  void
63  set_run_setup_scripts (bool run_setup_scripts)
64  {
65    sbuild::chroot::set_run_setup_scripts(run_setup_scripts);
66  }
67
68  virtual std::string
69  get_path () const
70  { return get_mount_location(); }
71
72  virtual void
73  setup_env (sbuild::chroot const& chroot,
74             sbuild::environment&  env) const
75  { sbuild::chroot::setup_env(chroot, env); }
76
77  virtual void
78  get_details (sbuild::chroot const&  chroot,
79               sbuild::format_detail& detail) const
80  { sbuild::chroot::get_details(chroot, detail); }
81
82  virtual void
83  setup_lock (setup_type type,
84              bool       lock,
85              int        status)
86  {}
87
88  virtual sbuild::chroot::session_flags
89  get_session_flags (sbuild::chroot const& chroot) const
90  { return sbuild::chroot::SESSION_CREATE; }
91
92  virtual void
93  get_keyfile (sbuild::chroot const& chroot,
94               sbuild::keyfile&      keyfile) const
95  { sbuild::chroot::get_keyfile(chroot, keyfile); }
96
97  virtual void
98  set_keyfile (sbuild::chroot&        chroot,
99               sbuild::keyfile const& keyfile,
100               sbuild::string_list&   used_keys)
101  { sbuild::chroot::set_keyfile(chroot, keyfile, used_keys); }
102};
103
104class test_chroot : public test_chroot_base<basic_chroot>
105{
106  CPPUNIT_TEST_SUITE(test_chroot);
107  CPPUNIT_TEST(test_name);
108  CPPUNIT_TEST(test_description);
109  CPPUNIT_TEST(test_mount_location);
110  CPPUNIT_TEST(test_priority);
111  CPPUNIT_TEST(test_groups);
112  CPPUNIT_TEST(test_root_groups);
113  CPPUNIT_TEST(test_aliases);
114  CPPUNIT_TEST(test_environment_filter);
115  CPPUNIT_TEST(test_active);
116  CPPUNIT_TEST(test_run_setup_scripts);
117  CPPUNIT_TEST(test_chroot_type);
118  CPPUNIT_TEST(test_setup_env);
119  CPPUNIT_TEST(test_setup_keyfile);
120  CPPUNIT_TEST(test_session_flags);
121  CPPUNIT_TEST(test_print_details);
122  CPPUNIT_TEST(test_print_config);
123  CPPUNIT_TEST_SUITE_END();
124
125public:
126  test_chroot():
127    test_chroot_base<basic_chroot>()
128  {}
129
130  void test_name()
131  {
132    chroot->set_name("test-name-example");
133    CPPUNIT_ASSERT(chroot->get_name() == "test-name-example");
134  }
135
136  void test_description()
137  {
138    chroot->set_description("test-description-example");
139    CPPUNIT_ASSERT(chroot->get_description() == "test-description-example");
140  }
141
142  void test_mount_location()
143  {
144    chroot->set_mount_location("/mnt/mount-location/example");
145    CPPUNIT_ASSERT(chroot->get_mount_location() ==
146                   "/mnt/mount-location/example");
147  }
148
149  void test_priority()
150  {
151    chroot->set_priority(6);
152    CPPUNIT_ASSERT(chroot->get_priority() == 6);
153  }
154
155  void test_groups()
156  {
157    sbuild::string_list groups;
158    groups.push_back("schroot");
159    groups.push_back("sbuild-users");
160    groups.push_back("fred");
161    groups.push_back("users");
162
163    test_list(*chroot.get(),
164              groups,
165              &sbuild::chroot::get_groups,
166              &sbuild::chroot::set_groups);
167  }
168
169  void test_root_groups()
170  {
171    sbuild::string_list groups;
172    groups.push_back("schroot");
173    groups.push_back("trusted");
174    groups.push_back("root");
175
176    test_list(*chroot.get(),
177              groups,
178              &sbuild::chroot::get_root_groups,
179              &sbuild::chroot::set_root_groups);
180  }
181
182  void test_aliases()
183  {
184    sbuild::string_list aliases;
185    aliases.push_back("alias1");
186    aliases.push_back("alias2");
187
188    test_list(*chroot.get(),
189              aliases,
190              &sbuild::chroot::get_aliases,
191              &sbuild::chroot::set_aliases);
192  }
193
194  void test_environment_filter()
195  {
196    sbuild::regex r("foo|bar|baz");
197
198    chroot->set_environment_filter(r);
199
200    CPPUNIT_ASSERT(chroot->get_environment_filter().compare(r) == 0);
201  }
202
203  void test_active()
204  {
205    CPPUNIT_ASSERT(chroot->get_active() == false);
206  }
207
208  void test_run_setup_scripts()
209  {
210    std::tr1::shared_ptr<basic_chroot> c = std::tr1::dynamic_pointer_cast<basic_chroot>(chroot);
211
212    CPPUNIT_ASSERT(chroot->get_run_setup_scripts() == true);
213    c->set_run_setup_scripts(false);
214    CPPUNIT_ASSERT(chroot->get_run_setup_scripts() == false);
215    c->set_run_setup_scripts(true);
216    CPPUNIT_ASSERT(chroot->get_run_setup_scripts() == true);
217  }
218
219  void test_chroot_type()
220  {
221    CPPUNIT_ASSERT(chroot->get_chroot_type() == "test");
222  }
223
224  void test_setup_env()
225  {
226    sbuild::environment expected;
227    setup_env_chroot(expected);
228    expected.add("CHROOT_TYPE",           "test");
229    expected.add("CHROOT_MOUNT_LOCATION", "/mnt/mount-location");
230    expected.add("CHROOT_PATH",           "/mnt/mount-location");
231    expected.add("CHROOT_SESSION_CLONE",  "false");
232    expected.add("CHROOT_SESSION_CREATE", "true");
233    expected.add("CHROOT_SESSION_PURGE",  "false");
234
235    sbuild::environment observed;
236    chroot->setup_env(observed);
237
238    test_chroot_base<basic_chroot>::test_setup_env(observed, expected);
239  }
240
241  void test_setup_keyfile()
242  {
243    sbuild::keyfile expected;
244    std::string group = chroot->get_name();
245    setup_keyfile_chroot(expected, group);
246    expected.set_value(group, "type", "test");
247
248    test_chroot_base<basic_chroot>::test_setup_keyfile
249      (chroot, expected, group);
250  }
251
252  void test_session_flags()
253  {
254    CPPUNIT_ASSERT(chroot->get_session_flags() ==
255                   sbuild::chroot::SESSION_CREATE);
256  }
257
258  void test_print_details()
259  {
260    std::ostringstream os;
261    os << chroot;
262    // TODO: Compare output.
263    CPPUNIT_ASSERT(!os.str().empty());
264  }
265
266  void test_print_config()
267  {
268    std::ostringstream os;
269    sbuild::keyfile config;
270    config << chroot;
271    os << config;
272    // TODO: Compare output.
273    CPPUNIT_ASSERT(!os.str().empty());
274  }
275};
276
277CPPUNIT_TEST_SUITE_REGISTRATION(test_chroot);
Note: See TracBrowser for help on using the repository browser.