[24167] | 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-file.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 | |
---|
| 32 | using namespace CppUnit; |
---|
| 33 | |
---|
| 34 | using sbuild::_; |
---|
| 35 | |
---|
| 36 | class chroot_file : public sbuild::chroot_file |
---|
| 37 | { |
---|
| 38 | public: |
---|
| 39 | chroot_file(): |
---|
| 40 | sbuild::chroot_file() |
---|
| 41 | {} |
---|
| 42 | |
---|
| 43 | virtual ~chroot_file() |
---|
| 44 | {} |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | class test_chroot_file : public test_chroot_base<chroot_file> |
---|
| 48 | { |
---|
| 49 | CPPUNIT_TEST_SUITE(test_chroot_file); |
---|
| 50 | CPPUNIT_TEST(test_file); |
---|
| 51 | CPPUNIT_TEST(test_chroot_type); |
---|
| 52 | CPPUNIT_TEST(test_repack); |
---|
| 53 | CPPUNIT_TEST(test_setup_env); |
---|
| 54 | CPPUNIT_TEST(test_setup_env_session); |
---|
| 55 | CPPUNIT_TEST(test_setup_env_source); |
---|
| 56 | CPPUNIT_TEST(test_setup_keyfile); |
---|
| 57 | CPPUNIT_TEST(test_setup_keyfile_session); |
---|
| 58 | CPPUNIT_TEST(test_setup_keyfile_source); |
---|
| 59 | CPPUNIT_TEST(test_session_flags); |
---|
| 60 | CPPUNIT_TEST(test_print_details); |
---|
| 61 | CPPUNIT_TEST(test_print_config); |
---|
| 62 | CPPUNIT_TEST(test_run_setup_scripts); |
---|
| 63 | CPPUNIT_TEST_SUITE_END(); |
---|
| 64 | |
---|
| 65 | public: |
---|
| 66 | test_chroot_file(): |
---|
| 67 | test_chroot_base<chroot_file>() |
---|
| 68 | {} |
---|
| 69 | |
---|
| 70 | void setUp() |
---|
| 71 | { |
---|
| 72 | test_chroot_base<chroot_file>::setUp(); |
---|
| 73 | CPPUNIT_ASSERT(session); |
---|
| 74 | CPPUNIT_ASSERT(source); |
---|
| 75 | CPPUNIT_ASSERT(!chroot_union); |
---|
| 76 | CPPUNIT_ASSERT(!session_union); |
---|
| 77 | CPPUNIT_ASSERT(!source_union); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | virtual void setup_chroot_props (sbuild::chroot::ptr& chroot) |
---|
| 81 | { |
---|
| 82 | test_chroot_base<chroot_file>::setup_chroot_props(chroot); |
---|
| 83 | |
---|
| 84 | std::tr1::shared_ptr<sbuild::chroot_file> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_file>(chroot); |
---|
| 85 | |
---|
| 86 | c->set_file("/srv/chroot/example.tar.bz2"); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void |
---|
| 90 | test_file() |
---|
| 91 | { |
---|
| 92 | std::tr1::shared_ptr<sbuild::chroot_file> c = std::tr1::dynamic_pointer_cast<sbuild::chroot_file>(chroot); |
---|
| 93 | CPPUNIT_ASSERT(c); |
---|
| 94 | c->set_file("/srv/chroot-images/unstable.tar.gz"); |
---|
| 95 | CPPUNIT_ASSERT(c->get_file() == "/srv/chroot-images/unstable.tar.gz"); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void test_chroot_type() |
---|
| 99 | { |
---|
| 100 | CPPUNIT_ASSERT(chroot->get_chroot_type() == "file"); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void test_repack() |
---|
| 104 | { |
---|
| 105 | std::tr1::shared_ptr<sbuild::chroot_file> fc = std::tr1::dynamic_pointer_cast<sbuild::chroot_file>(chroot); |
---|
| 106 | std::tr1::shared_ptr<sbuild::chroot_file> fss = std::tr1::dynamic_pointer_cast<sbuild::chroot_file>(session); |
---|
| 107 | std::tr1::shared_ptr<sbuild::chroot_file> fs = std::tr1::dynamic_pointer_cast<sbuild::chroot_file>(source); |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | CPPUNIT_ASSERT(fc->get_file_repack() == false); |
---|
| 111 | CPPUNIT_ASSERT(fss->get_file_repack() == false); |
---|
| 112 | CPPUNIT_ASSERT(fs->get_file_repack() == true); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void setup_env_gen(sbuild::environment &expected) |
---|
| 116 | { |
---|
| 117 | setup_env_chroot(expected); |
---|
| 118 | expected.add("CHROOT_TYPE", "file"); |
---|
| 119 | expected.add("CHROOT_FILE", "/srv/chroot/example.tar.bz2"); |
---|
| 120 | expected.add("CHROOT_FILE_REPACK", "false"); |
---|
| 121 | expected.add("CHROOT_FILE_UNPACK_DIR", SCHROOT_FILE_UNPACK_DIR); |
---|
| 122 | expected.add("CHROOT_MOUNT_LOCATION", "/mnt/mount-location"); |
---|
| 123 | expected.add("CHROOT_PATH", "/mnt/mount-location"); |
---|
| 124 | expected.add("CHROOT_SESSION_CLONE", "true"); |
---|
| 125 | expected.add("CHROOT_SESSION_CREATE", "true"); |
---|
| 126 | expected.add("CHROOT_SESSION_PURGE", "false"); |
---|
| 127 | |
---|
| 128 | test_chroot_base<chroot_file>::test_setup_env(chroot, expected); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void test_setup_env() |
---|
| 132 | { |
---|
| 133 | sbuild::environment expected; |
---|
| 134 | setup_env_gen(expected); |
---|
| 135 | expected.add("CHROOT_FILE_REPACK", "false"); |
---|
| 136 | expected.add("CHROOT_SESSION_CLONE", "true"); |
---|
| 137 | expected.add("CHROOT_SESSION_CREATE", "true"); |
---|
| 138 | expected.add("CHROOT_SESSION_PURGE", "false"); |
---|
| 139 | |
---|
| 140 | test_chroot_base<chroot_file>::test_setup_env(chroot, expected); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void test_setup_env_session() |
---|
| 144 | { |
---|
| 145 | sbuild::environment expected; |
---|
| 146 | setup_env_gen(expected); |
---|
| 147 | expected.add("CHROOT_NAME", "test-session-name"); |
---|
| 148 | expected.add("CHROOT_DESCRIPTION", chroot->get_description() + ' ' + _("(session chroot)")); |
---|
| 149 | expected.add("CHROOT_FILE_REPACK", "false"); |
---|
| 150 | expected.add("CHROOT_SESSION_CLONE", "false"); |
---|
| 151 | expected.add("CHROOT_SESSION_CREATE", "false"); |
---|
| 152 | expected.add("CHROOT_SESSION_PURGE", "true"); |
---|
| 153 | |
---|
| 154 | test_chroot_base<chroot_file>::test_setup_env(session, expected); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void test_setup_env_source() |
---|
| 158 | { |
---|
| 159 | sbuild::environment expected; |
---|
| 160 | setup_env_gen(expected); |
---|
| 161 | expected.add("CHROOT_NAME", "test-name-source"); |
---|
| 162 | expected.add("CHROOT_DESCRIPTION", chroot->get_description() + ' ' + _("(source chroot)")); |
---|
| 163 | expected.add("CHROOT_FILE_REPACK", "true"); |
---|
| 164 | expected.add("CHROOT_SESSION_CLONE", "false"); |
---|
| 165 | expected.add("CHROOT_SESSION_CREATE", "true"); |
---|
| 166 | expected.add("CHROOT_SESSION_PURGE", "false"); |
---|
| 167 | |
---|
| 168 | test_chroot_base<chroot_file>::test_setup_env(source, expected); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void setup_keyfile_file(sbuild::keyfile &expected, const std::string group) |
---|
| 172 | { |
---|
| 173 | expected.set_value(group, "type", "file"); |
---|
| 174 | expected.set_value(group, "file", "/srv/chroot/example.tar.bz2"); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void test_setup_keyfile() |
---|
| 178 | { |
---|
| 179 | sbuild::keyfile expected; |
---|
| 180 | const std::string group(chroot->get_name()); |
---|
| 181 | setup_keyfile_chroot(expected, group); |
---|
| 182 | setup_keyfile_source(expected, group); |
---|
| 183 | setup_keyfile_file(expected, group); |
---|
| 184 | |
---|
| 185 | test_chroot_base<chroot_file>::test_setup_keyfile |
---|
| 186 | (chroot, expected, group); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void test_setup_keyfile_session() |
---|
| 190 | { |
---|
| 191 | sbuild::keyfile expected; |
---|
| 192 | const std::string group(session->get_name()); |
---|
| 193 | setup_keyfile_session(expected, group); |
---|
| 194 | setup_keyfile_file(expected, group); |
---|
| 195 | expected.set_value(group, "name", "test-session-name"); |
---|
| 196 | expected.set_value(group, "file-repack", "false"); |
---|
| 197 | expected.set_value(group, "mount-location", "/mnt/mount-location"); |
---|
| 198 | setup_keyfile_session_clone(expected, group); |
---|
| 199 | |
---|
| 200 | test_chroot_base<chroot_file>::test_setup_keyfile |
---|
| 201 | (session, expected, group); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void test_setup_keyfile_source() |
---|
| 205 | { |
---|
| 206 | sbuild::keyfile expected; |
---|
| 207 | const std::string group(source->get_name()); |
---|
| 208 | setup_keyfile_chroot(expected, group); |
---|
| 209 | setup_keyfile_source_clone(expected, group); |
---|
| 210 | setup_keyfile_file(expected, group); |
---|
| 211 | expected.set_value(group, "description", chroot->get_description() + ' ' + _("(source chroot)")); |
---|
| 212 | |
---|
| 213 | test_chroot_base<chroot_file>::test_setup_keyfile |
---|
| 214 | (source, expected, group); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void test_session_flags() |
---|
| 218 | { |
---|
| 219 | CPPUNIT_ASSERT(chroot->get_session_flags() == |
---|
| 220 | (sbuild::chroot::SESSION_CREATE | |
---|
| 221 | sbuild::chroot::SESSION_CLONE)); |
---|
| 222 | |
---|
| 223 | CPPUNIT_ASSERT(session->get_session_flags() == |
---|
| 224 | sbuild::chroot::SESSION_PURGE); |
---|
| 225 | |
---|
| 226 | CPPUNIT_ASSERT(source->get_session_flags() == |
---|
| 227 | sbuild::chroot::SESSION_CREATE); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | void test_print_details() |
---|
| 231 | { |
---|
| 232 | std::ostringstream os; |
---|
| 233 | os << chroot; |
---|
| 234 | // TODO: Compare output. |
---|
| 235 | CPPUNIT_ASSERT(!os.str().empty()); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void test_print_config() |
---|
| 239 | { |
---|
| 240 | std::ostringstream os; |
---|
| 241 | sbuild::keyfile config; |
---|
| 242 | config << chroot; |
---|
| 243 | os << config; |
---|
| 244 | // TODO: Compare output. |
---|
| 245 | CPPUNIT_ASSERT(!os.str().empty()); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | void test_run_setup_scripts() |
---|
| 249 | { |
---|
| 250 | CPPUNIT_ASSERT(chroot->get_run_setup_scripts()); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | }; |
---|
| 254 | |
---|
| 255 | CPPUNIT_TEST_SUITE_REGISTRATION(test_chroot_file); |
---|