summaryrefslogtreecommitdiff
path: root/tests/unittests/test_temp_utils.py
diff options
context:
space:
mode:
authorJames Falcon <james.falcon@canonical.com>2021-12-15 20:16:38 -0600
committerGitHub <noreply@github.com>2021-12-15 19:16:38 -0700
commitbae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf (patch)
tree1fbb3269fc87e39832e3286ef42eefd2b23fcd44 /tests/unittests/test_temp_utils.py
parent2bcf4fa972fde686c2e3141c58e640640b44dd00 (diff)
downloadvyos-cloud-init-bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf.tar.gz
vyos-cloud-init-bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf.zip
Adopt Black and isort (SC-700) (#1157)
Applied Black and isort, fixed any linting issues, updated tox.ini and CI.
Diffstat (limited to 'tests/unittests/test_temp_utils.py')
-rw-r--r--tests/unittests/test_temp_utils.py118
1 files changed, 68 insertions, 50 deletions
diff --git a/tests/unittests/test_temp_utils.py b/tests/unittests/test_temp_utils.py
index 9d56d0d0..e91f389b 100644
--- a/tests/unittests/test_temp_utils.py
+++ b/tests/unittests/test_temp_utils.py
@@ -2,30 +2,33 @@
"""Tests for cloudinit.temp_utils"""
+import os
+
from cloudinit.temp_utils import mkdtemp, mkstemp, tempdir
from tests.unittests.helpers import CiTestCase, wrap_and_call
-import os
class TestTempUtils(CiTestCase):
-
def test_mkdtemp_default_non_root(self):
"""mkdtemp creates a dir under /tmp for the unprivileged."""
calls = []
def fake_mkdtemp(*args, **kwargs):
calls.append(kwargs)
- return '/fake/return/path'
+ return "/fake/return/path"
retval = wrap_and_call(
- 'cloudinit.temp_utils',
- {'os.getuid': 1000,
- 'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
- '_TMPDIR': {'new': None},
- 'os.path.isdir': True},
- mkdtemp)
- self.assertEqual('/fake/return/path', retval)
- self.assertEqual([{'dir': '/tmp'}], calls)
+ "cloudinit.temp_utils",
+ {
+ "os.getuid": 1000,
+ "tempfile.mkdtemp": {"side_effect": fake_mkdtemp},
+ "_TMPDIR": {"new": None},
+ "os.path.isdir": True,
+ },
+ mkdtemp,
+ )
+ self.assertEqual("/fake/return/path", retval)
+ self.assertEqual([{"dir": "/tmp"}], calls)
def test_mkdtemp_default_non_root_needs_exe(self):
"""mkdtemp creates a dir under /var/tmp/cloud-init when needs_exe."""
@@ -33,17 +36,21 @@ class TestTempUtils(CiTestCase):
def fake_mkdtemp(*args, **kwargs):
calls.append(kwargs)
- return '/fake/return/path'
+ return "/fake/return/path"
retval = wrap_and_call(
- 'cloudinit.temp_utils',
- {'os.getuid': 1000,
- 'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
- '_TMPDIR': {'new': None},
- 'os.path.isdir': True},
- mkdtemp, needs_exe=True)
- self.assertEqual('/fake/return/path', retval)
- self.assertEqual([{'dir': '/var/tmp/cloud-init'}], calls)
+ "cloudinit.temp_utils",
+ {
+ "os.getuid": 1000,
+ "tempfile.mkdtemp": {"side_effect": fake_mkdtemp},
+ "_TMPDIR": {"new": None},
+ "os.path.isdir": True,
+ },
+ mkdtemp,
+ needs_exe=True,
+ )
+ self.assertEqual("/fake/return/path", retval)
+ self.assertEqual([{"dir": "/var/tmp/cloud-init"}], calls)
def test_mkdtemp_default_root(self):
"""mkdtemp creates a dir under /run/cloud-init for the privileged."""
@@ -51,17 +58,20 @@ class TestTempUtils(CiTestCase):
def fake_mkdtemp(*args, **kwargs):
calls.append(kwargs)
- return '/fake/return/path'
+ return "/fake/return/path"
retval = wrap_and_call(
- 'cloudinit.temp_utils',
- {'os.getuid': 0,
- 'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
- '_TMPDIR': {'new': None},
- 'os.path.isdir': True},
- mkdtemp)
- self.assertEqual('/fake/return/path', retval)
- self.assertEqual([{'dir': '/run/cloud-init/tmp'}], calls)
+ "cloudinit.temp_utils",
+ {
+ "os.getuid": 0,
+ "tempfile.mkdtemp": {"side_effect": fake_mkdtemp},
+ "_TMPDIR": {"new": None},
+ "os.path.isdir": True,
+ },
+ mkdtemp,
+ )
+ self.assertEqual("/fake/return/path", retval)
+ self.assertEqual([{"dir": "/run/cloud-init/tmp"}], calls)
def test_mkstemp_default_non_root(self):
"""mkstemp creates secure tempfile under /tmp for the unprivileged."""
@@ -69,17 +79,20 @@ class TestTempUtils(CiTestCase):
def fake_mkstemp(*args, **kwargs):
calls.append(kwargs)
- return '/fake/return/path'
+ return "/fake/return/path"
retval = wrap_and_call(
- 'cloudinit.temp_utils',
- {'os.getuid': 1000,
- 'tempfile.mkstemp': {'side_effect': fake_mkstemp},
- '_TMPDIR': {'new': None},
- 'os.path.isdir': True},
- mkstemp)
- self.assertEqual('/fake/return/path', retval)
- self.assertEqual([{'dir': '/tmp'}], calls)
+ "cloudinit.temp_utils",
+ {
+ "os.getuid": 1000,
+ "tempfile.mkstemp": {"side_effect": fake_mkstemp},
+ "_TMPDIR": {"new": None},
+ "os.path.isdir": True,
+ },
+ mkstemp,
+ )
+ self.assertEqual("/fake/return/path", retval)
+ self.assertEqual([{"dir": "/tmp"}], calls)
def test_mkstemp_default_root(self):
"""mkstemp creates a secure tempfile in /run/cloud-init for root."""
@@ -87,31 +100,36 @@ class TestTempUtils(CiTestCase):
def fake_mkstemp(*args, **kwargs):
calls.append(kwargs)
- return '/fake/return/path'
+ return "/fake/return/path"
retval = wrap_and_call(
- 'cloudinit.temp_utils',
- {'os.getuid': 0,
- 'tempfile.mkstemp': {'side_effect': fake_mkstemp},
- '_TMPDIR': {'new': None},
- 'os.path.isdir': True},
- mkstemp)
- self.assertEqual('/fake/return/path', retval)
- self.assertEqual([{'dir': '/run/cloud-init/tmp'}], calls)
+ "cloudinit.temp_utils",
+ {
+ "os.getuid": 0,
+ "tempfile.mkstemp": {"side_effect": fake_mkstemp},
+ "_TMPDIR": {"new": None},
+ "os.path.isdir": True,
+ },
+ mkstemp,
+ )
+ self.assertEqual("/fake/return/path", retval)
+ self.assertEqual([{"dir": "/run/cloud-init/tmp"}], calls)
def test_tempdir_error_suppression(self):
"""test tempdir suppresses errors during directory removal."""
with self.assertRaises(OSError):
- with tempdir(prefix='cloud-init-dhcp-') as tdir:
+ with tempdir(prefix="cloud-init-dhcp-") as tdir:
os.rmdir(tdir)
# As a result, the directory is already gone,
# so shutil.rmtree should raise OSError
- with tempdir(rmtree_ignore_errors=True,
- prefix='cloud-init-dhcp-') as tdir:
+ with tempdir(
+ rmtree_ignore_errors=True, prefix="cloud-init-dhcp-"
+ ) as tdir:
os.rmdir(tdir)
# Since the directory is already gone, shutil.rmtree would raise
# OSError, but we suppress that
+
# vi: ts=4 expandtab