|
| 1 | +# Copyright 2021 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +import os |
| 17 | +import time |
| 18 | +import unittest |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +from kubernetes import client |
| 22 | +from kubernetes.client.rest import ApiException |
| 23 | + |
| 24 | +from ..leaderelectionrecord import LeaderElectionRecord |
| 25 | +from .leaselock import LeaseLock |
| 26 | + |
| 27 | + |
| 28 | +def make_lock(): |
| 29 | + with mock.patch.object(client, 'CoordinationV1Api'): |
| 30 | + lock = LeaseLock('lock', 'default', 'candidate') |
| 31 | + lock.api_instance = mock.MagicMock() |
| 32 | + return lock |
| 33 | + |
| 34 | + |
| 35 | +UTC = datetime.timezone.utc |
| 36 | + |
| 37 | + |
| 38 | +class LeaseLockTest(unittest.TestCase): |
| 39 | + |
| 40 | + def test_create_writes_the_election_record_to_the_spec(self): |
| 41 | + lock = make_lock() |
| 42 | + record = LeaderElectionRecord('candidate', '17', |
| 43 | + '2026-08-29 01:02:03.456789', |
| 44 | + '2026-08-29 01:02:03.456789') |
| 45 | + |
| 46 | + self.assertTrue(lock.create('lock', 'default', record)) |
| 47 | + |
| 48 | + namespace, body = lock.api_instance.create_namespaced_lease.call_args[0] |
| 49 | + self.assertEqual('default', namespace) |
| 50 | + self.assertEqual('lock', body.metadata.name) |
| 51 | + self.assertEqual('candidate', body.spec.holder_identity) |
| 52 | + self.assertEqual(17, body.spec.lease_duration_seconds) |
| 53 | + # stored as UTC on the wire, denoting the same instant as the |
| 54 | + # local wall clock the election record carries |
| 55 | + self.assertEqual(UTC, body.spec.acquire_time.tzinfo) |
| 56 | + self.assertEqual('2026-08-29 01:02:03.456789', |
| 57 | + str(body.spec.acquire_time.astimezone() |
| 58 | + .replace(tzinfo=None))) |
| 59 | + self.assertEqual(body.spec.acquire_time, body.spec.renew_time) |
| 60 | + |
| 61 | + def test_create_returns_false_when_the_api_fails(self): |
| 62 | + lock = make_lock() |
| 63 | + lock.api_instance.create_namespaced_lease.side_effect = ApiException( |
| 64 | + status=409, reason='Conflict') |
| 65 | + record = LeaderElectionRecord('candidate', '17', '2026-08-29 01:02:03', |
| 66 | + '2026-08-29 01:02:03') |
| 67 | + |
| 68 | + self.assertFalse(lock.create('lock', 'default', record)) |
| 69 | + |
| 70 | + def test_get_returns_the_exception_when_the_lease_is_missing(self): |
| 71 | + lock = make_lock() |
| 72 | + expected = ApiException(status=404, reason='Not Found') |
| 73 | + lock.api_instance.read_namespaced_lease.side_effect = expected |
| 74 | + |
| 75 | + status, response = lock.get('lock', 'default') |
| 76 | + |
| 77 | + self.assertFalse(status) |
| 78 | + self.assertIs(expected, response) |
| 79 | + |
| 80 | + def test_get_reads_the_election_record_from_the_lease(self): |
| 81 | + lock = make_lock() |
| 82 | + acquired = datetime.datetime(2026, 8, 29, 1, 2, 3, 456789) |
| 83 | + lock.api_instance.read_namespaced_lease.return_value = client.V1Lease( |
| 84 | + metadata={'name': 'lock'}, |
| 85 | + spec=client.V1LeaseSpec(holder_identity='candidate', |
| 86 | + lease_duration_seconds=17, |
| 87 | + acquire_time=acquired, |
| 88 | + renew_time=acquired)) |
| 89 | + |
| 90 | + status, record = lock.get('lock', 'default') |
| 91 | + |
| 92 | + self.assertTrue(status) |
| 93 | + self.assertEqual('candidate', record.holder_identity) |
| 94 | + self.assertEqual('17', record.lease_duration) |
| 95 | + self.assertEqual('2026-08-29 01:02:03.456789', record.acquire_time) |
| 96 | + |
| 97 | + def test_get_on_a_lease_without_a_spec_returns_an_empty_record(self): |
| 98 | + lock = make_lock() |
| 99 | + lock.api_instance.read_namespaced_lease.return_value = client.V1Lease( |
| 100 | + metadata={'name': 'lock'}, spec=None) |
| 101 | + |
| 102 | + status, record = lock.get('lock', 'default') |
| 103 | + |
| 104 | + self.assertTrue(status) |
| 105 | + self.assertIsNone(record.holder_identity) |
| 106 | + |
| 107 | + def test_record_survives_a_write_and_read_unchanged(self): |
| 108 | + """leaderelection.py compares the stored record with the observed one |
| 109 | + using __dict__, so a round trip has to come back identical.""" |
| 110 | + lock = make_lock() |
| 111 | + now = datetime.datetime.fromtimestamp(1787000000.5) |
| 112 | + record = LeaderElectionRecord('candidate', str(17), str(now), str(now)) |
| 113 | + |
| 114 | + spec = lock.get_lease_spec(record) |
| 115 | + read_back = lock.get_lock_object( |
| 116 | + client.V1Lease(metadata={'name': 'lock'}, spec=spec)) |
| 117 | + |
| 118 | + self.assertEqual(record.__dict__, read_back.__dict__) |
| 119 | + |
| 120 | + def test_record_without_microseconds_survives_a_write_and_read(self): |
| 121 | + """str(datetime) drops the microseconds when they are exactly zero.""" |
| 122 | + lock = make_lock() |
| 123 | + now = datetime.datetime(2026, 8, 29, 1, 2, 3) |
| 124 | + self.assertEqual('2026-08-29 01:02:03', str(now)) |
| 125 | + record = LeaderElectionRecord('candidate', str(17), str(now), str(now)) |
| 126 | + |
| 127 | + spec = lock.get_lease_spec(record) |
| 128 | + read_back = lock.get_lock_object( |
| 129 | + client.V1Lease(metadata={'name': 'lock'}, spec=spec)) |
| 130 | + |
| 131 | + self.assertEqual(record.__dict__, read_back.__dict__) |
| 132 | + |
| 133 | + def test_update_replaces_the_lease_it_read(self): |
| 134 | + lock = make_lock() |
| 135 | + acquired = datetime.datetime(2026, 8, 29, 1, 2, 3, 456789) |
| 136 | + lock.api_instance.read_namespaced_lease.return_value = client.V1Lease( |
| 137 | + metadata={'name': 'lock'}, |
| 138 | + spec=client.V1LeaseSpec(holder_identity='other', |
| 139 | + lease_duration_seconds=17, |
| 140 | + acquire_time=acquired, |
| 141 | + renew_time=acquired)) |
| 142 | + lock.get('lock', 'default') |
| 143 | + |
| 144 | + record = LeaderElectionRecord('candidate', '17', |
| 145 | + '2026-08-29 01:02:03.456789', |
| 146 | + '2026-08-29 02:03:04.567890') |
| 147 | + self.assertTrue(lock.update('lock', 'default', record)) |
| 148 | + |
| 149 | + body = lock.api_instance.replace_namespaced_lease.call_args[1]['body'] |
| 150 | + self.assertEqual('candidate', body.spec.holder_identity) |
| 151 | + self.assertEqual('2026-08-29 02:03:04.567890', |
| 152 | + str(body.spec.renew_time.astimezone() |
| 153 | + .replace(tzinfo=None))) |
| 154 | + |
| 155 | + def test_update_returns_false_when_the_api_fails(self): |
| 156 | + lock = make_lock() |
| 157 | + lock.lease_reference = client.V1Lease(metadata={'name': 'lock'}, |
| 158 | + spec=client.V1LeaseSpec()) |
| 159 | + lock.api_instance.replace_namespaced_lease.side_effect = ApiException( |
| 160 | + status=409, reason='Conflict') |
| 161 | + record = LeaderElectionRecord('candidate', '17', '2026-08-29 01:02:03', |
| 162 | + '2026-08-29 01:02:03') |
| 163 | + |
| 164 | + self.assertFalse(lock.update('lock', 'default', record)) |
| 165 | + |
| 166 | + @unittest.skipUnless(hasattr(time, 'tzset'), 'requires tzset') |
| 167 | + def test_times_are_written_as_the_real_utc_instant(self): |
| 168 | + """The election record holds local wall clock. The Lease is shared |
| 169 | + with other clients, so it has to carry the real UTC instant.""" |
| 170 | + lock = make_lock() |
| 171 | + previous = os.environ.get('TZ') |
| 172 | + os.environ['TZ'] = 'Asia/Kolkata' # UTC+05:30, no DST |
| 173 | + time.tzset() |
| 174 | + try: |
| 175 | + record = LeaderElectionRecord('candidate', '17', |
| 176 | + '2026-08-29 01:02:03.456789', |
| 177 | + '2026-08-29 01:02:03.456789') |
| 178 | + spec = lock.get_lease_spec(record) |
| 179 | + |
| 180 | + self.assertEqual( |
| 181 | + datetime.datetime(2026, 8, 28, 19, 32, 3, 456789, tzinfo=UTC), |
| 182 | + spec.acquire_time) |
| 183 | + # and it still round trips back to the local wall clock |
| 184 | + read_back = lock.get_lock_object( |
| 185 | + client.V1Lease(metadata={'name': 'lock'}, spec=spec)) |
| 186 | + self.assertEqual(record.__dict__, read_back.__dict__) |
| 187 | + finally: |
| 188 | + if previous is None: |
| 189 | + os.environ.pop('TZ', None) |
| 190 | + else: |
| 191 | + os.environ['TZ'] = previous |
| 192 | + time.tzset() |
| 193 | + |
| 194 | + def test_update_before_get_or_create_does_not_raise(self): |
| 195 | + lock = make_lock() |
| 196 | + record = LeaderElectionRecord('candidate', '17', '2026-08-29 01:02:03', |
| 197 | + '2026-08-29 01:02:03') |
| 198 | + |
| 199 | + self.assertFalse(lock.update('lock', 'default', record)) |
| 200 | + |
| 201 | + def test_an_unparsable_time_is_reported(self): |
| 202 | + lock = make_lock() |
| 203 | + with self.assertRaises(ValueError): |
| 204 | + lock.time_str_to_iso('not a time') |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == '__main__': |
| 208 | + unittest.main() |
0 commit comments