From 217d5386b5b1cc5d0605395af41b315ce8427a0a Mon Sep 17 00:00:00 2001 From: Benjamin Rottler Date: Tue, 1 Aug 2023 14:38:57 +0200 Subject: [PATCH 1/2] Add error handling when updating non-existing record It can happen that a drone does not reach the AvailableState but only the DownState. In this case, updating the record in Auditor does not work, because no record has been created. In this case, Auditor returns a 400 BAD REQUEST. This commit adds error handling for this specific case. Other exceptions will not be handled. --- tardis/plugins/auditor.py | 13 ++++++++++++- tests/plugins_t/test_auditor.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tardis/plugins/auditor.py b/tardis/plugins/auditor.py index b2d120e4..0976a398 100644 --- a/tardis/plugins/auditor.py +++ b/tardis/plugins/auditor.py @@ -85,7 +85,18 @@ async def notify(self, state: State, resource_attributes: AttributeDict) -> None .replace(tzinfo=self._local_timezone) .astimezone(pytz.utc) ) - await self._client.update(record) + try: + await self._client.update(record) + except RuntimeError as e: + if str(e).startswith( + "Reqwest Error: HTTP status client error (400 Bad Request)" + ): + self.logger.debug( + f"Could not update record {record.record_id}, " + "it probably does not exist in the database" + ) + else: + raise def construct_record(self, resource_attributes: AttributeDict): """ diff --git a/tests/plugins_t/test_auditor.py b/tests/plugins_t/test_auditor.py index b1c7ce5a..61078316 100644 --- a/tests/plugins_t/test_auditor.py +++ b/tests/plugins_t/test_auditor.py @@ -143,6 +143,38 @@ def test_notify(self): self.assertEqual(self.client.add.call_count, 1) self.assertEqual(self.client.update.call_count, 1) + # test exception handling + self.client.update.side_effect = RuntimeError( + "Reqwest Error: HTTP status client error (400 Bad Request) " + "for url (http://127.0.0.1:8000/update)" + ) + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = RuntimeError( + "Reqwest Error: HTTP status client error (403 Forbidden) " + "for url (http://127.0.0.1:8000/update)" + ) + with self.assertRaises(RuntimeError): + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = ValueError("Other exception") + with self.assertRaises(ValueError): + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = None + def test_construct_record(self): record = self.plugin.construct_record(resource_attributes=self.test_param) From e513aac82f6911a62408419569aed87116f585c2 Mon Sep 17 00:00:00 2001 From: Manuel Giffels Date: Fri, 4 Aug 2023 11:27:34 +0200 Subject: [PATCH 2/2] Add unittest for not matching RegEx --- tests/plugins_t/test_auditor.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/plugins_t/test_auditor.py b/tests/plugins_t/test_auditor.py index 61078316..faf4b812 100644 --- a/tests/plugins_t/test_auditor.py +++ b/tests/plugins_t/test_auditor.py @@ -165,6 +165,14 @@ def test_notify(self): resource_attributes=self.test_param, ) + self.client.update.side_effect = RuntimeError("Does not match RegEx") + with self.assertRaises(RuntimeError): + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + self.client.update.side_effect = ValueError("Other exception") with self.assertRaises(ValueError): run_async(