2017-02-13 21:17:52 +00:00
|
|
|
import json
|
2017-02-13 18:50:14 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-02-13 20:01:09 +00:00
|
|
|
from buildtrigger.test.bitbucketmock import get_bitbucket_trigger
|
2017-02-13 21:17:52 +00:00
|
|
|
from buildtrigger.triggerutil import (SkipRequestException, ValidationRequestException,
|
|
|
|
InvalidPayloadException)
|
|
|
|
from endpoints.building import PreparedBuild
|
|
|
|
from util.morecollections import AttrDict
|
2017-02-13 18:50:14 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bitbucket_trigger():
|
2017-02-13 20:01:09 +00:00
|
|
|
return get_bitbucket_trigger()
|
|
|
|
|
|
|
|
|
|
|
|
def test_list_build_subdirs(bitbucket_trigger):
|
|
|
|
assert bitbucket_trigger.list_build_subdirs() == ['']
|
|
|
|
|
2017-02-13 18:50:14 +00:00
|
|
|
|
2017-02-13 20:01:09 +00:00
|
|
|
@pytest.mark.parametrize('subdir, contents', [
|
|
|
|
('', 'hello world'),
|
|
|
|
('somesubdir', 'hi universe'),
|
|
|
|
('unknownpath', None),
|
|
|
|
])
|
|
|
|
def test_load_dockerfile_contents(subdir, contents):
|
|
|
|
trigger = get_bitbucket_trigger(subdir)
|
|
|
|
assert trigger.load_dockerfile_contents() == contents
|
2017-02-13 18:50:14 +00:00
|
|
|
|
2017-02-13 21:17:52 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('payload, expected_error, expected_message', [
|
|
|
|
('{}', InvalidPayloadException, "'push' is a required property"),
|
|
|
|
|
|
|
|
# Valid payload:
|
|
|
|
('''{
|
|
|
|
"push": {
|
|
|
|
"changes": [{
|
|
|
|
"new": {
|
|
|
|
"name": "somechange",
|
|
|
|
"target": {
|
|
|
|
"hash": "aaaaaaa",
|
|
|
|
"message": "foo",
|
|
|
|
"date": "now",
|
|
|
|
"links": {
|
|
|
|
"html": {
|
|
|
|
"href": "somelink"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
"repository": {
|
|
|
|
"full_name": "foo/bar"
|
|
|
|
}
|
|
|
|
}''', None, None),
|
|
|
|
|
|
|
|
# Skip message:
|
|
|
|
('''{
|
|
|
|
"push": {
|
|
|
|
"changes": [{
|
|
|
|
"new": {
|
|
|
|
"name": "somechange",
|
|
|
|
"target": {
|
|
|
|
"hash": "aaaaaaa",
|
|
|
|
"message": "[skip build] foo",
|
|
|
|
"date": "now",
|
|
|
|
"links": {
|
|
|
|
"html": {
|
|
|
|
"href": "somelink"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
"repository": {
|
|
|
|
"full_name": "foo/bar"
|
|
|
|
}
|
|
|
|
}''', SkipRequestException, ''),
|
|
|
|
])
|
|
|
|
def test_handle_trigger_request(bitbucket_trigger, payload, expected_error, expected_message):
|
|
|
|
def get_payload():
|
|
|
|
return json.loads(payload)
|
|
|
|
|
|
|
|
request = AttrDict(dict(get_json=get_payload))
|
|
|
|
|
|
|
|
if expected_error is not None:
|
|
|
|
with pytest.raises(expected_error) as ipe:
|
|
|
|
bitbucket_trigger.handle_trigger_request(request)
|
|
|
|
assert ipe.value.message == expected_message
|
|
|
|
else:
|
|
|
|
assert isinstance(bitbucket_trigger.handle_trigger_request(request), PreparedBuild)
|