summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/sources/helpers/azure.py8
-rw-r--r--tests/unittests/test_datasource/test_azure_helper.py17
2 files changed, 15 insertions, 10 deletions
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
index 60f116e0..cb13187f 100644
--- a/cloudinit/sources/helpers/azure.py
+++ b/cloudinit/sources/helpers/azure.py
@@ -104,7 +104,7 @@ class OpenSSLManager(object):
}
def __init__(self):
- self.tmpdir = tempfile.TemporaryDirectory()
+ self.tmpdir = tempfile.mkdtemp()
self.certificate = None
self.generate_certificate()
@@ -113,7 +113,7 @@ class OpenSSLManager(object):
if self.certificate is not None:
LOG.debug('Certificate already generated.')
return
- with cd(self.tmpdir.name):
+ with cd(self.tmpdir):
util.subp([
'openssl', 'req', '-x509', '-nodes', '-subj',
'/CN=LinuxTransport', '-days', '32768', '-newkey', 'rsa:2048',
@@ -139,7 +139,7 @@ class OpenSSLManager(object):
b'',
certificates_content.encode('utf-8'),
]
- with cd(self.tmpdir.name):
+ with cd(self.tmpdir):
with open('Certificates.p7m', 'wb') as f:
f.write(b'\n'.join(lines))
out, _ = util.subp(
@@ -159,7 +159,7 @@ class OpenSSLManager(object):
current = []
keys = []
for certificate in certificates:
- with cd(self.tmpdir.name):
+ with cd(self.tmpdir):
public_key, _ = util.subp(
'openssl x509 -noout -pubkey |'
'ssh-keygen -i -m PKCS8 -f /dev/stdin',
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
index 47b77840..398a9007 100644
--- a/tests/unittests/test_datasource/test_azure_helper.py
+++ b/tests/unittests/test_datasource/test_azure_helper.py
@@ -273,15 +273,20 @@ class TestOpenSSLManager(TestCase):
self.subp = patches.enter_context(
mock.patch.object(azure_helper.util, 'subp'))
+ try:
+ self.open = patches.enter_context(
+ mock.patch('__builtin__.open'))
+ except ImportError:
+ self.open = patches.enter_context(
+ mock.patch('builtins.open'))
@mock.patch.object(azure_helper, 'cd', mock.MagicMock())
- @mock.patch.object(azure_helper.tempfile, 'TemporaryDirectory')
- def test_openssl_manager_creates_a_tmpdir(self, TemporaryDirectory):
+ @mock.patch.object(azure_helper.tempfile, 'mkdtemp')
+ def test_openssl_manager_creates_a_tmpdir(self, mkdtemp):
manager = azure_helper.OpenSSLManager()
- self.assertEqual(TemporaryDirectory.return_value, manager.tmpdir)
+ self.assertEqual(mkdtemp.return_value, manager.tmpdir)
- @mock.patch('builtins.open')
- def test_generate_certificate_uses_tmpdir(self, open):
+ def test_generate_certificate_uses_tmpdir(self):
subp_directory = {}
def capture_directory(*args, **kwargs):
@@ -289,7 +294,7 @@ class TestOpenSSLManager(TestCase):
self.subp.side_effect = capture_directory
manager = azure_helper.OpenSSLManager()
- self.assertEqual(manager.tmpdir.name, subp_directory['path'])
+ self.assertEqual(manager.tmpdir, subp_directory['path'])
class TestWALinuxAgentShim(TestCase):