Move trigger tests into pytest

Note that we split the tests between endpoints/building and buildtrigger
This commit is contained in:
Joseph Schorr 2018-07-19 11:10:19 -04:00
parent a5dc885fc6
commit 306e79e493
3 changed files with 79 additions and 104 deletions

View file

@ -0,0 +1,25 @@
import re
import pytest
from buildtrigger.triggerutil import matches_ref
@pytest.mark.parametrize('ref, filt, matches', [
('ref/heads/master', '.+', True),
('ref/heads/master', 'heads/.+', True),
('ref/heads/master', 'heads/master', True),
('ref/heads/slash/branch', 'heads/slash/branch', True),
('ref/heads/slash/branch', 'heads/.+', True),
('ref/heads/foobar', 'heads/master', False),
('ref/heads/master', 'tags/master', False),
('ref/heads/master', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)', True),
('ref/heads/alpha', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)', True),
('ref/heads/beta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)', True),
('ref/heads/gamma', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)', True),
('ref/heads/delta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)', False),
])
def test_matches_ref(ref, filt, matches):
assert matches_ref(ref, re.compile(filt)) == matches

View file

@ -1,6 +1,7 @@
import pytest import pytest
from data import model from data import model
from buildtrigger.triggerutil import raise_if_skipped_build, SkipRequestException
from endpoints.building import (start_build, PreparedBuild, MaximumBuildsQueuedException, from endpoints.building import (start_build, PreparedBuild, MaximumBuildsQueuedException,
BuildTriggerDisabledException) BuildTriggerDisabledException)
@ -41,3 +42,56 @@ def test_start_build_disabled_trigger(app):
with pytest.raises(BuildTriggerDisabledException): with pytest.raises(BuildTriggerDisabledException):
start_build(trigger.repository, build) start_build(trigger.repository, build)
@pytest.mark.parametrize('ref, expected_tags', [
('ref/heads/somebranch', ['somebranch']),
('ref/heads/master', ['master', 'latest']),
('ref/tags/somebranch', ['somebranch']),
('ref/tags/master', ['master', 'latest']),
('ref/heads/slash/branch', ['slash_branch']),
('ref/tags/slash/tag', ['slash_tag']),
('ref/heads/foobar#2', ['foobar_2']),
])
def test_tags_for_ref(ref, expected_tags):
prepared = PreparedBuild()
prepared.tags_from_ref(ref, default_branch='master')
assert set(prepared._tags) == set(expected_tags)
@pytest.mark.parametrize('metadata, config', [
({}, {}),
pytest.param({'ref': 'ref/heads/master'}, {'branchtag_regex': 'nothing'}, id='branchtag regex'),
pytest.param({
'ref': 'ref/heads/master',
'commit_info': {
'message': '[skip build]',
},
}, {}, id='commit message'),
])
def test_skip(metadata, config):
prepared = PreparedBuild()
prepared.metadata = metadata
config = config
with pytest.raises(SkipRequestException):
raise_if_skipped_build(prepared, config)
def test_does_not_skip():
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
'commit_info': {
'message': 'some cool message',
},
}
config = {
'branchtag_regex': '(master)|(heads/master)',
}
raise_if_skipped_build(prepared, config)

View file

@ -1,104 +0,0 @@
import unittest
import re
from buildtrigger.triggerutil import matches_ref, raise_if_skipped_build
from buildtrigger.triggerutil import SkipRequestException
from endpoints.building import PreparedBuild
class TestRegex(unittest.TestCase):
def assertDoesNotMatch(self, ref, filt):
self.assertFalse(matches_ref(ref, re.compile(filt)))
def assertMatches(self, ref, filt):
self.assertTrue(matches_ref(ref, re.compile(filt)))
def test_matches_ref(self):
self.assertMatches('ref/heads/master', '.+')
self.assertMatches('ref/heads/master', 'heads/.+')
self.assertMatches('ref/heads/master', 'heads/master')
self.assertMatches('ref/heads/slash/branch', 'heads/slash/branch')
self.assertMatches('ref/heads/slash/branch', 'heads/.+')
self.assertDoesNotMatch('ref/heads/foobar', 'heads/master')
self.assertDoesNotMatch('ref/heads/master', 'tags/master')
self.assertMatches('ref/heads/master', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
self.assertMatches('ref/heads/alpha', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
self.assertMatches('ref/heads/beta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
self.assertMatches('ref/heads/gamma', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
self.assertDoesNotMatch('ref/heads/delta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
class TestTags(unittest.TestCase):
def assertTagsForRef(self, ref, tags):
prepared = PreparedBuild()
prepared.tags_from_ref(ref, default_branch='master')
self.assertEquals(set(tags), set(prepared._tags))
def test_normal(self):
self.assertTagsForRef('ref/heads/somebranch', ['somebranch'])
self.assertTagsForRef('ref/heads/master', ['master', 'latest'])
self.assertTagsForRef('ref/tags/somebranch', ['somebranch'])
self.assertTagsForRef('ref/tags/master', ['master', 'latest'])
def test_slash(self):
self.assertTagsForRef('ref/heads/slash/branch', ['slash_branch'])
self.assertTagsForRef('ref/tags/slash/tag', ['slash_tag'])
def test_unusual(self):
self.assertTagsForRef('ref/heads/foobar#2', ['foobar_2'])
class TestSkipBuild(unittest.TestCase):
def testSkipNoMetadata(self):
prepared = PreparedBuild()
prepared.metadata = {}
config = {}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testSkipByBranchtagRegex(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
}
config = {
'branchtag_regex': 'nothing'
}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testSkipByMessage(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
'commit_info': {
'message': '[skip build]',
},
}
config = {}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testDoesNotSkip(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
'commit_info': {
'message': 'some cool message',
},
}
config = {
'branchtag_regex': '(master)|(heads/master)',
}
raise_if_skipped_build(prepared, config)
if __name__ == '__main__':
unittest.main()