async def test_watch_handler_error(self): s = store.new_store(store.meta_namespace_key_func) g = Reflector(TestLW.__new__(TestLW), V1Pod, s, 0) fw = watch.new_fake() await fw.stop() with self.assertRaises(Exception): await g._watch_handler(fw, {}, asyncio.Queue())
def watch_func(options): nonlocal expected_rvs rv = options["resource_version"] fw = watch.new_fake() self.assertEqual(rv, expected_rvs[0]) expected_rvs = expected_rvs[1:] asyncio.ensure_future(created_fakes.put(fw)) return fw
def watch_func(options): nonlocal watch_err if watch_err: raise watch_err watch_err = Exception("second watch") fw = watch.new_fake() async def coro(): for e in watch_ret: await fw.action(e["type"], e["object"]) await fw.stop() asyncio.ensure_future(coro()) return fw
async def test_fake(self): f = new_fake() table = [ { "t": EventType.ADDED, "s": TestType("foo") }, { "t": EventType.MODIFIED, "s": TestType("qux") }, { "t": EventType.MODIFIED, "s": TestType("bar") }, { "t": EventType.DELETED, "s": TestType("bar") }, { "t": EventType.ERROR, "s": TestType("error: blah") }, ] async def consumer(w): i = 0 async for got in w: expect = table[i] self.assertEqual(got["type"], expect["t"]) self.assertEqual(got["object"], expect["s"]) i += 1 self.assertEqual(i, len(table)) async def sender(): await f.add(TestType("foo")) await f.action(EventType.MODIFIED, TestType("qux")) await f.modify(TestType("bar")) await f.delete(TestType("bar")) await f.error(TestType("error: blah")) await f.stop() async with f: asyncio.ensure_future(sender()) await consumer(f)
async def test_run_until(self): s = store.new_store(store.meta_namespace_key_func) fw = watch.new_fake() def list_func(options): return V1PodList(metadata=V1ListMeta(resource_version="1"), items=[]) lister_watcher = TestLW(list_func, lambda _: fw) r = Reflector(lister_watcher, V1Pod, s, 0) task = asyncio.ensure_future(r.run()) await fw.add(V1Pod(metadata=V1ObjectMeta(name="bar"))) task.cancel() async def aw(): async for _ in fw: self.fail("Watch left open after stopping the watch") await asyncio.wait_for(aw(), wait.FOREVER_TEST_TIMEOUT)
async def test_watch_handler(self): s = store.new_store(store.meta_namespace_key_func) g = Reflector(TestLW.__new__(TestLW), V1Pod, s, 0) fw = watch.new_fake() await s.add(V1Pod(metadata=V1ObjectMeta(name="foo"))) await s.add(V1Pod(metadata=V1ObjectMeta(name="bar"))) async def aw(): await fw.add(V1Service(metadata=V1ObjectMeta(name="rejected"))) await fw.delete(V1Pod(metadata=V1ObjectMeta(name="foo"))) await fw.modify( V1Pod(metadata=V1ObjectMeta(name="bar", resource_version="55")) ) await fw.add( V1Pod(metadata=V1ObjectMeta(name="baz", resource_version="32")) ) await fw.stop() asyncio.ensure_future(aw()) options = {} await g._watch_handler(fw, options, asyncio.Queue()) def mk_pod(id_, rv): return V1Pod(metadata=V1ObjectMeta(name=id_, resource_version=rv)) table = [ {"pod": mk_pod("foo", ""), "exists": False}, {"pod": mk_pod("rejected", ""), "exists": False}, {"pod": mk_pod("bar", "55"), "exists": True}, {"pod": mk_pod("baz", "32"), "exists": True}, ] for item in table: obj = s.get(item["pod"]) exists = obj is not None self.assertIs(exists, item["exists"]) if not exists: continue self.assertEqual( obj.metadata.resource_version, item["pod"].metadata.resource_version ) self.assertEqual(options["resource_version"], "32") self.assertEqual(g.last_sync_resource_version(), "32")
async def test_close_watch_on_error(self): pod = V1Pod(metadata=V1ObjectMeta(name="bar")) fw = watch.new_fake() def list_func(options): return V1PodList(metadata=V1ListMeta(resource_version="1"), items=[]) lister_watcher = TestLW(list_func, lambda _: fw) r = Reflector( lister_watcher, V1Pod, store.new_store(store.meta_namespace_key_func), 0 ) asyncio.ensure_future(r.list_and_watch()) await fw.error(pod) async def aw(): async for _ in fw: self.fail("Watch left open after cancellation") await asyncio.wait_for(aw(), wait.FOREVER_TEST_TIMEOUT)
def watch_func(options): fw = watch.new_fake() return fw