|
1 | | -# Copyright 2024 The Kubernetes Authors. |
| 1 | +# Copyright 2026 The Kubernetes Authors. |
2 | 2 | # Licensed under the Apache License, Version 2.0 (the "License"). |
3 | 3 | # End-to-end tests for kubernetes.informer.SharedInformer. |
4 | 4 |
|
@@ -181,5 +181,45 @@ def test_resource_version_advances(self): |
181 | 181 | self.assertGreater(int(inf._resource_version), rv_before) |
182 | 182 |
|
183 | 183 |
|
| 184 | + def test_resync_fires_modified_for_existing_objects(self): |
| 185 | + """Periodic resync re-lists from the API server and fires MODIFIED for cached objects. |
| 186 | +
|
| 187 | + A short resync_period (5 s) is used so the test completes quickly. |
| 188 | + After the informer has cached the ConfigMap via the initial list, we |
| 189 | + wait for a MODIFIED event that is fired by the resync, verifying that |
| 190 | + the resync actually contacts the API server and triggers callbacks. |
| 191 | + """ |
| 192 | + name = "inf-rsync-" + _uid() |
| 193 | + self.api.create_namespaced_config_map(body=_cm(name), namespace="default") |
| 194 | + self.addCleanup(self._drop, name) |
| 195 | + |
| 196 | + added = threading.Event() |
| 197 | + resynced = threading.Event() |
| 198 | + |
| 199 | + inf = SharedInformer( |
| 200 | + list_func=self.api.list_namespaced_config_map, |
| 201 | + namespace="default", |
| 202 | + label_selector="inf-e2e=1", |
| 203 | + resync_period=5, |
| 204 | + ) |
| 205 | + inf.add_event_handler(ADDED, lambda o: added.set() if _name_of(o) == name else None) |
| 206 | + # The resync fires MODIFIED for existing cached objects; wait for it. |
| 207 | + inf.add_event_handler(MODIFIED, lambda o: resynced.set() if _name_of(o) == name else None) |
| 208 | + inf.start() |
| 209 | + self.addCleanup(inf.stop) |
| 210 | + |
| 211 | + # First, wait for the object to be added to the cache. |
| 212 | + self._expect(added, "ADDED/" + name) |
| 213 | + # Then wait for the resync to fire MODIFIED (allow up to 3× resync_period). |
| 214 | + if not resynced.wait(timeout=15): |
| 215 | + self.fail("Timeout waiting for resync MODIFIED/" + name) |
| 216 | + |
| 217 | + # Verify the cached object still holds the expected data. |
| 218 | + cached = inf.cache.get_by_key("default/" + name) |
| 219 | + self.assertIsNotNone(cached) |
| 220 | + data = cached.data if hasattr(cached, "data") else (cached.get("data") or {}) |
| 221 | + self.assertEqual(data.get("k"), "v") |
| 222 | + |
| 223 | + |
184 | 224 | if __name__ == "__main__": |
185 | 225 | unittest.main() |
0 commit comments