From be9c100fc3a2da7da30d8b93e3c970e5569ea7fc Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Fri, 15 Mar 2024 08:30:54 -0600 Subject: [PATCH 1/3] gpg: Handle temp directory with files --- cloudinit/gpg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudinit/gpg.py b/cloudinit/gpg.py index 9871a83c587..a51c8acedec 100644 --- a/cloudinit/gpg.py +++ b/cloudinit/gpg.py @@ -52,8 +52,8 @@ def __exit__(self, exc_typ, exc_value, traceback): def cleanup(self) -> None: """cleanup the gpg temporary directory and kill gpg""" self.kill_gpg() - if self.temp_dir and os.path.isdir(self.temp_dir.name): - os.rmdir(self.temp_dir.name) + if self.temp_dir: + self.temp_dir.cleanup() def export_armour(self, key: str) -> Optional[str]: """Export gpg key, armoured key gets returned""" From 38bc5b6971faa661e33fc34011773a31f68eb042 Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Fri, 15 Mar 2024 09:47:37 -0600 Subject: [PATCH 2/3] fix 3.7 --- cloudinit/gpg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/gpg.py b/cloudinit/gpg.py index a51c8acedec..6e7fc36a63a 100644 --- a/cloudinit/gpg.py +++ b/cloudinit/gpg.py @@ -52,7 +52,7 @@ def __exit__(self, exc_typ, exc_value, traceback): def cleanup(self) -> None: """cleanup the gpg temporary directory and kill gpg""" self.kill_gpg() - if self.temp_dir: + if self.temp_dir and os.path.isdir(self.temp_dir.name): self.temp_dir.cleanup() def export_armour(self, key: str) -> Optional[str]: From f05682d85346d4fa5106cd8c773ac1b782ddc76c Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Fri, 15 Mar 2024 10:24:43 -0600 Subject: [PATCH 3/3] test: Fix mocks for TemporaryDirectory.cleanup() --- tests/unittests/conftest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unittests/conftest.py b/tests/unittests/conftest.py index f8f06d42009..5a46646f241 100644 --- a/tests/unittests/conftest.py +++ b/tests/unittests/conftest.py @@ -1,6 +1,7 @@ import builtins import glob import os +import shutil from pathlib import Path from unittest import mock @@ -67,6 +68,9 @@ def m_gpg(): ("write_file", 1), ("write_json", 1), ], + shutil: [ + ("rmtree", 1), + ], }