2017-02-13 22:42:37 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
from buildtrigger.gitlabhandler import GitLabBuildTrigger
|
|
|
|
from util.morecollections import AttrDict
|
|
|
|
|
2017-03-21 21:24:11 +00:00
|
|
|
def get_gitlab_trigger(dockerfile_path=''):
|
2017-02-13 22:42:37 +00:00
|
|
|
trigger_obj = AttrDict(dict(auth_token='foobar', id='sometrigger'))
|
|
|
|
trigger = GitLabBuildTrigger(trigger_obj, {
|
|
|
|
'build_source': 'foo/bar',
|
2017-03-21 21:24:11 +00:00
|
|
|
'dockerfile_path': dockerfile_path,
|
2017-02-13 22:42:37 +00:00
|
|
|
'username': 'knownuser'
|
|
|
|
})
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
trigger._get_authorized_client = get_mock_gitlab(with_nulls=False)
|
2017-02-13 22:42:37 +00:00
|
|
|
return trigger
|
|
|
|
|
|
|
|
def adddeploykey_mock(project_id, name, public_key):
|
|
|
|
return {'id': 'foo'}
|
|
|
|
|
|
|
|
def addprojecthook_mock(project_id, webhook_url, push=False):
|
|
|
|
return {'id': 'foo'}
|
|
|
|
|
|
|
|
def get_currentuser_mock():
|
|
|
|
return {
|
|
|
|
'username': 'knownuser'
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:00:20 +00:00
|
|
|
def project(namespace, name, is_org=False):
|
2017-05-02 00:00:52 +00:00
|
|
|
project_access = None
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
if name != 'null':
|
|
|
|
if namespace == 'knownuser':
|
|
|
|
project_access = {
|
|
|
|
'access_level': 50,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
project_access = {
|
|
|
|
'access_level': 0,
|
|
|
|
}
|
2017-05-02 00:00:52 +00:00
|
|
|
|
2017-04-11 19:00:20 +00:00
|
|
|
data = {
|
2017-02-13 22:42:37 +00:00
|
|
|
'id': '%s/%s' % (namespace, name),
|
|
|
|
'default_branch': 'master',
|
|
|
|
'namespace': {
|
|
|
|
'id': namespace,
|
|
|
|
'path': namespace,
|
|
|
|
'name': namespace,
|
|
|
|
},
|
|
|
|
'path': name,
|
|
|
|
'path_with_namespace': '%s/%s' % (namespace, name),
|
|
|
|
'description': 'some %s repo' % name,
|
|
|
|
'last_activity_at': str(datetime.utcfromtimestamp(0)),
|
|
|
|
'web_url': 'https://bitbucket.org/%s/%s' % (namespace, name),
|
|
|
|
'ssh_url_to_repo': 'git://%s/%s' % (namespace, name),
|
|
|
|
'public': name != 'somerepo',
|
|
|
|
'permissions': {
|
2017-05-02 00:00:52 +00:00
|
|
|
'project_access': project_access,
|
2017-05-02 17:11:57 +00:00
|
|
|
'group_access': {'access_level': 0},
|
2017-02-13 22:42:37 +00:00
|
|
|
},
|
|
|
|
'owner': {
|
|
|
|
'avatar_url': 'avatarurl',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
if name == 'null':
|
2017-04-21 21:37:00 +00:00
|
|
|
del data['owner']['avatar_url']
|
|
|
|
data['namespace']['avatar'] = None
|
|
|
|
elif is_org:
|
2017-04-11 19:00:20 +00:00
|
|
|
del data['owner']['avatar_url']
|
|
|
|
data['namespace']['avatar'] = {'url': 'avatarurl'}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
def getprojects_mock(with_nulls=False):
|
|
|
|
if with_nulls:
|
2017-04-21 21:37:00 +00:00
|
|
|
def _getprojs(page=1, per_page=100):
|
|
|
|
return [
|
2017-05-02 17:11:57 +00:00
|
|
|
project('someorg', 'null', is_org=True),
|
2017-04-21 21:37:00 +00:00
|
|
|
]
|
|
|
|
return _getprojs
|
|
|
|
|
|
|
|
else:
|
|
|
|
def _getprojs(page=1, per_page=100):
|
|
|
|
return [
|
|
|
|
project('knownuser', 'somerepo'),
|
|
|
|
project('someorg', 'somerepo', is_org=True),
|
|
|
|
project('someorg', 'anotherrepo', is_org=True),
|
|
|
|
]
|
|
|
|
return _getprojs
|
2017-02-13 22:42:37 +00:00
|
|
|
|
|
|
|
def getproject_mock(project_name):
|
|
|
|
if project_name == 'knownuser/somerepo':
|
|
|
|
return project('knownuser', 'somerepo')
|
|
|
|
|
|
|
|
if project_name == 'foo/bar':
|
2017-04-11 19:00:20 +00:00
|
|
|
return project('foo', 'bar', is_org=True)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def getbranches_mock(project_id):
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'name': 'master',
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'otherbranch',
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
def getrepositorytags_mock(project_id):
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'name': 'sometag',
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'someothertag',
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
def getrepositorytree_mock(project_id, ref_name='master'):
|
|
|
|
return [
|
|
|
|
{'name': 'README'},
|
|
|
|
{'name': 'Dockerfile'},
|
|
|
|
]
|
|
|
|
|
|
|
|
def getrepositorycommit_mock(project_id, commit_sha):
|
|
|
|
if commit_sha != 'aaaaaaa':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
'message': 'some message',
|
|
|
|
'committed_date': 'now',
|
|
|
|
}
|
|
|
|
|
|
|
|
def getusers_mock(search=None):
|
|
|
|
if search == 'knownuser':
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'username': 'knownuser',
|
|
|
|
'avatar_url': 'avatarurl',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getbranch_mock(repo_id, branch):
|
|
|
|
if branch != 'master' and branch != 'otherbranch':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': branch,
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def gettag_mock(repo_id, tag):
|
|
|
|
if tag != 'sometag' and tag != 'someothertag':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': tag,
|
|
|
|
'commit': {
|
|
|
|
'id': 'aaaaaaa',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def getrawfile_mock(repo_id, branch_name, path):
|
2017-03-07 23:58:28 +00:00
|
|
|
if path == 'Dockerfile':
|
2017-02-13 22:42:37 +00:00
|
|
|
return 'hello world'
|
|
|
|
|
|
|
|
if path == 'somesubdir/Dockerfile':
|
|
|
|
return 'hi universe'
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
def get_mock_gitlab(with_nulls=False):
|
2017-04-21 21:37:00 +00:00
|
|
|
def _get_mock():
|
|
|
|
mock_gitlab = Mock()
|
|
|
|
mock_gitlab.host = 'https://bitbucket.org'
|
|
|
|
|
|
|
|
mock_gitlab.currentuser = Mock(side_effect=get_currentuser_mock)
|
|
|
|
mock_gitlab.getusers = Mock(side_effect=getusers_mock)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
mock_gitlab.getprojects = Mock(side_effect=getprojects_mock(with_nulls))
|
2017-04-21 21:37:00 +00:00
|
|
|
mock_gitlab.getproject = Mock(side_effect=getproject_mock)
|
|
|
|
mock_gitlab.getbranches = Mock(side_effect=getbranches_mock)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
mock_gitlab.getbranch = Mock(side_effect=getbranch_mock)
|
|
|
|
mock_gitlab.gettag = Mock(side_effect=gettag_mock)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
mock_gitlab.getrepositorytags = Mock(side_effect=getrepositorytags_mock)
|
|
|
|
mock_gitlab.getrepositorytree = Mock(side_effect=getrepositorytree_mock)
|
|
|
|
mock_gitlab.getrepositorycommit = Mock(side_effect=getrepositorycommit_mock)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
mock_gitlab.getrawfile = Mock(side_effect=getrawfile_mock)
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
mock_gitlab.adddeploykey = Mock(side_effect=adddeploykey_mock)
|
|
|
|
mock_gitlab.addprojecthook = Mock(side_effect=addprojecthook_mock)
|
|
|
|
mock_gitlab.deletedeploykey = Mock(return_value=True)
|
|
|
|
mock_gitlab.deleteprojecthook = Mock(return_value=True)
|
|
|
|
return mock_gitlab
|
2017-02-13 22:42:37 +00:00
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
return _get_mock
|