Skip to content

Commit

Permalink
test: Remove some CiTestCase tests (#5256)
Browse files Browse the repository at this point in the history
Includes:
 - test_atomic_helper.py
 - test_builtin_handlers.py
 - test_conftest.py (partial)
  • Loading branch information
TheRealFalcon committed May 5, 2024
1 parent f24ecef commit cf72d7f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 142 deletions.
37 changes: 18 additions & 19 deletions tests/unittests/test_atomic_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,66 @@
import stat

from cloudinit import atomic_helper
from tests.unittests.helpers import CiTestCase


class TestAtomicHelper(CiTestCase):
def test_basic_usage(self):
class TestAtomicHelper:
def test_basic_usage(self, tmp_path):
"""write_file takes bytes if no omode."""
path = self.tmp_path("test_basic_usage")
path = tmp_path / "test_basic_usage"
contents = b"Hey there\n"
atomic_helper.write_file(path, contents)
self.check_file(path, contents)

def test_string(self):
def test_string(self, tmp_path):
"""write_file can take a string with mode w."""
path = self.tmp_path("test_string")
path = tmp_path / "test_string"
contents = "Hey there\n"
atomic_helper.write_file(path, contents, omode="w")
self.check_file(path, contents, omode="r")

def test_file_permissions(self):
def test_file_permissions(self, tmp_path):
"""write_file with mode 400 works correctly."""
path = self.tmp_path("test_file_permissions")
path = tmp_path / "test_file_permissions"
contents = b"test_file_perms"
atomic_helper.write_file(path, contents, mode=0o400)
self.check_file(path, contents, perms=0o400)

def test_file_preserve_permissions(self):
def test_file_preserve_permissions(self, tmp_path):
"""create a file with mode 700, then write_file with mode 644."""
path = self.tmp_path("test_file_preserve_permissions")
path = tmp_path / "test_file_preserve_permissions"
contents = b"test_file_perms"
with open(path, mode="wb") as f:
f.write(b"test file preserve permissions")
os.chmod(f.name, 0o700)
atomic_helper.write_file(path, contents, preserve_mode=True)
self.check_file(path, contents, perms=0o700)

def test_write_json(self):
def test_write_json(self, tmp_path):
"""write_json output is readable json."""
path = self.tmp_path("test_write_json")
path = tmp_path / "test_write_json"
data = {"key1": "value1", "key2": ["i1", "i2"]}
atomic_helper.write_json(path, data)
with open(path, "r") as fp:
found = json.load(fp)
self.assertEqual(data, found)
assert data == found
self.check_perms(path, 0o644)

def check_file(self, path, content, omode=None, perms=0o644):
if omode is None:
omode = "rb"
self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isfile(path))
assert os.path.exists(path)
assert os.path.isfile(path)
with open(path, omode) as fp:
found = fp.read()
self.assertEqual(content, found)
assert content == found
self.check_perms(path, perms)

def check_perms(self, path, perms):
file_stat = os.stat(path)
self.assertEqual(perms, stat.S_IMODE(file_stat.st_mode))
assert perms == stat.S_IMODE(file_stat.st_mode)

def test_write_file_ensure_dirs(self):
path = self.tmp_path("ensure_dirs") + "/ensure/dir"
def test_write_file_ensure_dirs(self, tmp_path):
path = tmp_path / "ensure_dirs" / "ensure/dir"
contents = b"Hey there\n"
atomic_helper.write_file(path, contents)
self.check_file(path, contents)
Loading

0 comments on commit cf72d7f

Please sign in to comment.