/usr/lib/python3/dist-packages/plainbox/impl/test_ctrl.py is in python3-plainbox 0.5.3-2.
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 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 | # This file is part of Checkbox.
#
# Copyright 2012, 2013 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox 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 Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
plainbox.impl.test_ctrl
=======================
Test definitions for plainbox.impl.ctrl module
"""
from subprocess import CalledProcessError
from unittest import TestCase
import os
from plainbox.abc import IJobResult
from plainbox.abc import IProvider1
from plainbox.abc import IProviderBackend1
from plainbox.impl.applogic import PlainBoxConfig
from plainbox.impl.ctrl import CheckBoxExecutionController
from plainbox.impl.ctrl import CheckBoxSessionStateController
from plainbox.impl.ctrl import RootViaPTL1ExecutionController
from plainbox.impl.ctrl import RootViaPkexecExecutionController
from plainbox.impl.ctrl import RootViaSudoExecutionController
from plainbox.impl.ctrl import SymLinkNest
from plainbox.impl.ctrl import UserJobExecutionController
from plainbox.impl.ctrl import gen_rfc822_records_from_io_log
from plainbox.impl.depmgr import DependencyDuplicateError
from plainbox.impl.job import JobDefinition
from plainbox.impl.job import JobOutputTextSource
from plainbox.impl.resource import Resource
from plainbox.impl.resource import ResourceExpression
from plainbox.impl.secure.providers.v1 import Provider1
from plainbox.impl.secure.rfc822 import Origin
from plainbox.impl.secure.rfc822 import RFC822Record
from plainbox.impl.secure.rfc822 import RFC822SyntaxError
from plainbox.impl.session import JobReadinessInhibitor
from plainbox.impl.session import SessionState
from plainbox.vendor import extcmd
from plainbox.vendor import mock
class CheckBoxSessionStateControllerTests(TestCase):
def setUp(self):
self.ctrl = CheckBoxSessionStateController()
def test_get_dependency_set(self):
# Job with no dependencies
job_a = JobDefinition({})
self.assertEqual(
self.ctrl.get_dependency_set(job_a), set())
# Job with direct dependencies
job_b = JobDefinition({
'depends': 'j1, j2'
})
self.assertEqual(
self.ctrl.get_dependency_set(job_b),
{('direct', 'j1'), ('direct', 'j2')})
# Job with resouce dependencies
job_c = JobDefinition({
'requires': 'j3.attr == 1'
})
self.assertEqual(
self.ctrl.get_dependency_set(job_c),
{('resource', 'j3')})
# Job with both direct and resource dependencies
job_d = JobDefinition({
'depends': 'j4',
'requires': 'j5.attr == 1'
})
self.assertEqual(
self.ctrl.get_dependency_set(job_d),
{('direct', 'j4'), ('resource', 'j5')})
# Job with both direct and resource dependencies
# on the same job (j6)
job_e = JobDefinition({
'depends': 'j6',
'requires': 'j6.attr == 1'
})
self.assertEqual(
self.ctrl.get_dependency_set(job_e),
{('direct', 'j6'), ('resource', 'j6')})
def test_get_inhibitor_list_PENDING_RESOURCE(self):
# verify that jobs that require a resource that hasn't been
# invoked yet produce the PENDING_RESOURCE inhibitor
j1 = JobDefinition({
'id': 'j1',
'requires': 'j2.attr == "ok"'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
session_state.job_state_map['j2'].job = j2
session_state.resource_map = {}
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1),
[JobReadinessInhibitor(
JobReadinessInhibitor.PENDING_RESOURCE,
j2, ResourceExpression('j2.attr == "ok"'))])
def test_get_inhibitor_list_FAILED_RESOURCE(self):
# verify that jobs that require a resource that has been
# invoked and produced resources but the expression dones't
# evaluate to True produce the FAILED_RESOURCE inhibitor
j1 = JobDefinition({
'id': 'j1',
'requires': 'j2.attr == "ok"'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
session_state.job_state_map['j2'].job = j2
session_state.resource_map = {
'j2': [Resource({'attr': 'not-ok'})]
}
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1),
[JobReadinessInhibitor(
JobReadinessInhibitor.FAILED_RESOURCE,
j2, ResourceExpression('j2.attr == "ok"'))])
def test_get_inhibitor_list_good_resource(self):
# verify that jobs that require a resource that has been invoked and
# produced resources for which the expression evaluates to True don't
# have any inhibitors
j1 = JobDefinition({
'id': 'j1',
'requires': 'j2.attr == "ok"'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
session_state.resource_map = {
'j2': [Resource({'attr': 'ok'})]
}
session_state.job_state_map['j2'].job = j2
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1), [])
def test_get_inhibitor_list_PENDING_DEP(self):
# verify that jobs that depend on another job that hasn't
# been invoked yet produce the PENDING_DEP inhibitor
j1 = JobDefinition({
'id': 'j1',
'depends': 'j2'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
jsm_j2 = session_state.job_state_map['j2']
jsm_j2.job = j2
jsm_j2.result.outcome = IJobResult.OUTCOME_NONE
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1),
[JobReadinessInhibitor(
JobReadinessInhibitor.PENDING_DEP, j2, None)])
def test_get_inhibitor_list_FAILED_DEP(self):
# verify that jobs that depend on another job that ran but
# didn't result in OUTCOME_PASS produce the FAILED_DEP
# inhibitor.
j1 = JobDefinition({
'id': 'j1',
'depends': 'j2'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
jsm_j2 = session_state.job_state_map['j2']
jsm_j2.job = j2
jsm_j2.result.outcome = IJobResult.OUTCOME_FAIL
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1),
[JobReadinessInhibitor(
JobReadinessInhibitor.FAILED_DEP, j2, None)])
def test_get_inhibitor_list_good_dep(self):
# verify that jobs that depend on another job that ran and has outcome
# equal to OUTCOME_PASS don't have any inhibitors
j1 = JobDefinition({
'id': 'j1',
'depends': 'j2'
})
j2 = JobDefinition({
'id': 'j2'
})
session_state = mock.MagicMock(spec=SessionState)
jsm_j2 = session_state.job_state_map['j2']
jsm_j2.job = j2
jsm_j2.result.outcome = IJobResult.OUTCOME_PASS
self.assertEqual(
self.ctrl.get_inhibitor_list(session_state, j1), [])
def test_observe_result__normal(self):
job = mock.Mock(spec=JobDefinition)
result = mock.Mock(spec=IJobResult)
session_state = mock.MagicMock(spec=SessionState)
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
def test_observe_result__OUTCOME_NONE(self):
job = mock.Mock(spec=JobDefinition, plugin='resource')
result = mock.Mock(spec=IJobResult, outcome=IJobResult.OUTCOME_NONE)
session_state = mock.MagicMock(spec=SessionState)
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
# Ensure that a resource was *not* defined
self.assertEqual(session_state.set_resource_list.call_count, 0)
def test_observe_result__resource(self):
job = mock.Mock(spec=JobDefinition, plugin='resource')
result = mock.Mock(spec=IJobResult, outcome=IJobResult.OUTCOME_PASS)
result.get_io_log.return_value = [
(0, 'stdout', b'attr: value1\n'),
(0, 'stdout', b'\n'),
(0, 'stdout', b'attr: value2\n')]
session_state = mock.MagicMock(spec=SessionState)
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
# Ensure that new resource was defined
session_state.set_resource_list.assert_called_once_with(
job.id, [
Resource({'attr': 'value1'}), Resource({'attr': 'value2'})])
@mock.patch('plainbox.impl.ctrl.logger')
def test_observe_result__broken_resource(self, mock_logger):
job = mock.Mock(spec=JobDefinition, plugin='resource')
result = mock.Mock(spec=IJobResult, outcome=IJobResult.OUTCOME_PASS)
result.get_io_log.return_value = [(0, 'stdout', b'barf\n')]
session_state = mock.MagicMock(spec=SessionState)
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
# Ensure that a warning was logged
mock_logger.warning.assert_called_once_with(
"local script %s returned invalid RFC822 data: %s",
job, RFC822SyntaxError(
None, 1, "Unexpected non-empty line: 'barf\\n'"))
@mock.patch('plainbox.impl.ctrl.gen_rfc822_records_from_io_log')
def test_observe_result__local_typical(self, mock_gen):
"""
verify side effects of using observe_result() that would define a new
job
"""
# Create a session that knows about no jobs yet
# and happily adds jobs when add_job() gets called
session_state = mock.MagicMock(spec=SessionState)
session_state.add_job.side_effect = (
lambda new_job, recompute: new_job)
# Create a job of which result we'll be observing
job = mock.Mock(spec=JobDefinition, name='job', plugin='local')
# Create a result for the job we'll be observing
result = mock.Mock(
spec=IJobResult, name='result', outcome=IJobResult.OUTCOME_PASS)
# Mock what rfc822 parser returns
mock_gen.return_value = [mock.Mock(spec=RFC822Record, name='record')]
# Pretend that we are observing a 'result' of 'job'
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that new job was defined
session_state.add_job.assert_called_once_with(
job.create_child_job_from_record(), recompute=False)
# Ensure that we didn't try to change the origin of the new job
self.assertFalse(
job.create_child_job_from_record().update_origin.called)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
@mock.patch('plainbox.impl.ctrl.gen_rfc822_records_from_io_log')
@mock.patch('plainbox.impl.ctrl.logger')
def test_observe_result__local_imperfect_clash(
self, mock_logger, mock_gen):
"""
verify side effects of using observe_result() that would define a
already existing job with the non-identical definition.
We basically hope to see the old job being there intact and a warning
to be logged.
"""
# Create a session that already knows about 'existing_job'
# and raises a DependencyDuplicateError when add_job() gets called.
existing_job = mock.Mock(spec=JobDefinition, name='existing_job')
existing_job.id = 'generated'
clashing_job = mock.Mock(spec=JobDefinition, name='existing_job')
clashing_job.id = 'generated'
session_state = mock.MagicMock(spec=SessionState, name='session_state')
session_state.add_job.side_effect = DependencyDuplicateError(
existing_job, clashing_job)
# Create a job of which result we'll be observing
job = mock.Mock(spec=JobDefinition, name='job', plugin='local')
# Have job return clashing_job when create_child_job_record() is called
job.create_child_job_from_record.return_value = clashing_job
# Create a result for the job we'll be observing
result = mock.Mock(
spec=IJobResult, name='result', outcome=IJobResult.OUTCOME_PASS)
# Mock what rfc822 parser returns
mock_gen.return_value = [mock.Mock(spec=RFC822Record, name='record')]
# Pretend that we are observing a 'result' of 'job'
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that we tried to define a new job by calling add_job() with
# the clashing_job as argument
session_state.add_job.assert_called_once_with(
clashing_job, recompute=False)
# Ensure that existing job origin was *not* updated
self.assertFalse(existing_job.update_origin.called)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
# Ensure that a warning was logged
mock_logger.warning.assert_called_once_with(
("Local job %s produced job %r that collides with"
" an existing job %s (from %s), the new job was"
" discarded"),
job, clashing_job, existing_job, existing_job.origin)
@mock.patch('plainbox.impl.ctrl.gen_rfc822_records_from_io_log')
def test_observe_result__local_perfect_clash(self, mock_gen):
"""
verify side effects of using observe_result() that would define a
already existing job with the exactly identical definition.
We basically hope to see the old job being there but the origin field
should be updated to reflect the new association between 'existing_job'
and 'job'
"""
# Create a session that already knows about 'existing_job'
# and returns existing_job when add_job() gets called.
existing_job = mock.Mock(spec=JobDefinition, name='existing_job')
existing_job.id = 'generated'
session_state = mock.MagicMock(spec=SessionState, name='session_state')
session_state.add_job.side_effect = (
lambda new_job, recompute: existing_job)
# Create a job of which result we'll be observing
job = mock.Mock(spec=JobDefinition, name='job', plugin='local')
# Create a result for the job we'll be observing
result = mock.Mock(
spec=IJobResult, name='result', outcome=IJobResult.OUTCOME_PASS)
# Mock what rfc822 parser returns
mock_gen.return_value = [mock.Mock(spec=RFC822Record, name='record')]
# Pretend that we are observing a 'result' of 'job'
self.ctrl.observe_result(session_state, job, result)
# Ensure that result got stored
self.assertIs(
session_state.job_state_map[job.id].result, result)
# Ensure that we tried to define a new job using
# whatever create_child_job_from_record() returns.
session_state.add_job.assert_called_once_with(
job.create_child_job_from_record(), recompute=False)
# Ensure that the origin of the existing_job was copied
# from the origin of the generated job
existing_job.update_origin.assert_called_once_with(
job.create_child_job_from_record().origin)
# Ensure that signals got fired
session_state.on_job_state_map_changed.assert_called_once_with()
session_state.on_job_result_changed.assert_called_once_with(
job, result)
class FunctionTests(TestCase):
"""
unit tests for gen_rfc822_records_from_io_log()
"""
def test_parse_typical(self):
"""
verify typical operation without any parsing errors
"""
# Setup a mock job and result, give some io log to the result
job = mock.Mock(spec=JobDefinition)
result = mock.Mock(spec=IJobResult)
result.get_io_log.return_value = [
(0, 'stdout', b'attr: value1\n'),
(0, 'stdout', b'\n'),
(0, 'stdout', b'attr: value2\n')]
# Parse the IO log records
records = list(gen_rfc822_records_from_io_log(job, result))
# Ensure that we saw both records
self.assertEqual(records, [
RFC822Record(
{'attr': 'value1'}, Origin(JobOutputTextSource(job), 1, 1)),
RFC822Record(
{'attr': 'value2'}, Origin(JobOutputTextSource(job), 3, 3)),
])
@mock.patch('plainbox.impl.ctrl.logger')
def test_parse_error(self, mock_logger):
# Setup a mock job and result, give some io log to the result
job = mock.Mock(spec=JobDefinition)
result = mock.Mock(spec=IJobResult)
result.get_io_log.return_value = [
(0, 'stdout', b'attr: value1\n'),
(0, 'stdout', b'\n'),
(0, 'stdout', b'error\n'),
(0, 'stdout', b'\n'),
(0, 'stdout', b'attr: value2\n')]
# Parse the IO log records
records = list(gen_rfc822_records_from_io_log(job, result))
# Ensure that only the first record was generated
self.assertEqual(records, [
RFC822Record(
{'attr': 'value1'}, Origin(JobOutputTextSource(job), 1, 1)),
])
# Ensure that a warning was logged
mock_logger.warning.assert_called_once_with(
"local script %s returned invalid RFC822 data: %s",
job, RFC822SyntaxError(
None, 3, "Unexpected non-empty line: 'error\\n'"))
class SymLinkNestTests(TestCase):
"""
Tests for SymLinkNest class
"""
NEST_DIR = "nest"
def setUp(self):
self.nest = SymLinkNest(self.NEST_DIR)
def test_init(self):
"""
verify that SymLinkNest.__init__() stores its argument
"""
self.assertEqual(self.nest._dirname, self.NEST_DIR)
def test_add_provider(self):
"""
verify that add_provider() adds each executable
"""
provider = mock.Mock(name='provider', spec=Provider1)
provider.get_all_executables.return_value = ['exec1', 'exec2']
with mock.patch.object(self.nest, 'add_executable'):
self.nest.add_provider(provider)
self.nest.add_executable.assert_has_calls([
(('exec1',), {}),
(('exec2',), {})])
provider.get_all_executables.assert_called_once()
@mock.patch('os.symlink')
def test_add_executable(self, mock_symlink):
self.nest.add_executable('/usr/lib/foo/exec')
mock_symlink.assert_called_with(
'/usr/lib/foo/exec', 'nest/exec')
class CheckBoxExecutionControllerTestsMixIn:
"""
Mix-in class that defines tests for CheckBoxExecutionController
"""
SESSION_DIR = 'session-dir'
PROVIDER_LIST = [] # we don't need any here
NEST_DIR = 'nest-dir' # used as fake data only
CLS = CheckBoxExecutionController
@mock.patch('plainbox.impl.ctrl.check_output')
def setUp(self, mock_check_output):
self.ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
# Create mocked job definition.
# Put a mocked provider on the job and give it some values for:
# * extra_PYTHONPATH (optional, set it to None),
# * CHECKBOX_SHARE (mandatory)
self.job = mock.Mock(
name='job',
spec=JobDefinition,
provider=mock.Mock(
name='provider',
# XXX: create a new type that has both IProvider1 and
# IProviderBackend1 API This type will define the API of the
# mocked provider object. We need to access IProvider1.name
# (for RootViaPTL1ExecutionController) but we also need to
# access many of IProviderBackend1 APIs for all the other code
# here. Instead of fusing the interfaces we can just pass a
# "concrete provider" "interface" just for this set of tests.
spec=type(
'IProvider1+IProviderBackend1',
(IProviderBackend1, IProvider1), {}),
extra_PYTHONPATH=None,
CHECKBOX_SHARE='CHECKBOX_SHARE',
data_dir='data_dir'))
# Create mocked config.
# Put an empty dictionary of environment overrides
# that is expected by get_execution_environment()
self.config = mock.Mock(
name='config',
spec=PlainBoxConfig,
environment={})
# Create a mocked extcmd_popen
self.extcmd_popen = mock.Mock(
name='extcmd_popen',
spec=extcmd.ExternalCommand)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_init(self, mock_check_output):
"""
verify that __init__() stores session_dir
"""
session_dir = mock.Mock()
provider_list = mock.Mock()
ctrl = self.CLS(session_dir, provider_list)
self.assertIs(ctrl._session_dir, session_dir)
self.assertIs(ctrl._provider_list, provider_list)
@mock.patch('os.path.isdir')
@mock.patch('os.makedirs')
def test_execute_job(self, mock_makedirs, mock_os_path_isdir):
"""
verify that execute_job() correctly glues all the basic pieces
"""
# Call the tested method, execute_job() but mock-away
# methods that we're not testing here,
# get_execution_{command,environment}() and configured_filesystem()
with mock.patch.object(self.ctrl, 'get_execution_command'), \
mock.patch.object(self.ctrl, 'get_execution_environment'), \
mock.patch.object(self.ctrl, 'configured_filesystem'):
retval = self.ctrl.execute_job(
self.job, self.config, self.extcmd_popen)
# Ensure that call was invoked with command end environment (passed
# as keyword argument). Extract the return value of
# configured_filesystem() as nest_dir so that we can pass it to
# other calls to get their mocked return values.
# Urgh! is this doable somehow without all that?
nest_dir = self.ctrl.configured_filesystem()
self.extcmd_popen.call.assert_called_with(
self.ctrl.get_execution_command(
self.job, self.config, nest_dir),
env=self.ctrl.get_execution_environment(
self.job, self.config, nest_dir))
# Ensure that execute_job() returns the return value of call()
self.assertEqual(retval, self.extcmd_popen.call())
# Ensure that presence of CHECKBOX_DATA directory was checked for
mock_os_path_isdir.assert_called_with(self.ctrl.CHECKBOX_DATA)
def test_get_score_for_random_jobs(self):
# Ensure that score for random jobs is -1
self.assertEqual(self.ctrl.get_score(mock.Mock()), -1)
def test_get_score_for_checkbox_jobs(self):
# Ensure that mock for JobDefinition (which is checkbox job in
# disguise) is whatever get_checkbox_score() returns.
with mock.patch.object(
self.ctrl, 'get_checkbox_score') as mock_get_checkbox_score:
self.assertEqual(
self.ctrl.get_score(mock.Mock(spec=JobDefinition)),
mock_get_checkbox_score())
def test_CHECKBOX_DATA(self):
"""
verify the value of CHECKBOX_DATA
"""
self.assertEqual(
self.ctrl.CHECKBOX_DATA, "session-dir/CHECKBOX_DATA")
class UserJobExecutionControllerTests(CheckBoxExecutionControllerTestsMixIn,
TestCase):
"""
Tests for UserJobExecutionController
"""
CLS = UserJobExecutionController
def test_get_command(self):
"""
verify that we simply execute the command via bash
"""
self.assertEqual(
self.ctrl.get_execution_command(
self.job, self.config, self.NEST_DIR),
['bash', '-c', self.job.command])
def test_get_checkbox_score_for_jobs_without_user(self):
"""
verify that score for jobs without user override is one
"""
self.job.user = None
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 1)
@mock.patch('os.getuid')
def test_get_checkbox_score_for_jobs_with_user(self, mock_getuid):
"""
verify that score for jobs with an user override is minus one
"""
# Ensure we're not root, in case test suite *is* run by root.
mock_getuid.return_value = 1000
self.job.user = 'root'
self.assertEqual(self.ctrl.get_checkbox_score(self.job), -1)
@mock.patch('os.getuid')
def test_get_checkbox_score_as_root(self, mock_getuid):
"""
verify that score for jobs with an user override is 4 if I am root
"""
mock_getuid.return_value = 0 # Pretend to be root
self.job.user = 'root'
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 4)
@mock.patch.dict('os.environ', clear=True)
def test_get_execution_environment_resets_LANG(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that LANG is rese to C.UTF-8
self.assertEqual(env['LANG'], 'C.UTF-8')
@mock.patch.dict('os.environ', clear=True, PYTHONPATH='PYTHONPATH')
def test_get_execution_environment_keeps_PYTHONPATH(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that extra_PYTHONPATH is preprended to PYTHONPATH
self.assertEqual(env['PYTHONPATH'], 'PYTHONPATH')
@mock.patch.dict('os.environ', clear=True)
def test_get_execution_environment_uses_extra_PYTHONPATH(self):
# Set a extra_PYTHONPATH on the provider object
self.job.provider.extra_PYTHONPATH = 'extra_PYTHONPATH'
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that extra_PYTHONPATH is preprended to PYTHONPATH
self.assertTrue(env['PYTHONPATH'].startswith(
self.job.provider.extra_PYTHONPATH))
@mock.patch.dict('os.environ', clear=True, PYTHONPATH='PYTHONPATH')
def test_get_execution_environment_merges_PYTHONPATH(self):
# Set a extra_PYTHONPATH on the provider object
self.job.provider.extra_PYTHONPATH = 'extra_PYTHONPATH'
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that extra_PYTHONPATH is preprended to PYTHONPATH
self.assertTrue(env['PYTHONPATH'].startswith(
self.job.provider.extra_PYTHONPATH))
self.assertTrue(env['PYTHONPATH'].endswith('PYTHONPATH'))
@mock.patch.dict('os.environ', clear=True)
def test_get_execution_environment_sets_CHECKBOX_SHARE(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that CHECKBOX_SHARE is set to what the job provider wants
self.assertEqual(
env['CHECKBOX_SHARE'], self.job.provider.CHECKBOX_SHARE)
@mock.patch.dict('os.environ', clear=True)
def test_get_execution_environment_sets_CHECKBOX_DATA(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that CHECKBOX_DATA is set to what the controller wants
self.assertEqual(env['CHECKBOX_DATA'], self.ctrl.CHECKBOX_DATA)
@mock.patch.dict('os.environ', clear=True)
def test_get_execution_environment_respects_config_environment(self):
self.config.environment['key'] = 'value'
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that key=value was passed to the environment
self.assertEqual(env['key'], 'value')
@mock.patch.dict('os.environ', clear=True, key='old-value')
def test_get_execution_environment_preferes_existing_environment(self):
self.config.environment['key'] = 'value'
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that 'old-value' takes priority over 'value'
self.assertEqual(env['key'], 'old-value')
class RootViaPTL1ExecutionControllerTests(
CheckBoxExecutionControllerTestsMixIn, TestCase):
"""
Tests for RootViaPTL1ExecutionController
"""
CLS = RootViaPTL1ExecutionController
def test_get_execution_environment_is_None(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that the environment is None
self.assertEqual(env, None)
@mock.patch.dict('os.environ', clear=True, PATH='vanilla-path')
def test_get_command(self):
"""
verify that we run plainbox-trusted-launcher-1 as the desired user
"""
self.job.get_environ_settings.return_value = []
self.job.origin.source.job = mock.Mock(
name='generator_job',
spec=JobDefinition,
provider=mock.Mock(
name='provider',
spec=IProviderBackend1,
extra_PYTHONPATH=None,
data_dir="data_dir-generator",
CHECKBOX_SHARE='CHECKBOX_SHARE-generator'))
PATH = os.pathsep.join([self.NEST_DIR, 'vanilla-path'])
expected = [
'pkexec', '--user', self.job.user,
'plainbox-trusted-launcher-1',
'--generator', self.job.via,
'-G', 'CHECKBOX_DATA=session-dir/CHECKBOX_DATA',
'-G', 'CHECKBOX_SHARE=CHECKBOX_SHARE-generator',
'-G', 'LANG=C.UTF-8',
'-G', 'PATH={}'.format(PATH),
'-G', 'PLAINBOX_PROVIDER_DATA=data_dir-generator',
'-G', 'PLAINBOX_SESSION_SHARE=session-dir/CHECKBOX_DATA',
'--target', self.job.checksum,
'-T', 'CHECKBOX_DATA=session-dir/CHECKBOX_DATA',
'-T', 'CHECKBOX_SHARE=CHECKBOX_SHARE',
'-T', 'LANG=C.UTF-8',
'-T', 'PATH={}'.format(PATH),
'-T', 'PLAINBOX_PROVIDER_DATA=data_dir',
'-T', 'PLAINBOX_SESSION_SHARE=session-dir/CHECKBOX_DATA',
]
actual = self.ctrl.get_execution_command(
self.job, self.config, self.NEST_DIR)
self.assertEqual(actual, expected)
@mock.patch.dict('os.environ', clear=True, PATH='vanilla-path')
def test_get_command_without_via(self):
"""
verify that we run plainbox-trusted-launcher-1 as the desired user
"""
self.job.get_environ_settings.return_value = []
self.job.via = None
PATH = os.pathsep.join([self.NEST_DIR, 'vanilla-path'])
expected = [
'pkexec', '--user', self.job.user,
'plainbox-trusted-launcher-1',
'--target', self.job.checksum,
'-T', 'CHECKBOX_DATA=session-dir/CHECKBOX_DATA',
'-T', 'CHECKBOX_SHARE=CHECKBOX_SHARE',
'-T', 'LANG=C.UTF-8',
'-T', 'PATH={}'.format(PATH),
'-T', 'PLAINBOX_PROVIDER_DATA=data_dir',
'-T', 'PLAINBOX_SESSION_SHARE=session-dir/CHECKBOX_DATA',
]
actual = self.ctrl.get_execution_command(
self.job, self.config, self.NEST_DIR)
self.assertEqual(actual, expected)
def test_get_checkbox_score_for_other_providers(self):
# Ensure that the job provider is not Provider1
self.assertNotIsInstance(self.job.provider, Provider1)
# Ensure that we get a negative score of minus one
self.assertEqual(self.ctrl.get_checkbox_score(self.job), -1)
def test_get_checkbox_score_for_insecure_provider1(self):
# Assume that the job is coming from Provider1 provider
# but the provider itself is insecure
self.job.provider = mock.Mock(spec=Provider1, secure=False)
# Ensure that we get a negative score of minus one
self.assertEqual(self.ctrl.get_checkbox_score(self.job), -1)
def test_get_checkbox_score_for_secure_provider_and_user_job(self):
# Assume that the job is coming from Provider1 provider
# and the provider is secure
self.job.provider = mock.Mock(spec=Provider1, secure=True)
# Assume that the job runs as the current user
self.job.user = None
# Ensure that we get a neutral score of zero
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 0)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_get_checkbox_score_for_secure_provider_root_job_with_policy(
self, mock_check_output):
# Assume that the job is coming from Provider1 provider
# and the provider is secure
self.job.provider = mock.Mock(spec=Provider1, secure=True)
# Assume that the job runs as root
self.job.user = 'root'
# Ensure we get the right action id from pkaction(1)
mock_check_output.return_value = \
b"org.freedesktop.policykit.pkexec.run-plainbox-job\n"
# Ensure that we get a positive score of three
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
self.assertEqual(ctrl.get_checkbox_score(self.job), 3)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_get_checkbox_score_for_secure_provider_root_job_with_policy_2(
self, mock_check_output):
# Assume that the job is coming from Provider1 provider
# and the provider is secure
self.job.provider = mock.Mock(spec=Provider1, secure=True)
# Assume that the job runs as root
self.job.user = 'root'
# Ensure we get the right action id from pkaction(1) even with
# polikt version < 0.110 (pkaction always exists with status 1), see:
# https://bugs.freedesktop.org/show_bug.cgi?id=29936#attach_78263
mock_check_output.side_effect = CalledProcessError(
1, '', b"org.freedesktop.policykit.pkexec.run-plainbox-job\n")
# Ensure that we get a positive score of three
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
self.assertEqual(ctrl.get_checkbox_score(self.job), 3)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_get_checkbox_score_for_secure_provider_root_job_no_policy(
self, mock_check_output):
# Assume that the job is coming from Provider1 provider
# and the provider is secure
self.job.provider = mock.Mock(spec=Provider1, secure=True)
# Assume that the job runs as root
self.job.user = 'root'
# Ensure pkaction(1) return nothing
mock_check_output.return_value = "No action with action id BLAHBLAH"
# Ensure that we get a positive score of two
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
self.assertEqual(ctrl.get_checkbox_score(self.job), 0)
class RootViaPkexecExecutionControllerTests(
CheckBoxExecutionControllerTestsMixIn, TestCase):
"""
Tests for RootViaPkexecExecutionController
"""
CLS = RootViaPkexecExecutionController
def test_get_execution_environment_is_None(self):
# Call the tested method
env = self.ctrl.get_execution_environment(
self.job, self.config, self.NEST_DIR)
# Ensure that the environment is None
self.assertEqual(env, None)
@mock.patch.dict('os.environ', clear=True, PATH='vanilla-path')
def test_get_command(self):
"""
verify that we run env(1) + bash(1) as the target user
"""
self.job.get_environ_settings.return_value = []
self.assertEqual(
self.ctrl.get_execution_command(
self.job, self.config, self.NEST_DIR),
['pkexec', '--user', self.job.user,
'env',
'CHECKBOX_DATA=session-dir/CHECKBOX_DATA',
'CHECKBOX_SHARE=CHECKBOX_SHARE',
'LANG=C.UTF-8',
'PATH={}'.format(
os.pathsep.join([self.NEST_DIR, 'vanilla-path'])),
'PLAINBOX_PROVIDER_DATA=data_dir',
'PLAINBOX_SESSION_SHARE=session-dir/CHECKBOX_DATA',
'bash', '-c', self.job.command])
def test_get_checkbox_score_for_user_jobs(self):
# Assume that the job runs as the current user
self.job.user = None
# Ensure that we get a neutral score of zero
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 0)
def test_get_checkbox_score_for_root_jobs(self):
# Assume that the job runs as the root user
self.job.user = 'root'
# Ensure that we get a positive score of one
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 1)
class RootViaSudoExecutionControllerTests(
CheckBoxExecutionControllerTestsMixIn, TestCase):
"""
Tests for RootViaSudoExecutionController
"""
CLS = RootViaSudoExecutionController
@mock.patch.dict('os.environ', clear=True, PATH='vanilla-path')
def test_get_command(self):
"""
verify that we run sudo(8)
"""
self.job.get_environ_settings.return_value = []
self.assertEqual(
self.ctrl.get_execution_command(
self.job, self.config, self.NEST_DIR),
['sudo', '-u', self.job.user, 'env',
'CHECKBOX_DATA=session-dir/CHECKBOX_DATA',
'CHECKBOX_SHARE=CHECKBOX_SHARE',
'LANG=C.UTF-8',
'PATH={}'.format(
os.pathsep.join([self.NEST_DIR, 'vanilla-path'])),
'PLAINBOX_PROVIDER_DATA=data_dir',
'PLAINBOX_SESSION_SHARE=session-dir/CHECKBOX_DATA',
'bash', '-c', self.job.command])
SUDO, ADMIN = range(2)
# Mock gid's for 'sudo' and 'admin'
def fake_getgrnam(self, name):
if name == 'sudo':
return mock.Mock(gr_gid=self.SUDO)
elif name == 'admin':
return mock.Mock(gr_gid=self.ADMIN)
else:
raise ValueError("unexpected group name")
@mock.patch('grp.getgrnam')
@mock.patch('posix.getgroups')
def test_user_can_sudo__sudo_group(self, mock_getgroups, mock_getgrnam):
# Mock gid's for 'sudo' and 'admin'
mock_getgrnam.side_effect = self.fake_getgrnam
# Mock that the current user is a member of group 1 ('sudo')
mock_getgroups.return_value = [self.SUDO]
# Create a fresh execution controller
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
# Ensure that the user can use sudo
self.assertTrue(ctrl.user_can_sudo)
@mock.patch('grp.getgrnam')
@mock.patch('posix.getgroups')
def test_user_can_sudo__admin_group(self, mock_getgroups, mock_getgrnam):
sudo, admin = range(2)
# Mock gid's for 'sudo' and 'admin'
mock_getgrnam.side_effect = self.fake_getgrnam
# Mock that the current user is a member of group 1 ('admin')
mock_getgroups.return_value = [self.ADMIN]
# Create a fresh execution controller
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
# Ensure that the user can use sudo
self.assertTrue(ctrl.user_can_sudo)
@mock.patch('grp.getgrnam')
@mock.patch('posix.getgroups')
def test_user_can_sudo__no_groups(self, mock_getgroups, mock_getgrnam):
sudo, admin = range(2)
# Mock gid's for 'sudo' and 'admin'
mock_getgrnam.side_effect = self.fake_getgrnam
# Mock that the current user not a member of any group
mock_getgroups.return_value = []
# Create a fresh execution controller
ctrl = self.CLS(self.SESSION_DIR, self.PROVIDER_LIST)
# Ensure that the user can use sudo
self.assertFalse(ctrl.user_can_sudo)
def test_get_checkbox_score_without_sudo(self):
# Assume that the user cannot use sudo
self.ctrl.user_can_sudo = False
# Ensure that we get a negative score for this controller
self.assertEqual(self.ctrl.get_checkbox_score(self.job), -1)
def test_get_checkbox_score_with_sudo(self):
# Assume that the user can use sudo
self.ctrl.user_can_sudo = True
# Ensure that we get a positive score for this controller
# The score is actually 2 to be better than the pkexec controller
self.assertEqual(self.ctrl.get_checkbox_score(self.job), 2)
def test_get_checkbox_score_for_non_root_jobs(self):
# Assume that the user can use sudo
self.ctrl.user_can_sudo = True
# But don't require root for the jobs itself
self.job.user = None
# Ensure that we get a negative score for this controller
self.assertEqual(self.ctrl.get_checkbox_score(self.job), -1)
|