forked from mtusi2026/mtusi2026lab4
47 lines
2.1 KiB
Python
47 lines
2.1 KiB
Python
"""Regression checks for private readiness probes; public client is unchanged."""
|
|
import os
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from common import Client, wait_ready
|
|
|
|
|
|
class ReadinessTests(unittest.TestCase):
|
|
def test_local_default_matches_published_address(self):
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
self.assertEqual(Client().base, 'http://127.0.0.1:8080')
|
|
|
|
def test_custom_environment_and_direct_backend_are_preserved(self):
|
|
with patch.dict(os.environ, {'BASE_URL': 'http://test.example:8080/'}):
|
|
self.assertEqual(Client().base, 'http://test.example:8080')
|
|
self.assertEqual(Client('http://api:8080').base, 'http://api:8080')
|
|
|
|
def test_one_ready_replica_does_not_satisfy_two(self):
|
|
with patch('common.Client') as client, patch('common.time.sleep'):
|
|
client.return_value.call.side_effect = [
|
|
{'headers': {'X-Instance-Id': name}}
|
|
for name in ('one', 'one', 'two')]
|
|
wait_ready(count=2)
|
|
self.assertEqual(client.return_value.call.call_count, 3)
|
|
|
|
def test_unavailable_interval_invalidates_old_observations(self):
|
|
with patch('common.Client') as client, patch('common.time.sleep'):
|
|
client.return_value.call.side_effect = [
|
|
{'headers': {'X-Instance-Id': 'old'}},
|
|
AssertionError('not ready'),
|
|
{'headers': {'X-Instance-Id': 'new-one'}},
|
|
{'headers': {'X-Instance-Id': 'new-two'}}]
|
|
wait_ready(count=2)
|
|
self.assertEqual(client.return_value.call.call_count, 4)
|
|
|
|
def test_timeout_reports_missing_replica_count(self):
|
|
with patch('common.Client') as client, patch('common.time.sleep'), \
|
|
patch('common.time.monotonic', side_effect=[0, 0, 2]):
|
|
client.return_value.call.return_value = {'headers': {'X-Instance-Id': 'one'}}
|
|
with self.assertRaisesRegex(RuntimeError, 'expected 2 distinct replicas, observed 1'):
|
|
wait_ready(count=2, seconds=1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|