Add shared managed lock broker
This commit is contained in:
@@ -257,6 +257,18 @@ class NativeReviewTests(unittest.TestCase):
|
||||
lease.deadline_monotonic = time.monotonic() + 30
|
||||
(wt / 'a.txt').write_text('dirty')
|
||||
with self.assertRaises(m.ValidationError): lease.check(p)
|
||||
# Every failed source validation must close all transient FDs.
|
||||
before_fds = len(list((Path('/proc/self/fd')).iterdir()))
|
||||
for _ in range(20):
|
||||
with self.assertRaises(m.ValidationError): lease.check(p)
|
||||
after_fds = len(list((Path('/proc/self/fd')).iterdir()))
|
||||
self.assertLessEqual(after_fds, before_fds + 1)
|
||||
(wt / 'a.txt').unlink()
|
||||
for directory in [wt.parent.parent, wt.parent, wt]:
|
||||
directory.chmod(0o770)
|
||||
with self.subTest(directory=directory), self.assertRaises(m.ValidationError):
|
||||
lease.check(p)
|
||||
directory.chmod(0o700)
|
||||
finally:
|
||||
os.close(fd)
|
||||
def test_cli_preflight_and_saved_wire_verification_are_read_only(self):
|
||||
@@ -538,6 +550,41 @@ class NativeReviewTests(unittest.TestCase):
|
||||
with self.assertRaises(m.ValidationError): m.publish(p, before, FakeAPI(p, before, after, exact), lease, directory)
|
||||
self.assertFalse(directory.exists())
|
||||
|
||||
def test_broker_issue_lease_accepts_authenticated_matching_status(self):
|
||||
m = self.module(); p, before, after, exact = publication_fixture()
|
||||
now = time.monotonic()
|
||||
lease = m.BrokerIssueLease('/trusted/handle.json', '/trusted/policy.md', now + 30)
|
||||
handle = {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'role': 'gitea-review-agent',
|
||||
'pid': 1234, 'deadline': now + 20, 'token': 'not-returned', 'socket': '/trusted/broker.sock'}
|
||||
status = {'status': 'held', 'owner': 'acme', 'repository': 'repo', 'issue': 3,
|
||||
'role': 'gitea-review-agent', 'pid': 1234, 'deadline': handle['deadline']}
|
||||
from unittest.mock import patch
|
||||
with patch.object(m._broker, 'read_handle_metadata', return_value={k: v for k, v in handle.items() if k != 'token'}), \
|
||||
patch.object(m._broker, 'request_handle', return_value=status), \
|
||||
patch.object(m, '_validate_registered_source') as source:
|
||||
self.assertIsNone(lease.check(p))
|
||||
source.assert_called_once_with(p, '/trusted/policy.md', lease.deadline_monotonic)
|
||||
|
||||
def test_broker_issue_lease_rejects_wrong_identity_status_or_deadline(self):
|
||||
m = self.module(); p, before, after, exact = publication_fixture(); now = time.monotonic()
|
||||
lease = m.BrokerIssueLease('/trusted/handle.json', '/trusted/policy.md', now + 30)
|
||||
handle = {'pid': 1234, 'deadline': now + 20, 'token': 'hidden', 'socket': '/trusted/broker.sock'}
|
||||
cases = [
|
||||
{'status': 'held', 'owner': 'acme', 'repository': 'repo', 'issue': 3, 'role': 'gitea-spec-agent', 'pid': 1234, 'deadline': handle['deadline']},
|
||||
{'status': 'held', 'owner': 'other', 'repository': 'repo', 'issue': 3, 'role': 'gitea-review-agent', 'pid': 1234, 'deadline': handle['deadline']},
|
||||
{'status': 'released', 'owner': 'acme', 'repository': 'repo', 'issue': 3, 'role': 'gitea-review-agent', 'pid': 1234, 'deadline': handle['deadline']},
|
||||
{'status': 'held', 'owner': 'acme', 'repository': 'repo', 'issue': 3, 'role': 'gitea-review-agent', 'pid': 9999, 'deadline': handle['deadline']},
|
||||
{'status': 'held', 'owner': 'acme', 'repository': 'repo', 'issue': 3, 'role': 'gitea-review-agent', 'pid': 1234, 'deadline': now + 40},
|
||||
]
|
||||
from unittest.mock import patch
|
||||
for status in cases:
|
||||
with self.subTest(status=status):
|
||||
with patch.object(m._broker, 'read_handle_metadata', return_value={k: v for k, v in handle.items() if k != 'token'}), \
|
||||
patch.object(m._broker, 'request_handle', return_value=status), \
|
||||
patch.object(m, '_validate_registered_source') as source:
|
||||
with self.assertRaises(m.ValidationError): lease.check(p)
|
||||
source.assert_not_called()
|
||||
|
||||
def test_journal_intent_fields_are_validated_on_resume(self):
|
||||
m = self.module()
|
||||
for field in ['schema', 'head', 'event', 'body_sha256']:
|
||||
|
||||
Reference in New Issue
Block a user