From 3b96edd8d23c65c6788a9980114a7e1c220c9640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Fri, 4 Oct 2019 15:00:50 +0100 Subject: [PATCH] Fix failing unit tests for batch async --- salt/cli/batch_async.py | 2 +- tests/unit/cli/test_batch_async.py | 66 +++++++++++++++++------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/salt/cli/batch_async.py b/salt/cli/batch_async.py index 89405ba917..b2d04f9d4d 100644 --- a/salt/cli/batch_async.py +++ b/salt/cli/batch_async.py @@ -91,7 +91,7 @@ class BatchAsync: keep_loop=True, ) self.scheduled = False - self.patterns = {} + self.patterns = set() def __set_event_handler(self): ping_return_pattern = "salt/job/{}/ret/*".format(self.ping_jid) diff --git a/tests/unit/cli/test_batch_async.py b/tests/unit/cli/test_batch_async.py index 66332a548a..c18b42be57 100644 --- a/tests/unit/cli/test_batch_async.py +++ b/tests/unit/cli/test_batch_async.py @@ -61,8 +61,8 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): ret = self.batch.start() # assert start_batch is called later with batch_presence_ping_timeout as param self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - (self.batch.batch_presence_ping_timeout, self.batch.start_batch), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.start_batch,), ) # assert test.ping called self.assertEqual( @@ -81,8 +81,8 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): ret = self.batch.start() # assert start_batch is called later with gather_job_timeout as param self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - (self.batch.opts["gather_job_timeout"], self.batch.start_batch), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.start_batch,), ) def test_batch_fire_start_event(self): @@ -107,12 +107,11 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): def test_start_batch_calls_next(self): self.batch.run_next = MagicMock(return_value=MagicMock()) self.batch.event = MagicMock() - future = tornado.gen.Future() - future.set_result(None) - self.batch.run_next = MagicMock(return_value=future) self.batch.start_batch() self.assertEqual(self.batch.initialized, True) - self.assertEqual(len(self.batch.run_next.mock_calls), 1) + self.assertEqual( + self.batch.event.io_loop.spawn_callback.call_args[0], (self.batch.run_next,) + ) def test_batch_fire_done_event(self): self.batch.targeted_minions = {"foo", "baz", "bar"} @@ -147,14 +146,14 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): future = tornado.gen.Future() future.set_result({"minions": ["foo", "bar"]}) self.batch.local.run_job_async.return_value = future - ret = self.batch.run_next().result() + self.batch.run_next() self.assertEqual( self.batch.local.run_job_async.call_args[0], ({"foo", "bar"}, "my.fun", [], "list"), ) self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - (self.batch.opts["timeout"], self.batch.find_job, {"foo", "bar"}), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.find_job, {"foo", "bar"}), ) self.assertEqual(self.batch.active, {"bar", "foo"}) @@ -249,15 +248,21 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): self.assertEqual(self.batch.active, set()) self.assertEqual(self.batch.done_minions, {"foo"}) self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - (self.batch.batch_delay, self.batch.run_next), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.schedule_next,), ) def test_batch__event_handler_find_job_return(self): self.batch.event = MagicMock( - unpack=MagicMock(return_value=("salt/job/1236/ret/foo", {"id": "foo"})) + unpack=MagicMock( + return_value=( + "salt/job/1236/ret/foo", + {"id": "foo", "return": "deadbeaf"}, + ) + ) ) self.batch.start() + self.batch.patterns.add(("salt/job/1236/ret/*", "find_job_return")) self.batch._BatchAsync__event_handler(MagicMock()) self.assertEqual(self.batch.find_job_returned, {"foo"}) @@ -274,14 +279,13 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): future = tornado.gen.Future() future.set_result({}) self.batch.local.run_job_async.return_value = future + self.batch.minions = {"foo", "bar"} + self.batch.jid_gen = MagicMock(return_value="1234") + tornado.gen.sleep = MagicMock(return_value=future) self.batch.find_job({"foo", "bar"}) self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - ( - self.batch.opts["gather_job_timeout"], - self.batch.check_find_job, - {"foo", "bar"}, - ), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.check_find_job, {"foo", "bar"}, "1234"), ) @tornado.testing.gen_test @@ -291,17 +295,21 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): future = tornado.gen.Future() future.set_result({}) self.batch.local.run_job_async.return_value = future + self.batch.minions = {"foo", "bar"} + self.batch.jid_gen = MagicMock(return_value="1234") + tornado.gen.sleep = MagicMock(return_value=future) self.batch.find_job({"foo", "bar"}) self.assertEqual( - self.batch.event.io_loop.call_later.call_args[0], - (self.batch.opts["gather_job_timeout"], self.batch.check_find_job, {"foo"}), + self.batch.event.io_loop.spawn_callback.call_args[0], + (self.batch.check_find_job, {"foo"}, "1234"), ) def test_batch_check_find_job_did_not_return(self): self.batch.event = MagicMock() self.batch.active = {"foo"} self.batch.find_job_returned = set() - self.batch.check_find_job({"foo"}) + self.batch.patterns = {("salt/job/1234/ret/*", "find_job_return")} + self.batch.check_find_job({"foo"}, jid="1234") self.assertEqual(self.batch.find_job_returned, set()) self.assertEqual(self.batch.active, set()) self.assertEqual(len(self.batch.event.io_loop.add_callback.mock_calls), 0) @@ -309,9 +317,10 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): def test_batch_check_find_job_did_return(self): self.batch.event = MagicMock() self.batch.find_job_returned = {"foo"} - self.batch.check_find_job({"foo"}) + self.batch.patterns = {("salt/job/1234/ret/*", "find_job_return")} + self.batch.check_find_job({"foo"}, jid="1234") self.assertEqual( - self.batch.event.io_loop.add_callback.call_args[0], + self.batch.event.io_loop.spawn_callback.call_args[0], (self.batch.find_job, {"foo"}), ) @@ -332,7 +341,8 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): # both not yet done but only 'foo' responded to find_job not_done = {"foo", "bar"} - self.batch.check_find_job(not_done) + self.batch.patterns = {("salt/job/1234/ret/*", "find_job_return")} + self.batch.check_find_job(not_done, jid="1234") # assert 'bar' removed from active self.assertEqual(self.batch.active, {"foo"}) @@ -342,7 +352,7 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): # assert 'find_job' schedueled again only for 'foo' self.assertEqual( - self.batch.event.io_loop.add_callback.call_args[0], + self.batch.event.io_loop.spawn_callback.call_args[0], (self.batch.find_job, {"foo"}), ) @@ -350,4 +360,4 @@ class AsyncBatchTestCase(AsyncTestCase, TestCase): self.batch.event = MagicMock() self.batch.scheduled = True self.batch.schedule_next() - self.assertEqual(len(self.batch.event.io_loop.call_later.mock_calls), 0) + self.assertEqual(len(self.batch.event.io_loop.spawn_callback.mock_calls), 0) -- 2.29.2