From 86106035dc542065f5bdabe920c2674c4e9f53db Mon Sep 17 00:00:00 2001 From: Adam Cmiel Date: Tue, 23 Apr 2019 13:58:39 +0200 Subject: [PATCH] Add unit tests for hub_containerbuild plugin * OSBS-7139 Signed-off-by: Adam Cmiel --- test.sh | 5 ++++ tests/test_hub_containerbuild.py | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/test_hub_containerbuild.py diff --git a/test.sh b/test.sh index 7954895a..c579c407 100755 --- a/test.sh +++ b/test.sh @@ -73,5 +73,10 @@ if [[ $OS != "fedora" ]]; then $RUN $PYTHON get-pip.py fi +# "mock" koji-hub (just to prevent ImportError, actual mocking done in tests) +KOJIHUB_PATH='/usr/share/koji-hub' +$RUN mkdir -p "$KOJIHUB_PATH" +$RUN touch "$KOJIHUB_PATH/kojihub.py" + # Run tests $RUN pytest -vv tests --cov koji_containerbuild diff --git a/tests/test_hub_containerbuild.py b/tests/test_hub_containerbuild.py new file mode 100644 index 00000000..b55d6318 --- /dev/null +++ b/tests/test_hub_containerbuild.py @@ -0,0 +1,41 @@ +import koji +import pytest +from flexmock import flexmock + +from koji_containerbuild.plugins import hub_containerbuild + + +@pytest.mark.parametrize('admin_perms', [True, False]) +@pytest.mark.parametrize('priority', [1, 0, None, -1]) +def test_priority_permissions(priority, admin_perms): + src, target = 'source', 'target' + + task_opts = {} + if priority: + task_opts['priority'] = koji.PRIO_DEFAULT + priority + + session = flexmock() + (session + .should_receive('hasPerm') + .with_args('admin') + .and_return(admin_perms)) + hub_containerbuild.context = flexmock(session=session) + + should_succeed = priority is None or priority >= 0 or admin_perms + + kojihub = flexmock() + (kojihub + .should_receive('make_task') + .with_args('buildContainer', [src, target, {}], + channel='container', **task_opts) + .times(1 if should_succeed else 0)) + hub_containerbuild.kojihub = kojihub + + if should_succeed: + hub_containerbuild.buildContainer(src, target, priority=priority) + else: + with pytest.raises(koji.ActionNotAllowed) as exc_info: + hub_containerbuild.buildContainer(src, target, priority=priority) + + e = exc_info.value + assert str(e) == 'only admins may create high-priority tasks'