/usr/lib/python3/dist-packages/click/tests/test_database.py is in python3-click-package 0.4.43+16.04.20160203-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 | # Copyright (C) 2013 Canonical Ltd.
# Author: Colin Watson <cjwatson@ubuntu.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for click.database."""
from __future__ import print_function
__metaclass__ = type
__all__ = [
"TestClickDB",
"TestClickInstalledPackage",
"TestClickSingleDB",
]
from functools import partial
from itertools import takewhile
import json
import os
import unittest
from gi.repository import Click, GLib
from six import integer_types
from click.json_helpers import json_array_to_python, json_object_to_python
from click.tests.gimock_types import Passwd
from click.tests.helpers import TestCase, mkfile, touch
class TestClickInstalledPackage(TestCase):
def setUp(self):
super(TestClickInstalledPackage, self).setUp()
self.foo = Click.InstalledPackage.new(
"foo", "1.0", "/path/to/foo/1.0", False)
self.foo_clone = Click.InstalledPackage.new(
"foo", "1.0", "/path/to/foo/1.0", False)
self.foo_different_version = Click.InstalledPackage.new(
"foo", "2.0", "/path/to/foo/1.0", False)
self.foo_different_path = Click.InstalledPackage.new(
"foo", "1.0", "/path/to/foo/2.0", False)
self.foo_different_writeable = Click.InstalledPackage.new(
"foo", "1.0", "/path/to/foo/1.0", True)
self.bar = Click.InstalledPackage.new(
"bar", "1.0", "/path/to/foo/1.0", False)
def test_hash(self):
self.assertIsInstance(self.foo.hash(), integer_types)
self.assertEqual(self.foo.hash(), self.foo_clone.hash())
self.assertNotEqual(self.foo.hash(), self.foo_different_version.hash())
self.assertNotEqual(self.foo.hash(), self.foo_different_path.hash())
self.assertNotEqual(
self.foo.hash(), self.foo_different_writeable.hash())
self.assertNotEqual(self.foo.hash(), self.bar.hash())
# GLib doesn't allow passing an InstalledPackage as an argument here.
@unittest.expectedFailure
def test_equal_to(self):
self.assertTrue(self.foo.equal_to(self.foo_clone))
self.assertFalse(self.foo.equal_to(self.foo_different_version))
self.assertFalse(self.foo.equal_to(self.foo_different_path))
self.assertFalse(self.foo.equal_to(self.foo_different_writeable))
self.assertFalse(self.foo.equal_to(self.bar))
class TestClickSingleDB(TestCase):
def setUp(self):
super(TestClickSingleDB, self).setUp()
self.use_temp_dir()
self.master_db = Click.DB()
self.master_db.add(self.temp_dir)
self.db = self.master_db.get(self.master_db.props.size - 1)
self.spawn_calls = []
def g_spawn_sync_side_effect(self, status_map, working_directory, argv,
envp, flags, child_setup, user_data,
standard_output, standard_error, exit_status,
error):
self.spawn_calls.append(list(takewhile(lambda x: x is not None, argv)))
if argv[0] in status_map:
exit_status[0] = status_map[argv[0]]
else:
self.delegate_to_original("g_spawn_sync")
return 0
def _installed_packages_tuplify(self, ip):
return [(p.props.package, p.props.version, p.props.path) for p in ip]
def test_path(self):
path = os.path.join(self.temp_dir, "a", "1.0")
os.makedirs(path)
self.assertEqual(path, self.db.get_path("a", "1.0"))
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST, self.db.get_path, "a", "1.1")
def test_has_package_version(self):
os.makedirs(os.path.join(self.temp_dir, "a", "1.0"))
self.assertTrue(self.db.has_package_version("a", "1.0"))
self.assertFalse(self.db.has_package_version("a", "1.1"))
def test_packages_current(self):
os.makedirs(os.path.join(self.temp_dir, "a", "1.0"))
os.makedirs(os.path.join(self.temp_dir, "a", "1.1"))
a_current = os.path.join(self.temp_dir, "a", "current")
os.symlink("1.1", a_current)
os.makedirs(os.path.join(self.temp_dir, "b", "0.1"))
b_current = os.path.join(self.temp_dir, "b", "current")
os.symlink("0.1", b_current)
os.makedirs(os.path.join(self.temp_dir, "c", "2.0"))
self.assertEqual([
("a", "1.1", a_current),
("b", "0.1", b_current),
], self._installed_packages_tuplify(
self.db.get_packages(all_versions=False)))
def test_packages_all(self):
os.makedirs(os.path.join(self.temp_dir, "a", "1.0"))
os.makedirs(os.path.join(self.temp_dir, "a", "1.1"))
os.symlink("1.1", os.path.join(self.temp_dir, "a", "current"))
os.makedirs(os.path.join(self.temp_dir, "b", "0.1"))
os.symlink("0.1", os.path.join(self.temp_dir, "b", "current"))
os.makedirs(os.path.join(self.temp_dir, "c", "2.0"))
self.assertEqual([
("a", "1.0", os.path.join(self.temp_dir, "a", "1.0")),
("a", "1.1", os.path.join(self.temp_dir, "a", "1.1")),
("b", "0.1", os.path.join(self.temp_dir, "b", "0.1")),
("c", "2.0", os.path.join(self.temp_dir, "c", "2.0")),
], self._installed_packages_tuplify(
self.db.get_packages(all_versions=True)))
def test_packages_all_ignores_non_directory(self):
os.makedirs(os.path.join(self.temp_dir, "a", "1.0"))
touch(os.path.join(self.temp_dir, "file"))
self.assertEqual([
("a", "1.0", os.path.join(self.temp_dir, "a", "1.0")),
], self._installed_packages_tuplify(
self.db.get_packages(all_versions=True)))
def test_manifest(self):
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
manifest_obj = {
"name": "a", "version": "1.0", "hooks": {"a-app": {}},
"_should_be_removed": "",
}
with mkfile(manifest_path) as manifest:
json.dump(manifest_obj, manifest)
del manifest_obj["_should_be_removed"]
manifest_obj["_directory"] = os.path.join(self.temp_dir, "a", "1.0")
self.assertEqual(
manifest_obj,
json_object_to_python(self.db.get_manifest("a", "1.0")))
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST,
self.db.get_manifest, "a", "1.1")
self.assertEqual(
manifest_obj,
json.loads(self.db.get_manifest_as_string("a", "1.0")))
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST,
self.db.get_manifest_as_string, "a", "1.1")
def test_manifest_bad(self):
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
print("{bad syntax", file=manifest)
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST, self.db.get_manifest, "a", "1.0")
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST,
self.db.get_manifest_as_string, "a", "1.0")
manifest_path = os.path.join(
self.temp_dir, "a", "1.1", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
print("[0]", file=manifest)
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST, self.db.get_manifest, "a", "1.1")
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST,
self.db.get_manifest_as_string, "a", "1.1")
def test_app_running(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
preloads["click_find_on_path"].return_value = True
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 0})
self.assertTrue(self.db.app_running("foo", "bar", "1.0"))
self.assertEqual(
[[b"ubuntu-app-pid", b"foo_bar_1.0"]], self.spawn_calls)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 1 << 8})
self.assertFalse(self.db.app_running("foo", "bar", "1.0"))
def test_any_app_running_ubuntu_app_pid(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
preloads["click_find_on_path"].side_effect = (
lambda command: command == b"ubuntu-app-pid")
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 0})
self.assertTrue(self.db.any_app_running("a", "1.0"))
self.assertEqual(
[[b"ubuntu-app-pid", b"a_a-app_1.0"]], self.spawn_calls)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 1 << 8})
self.assertFalse(self.db.any_app_running("a", "1.0"))
def test_any_app_running_upstart_app_pid(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
preloads["click_find_on_path"].side_effect = (
lambda command: command == b"upstart-app-pid")
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"upstart-app-pid": 0})
self.assertTrue(self.db.any_app_running("a", "1.0"))
self.assertEqual(
[[b"upstart-app-pid", b"a_a-app_1.0"]], self.spawn_calls)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"upstart-app-pid": 1 << 8})
self.assertFalse(self.db.any_app_running("a", "1.0"))
def test_any_app_running_no_app_pid_command(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
preloads["click_find_on_path"].return_value = False
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 0})
self.assertFalse(self.db.any_app_running("a", "1.0"))
def test_any_app_running_missing_app(self):
with self.run_in_subprocess("click_find_on_path") as (enter, preloads):
enter()
preloads["click_find_on_path"].side_effect = (
lambda command: command == b"ubuntu-app-pid")
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST,
self.db.any_app_running, "a", "1.0")
def test_any_app_running_bad_manifest(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
print("{bad syntax", file=manifest)
preloads["click_find_on_path"].side_effect = (
lambda command: command == b"ubuntu-app-pid")
self.assertFalse(self.db.any_app_running("a", "1.0"))
self.assertFalse(preloads["g_spawn_sync"].called)
def test_any_app_running_no_hooks(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
manifest_path = os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({}, manifest)
preloads["click_find_on_path"].side_effect = (
lambda command: command == b"ubuntu-app-pid")
self.assertFalse(self.db.any_app_running("a", "1.0"))
self.assertFalse(preloads["g_spawn_sync"].called)
def test_maybe_remove_registered(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
version_path = os.path.join(self.temp_dir, "a", "1.0")
manifest_path = os.path.join(
version_path, ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
user_path = os.path.join(
self.temp_dir, ".click", "users", "test-user", "a")
os.makedirs(os.path.dirname(user_path))
os.symlink(version_path, user_path)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 0})
preloads["click_find_on_path"].return_value = True
self.db.maybe_remove("a", "1.0")
self.assertTrue(os.path.exists(version_path))
self.assertTrue(os.path.exists(user_path))
def test_maybe_remove_running(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
version_path = os.path.join(self.temp_dir, "a", "1.0")
manifest_path = os.path.join(
version_path, ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 0})
preloads["click_find_on_path"].return_value = True
self.db.maybe_remove("a", "1.0")
self.assertTrue(os.path.exists(version_path))
def test_maybe_remove_not_running(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync",
) as (enter, preloads):
enter()
os.environ["TEST_QUIET"] = "1"
version_path = os.path.join(self.temp_dir, "a", "1.0")
manifest_path = os.path.join(
version_path, ".click", "info", "a.manifest")
with mkfile(manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
current_path = os.path.join(self.temp_dir, "a", "current")
os.symlink("1.0", current_path)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 1 << 8})
preloads["click_find_on_path"].return_value = True
self.db.maybe_remove("a", "1.0")
self.assertFalse(os.path.exists(os.path.join(self.temp_dir, "a")))
def test_gc(self):
with self.run_in_subprocess(
"click_find_on_path", "g_spawn_sync", "getpwnam"
) as (enter, preloads):
enter()
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1)))
os.environ["TEST_QUIET"] = "1"
a_path = os.path.join(self.temp_dir, "a", "1.0")
a_manifest_path = os.path.join(
a_path, ".click", "info", "a.manifest")
with mkfile(a_manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
b_path = os.path.join(self.temp_dir, "b", "1.0")
b_manifest_path = os.path.join(
b_path, ".click", "info", "b.manifest")
with mkfile(b_manifest_path) as manifest:
json.dump({"hooks": {"b-app": {}}}, manifest)
c_path = os.path.join(self.temp_dir, "c", "1.0")
c_manifest_path = os.path.join(
c_path, ".click", "info", "c.manifest")
with mkfile(c_manifest_path) as manifest:
json.dump({"hooks": {"c-app": {}}}, manifest)
a_user_path = os.path.join(
self.temp_dir, ".click", "users", "test-user", "a")
os.makedirs(os.path.dirname(a_user_path))
os.symlink(a_path, a_user_path)
b_gcinuse_path = os.path.join(
self.temp_dir, ".click", "users", "@gcinuse", "b")
os.makedirs(os.path.dirname(b_gcinuse_path))
os.symlink(b_path, b_gcinuse_path)
preloads["g_spawn_sync"].side_effect = partial(
self.g_spawn_sync_side_effect, {b"ubuntu-app-pid": 1 << 8})
preloads["click_find_on_path"].return_value = True
self.db.gc()
self.assertTrue(os.path.exists(a_path))
self.assertFalse(os.path.exists(b_gcinuse_path))
self.assertFalse(os.path.exists(b_path))
self.assertFalse(os.path.exists(c_path))
def test_gc_ignores_non_directory(self):
with self.run_in_subprocess(
"getpwnam"
) as (enter, preloads):
enter()
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1)))
a_path = os.path.join(self.temp_dir, "a", "1.0")
a_manifest_path = os.path.join(
a_path, ".click", "info", "a.manifest")
with mkfile(a_manifest_path) as manifest:
json.dump({"hooks": {"a-app": {}}}, manifest)
a_user_path = os.path.join(
self.temp_dir, ".click", "users", "test-user", "a")
os.makedirs(os.path.dirname(a_user_path))
os.symlink(a_path, a_user_path)
touch(os.path.join(self.temp_dir, "file"))
self.db.gc()
self.assertTrue(os.path.exists(a_path))
# Test that bug #1479001 is fixed. Uses the following scenario:
#
# - Two databases: db1 and db2.
# - One package, "test-package":
# - Versions 1 and 3 installed in db1
# - Version 2 installed in db2
# - User has a registration in db2 for version 2, where the registration
# timestamp precedes the installation of version 3.
#
# In this case, bug #1479001 expects that the user's registration would
# be updated to 3, since it was installed after the user registered for
# 2, which implies that the user would like the update to 3.
def test_gc_fixes_old_user_registrations(self):
with self.run_in_subprocess("getpwnam") as (enter, preloads):
enter()
# Setup the system hook
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_dir=b"/foo")))
# Setup both databases
db1 = os.path.join(self.temp_dir, "db1")
db2 = os.path.join(self.temp_dir, "db2")
db = Click.DB()
db.add(db1)
db.add(db2)
# Prepare common manifest for the packages
manifest = {"hooks": {"test-app": {"test": "foo"}}}
# Setup versions 1.0 and 3.0 of package in db1
version1 = os.path.join(db1, "test-package", "1.0")
with mkfile(os.path.join(version1, ".click", "info",
"test-package.manifest")) as f:
json.dump(manifest, f)
version3 = os.path.join(db1, "test-package", "3.0")
with mkfile(os.path.join(version3, ".click", "info",
"test-package.manifest")) as f:
json.dump(manifest, f)
# Setup version 0.2 of package in db2
version2 = os.path.join(db2, "test-package", "2.0")
with mkfile(os.path.join(version2, ".click", "info",
"test-package.manifest")) as f:
json.dump(manifest, f)
# Setup the user registration for 2.0 in db2.
registrationPath = os.path.join(
db2, ".click", "users", "foo", "test-package")
os.makedirs(os.path.dirname(registrationPath))
os.symlink(version2, registrationPath)
# Run the garbage collection to update the registrations.
db.gc()
# Verify that the user still has a registration for the package,
# and that it's now registered for version 3.0.
self.assertTrue(os.path.lexists(registrationPath))
self.assertEqual(version3, os.readlink(registrationPath))
user_db = Click.User.for_user(db, "foo")
try:
version = user_db.get_version("test-package")
self.assertEqual("3.0", version)
except:
self.fail("No user registration for 'test-package'")
def _make_ownership_test(self):
path = os.path.join(self.temp_dir, "a", "1.0")
touch(os.path.join(path, ".click", "info", "a.manifest"))
os.symlink("1.0", os.path.join(self.temp_dir, "a", "current"))
user_path = os.path.join(
self.temp_dir, ".click", "users", "test-user", "a")
os.makedirs(os.path.dirname(user_path))
os.symlink(path, user_path)
touch(os.path.join(self.temp_dir, ".click", "log"))
def _set_stat_side_effect(self, preloads, side_effect, limit):
limit = limit.encode()
preloads["__xstat"].side_effect = (
lambda ver, path, buf: side_effect(
"__xstat", limit, ver, path, buf))
preloads["__xstat64"].side_effect = (
lambda ver, path, buf: side_effect(
"__xstat64", limit, ver, path, buf))
def test_ensure_ownership_quick_if_correct(self):
def stat_side_effect(name, limit, ver, path, buf):
st = self.convert_stat_pointer(name, buf)
if path == limit:
st.st_uid = 1
st.st_gid = 1
return 0
else:
self.delegate_to_original(name)
return -1
with self.run_in_subprocess(
"chown", "getpwnam", "__xstat", "__xstat64",
) as (enter, preloads):
enter()
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1)))
self._set_stat_side_effect(
preloads, stat_side_effect, self.db.props.root)
self._make_ownership_test()
self.db.ensure_ownership()
self.assertFalse(preloads["chown"].called)
def test_ensure_ownership(self):
def stat_side_effect(name, limit, ver, path, buf):
st = self.convert_stat_pointer(name, buf)
if path == limit:
st.st_uid = 2
st.st_gid = 2
return 0
else:
self.delegate_to_original(name)
return -1
with self.run_in_subprocess(
"chown", "getpwnam", "__xstat", "__xstat64",
) as (enter, preloads):
enter()
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1)))
self._set_stat_side_effect(
preloads, stat_side_effect, self.db.props.root)
self._make_ownership_test()
self.db.ensure_ownership()
expected_paths = [
self.temp_dir,
os.path.join(self.temp_dir, ".click"),
os.path.join(self.temp_dir, ".click", "log"),
os.path.join(self.temp_dir, ".click", "users"),
os.path.join(self.temp_dir, "a"),
os.path.join(self.temp_dir, "a", "1.0"),
os.path.join(self.temp_dir, "a", "1.0", ".click"),
os.path.join(self.temp_dir, "a", "1.0", ".click", "info"),
os.path.join(
self.temp_dir, "a", "1.0", ".click", "info", "a.manifest"),
os.path.join(self.temp_dir, "a", "current"),
]
self.assertCountEqual(
[path.encode() for path in expected_paths],
[args[0][0] for args in preloads["chown"].call_args_list])
self.assertCountEqual(
[(1, 1)],
set(args[0][1:] for args in preloads["chown"].call_args_list))
def test_ensure_ownership_missing_clickpkg_user(self):
with self.run_in_subprocess("getpwnam") as (enter, preloads):
enter()
preloads["getpwnam"].return_value = None
self.assertRaisesDatabaseError(
Click.DatabaseError.ENSURE_OWNERSHIP, self.db.ensure_ownership)
def test_ensure_ownership_failed_chown(self):
def stat_side_effect(name, limit, ver, path, buf):
st = self.convert_stat_pointer(name, buf)
if path == limit:
st.st_uid = 2
st.st_gid = 2
return 0
else:
self.delegate_to_original(name)
return -1
with self.run_in_subprocess(
"chown", "getpwnam", "__xstat", "__xstat64",
) as (enter, preloads):
enter()
preloads["chown"].return_value = -1
preloads["getpwnam"].side_effect = (
lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1)))
self._set_stat_side_effect(
preloads, stat_side_effect, self.db.props.root)
self._make_ownership_test()
self.assertRaisesDatabaseError(
Click.DatabaseError.ENSURE_OWNERSHIP, self.db.ensure_ownership)
class TestClickDB(TestCase):
def setUp(self):
super(TestClickDB, self).setUp()
self.use_temp_dir()
def _installed_packages_tuplify(self, ip):
return [
(p.props.package, p.props.version, p.props.path, p.props.writeable)
for p in ip]
def test_read_configuration(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = /a", file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = /b", file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
db.add("/c")
self.assertEqual(3, db.props.size)
self.assertEqual(
["/a", "/b", "/c"],
[db.get(i).props.root for i in range(db.props.size)])
def test_no_read(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = /a", file=a)
db = Click.DB()
self.assertEqual(0, db.props.size)
def test_no_db_conf_errors(self):
db = Click.DB()
self.assertRaisesDatabaseError(
Click.DatabaseError.INVALID, db.get, 0)
self.assertEqual(db.props.overlay, "")
self.assertRaisesDatabaseError(
Click.DatabaseError.INVALID, db.maybe_remove, "something", "1.0")
self.assertRaisesDatabaseError(
Click.DatabaseError.INVALID, db.gc)
self.assertRaisesDatabaseError(
Click.DatabaseError.INVALID, db.ensure_ownership)
def test_read_nonexistent(self):
db = Click.DB()
db.read(db_dir=os.path.join(self.temp_dir, "nonexistent"))
self.assertEqual(0, db.props.size)
def test_read_not_directory(self):
path = os.path.join(self.temp_dir, "file")
touch(path)
db = Click.DB()
self.assertRaisesFileError(GLib.FileError.NOTDIR, db.read, db_dir=path)
def test_add(self):
db = Click.DB()
self.assertEqual(0, db.props.size)
db.add("/new/root")
self.assertEqual(1, db.props.size)
self.assertEqual("/new/root", db.get(0).props.root)
def test_overlay(self):
with open(os.path.join(self.temp_dir, "00_custom.conf"), "w") as f:
print("[Click Database]", file=f)
print("root = /custom", file=f)
with open(os.path.join(self.temp_dir, "99_default.conf"), "w") as f:
print("[Click Database]", file=f)
print("root = /opt/click.ubuntu.com", file=f)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertEqual("/opt/click.ubuntu.com", db.props.overlay)
def test_path(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST, db.get_path, "pkg", "1.0")
os.makedirs(os.path.join(self.temp_dir, "a", "pkg", "1.0"))
self.assertEqual(
os.path.join(self.temp_dir, "a", "pkg", "1.0"),
db.get_path("pkg", "1.0"))
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST, db.get_path, "pkg", "1.1")
os.makedirs(os.path.join(self.temp_dir, "b", "pkg", "1.0"))
# The deepest copy of the same package/version is still preferred.
self.assertEqual(
os.path.join(self.temp_dir, "a", "pkg", "1.0"),
db.get_path("pkg", "1.0"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg", "1.1"))
self.assertEqual(
os.path.join(self.temp_dir, "b", "pkg", "1.1"),
db.get_path("pkg", "1.1"))
def test_has_package_version(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertFalse(db.has_package_version("pkg", "1.0"))
os.makedirs(os.path.join(self.temp_dir, "a", "pkg", "1.0"))
self.assertTrue(db.has_package_version("pkg", "1.0"))
self.assertFalse(db.has_package_version("pkg", "1.1"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg", "1.0"))
self.assertTrue(db.has_package_version("pkg", "1.0"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg", "1.1"))
self.assertTrue(db.has_package_version("pkg", "1.1"))
def test_packages_current(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertEqual([], list(db.get_packages(all_versions=False)))
os.makedirs(os.path.join(self.temp_dir, "a", "pkg1", "1.0"))
os.symlink("1.0", os.path.join(self.temp_dir, "a", "pkg1", "current"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg1", "1.1"))
pkg1_current = os.path.join(self.temp_dir, "b", "pkg1", "current")
os.symlink("1.1", pkg1_current)
os.makedirs(os.path.join(self.temp_dir, "b", "pkg2", "0.1"))
pkg2_current = os.path.join(self.temp_dir, "b", "pkg2", "current")
os.symlink("0.1", pkg2_current)
self.assertEqual([
("pkg1", "1.1", pkg1_current, True),
("pkg2", "0.1", pkg2_current, True),
], self._installed_packages_tuplify(
db.get_packages(all_versions=False)))
def test_packages_all(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertEqual([], list(db.get_packages(all_versions=True)))
os.makedirs(os.path.join(self.temp_dir, "a", "pkg1", "1.0"))
os.symlink("1.0", os.path.join(self.temp_dir, "a", "pkg1", "current"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg1", "1.1"))
os.symlink("1.1", os.path.join(self.temp_dir, "b", "pkg1", "current"))
os.makedirs(os.path.join(self.temp_dir, "b", "pkg2", "0.1"))
os.symlink("0.1", os.path.join(self.temp_dir, "b", "pkg2", "current"))
self.assertEqual([
("pkg1", "1.1", os.path.join(self.temp_dir, "b", "pkg1", "1.1"),
True),
("pkg2", "0.1", os.path.join(self.temp_dir, "b", "pkg2", "0.1"),
True),
("pkg1", "1.0", os.path.join(self.temp_dir, "a", "pkg1", "1.0"),
False),
], self._installed_packages_tuplify(
db.get_packages(all_versions=True)))
def test_manifest(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST, db.get_manifest, "pkg", "1.0")
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST,
db.get_manifest_as_string, "pkg", "1.0")
a_manifest_path = os.path.join(
self.temp_dir, "a", "pkg", "1.0", ".click", "info", "pkg.manifest")
a_manifest_obj = {"name": "pkg", "version": "1.0"}
with mkfile(a_manifest_path) as a_manifest:
json.dump(a_manifest_obj, a_manifest)
a_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "a", "pkg", "1.0")
self.assertEqual(
a_manifest_obj,
json_object_to_python(db.get_manifest("pkg", "1.0")))
self.assertEqual(
a_manifest_obj,
json.loads(db.get_manifest_as_string("pkg", "1.0")))
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST, db.get_manifest, "pkg", "1.1")
self.assertRaisesDatabaseError(
Click.DatabaseError.DOES_NOT_EXIST,
db.get_manifest_as_string, "pkg", "1.1")
b_manifest_path = os.path.join(
self.temp_dir, "b", "pkg", "1.1", ".click", "info", "pkg.manifest")
b_manifest_obj = {"name": "pkg", "version": "1.1"}
with mkfile(b_manifest_path) as b_manifest:
json.dump(b_manifest_obj, b_manifest)
b_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "b", "pkg", "1.1")
self.assertEqual(
b_manifest_obj,
json_object_to_python(db.get_manifest("pkg", "1.1")))
self.assertEqual(
b_manifest_obj,
json.loads(db.get_manifest_as_string("pkg", "1.1")))
def test_manifest_bad(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
db = Click.DB()
db.read(db_dir=self.temp_dir)
manifest_path = os.path.join(
self.temp_dir, "a", "pkg", "1.0", ".click", "info", "pkg.manifest")
with mkfile(manifest_path) as manifest:
print("{bad syntax", file=manifest)
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST, db.get_manifest, "pkg", "1.0")
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST,
db.get_manifest_as_string, "pkg", "1.0")
manifest_path = os.path.join(
self.temp_dir, "a", "pkg", "1.1", ".click", "info", "pkg.manifest")
with mkfile(manifest_path) as manifest:
print("[0]", file=manifest)
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST, db.get_manifest, "pkg", "1.0")
self.assertRaisesDatabaseError(
Click.DatabaseError.BAD_MANIFEST,
db.get_manifest_as_string, "pkg", "1.0")
def test_manifests_current(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertEqual(
[], json_array_to_python(db.get_manifests(all_versions=False)))
self.assertEqual(
[], json.loads(db.get_manifests_as_string(all_versions=False)))
a_pkg1_manifest_path = os.path.join(
self.temp_dir, "a", "pkg1", "1.0",
".click", "info", "pkg1.manifest")
a_pkg1_manifest_obj = {"name": "pkg1", "version": "1.0"}
with mkfile(a_pkg1_manifest_path) as a_pkg1_manifest:
json.dump(a_pkg1_manifest_obj, a_pkg1_manifest)
os.symlink("1.0", os.path.join(self.temp_dir, "a", "pkg1", "current"))
b_pkg1_manifest_path = os.path.join(
self.temp_dir, "b", "pkg1", "1.1",
".click", "info", "pkg1.manifest")
b_pkg1_manifest_obj = {"name": "pkg1", "version": "1.1"}
with mkfile(b_pkg1_manifest_path) as b_pkg1_manifest:
json.dump(b_pkg1_manifest_obj, b_pkg1_manifest)
os.symlink("1.1", os.path.join(self.temp_dir, "b", "pkg1", "current"))
b_pkg2_manifest_path = os.path.join(
self.temp_dir, "b", "pkg2", "0.1",
".click", "info", "pkg2.manifest")
b_pkg2_manifest_obj = {"name": "pkg2", "version": "0.1"}
with mkfile(b_pkg2_manifest_path) as b_pkg2_manifest:
json.dump(b_pkg2_manifest_obj, b_pkg2_manifest)
os.symlink("0.1", os.path.join(self.temp_dir, "b", "pkg2", "current"))
b_pkg1_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "b", "pkg1", "1.1")
b_pkg1_manifest_obj["_removable"] = 1
b_pkg2_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "b", "pkg2", "0.1")
b_pkg2_manifest_obj["_removable"] = 1
self.assertEqual(
[b_pkg1_manifest_obj, b_pkg2_manifest_obj],
json_array_to_python(db.get_manifests(all_versions=False)))
self.assertEqual(
[b_pkg1_manifest_obj, b_pkg2_manifest_obj],
json.loads(db.get_manifests_as_string(all_versions=False)))
def test_manifests_all(self):
with open(os.path.join(self.temp_dir, "a.conf"), "w") as a:
print("[Click Database]", file=a)
print("root = %s" % os.path.join(self.temp_dir, "a"), file=a)
with open(os.path.join(self.temp_dir, "b.conf"), "w") as b:
print("[Click Database]", file=b)
print("root = %s" % os.path.join(self.temp_dir, "b"), file=b)
db = Click.DB()
db.read(db_dir=self.temp_dir)
self.assertEqual(
[], json_array_to_python(db.get_manifests(all_versions=True)))
self.assertEqual(
[], json.loads(db.get_manifests_as_string(all_versions=True)))
a_pkg1_manifest_path = os.path.join(
self.temp_dir, "a", "pkg1", "1.0",
".click", "info", "pkg1.manifest")
a_pkg1_manifest_obj = {"name": "pkg1", "version": "1.0"}
with mkfile(a_pkg1_manifest_path) as a_pkg1_manifest:
json.dump(a_pkg1_manifest_obj, a_pkg1_manifest)
os.symlink("1.0", os.path.join(self.temp_dir, "a", "pkg1", "current"))
b_pkg1_manifest_path = os.path.join(
self.temp_dir, "b", "pkg1", "1.1",
".click", "info", "pkg1.manifest")
b_pkg1_manifest_obj = {"name": "pkg1", "version": "1.1"}
with mkfile(b_pkg1_manifest_path) as b_pkg1_manifest:
json.dump(b_pkg1_manifest_obj, b_pkg1_manifest)
os.symlink("1.1", os.path.join(self.temp_dir, "b", "pkg1", "current"))
b_pkg2_manifest_path = os.path.join(
self.temp_dir, "b", "pkg2", "0.1",
".click", "info", "pkg2.manifest")
b_pkg2_manifest_obj = {"name": "pkg2", "version": "0.1"}
with mkfile(b_pkg2_manifest_path) as b_pkg2_manifest:
json.dump(b_pkg2_manifest_obj, b_pkg2_manifest)
os.symlink("0.1", os.path.join(self.temp_dir, "b", "pkg2", "current"))
a_pkg1_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "a", "pkg1", "1.0")
a_pkg1_manifest_obj["_removable"] = 0
b_pkg1_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "b", "pkg1", "1.1")
b_pkg1_manifest_obj["_removable"] = 1
b_pkg2_manifest_obj["_directory"] = os.path.join(
self.temp_dir, "b", "pkg2", "0.1")
b_pkg2_manifest_obj["_removable"] = 1
self.assertEqual(
[b_pkg1_manifest_obj, b_pkg2_manifest_obj, a_pkg1_manifest_obj],
json_array_to_python(db.get_manifests(all_versions=True)))
self.assertEqual(
[b_pkg1_manifest_obj, b_pkg2_manifest_obj, a_pkg1_manifest_obj],
json.loads(db.get_manifests_as_string(all_versions=True)))
|