Merge "Add test for locked branch filtering on validation"

This commit is contained in:
Zuul
2025-03-05 11:43:11 +00:00
committed by Gerrit Code Review
3 changed files with 65 additions and 2 deletions
+1
View File
@@ -382,6 +382,7 @@ class GithubDriverMock(GithubDriver):
additional_event_queues, git_url_with_auth):
super(GithubDriverMock, self).__init__()
self.registry = registry
self.test_config = test_config
self.changes = test_config.changes
self.config = config
self.upstream_root = upstream_root
+12 -2
View File
@@ -1291,8 +1291,17 @@ class FakeGithubClient(object):
def addProject(self, project):
owner, proj = project.name.split('/')
self._data.repos[(owner, proj)] = FakeRepository(
repo = FakeRepository(
project.name, self._data)
self._data.repos[(owner, proj)] = repo
github_config =\
self._data.fake_github_connection.driver.test_config.driver.github
if bprs := github_config.get('branch_protection_rules', {}
).get(project.name, {}):
for branch, rule_configs in bprs.items():
for rule_config in rule_configs:
repo._set_branch_protection(
branch, protected=True, **rule_config)
def addProjectByName(self, project_name):
owner, proj = project_name.split('/')
@@ -1523,7 +1532,8 @@ class FakeGithubConnection(githubconnection.GithubConnection):
client_manager_class = FakeGithubClientManager
def __init__(self, driver, connection_name, connection_config,
changes_db=None, upstream_root=None, git_url_with_auth=False):
changes_db=None, upstream_root=None,
git_url_with_auth=False):
super(FakeGithubConnection, self).__init__(driver, connection_name,
connection_config)
self.connection_name = connection_name
+52
View File
@@ -43,6 +43,7 @@ from tests.base import (
BaseTestCase,
ZuulGithubAppTestCase,
ZuulTestCase,
driver_config,
iterate_timeout,
okay_tracebacks,
simple_layout,
@@ -2072,6 +2073,57 @@ class TestGithubLockedBranches(ZuulTestCase):
self.assertEqual(0, len(tpc2.parsed_branch_config.keys()))
self.assertEqual(0, len(tpc3.parsed_branch_config.keys()))
@driver_config('github', branch_protection_rules={
'org/project2': {
'master': [
{'locked': True},
]
}
})
def test_exclude_locked_branches_startup(self):
# Make sure that we exclude locked branches on startup as well
# (this exercises a different code path in the branch cache).
tenant = self.scheds.first.sched.abide.tenants\
.get('tenant-one')
project1 = list(tenant.untrusted_projects)[0]
project2 = list(tenant.untrusted_projects)[1]
project3 = list(tenant.untrusted_projects)[2]
tpc1 = tenant.project_configs[project1.canonical_name]
tpc2 = tenant.project_configs[project2.canonical_name]
tpc3 = tenant.project_configs[project3.canonical_name]
# project 1 should have parsed master
self.assertIn('master', tpc1.parsed_branch_config.keys())
# project 1 should not have a master branch because it's locked
self.assertEqual(0, len(tpc2.parsed_branch_config.keys()))
# project 3 should not because it excludes unprotected
self.assertEqual(0, len(tpc3.parsed_branch_config.keys()))
class TestGithubLockedBranchesValidation(ZuulTestCase):
config_file = 'zuul-github-driver.conf'
tenant_config_file = 'config/locked-branches/main.yaml'
scheduler_count = 1
validate_tenants = ['tenant-one']
@driver_config('github', branch_protection_rules={
'org/project2': {
'master': [
{'locked': True},
]
}
})
def test_exclude_locked_branches_validation(self):
self.assertEqual(2, len(self.merge_job_history['cat']))
projects = {
x.payload['project']
for x in self.merge_job_history['cat']
}
self.assertEqual({'org/common-config', 'org/project1'},
projects)
class TestGithubWebhook(ZuulTestCase):
config_file = 'zuul-github-driver.conf'