From 96b7e606cf354b9ef0d2c790dda9a14a88b112f0 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sun, 13 Nov 2016 16:49:56 +0000 Subject: [PATCH] Add simple RC bugs unit test Signed-off-by: Niels Thykier --- .coveragerc | 6 ++ .travis.yml | 3 +- INSTALL | 1 + ci/britney-coverage.sh | 2 +- .../rc-bugs/basic/rc-bugs-testing | 2 + .../rc-bugs/basic/rc-bugs-unstable | 2 + tests/test_policy.py | 97 +++++++++++++++++++ 7 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 .coveragerc create mode 100644 tests/policy-test-data/rc-bugs/basic/rc-bugs-testing create mode 100644 tests/policy-test-data/rc-bugs/basic/rc-bugs-unstable create mode 100644 tests/test_policy.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..2937954 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,6 @@ +[run] +branch = True +omit = + *_boot*.py + */dist-packages/* + */tests/* diff --git a/.travis.yml b/.travis.yml index 1986e46..8c90a95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,10 @@ before_install: install: # install build dependencies - - sudo apt-get install -qq --no-install-recommends python3 python3-apt python3-yaml python3-coverage rsync libclass-accessor-perl + - sudo apt-get install -qq --no-install-recommends python3 python3-apt python3-yaml python3-coverage python3-nose rsync libclass-accessor-perl script: + - nosetests3 -v --with-coverage - britney2-tests/bin/runtests ./ci/britney-coverage.sh britney2-tests/t test-out - britney2-tests/bin/runtests ./britney.py britney2-tests/live-data test-out-live-data after_success: diff --git a/INSTALL b/INSTALL index e91baa7..4b1b199 100644 --- a/INSTALL +++ b/INSTALL @@ -7,3 +7,4 @@ Requirements: * Python 3 aptitude install python3 * Python APT/DPKG bindings aptitude install python3-apt * Python YAML library aptitude install python3-yaml + * Python nose tests (testing) aptitude install python3-nose diff --git a/ci/britney-coverage.sh b/ci/britney-coverage.sh index 0f03310..cb21c42 100755 --- a/ci/britney-coverage.sh +++ b/ci/britney-coverage.sh @@ -3,4 +3,4 @@ # This script is used on (e.g.) Travis CI to collect coverage dir=$(dirname "$(dirname "$0")") -exec python3-coverage run --omit '*/yaml/*.py' --branch --append "$dir/britney.py" "$@" +exec python3-coverage run --rcfile "$dir/.coveragerc" --append "$dir/britney.py" "$@" diff --git a/tests/policy-test-data/rc-bugs/basic/rc-bugs-testing b/tests/policy-test-data/rc-bugs/basic/rc-bugs-testing new file mode 100644 index 0000000..be0f3ea --- /dev/null +++ b/tests/policy-test-data/rc-bugs/basic/rc-bugs-testing @@ -0,0 +1,2 @@ +fixes-bug 123456 +not-a-regression 123457 diff --git a/tests/policy-test-data/rc-bugs/basic/rc-bugs-unstable b/tests/policy-test-data/rc-bugs/basic/rc-bugs-unstable new file mode 100644 index 0000000..bfa48ca --- /dev/null +++ b/tests/policy-test-data/rc-bugs/basic/rc-bugs-unstable @@ -0,0 +1,2 @@ +not-a-regression 123457 +regression 123458 diff --git a/tests/test_policy.py b/tests/test_policy.py new file mode 100644 index 0000000..c1e2482 --- /dev/null +++ b/tests/test_policy.py @@ -0,0 +1,97 @@ +import unittest +import os + +from britney2 import SuiteInfo, SourcePackage +from britney2.excuse import Excuse +from britney2.hints import HintParser +from britney2.policies.policy import RCBugPolicy, PolicyVerdict + +POLICY_DATA_BASE_DIR = os.path.join(os.path.dirname(__file__), 'policy-test-data') + + +def initialize_policy(test_name, policy_class, *args, **kwargs): + test_dir = os.path.join(POLICY_DATA_BASE_DIR, test_name) + options = MockObject(state_dir=test_dir, verbose=0, **kwargs) + suite_info = { + 'testing': SuiteInfo('testing', os.path.join(test_dir, 'testing'), ''), + 'unstable': SuiteInfo('unstable', os.path.join(test_dir, 'unstable'), ''), + } + policy = policy_class(options, suite_info, *args) + fake_britney = MockObject(log=lambda x, y='I': None) + hint_parser = HintParser(fake_britney) + policy.initialise(fake_britney) + policy.register_hints(hint_parser) + policy.hints = hint_parser.hints + return policy + + +def create_excuse(name): + return Excuse(name) + + +def create_source_package(version, section='devel', binaries=None): + if binaries is None: + binaries = [] + return SourcePackage(version, section, binaries, 'Random tester', False) + + +def create_policy_objects(source_name, target_version, source_version): + return ( + create_source_package(target_version), + create_source_package(source_version), + create_excuse(source_name), + {}, + ) + + +class MockObject(object): + + def __init__(self, **kwargs): + for key, value in kwargs.items(): + setattr(self, key, value) + + +class TestRCBugsPolicy(unittest.TestCase): + + def test_no_bugs(self): + src_name = 'has-no-bugs' + src_t, src_u, excuse, policy_info = create_policy_objects(src_name, '1.0', '2.0') + policy = initialize_policy('rc-bugs/basic', RCBugPolicy) + verdict = policy.apply_policy(policy_info, 'unstable', src_name, src_t, src_u, excuse) + assert verdict == PolicyVerdict.PASS + assert set(policy_info['rc-bugs']['unique-source-bugs']) == set() + assert set(policy_info['rc-bugs']['unique-target-bugs']) == set() + assert set(policy_info['rc-bugs']['shared-bugs']) == set() + + def test_regression(self): + src_name = 'regression' + src_t, src_u, excuse, policy_info = create_policy_objects(src_name, '1.0', '2.0') + policy = initialize_policy('rc-bugs/basic', RCBugPolicy) + verdict = policy.apply_policy(policy_info, 'unstable', src_name, src_t, src_u, excuse) + assert verdict == PolicyVerdict.REJECTED_PERMANENTLY + assert set(policy_info['rc-bugs']['unique-source-bugs']) == {'123458'} + assert set(policy_info['rc-bugs']['unique-target-bugs']) == set() + assert set(policy_info['rc-bugs']['shared-bugs']) == set() + + def test_not_a_regression(self): + src_name = 'not-a-regression' + src_t, src_u, excuse, policy_info = create_policy_objects(src_name, '1.0', '2.0') + policy = initialize_policy('rc-bugs/basic', RCBugPolicy) + verdict = policy.apply_policy(policy_info, 'unstable', src_name, src_t, src_u, excuse) + assert verdict == PolicyVerdict.PASS + assert set(policy_info['rc-bugs']['unique-source-bugs']) == set() + assert set(policy_info['rc-bugs']['unique-target-bugs']) == set() + assert set(policy_info['rc-bugs']['shared-bugs']) == {'123457'} + + def test_improvement(self): + src_name = 'fixes-bug' + src_t, src_u, excuse, policy_info = create_policy_objects(src_name, '1.0', '2.0') + policy = initialize_policy('rc-bugs/basic', RCBugPolicy) + verdict = policy.apply_policy(policy_info, 'unstable', src_name, src_t, src_u, excuse) + assert verdict == PolicyVerdict.PASS + assert set(policy_info['rc-bugs']['unique-source-bugs']) == set() + assert set(policy_info['rc-bugs']['unique-target-bugs']) == {'123456'} + assert set(policy_info['rc-bugs']['shared-bugs']) == set() + +if __name__ == '__main__': + unittest.main()