Add unit tests for smc_request_processsor

Finish creating tests for create_new_repo method and continue with
create_new_branch method.

Signed-off-by: Michal Konečný <mkonecny@redhat.com>
This commit is contained in:
Michal Konečný 2022-03-17 16:44:11 +01:00
parent 92497811d2
commit cc26011ee2
2 changed files with 689 additions and 10 deletions

View file

@ -766,3 +766,676 @@ class TestCreateNewRepo:
message="The Pagure project already exists",
reason="Invalid"
)
def test_create_new_repo_master_branch(self):
"""
Assert that ticket will be closed when branch is set to master branch.
Master branch is no longer allowed.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "master"
namespace = "rpms"
bug_id = "123"
action = "new_repo"
sls = {
"rawhide": "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
self.toddler.dist_git.get_project.return_value = None
self.toddler.create_new_repo(issue, json)
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message="Branch `master` cannot be created, please request the right branch.",
reason="Invalid"
)
def test_create_new_repo_unsupported_namespace(self):
"""
Assert that ticket will be closed when requested namespace is not recognized.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "rawhide"
namespace = "invalid"
bug_id = "123"
action = "new_repo"
sls = {
"rawhide": "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
self.toddler.dist_git.get_project.return_value = None
self.toddler.create_new_repo(issue, json)
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
error = ("The requested namespace '{0}' is not recognized. "
"Currently supported namespaces are: "
"rpms, container, flatpaks, modules, tests.".format(namespace))
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=error,
reason="Invalid"
)
@patch('toddlers.utils.pdc.get_branch')
def test_create_new_repo_branch_exist(self, mock_pdc_get_branch):
"""
Assert that ticket will be closed when branch already exist in PDC.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "rawhide"
namespace = "rpms"
bug_id = "123"
action = "new_repo"
sls = {
"rawhide": "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
self.toddler.dist_git.get_project.return_value = None
mock_pdc_get_branch.return_value = branch
self.toddler.create_new_repo(issue, json)
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
mock_pdc_get_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message="The PDC branch already exists",
reason="Invalid"
)
@pytest.mark.parametrize(
"namespace, branch",
[
("rpms", "rawhide"),
("container", "rawhide"),
("flatpaks", "stable"),
("modules", "rawhide"),
]
)
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_repo_namespaces(self, mock_pdc, namespace, branch):
"""
Assert that ticket will be processed when everything is in order and namespace is correct.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
bug_id = ""
action = "new_repo"
sls = {
branch: "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
dist_git_url = "https://src.fp.o"
self.toddler.dist_git.get_project.return_value = None
self.toddler.dist_git._pagure_url = dist_git_url
mock_pdc.get_branch.return_value = None
self.toddler.create_new_repo(issue, json)
# asserts
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
self.toddler.dist_git.new_project.assert_called_with(
namespace, repo, '', '', branch, initial_commit=True, alias=True
)
self.toddler.dist_git.set_monitoring_status.assert_called_with(
namespace, repo, monitor
)
self.toddler.dist_git.change_project_main_admin.assert_called_with(
namespace, repo, "zlopez"
)
mock_pdc.get_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
mock_pdc.new_global_component.assert_called_with(
repo, "{0}/{1}/{2}".format(dist_git_url, namespace, repo)
)
mock_pdc.new_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
mock_pdc.new_sla_to_branch.assert_called_with(
branch, sls[branch], repo, branch, namespace.rstrip('s')
)
message = "The Pagure repository was created at {0}/{1}/{2}".format(
dist_git_url, namespace, repo)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=message,
reason="Processed"
)
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_repo_tests_namespace(self, mock_pdc):
"""
Assert that ticket will be processed when everything is in order and namespace is set to tests.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "main"
namespace = "tests"
bug_id = ""
action = "new_repo"
sls = {
branch: "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
dist_git_url = "https://src.fp.o"
self.toddler.dist_git.get_project.return_value = None
self.toddler.dist_git._pagure_url = dist_git_url
mock_pdc.get_branch.return_value = None
self.toddler.create_new_repo(issue, json)
# asserts
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
self.toddler.dist_git.new_project.assert_called_with(
namespace, repo, '', '', branch, initial_commit=True, alias=True
)
self.toddler.dist_git.set_monitoring_status.assert_called_with(
namespace, repo, monitor
)
self.toddler.dist_git.change_project_main_admin.assert_called_with(
namespace, repo, "zlopez"
)
mock_pdc.get_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
message = "The Pagure repository was created at {0}/{1}/{2}".format(
dist_git_url, namespace, repo)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=message,
reason="Processed"
)
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_repo_non_default_branch(self, mock_pdc):
"""
Assert that ticket will be processed when everything is in order and requested branch is not default.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "f35"
namespace = "rpms"
bug_id = ""
action = "new_repo"
sls = {
branch: "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
self.toddler.branch_slas = {
"rawhide": {
"rawhide": "2050-06-01"
}
}
dist_git_url = "https://src.fp.o"
self.toddler.dist_git.get_project.return_value = None
self.toddler.dist_git._pagure_url = dist_git_url
mock_pdc.get_branch.return_value = None
self.toddler.create_new_repo(issue, json)
# asserts
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
self.toddler.dist_git.new_project.assert_called_with(
namespace, repo, '', '', "rawhide", initial_commit=True, alias=True
)
self.toddler.dist_git.set_monitoring_status.assert_called_with(
namespace, repo, monitor
)
self.toddler.dist_git.change_project_main_admin.assert_called_with(
namespace, repo, "zlopez"
)
mock_pdc.get_branch.assert_has_calls(
[
call(repo, "rawhide", namespace.rstrip('s')),
call(repo, branch, namespace.rstrip('s')),
]
)
mock_pdc.new_global_component.assert_called_with(
repo, "{0}/{1}/{2}".format(dist_git_url, namespace, repo)
)
mock_pdc.new_branch.assert_has_calls(
[
call(repo, "rawhide", namespace.rstrip('s')),
call(repo, branch, namespace.rstrip('s')),
]
)
mock_pdc.new_sla_to_branch.assert_has_calls(
[
call("rawhide", "2050-06-01", repo, "rawhide", namespace.rstrip('s')),
call(branch, sls[branch], repo, branch, namespace.rstrip('s')),
]
)
message = ('The Pagure repository was created at {0}/{1}/{2}. '
'You may commit to the branch "{3}" in about '
'10 minutes.'.format(dist_git_url, namespace, repo, branch)
)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=message,
reason="Processed"
)
@patch("toddlers.plugins.scm_request_processor.bugzilla_system")
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_repo_update_bug(self, mock_pdc, mock_bz):
"""
Assert that bug will be updated if the bug_id is provided.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "rawhide"
namespace = "rpms"
bug_id = "123"
action = "new_repo"
sls = {
branch: "2050-06-01"
}
monitor = "monitor"
exception = True
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"bug_id": bug_id,
"action": action,
"sls": sls,
"monitor": monitor,
"exception": exception
}
dist_git_url = "https://src.fp.o"
self.toddler.dist_git.get_project.return_value = None
self.toddler.dist_git._pagure_url = dist_git_url
mock_pdc.get_branch.return_value = None
self.toddler.create_new_repo(issue, json)
# asserts
self.toddler.dist_git.get_project.assert_called_with(
namespace, repo
)
self.toddler.dist_git.new_project.assert_called_with(
namespace, repo, '', '', branch, initial_commit=True, alias=True
)
self.toddler.dist_git.set_monitoring_status.assert_called_with(
namespace, repo, monitor
)
self.toddler.dist_git.change_project_main_admin.assert_called_with(
namespace, repo, "zlopez"
)
mock_pdc.get_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
mock_pdc.new_global_component.assert_called_with(
repo, "{0}/{1}/{2}".format(dist_git_url, namespace, repo)
)
mock_pdc.new_branch.assert_called_with(repo, branch, namespace.rstrip('s'))
mock_pdc.new_sla_to_branch.assert_called_with(
branch, sls[branch], repo, branch, namespace.rstrip('s')
)
message = "The Pagure repository was created at {0}/{1}/{2}".format(
dist_git_url, namespace, repo)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=message,
reason="Processed"
)
mock_bz.comment_on_bug.assert_called_with(bug_id, message)
class TestCreateNewBranch:
"""
Test class for `toddlers.plugins.scm_request_processor.SCMRequestProcessor.create_new_branch` method.
"""
def setup(self):
"""
Initialize toddler.
"""
self.toddler = scm_request_processor.SCMRequestProcessor()
self.toddler.pagure_io = Mock()
self.toddler.dist_git = Mock()
def test_create_new_branch_missing_required_key(self):
"""
Assert that ticket will be closed if required key is missing in request.
"""
issue = {
"id": 100,
}
self.toddler.create_new_branch(issue, {})
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message="Invalid body, missing required field: action",
reason="Invalid"
)
def test_create_new_branch_no_contributors(self):
"""
Assert that ticket will be closed if contributors are not retrieved.
"""
issue = {
"id": 100,
}
repo = "repo"
branch = "rawhide"
namespace = "rpms"
action = "new_branch"
sls = {
branch: "2050-06-01"
}
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"action": action,
"sls": sls,
}
self.toddler.dist_git.get_project_contributors.return_value = None
self.toddler.create_new_branch(issue, json)
# Asserts
self.toddler.dist_git.get_project_contributors.assert_called_with(namespace, repo)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message="The dist git repository doesn't exist",
reason="Invalid"
)
@patch("toddlers.plugins.scm_request_processor.SCMRequestProcessor.valid_epel_package")
def test_create_new_branch_invalid_epel(self, mock_valid_epel_package):
"""
Assert that ticket will be closed if repo is invalid EPEL repo.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "epel8"
namespace = "rpms"
action = "new_branch"
sls = {
"rawhide": "2050-06-01"
}
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"action": action,
"sls": sls,
}
mock_valid_epel_package.return_value = False
self.toddler.create_new_branch(issue, json)
# Asserts
self.toddler.dist_git.get_project_contributors.assert_called_with(namespace, repo)
mock_valid_epel_package.assert_called_with(
repo, branch
)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=scm_request_processor.INVALID_EPEL_ERROR,
reason="Invalid"
)
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_branch_branch_exists(self, mock_pdc):
"""
Assert that ticket will be closed if branch already exists in PDC.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "rawhide"
namespace = "rpms"
action = "new_branch"
sls = {
"rawhide": "2050-06-01"
}
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"action": action,
"sls": sls,
}
self.toddler.create_new_branch(issue, json)
# Asserts
self.toddler.dist_git.get_project_contributors.assert_called_with(namespace, repo)
mock_pdc.get_branch.assert_called_with(repo, branch, namespace.strip().rstrip('s'))
message = \
"The branch in PDC already exists, you can now create it yourself as follows:\n" \
"Check in the project's settings if you have activated the git hook preventing" \
"new git branches from being created and if you did, de-activate it.\n" \
"Then simply run in cloned repository: " \
"``git checkout -b <branch_name> && git push -u origin <branch_name>``.\n" \
"``<branch_name>`` is the name of the branch you requested. \n" \
"You only need to do this once and you can then use fedpkg as you normally do."
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message=message,
reason="Invalid"
)
@patch("toddlers.plugins.scm_request_processor.fedora_account")
@patch("toddlers.plugins.scm_request_processor.pdc")
def test_create_new_branch_requester_is_not_maintainer(self, mock_pdc, mock_fedora_account):
"""
Assert that ticket will be closed if requester is not a maintainer of package.
"""
issue = {
"id": 100,
"user": {
"name": "zlopez"
}
}
repo = "repo"
branch = "rawhide"
namespace = "rpms"
action = "new_branch"
sls = {
"rawhide": "2050-06-01"
}
json = {
"repo": repo,
"branch": branch,
"namespace": namespace,
"action": action,
"sls": sls,
}
self.toddler.dist_git.get_project_contributors.return_value = {
"users": {
"admin": [],
"commit": [],
"collaborators": [{
"user": "",
"branches": "rawhide"
}]
},
"groups": {
"admin": ["group"],
"commit": [],
"collaborators": [{
"user": "",
"branches": "rawhide"
}]
}
}
mock_pdc.get_branch.return_value = None
mock_fedora_account.user_member_of.return_value = False
self.toddler.create_new_branch(issue, json)
# Asserts
self.toddler.dist_git.get_project_contributors.assert_called_with(namespace, repo)
mock_pdc.get_branch.assert_called_with(repo, branch, namespace.strip().rstrip('s'))
mock_fedora_account.user_member_of.assert_called_with(
mock_fedora_account.get_user_by_username(), "group"
)
self.toddler.pagure_io.close_issue.assert_called_with(
100, namespace=scm_request_processor.PROJECT_NAMESPACE,
message="zlopez is not a maintainer of the {0} package".format(repo),
reason="Invalid"
)

View file

@ -419,9 +419,17 @@ class SCMRequestProcessor(ToddlerBase):
elif namespace in ['tests']:
default_branch = 'main'
else:
msg = (' WARNING: The requester requested a non default '
'branch.')
_log.warning(msg, fg='yellow')
error = ("The requested namespace '{0}' is not recognized. "
"Currently supported namespaces are: "
"rpms, container, flatpaks, modules, tests.".format(namespace))
_log.warning(error)
self.pagure_io.close_issue(
issue["id"],
namespace=PROJECT_NAMESPACE,
message=error,
reason="Invalid"
)
return
existing_branch = None
if branch_name != default_branch:
_log.info('- Checking if {0} default branch already exists in PDC.'.format(default_branch))
@ -459,14 +467,14 @@ class SCMRequestProcessor(ToddlerBase):
for sla, eol in self.branch_slas[default_branch].items():
_log.debug("Adding SLA {0}:{1} to branch '{2}' in PDC".format(sla, eol, default_branch))
pdc.new_sla_to_branch(
sla, eol, repo, default_branch, branch_type)
sla, eol, repo, default_branch, component_type)
_log.debug("Creating branch '{0}' in PDC".format(branch_name))
pdc.new_branch(repo, branch_name, branch_type)
pdc.new_branch(repo, branch_name, component_type)
for sla, eol in issue_body_json['sls'].items():
_log.debug("Adding SLA {0}:{1} to branch '{2}' in PDC".format(sla, eol, branch_name))
pdc.new_sla_to_branch(
sla, eol, repo, branch_name, branch_type)
sla, eol, repo, branch_name, component_type)
# Create the Pagure repo
_log.debug("Creating a repo '{0}' in dist-git".format(repo))
@ -515,7 +523,7 @@ class SCMRequestProcessor(ToddlerBase):
self.pagure_io.close_issue(
issue["id"],
namespace=PROJECT_NAMESPACE,
message="Invalid body, keys: %s missing" % key,
message="Invalid body, missing required field: %s" % key,
reason="Invalid"
)
return
@ -530,7 +538,7 @@ class SCMRequestProcessor(ToddlerBase):
self.pagure_io.close_issue(
issue["id"],
namespace=PROJECT_NAMESPACE,
message="The dist git repository doesn't exist ",
message="The dist git repository doesn't exist",
reason="Invalid"
)
return
@ -567,9 +575,7 @@ class SCMRequestProcessor(ToddlerBase):
return
issue_id = issue['id']
issue_title = issue['title'].strip()
issue_owner = issue['user']['name']
issue_ui_url = issue["full_url"]
# Check if the branch requestor is one of the maintainers or part of the groups
_log.info('- Checking if {0} is one of the maintainers of the package'.format(issue_owner))