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