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-lvm-snapshot.h> |
---|
22 | #include <sbuild/sbuild-chroot-facet-mountable.h> |
---|
23 | #include <sbuild/sbuild-i18n.h> |
---|
24 | #include <sbuild/sbuild-util.h> |
---|
25 | |
---|
26 | #include "test-helpers.h" |
---|
27 | #include "test-sbuild-chroot.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <set> |
---|
31 | |
---|
32 | #include <cppunit/extensions/HelperMacros.h> |
---|
33 | |
---|
34 | using namespace CppUnit; |
---|
35 | |
---|
36 | using sbuild::_; |
---|
37 | |
---|
38 | class chroot_lvm_snapshot : public sbuild::chroot_lvm_snapshot |
---|
39 | { |
---|
40 | public: |
---|
41 | chroot_lvm_snapshot(): |
---|
42 | sbuild::chroot_lvm_snapshot() |
---|
43 | {} |
---|
44 | |
---|
45 | virtual ~chroot_lvm_snapshot() |
---|
46 | {} |
---|
47 | }; |
---|
48 | |
---|
49 | class test_chroot_lvm_snapshot : public test_chroot_base<chroot_lvm_snapshot> |
---|
50 | { |
---|
51 | CPPUNIT_TEST_SUITE(test_chroot_lvm_snapshot); |
---|
52 | CPPUNIT_TEST(test_snapshot_device); |
---|
53 | CPPUNIT_TEST(test_snapshot_options); |
---|
54 | CPPUNIT_TEST(test_chroot_type); |
---|
55 | CPPUNIT_TEST(test_setup_env); |
---|
56 | CPPUNIT_TEST(test_setup_env_session); |
---|
57 | CPPUNIT_TEST(test_setup_env_source); |
---|
58 | CPPUNIT_TEST(test_setup_keyfile); |
---|
59 | CPPUNIT_TEST(test_setup_keyfile_session); |
---|
60 | CPPUNIT_TEST(test_setup_keyfile_source); |
---|
61 | CPPUNIT_TEST(test_session_flags); |
---|
62 | CPPUNIT_TEST(test_print_details); |
---|
63 | CPPUNIT_TEST(test_print_config); |
---|
64 | CPPUNIT_TEST(test_run_setup_scripts); |
---|
65 | CPPUNIT_TEST_SUITE_END(); |
---|
66 | |
---|
67 | public: |
---|
68 | test_chroot_lvm_snapshot(): |
---|
69 | test_chroot_base<chroot_lvm_snapshot>() |
---|
70 | {} |
---|
71 | |
---|
72 | void setUp() |
---|
73 | { |
---|
74 | test_chroot_base<chroot_lvm_snapshot>::setUp(); |
---|
75 | CPPUNIT_ASSERT(session); |
---|
76 | CPPUNIT_ASSERT(source); |
---|
77 | CPPUNIT_ASSERT(!chroot_union); |
---|
78 | CPPUNIT_ASSERT(!session_union); |
---|
79 | CPPUNIT_ASSERT(!source_union); |
---|
80 | } |
---|
81 | |
---|
82 | virtual void setup_chroot_props (sbuild::chroot::ptr& chroot) |
---|
83 | { |
---|
84 | test_chroot_base<chroot_lvm_snapshot>::setup_chroot_props(chroot); |
---|
85 | |
---|
86 | std::tr1::shared_ptr<sbuild::chroot_lvm_snapshot> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_lvm_snapshot>(chroot); |
---|
87 | |
---|
88 | c->set_device("/dev/volgroup/testdev"); |
---|
89 | c->set_snapshot_options("--size 1G"); |
---|
90 | |
---|
91 | sbuild::chroot_facet_mountable::ptr pmnt(chroot->get_facet<sbuild::chroot_facet_mountable>()); |
---|
92 | CPPUNIT_ASSERT(pmnt); |
---|
93 | |
---|
94 | pmnt->set_mount_options("-t jfs -o quota,rw"); |
---|
95 | pmnt->set_location("/squeeze"); |
---|
96 | //c->set_snapshot_device("/dev/volgroup/snaptestdev"); |
---|
97 | } |
---|
98 | |
---|
99 | void |
---|
100 | test_snapshot_device() |
---|
101 | { |
---|
102 | std::tr1::shared_ptr<sbuild::chroot_lvm_snapshot> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_lvm_snapshot>(chroot); |
---|
103 | CPPUNIT_ASSERT(c); |
---|
104 | c->set_snapshot_device("/dev/volgroup/some/snapshot/device"); |
---|
105 | CPPUNIT_ASSERT(c->get_snapshot_device() == "/dev/volgroup/some/snapshot/device"); |
---|
106 | } |
---|
107 | |
---|
108 | void |
---|
109 | test_snapshot_options() |
---|
110 | { |
---|
111 | std::tr1::shared_ptr<sbuild::chroot_lvm_snapshot> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_lvm_snapshot>(chroot); |
---|
112 | CPPUNIT_ASSERT(c); |
---|
113 | c->set_snapshot_options("-o opt1,opt2"); |
---|
114 | CPPUNIT_ASSERT(c->get_snapshot_options() == "-o opt1,opt2"); |
---|
115 | } |
---|
116 | |
---|
117 | void test_chroot_type() |
---|
118 | { |
---|
119 | CPPUNIT_ASSERT(chroot->get_chroot_type() == "lvm-snapshot"); |
---|
120 | } |
---|
121 | |
---|
122 | void setup_env_gen(sbuild::environment &expected) |
---|
123 | { |
---|
124 | setup_env_chroot(expected); |
---|
125 | expected.add("CHROOT_LOCATION", "/squeeze"); |
---|
126 | expected.add("CHROOT_MOUNT_LOCATION", "/mnt/mount-location"); |
---|
127 | expected.add("CHROOT_PATH", "/mnt/mount-location/squeeze"); |
---|
128 | expected.add("CHROOT_DEVICE", "/dev/volgroup/testdev"); |
---|
129 | expected.add("CHROOT_MOUNT_OPTIONS", "-t jfs -o quota,rw"); |
---|
130 | } |
---|
131 | |
---|
132 | void test_setup_env() |
---|
133 | { |
---|
134 | sbuild::environment expected; |
---|
135 | setup_env_gen(expected); |
---|
136 | expected.add("CHROOT_TYPE", "lvm-snapshot"); |
---|
137 | expected.add("CHROOT_LVM_SNAPSHOT_OPTIONS", "--size 1G"); |
---|
138 | expected.add("CHROOT_SESSION_CLONE", "true"); |
---|
139 | expected.add("CHROOT_SESSION_CREATE", "true"); |
---|
140 | expected.add("CHROOT_SESSION_PURGE", "false"); |
---|
141 | |
---|
142 | test_chroot_base<chroot_lvm_snapshot>::test_setup_env(chroot, expected); |
---|
143 | } |
---|
144 | |
---|
145 | void test_setup_env_session() |
---|
146 | { |
---|
147 | std::tr1::shared_ptr<sbuild::chroot_lvm_snapshot> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_lvm_snapshot>(chroot); |
---|
148 | |
---|
149 | sbuild::environment expected; |
---|
150 | setup_env_gen(expected); |
---|
151 | expected.add("CHROOT_TYPE", "lvm-snapshot"); |
---|
152 | expected.add("CHROOT_NAME", "test-session-name"); |
---|
153 | expected.add("CHROOT_DESCRIPTION", chroot->get_description() + ' ' + _("(session chroot)")); |
---|
154 | expected.add("CHROOT_MOUNT_DEVICE", "/dev/volgroup/test-session-name"); |
---|
155 | expected.add("CHROOT_LVM_SNAPSHOT_NAME", "test-session-name"); |
---|
156 | expected.add("CHROOT_LVM_SNAPSHOT_DEVICE", "/dev/volgroup/test-session-name"); |
---|
157 | expected.add("CHROOT_LVM_SNAPSHOT_OPTIONS", "--size 1G"); |
---|
158 | expected.add("CHROOT_SESSION_CLONE", "false"); |
---|
159 | expected.add("CHROOT_SESSION_CREATE", "false"); |
---|
160 | expected.add("CHROOT_SESSION_PURGE", "true"); |
---|
161 | |
---|
162 | test_chroot_base<chroot_lvm_snapshot>::test_setup_env(session, expected); |
---|
163 | } |
---|
164 | |
---|
165 | void test_setup_env_source() |
---|
166 | { |
---|
167 | sbuild::environment expected; |
---|
168 | setup_env_gen(expected); |
---|
169 | expected.add("CHROOT_TYPE", "block-device"); |
---|
170 | expected.add("CHROOT_NAME", "test-name-source"); |
---|
171 | expected.add("CHROOT_DESCRIPTION", chroot->get_description() + ' ' + _("(source chroot)")); |
---|
172 | expected.add("CHROOT_MOUNT_DEVICE", "/dev/volgroup/testdev"); |
---|
173 | expected.add("CHROOT_SESSION_CLONE", "false"); |
---|
174 | expected.add("CHROOT_SESSION_CREATE", "true"); |
---|
175 | expected.add("CHROOT_SESSION_PURGE", "false"); |
---|
176 | expected.add("CHROOT_UNION_TYPE", "none"); |
---|
177 | |
---|
178 | test_chroot_base<chroot_lvm_snapshot>::test_setup_env(source, expected); |
---|
179 | } |
---|
180 | |
---|
181 | void setup_keyfile_lvm(sbuild::keyfile &expected, std::string group) |
---|
182 | { |
---|
183 | expected.set_value(group, "device", "/dev/volgroup/testdev"); |
---|
184 | expected.set_value(group, "location", "/squeeze"); |
---|
185 | expected.set_value(group, "mount-options", "-t jfs -o quota,rw"); |
---|
186 | } |
---|
187 | |
---|
188 | void test_setup_keyfile() |
---|
189 | { |
---|
190 | sbuild::keyfile expected; |
---|
191 | std::string group = chroot->get_name(); |
---|
192 | setup_keyfile_chroot(expected, group); |
---|
193 | setup_keyfile_source(expected, group); |
---|
194 | setup_keyfile_lvm(expected, group); |
---|
195 | expected.set_value(group, "type", "lvm-snapshot"); |
---|
196 | expected.set_value(group, "lvm-snapshot-options", "--size 1G"); |
---|
197 | |
---|
198 | test_chroot_base<chroot_lvm_snapshot>::test_setup_keyfile |
---|
199 | (chroot,expected, chroot->get_name()); |
---|
200 | } |
---|
201 | |
---|
202 | void test_setup_keyfile_session() |
---|
203 | { |
---|
204 | sbuild::keyfile expected; |
---|
205 | const std::string group(session->get_name()); |
---|
206 | setup_keyfile_session(expected, group); |
---|
207 | setup_keyfile_lvm(expected, group); |
---|
208 | expected.set_value(group, "type", "lvm-snapshot"); |
---|
209 | expected.set_value(group, "name", "test-session-name"); |
---|
210 | expected.set_value(group, "description", chroot->get_description() + ' ' + _("(session chroot)")); |
---|
211 | expected.set_value(group, "aliases", ""); |
---|
212 | expected.set_value(group, "lvm-snapshot-device", "/dev/volgroup/test-session-name"); |
---|
213 | expected.set_value(group, "mount-device", "/dev/volgroup/test-session-name"); |
---|
214 | expected.set_value(group, "mount-location", "/mnt/mount-location"); |
---|
215 | expected.set_value(group, "mount-device", "/dev/volgroup/test-session-name"); |
---|
216 | |
---|
217 | test_chroot_base<chroot_lvm_snapshot>::test_setup_keyfile |
---|
218 | (session, expected, group); |
---|
219 | } |
---|
220 | |
---|
221 | void test_setup_keyfile_source() |
---|
222 | { |
---|
223 | sbuild::keyfile expected; |
---|
224 | const std::string group(source->get_name()); |
---|
225 | setup_keyfile_chroot(expected, group); |
---|
226 | setup_keyfile_lvm(expected, group); |
---|
227 | expected.set_value(group, "type", "block-device"); |
---|
228 | expected.set_value(group, "description", chroot->get_description() + ' ' + _("(source chroot)")); |
---|
229 | expected.set_value(group, "aliases", "test-alias-1-source,test-alias-2-source"); |
---|
230 | expected.set_value(group, "union-type", "none"); |
---|
231 | setup_keyfile_source_clone(expected, group); |
---|
232 | |
---|
233 | test_chroot_base<chroot_lvm_snapshot>::test_setup_keyfile |
---|
234 | (source, expected, group); |
---|
235 | } |
---|
236 | |
---|
237 | void test_session_flags() |
---|
238 | { |
---|
239 | CPPUNIT_ASSERT(chroot->get_session_flags() == |
---|
240 | (sbuild::chroot::SESSION_CREATE | |
---|
241 | sbuild::chroot::SESSION_CLONE)); |
---|
242 | |
---|
243 | CPPUNIT_ASSERT(session->get_session_flags() == |
---|
244 | (sbuild::chroot::SESSION_PURGE)); |
---|
245 | |
---|
246 | /// @todo: Should return NOFLAGS? This depends upon if source |
---|
247 | /// chroots need transforming into sessions as well (which should |
---|
248 | /// probably happen and be tested for independently). |
---|
249 | CPPUNIT_ASSERT(source->get_session_flags() == |
---|
250 | (sbuild::chroot::SESSION_CREATE)); |
---|
251 | } |
---|
252 | |
---|
253 | void test_print_details() |
---|
254 | { |
---|
255 | std::ostringstream os; |
---|
256 | os << chroot; |
---|
257 | // TODO: Compare output. |
---|
258 | CPPUNIT_ASSERT(!os.str().empty()); |
---|
259 | } |
---|
260 | |
---|
261 | void test_print_config() |
---|
262 | { |
---|
263 | std::ostringstream os; |
---|
264 | sbuild::keyfile config; |
---|
265 | config << chroot; |
---|
266 | os << config; |
---|
267 | // TODO: Compare output. |
---|
268 | CPPUNIT_ASSERT(!os.str().empty()); |
---|
269 | } |
---|
270 | |
---|
271 | void test_run_setup_scripts() |
---|
272 | { |
---|
273 | CPPUNIT_ASSERT(chroot->get_run_setup_scripts()); |
---|
274 | } |
---|
275 | |
---|
276 | }; |
---|
277 | |
---|
278 | CPPUNIT_TEST_SUITE_REGISTRATION(test_chroot_lvm_snapshot); |
---|