From d3e343a3bda79f8b200783bb8391cae66e522f70 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sun, 13 Nov 2016 19:16:20 +0000 Subject: [PATCH] AgePolicy: Fix bug in creating the age file Signed-off-by: Niels Thykier --- britney2/policies/policy.py | 2 +- .../age/missing-age-file/age-policy-urgencies | 1 + tests/test_policy.py | 33 +++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/policy-test-data/age/missing-age-file/age-policy-urgencies diff --git a/britney2/policies/policy.py b/britney2/policies/policy.py index 4a0e88b..2d936ee 100644 --- a/britney2/policies/policy.py +++ b/britney2/policies/policy.py @@ -326,7 +326,7 @@ class AgePolicy(BasePolicy): # If we using the legacy name, then just give up raise self.log("%s does not appear to exist. Creating it" % filename) - with open(filename, mode='wx', encoding='utf-8'): + with open(filename, mode='x', encoding='utf-8'): pass def _read_urgencies_file(self, britney): diff --git a/tests/policy-test-data/age/missing-age-file/age-policy-urgencies b/tests/policy-test-data/age/missing-age-file/age-policy-urgencies new file mode 100644 index 0000000..1bb8bf6 --- /dev/null +++ b/tests/policy-test-data/age/missing-age-file/age-policy-urgencies @@ -0,0 +1 @@ +# empty diff --git a/tests/test_policy.py b/tests/test_policy.py index 743112c..e6d569b 100644 --- a/tests/test_policy.py +++ b/tests/test_policy.py @@ -4,11 +4,12 @@ import os from britney2 import SuiteInfo, SourcePackage from britney2.excuse import Excuse from britney2.hints import HintParser -from britney2.policies.policy import RCBugPolicy, PolicyVerdict +from britney2.policies.policy import AgePolicy, RCBugPolicy, PolicyVerdict POLICY_DATA_BASE_DIR = os.path.join(os.path.dirname(__file__), 'policy-test-data') TEST_HINTER = 'test-hinter' HINTS_ALL = ('ALL') +DEFAULT_URGENCY = 'medium' def initialize_policy(test_name, policy_class, *args, **kwargs): @@ -17,7 +18,7 @@ def initialize_policy(test_name, policy_class, *args, **kwargs): if 'hints' in kwargs: hints = kwargs['hints'] del kwargs['hints'] - options = MockObject(state_dir=test_dir, verbose=0, **kwargs) + options = MockObject(state_dir=test_dir, verbose=0, default_urgency=DEFAULT_URGENCY, **kwargs) suite_info = { 'testing': SuiteInfo('testing', os.path.join(test_dir, 'testing'), ''), 'unstable': SuiteInfo('unstable', os.path.join(test_dir, 'unstable'), ''), @@ -137,5 +138,33 @@ class TestRCBugsPolicy(unittest.TestCase): assert policy_info['rc-bugs']['ignored-bugs']['issued-by'] == TEST_HINTER assert set(policy_info['rc-bugs']['shared-bugs']) == set() + +class TestAgePolicy(unittest.TestCase): + + DEFAULT_MIN_DAYS = { + 'emergency': 0, + 'critical': 0, + 'high': 2, + 'medium': 5, + 'low': 10, + } + + def test_missing_age_file(self): + age_file = os.path.join(POLICY_DATA_BASE_DIR, 'age', 'missing-age-file', 'age-policy-dates') + assert not os.path.exists(age_file) + + try: + src_name = 'unlisted-source-package' + src_t, src_u, excuse, policy_info = create_policy_objects(src_name, '1.0', '2.0') + policy = initialize_policy('age/missing-age-file', AgePolicy, TestAgePolicy.DEFAULT_MIN_DAYS) + verdict = policy.apply_policy(policy_info, 'unstable', src_name, src_t, src_u, excuse) + assert os.path.exists(age_file) + assert verdict == PolicyVerdict.REJECTED_TEMPORARILY + assert policy_info['age']['age-requirement'] == TestAgePolicy.DEFAULT_MIN_DAYS[DEFAULT_URGENCY] + assert policy_info['age']['current-age'] == 0 + finally: + if os.path.exists(age_file): + os.unlink(age_file) + if __name__ == '__main__': unittest.main()