From 35ab6567afa532d88cf0bdd2bb28c32acfe69aac Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Mon, 21 Dec 2020 09:57:46 +0200 Subject: [PATCH] ipatests: fix race condition in finalizer of encrypted backup test When using a fixture, we get a temporary directory created and then removed by pytest. Pytest uses `shutil.rmtree` call which collects all files in the directory being removed and then removes them one by one. At the point of removal of our GNUPGHOME directory, gpg daemon is being shut down and there might still be an agent UNIX domain socket. The removal actually overlaps in time with shut down of the gpg daemon, thus causing `shutil.rmtree()` to fail when an agent UNIX domain socket is removed by the daemon. Change the way how we run the gpg agent to use a temporary systemd service. Stop the service in the finalizer method so that systemd would send SIGTERM signal and the gpg agent would clean itself up. Signed-off-by: Alexander Bokovoy Reviewed-By: Stanislav Levin --- .../test_install/test_installutils.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ipatests/test_ipaserver/test_install/test_installutils.py b/ipatests/test_ipaserver/test_install/test_installutils.py index cb4b88d06..1dfb735fd 100644 --- a/ipatests/test_ipaserver/test_install/test_installutils.py +++ b/ipatests/test_ipaserver/test_install/test_installutils.py @@ -55,21 +55,26 @@ def gpgkey(request, tempdir): f.write("allow-preset-passphrase\n") # daemonize agent (detach from the console and run in the background) - subprocess.Popen( - [paths.GPG_AGENT, '--batch', '--daemon'], - env=env, stdout=devnull, stderr=devnull + subprocess.run( + [paths.SYSTEMD_RUN, '--service-type=forking', + '--setenv=GNUPGHOME={}'.format(gnupghome), + '--setenv=LC_ALL=C.UTF-8', + '--setenv=LANGUAGE=C', + '--unit=gpg-agent', paths.GPG_AGENT, '--daemon', '--batch'], + check=True, + env=env, ) def fin(): + subprocess.run( + [paths.SYSTEMCTL, 'stop', 'gpg-agent'], + check=True, + env=env, + ) if orig_gnupghome is not None: os.environ['GNUPGHOME'] = orig_gnupghome else: os.environ.pop('GNUPGHOME', None) - subprocess.run( - [paths.GPG_CONF, '--kill', 'all'], - check=True, - env=env, - ) request.addfinalizer(fin)