From 7f23fbaedd64e3d02bff00627f58b6324734bf35 Mon Sep 17 00:00:00 2001 From: Tom Weininger Date: Thu, 21 Apr 2022 11:40:57 +0200 Subject: [PATCH] Fix AttributeError in exception handler The exception handler of save_state() caused an AttributeError because it tried to get the output attribute of the Exception, which does not exist in general. Added a test to cover this scenario. Change-Id: I36acdee1f8782b17c234f6b250facc5c8d0aaf87 (cherry picked from commit 384b83d88b436c8e657b09f97b12e42d7c9c339b) (cherry picked from commit 7aaac224cb4296a4802e4c21c76ba9d010335333) (cherry picked from commit 7adeb21c42fdb6431718fcfcd9ad53b8ae077aa5) (cherry picked from commit 3ee4acb6501c641f19e223b1937c43c0af2fc591) (cherry picked from commit b30d902226e71ad17df643d9df4acfdc20470f26) --- octavia/amphorae/backends/utils/haproxy_query.py | 3 +-- .../amphorae/backends/utils/test_haproxy_query.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/octavia/amphorae/backends/utils/haproxy_query.py b/octavia/amphorae/backends/utils/haproxy_query.py index 2783b2b96b..ae24581c13 100644 --- a/octavia/amphorae/backends/utils/haproxy_query.py +++ b/octavia/amphorae/backends/utils/haproxy_query.py @@ -153,6 +153,5 @@ class HAProxyQuery(object): except Exception as e: # Catch any exception - may be socket issue, or write permission # issue as well. - LOG.warning("Unable to save state: %(err)s %(output)s", - {'err': e, 'output': e.output}) + LOG.warning("Unable to save state: %r", e) return False diff --git a/octavia/tests/unit/amphorae/backends/utils/test_haproxy_query.py b/octavia/tests/unit/amphorae/backends/utils/test_haproxy_query.py index b735097ea4..5d453572cf 100644 --- a/octavia/tests/unit/amphorae/backends/utils/test_haproxy_query.py +++ b/octavia/tests/unit/amphorae/backends/utils/test_haproxy_query.py @@ -159,3 +159,15 @@ class QueryTestCase(base.TestCase): self.q.save_state(filename) mock_fh.write.assert_called_once_with('DATA') + + def test_save_state_error(self): + """save_state() should swallow exceptions""" + filename = 'state_file' + + query_mock = mock.Mock(side_effect=OSError()) + self.q._query = query_mock + + try: + self.q.save_state(filename) + except Exception as ex: + self.fail("save_state() raised %r unexpectedly!" % ex)