From de8494b16bc50c60a8438f2cae1f8c88e8949f7a Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 17 Feb 2017 12:18:56 +0100 Subject: python: Fix teardown of ephemeral contexts. * lang/python/tests/support.py (EphemeralContext): New function. * lang/python/tests/t-quick-key-creation.py: Use the new function to manage ephemeral contexts. * lang/python/tests/t-quick-key-manipulation.py: Likewise. * lang/python/tests/t-quick-subkey-creation.py: Likewise. -- Previously, there was a problem with cleaning up ephemeral home directories. shutil.rmtree deleted the agents main socket, gpg-agent detected that, and deleted the other sockets as well, racing shutil.rmtree which did not cope will with that. Fix this by asking the agent nicely to shut down. Signed-off-by: Justus Winter --- lang/python/tests/support.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lang/python/tests/support.py') diff --git a/lang/python/tests/support.py b/lang/python/tests/support.py index ed5bf615..a381270d 100644 --- a/lang/python/tests/support.py +++ b/lang/python/tests/support.py @@ -18,9 +18,12 @@ from __future__ import absolute_import, print_function, unicode_literals del absolute_import, print_function, unicode_literals +import contextlib +import shutil import sys import os import tempfile +import time import gpg # known keys @@ -85,5 +88,24 @@ else: self.path = tempfile.mkdtemp() return self.path def __exit__(self, *args): - import shutil shutil.rmtree(self.path) + +@contextlib.contextmanager +def EphemeralContext(): + with TemporaryDirectory() as tmp: + home = os.environ['GNUPGHOME'] + shutil.copy(os.path.join(home, "gpg.conf"), tmp) + shutil.copy(os.path.join(home, "gpg-agent.conf"), tmp) + + with gpg.Context(home_dir=tmp) as ctx: + yield ctx + + # Ask the agent to quit. + agent_socket = os.path.join(tmp, "S.gpg-agent") + ctx.protocol = gpg.constants.protocol.ASSUAN + ctx.set_engine_info(ctx.protocol, file_name=agent_socket) + ctx.assuan_transact(["KILLAGENT"]) + + # Block until it is really gone. + while os.path.exists(agent_socket): + time.sleep(.01) -- cgit v1.2.3