From 715d31e09f939844bd27f2d4ed8c9207598a5938 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Thu, 5 May 2022 15:03:58 -0400 Subject: [PATCH] Catch FileExistsError when creating /etc/corosync dir. Hooks are expected to be idempotent, if the install hook for whatever reason needs to be re-run and the /etc/corosync directory already exists, because for example it was created in a previous run, the exception FileExistsError will be raised, this change captures the exception and moves on. Change-Id: If43a5c95bb59c9cca7f1a975214a9f013ad6f4d6 Closes-Bug: #1971762 --- hooks/hooks.py | 5 ++++- unit_tests/test_hacluster_hooks.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hooks/hooks.py b/hooks/hooks.py index c988a45..df3610b 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -153,7 +153,10 @@ DEPRECATED_TRANSPORT_VALUES = {"multicast": "udp", "unicast": "udpu"} def install(): # LP:1874719 Configure a corosync.conf file to avoid a spurious node1 to # be created in the cluster. - os.mkdir('/etc/corosync', mode=0o755) + try: + os.mkdir('/etc/corosync', mode=0o755) + except FileExistsError: + pass if emit_corosync_conf(): log('Installed initial corosync.conf file', level=INFO) else: diff --git a/unit_tests/test_hacluster_hooks.py b/unit_tests/test_hacluster_hooks.py index 6c43d61..f99b875 100644 --- a/unit_tests/test_hacluster_hooks.py +++ b/unit_tests/test_hacluster_hooks.py @@ -402,6 +402,15 @@ class TestHooks(test_utils.CharmTestCase): apt_install.assert_called_once_with(expected_pkgs, fatal=True) setup_ocf_files.assert_called_once_with() + mkdir.reset_mock() + + def raise_(): + raise FileExistsError() + + mkdir.side_effect = lambda p, mode: raise_() + hooks.install() + mkdir.assert_called_once_with('/etc/corosync', mode=0o755) + @mock.patch('pcmk.set_property') @mock.patch.object(hooks, 'is_stonith_configured') @mock.patch.object(hooks, 'configure_stonith')