Skip to content

Commit

Permalink
fstab: Add a simple test to read fstab using our code
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechtrefny committed Feb 13, 2025
1 parent 68da0a9 commit 9044d70
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions tests/storage_tests/fstab_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def setUp(self):
def _clean_up(self):

self.storage.fstab.dest_file = None
self.storage.fstab.src_file = None

self.storage.reset()
for disk in self.storage.disks:
Expand Down Expand Up @@ -91,6 +92,12 @@ def test_fstab(self):
self.assertTrue("54321 2" in contents)
self.assertTrue("optionA,optionB" in contents)

# check that we can read and parse the fstab written above
self.storage.fstab.src_file = fstab_path
self.storage.reset()
self.assertEqual(str(self.storage.fstab),
"/dev/mapper/blivetTestVG-blivetTestLVMine\t/mnt/test2\text4\toptionA,optionB\t54321\t2\t\n")

dev = self.storage.devicetree.get_device_by_name("blivetTestVG-blivetTestLVMine")
self.storage.recursive_remove(dev)

Expand Down
43 changes: 36 additions & 7 deletions tests/unit_tests/fstab_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import os
import tempfile
import unittest
from unittest.mock import Mock

Expand All @@ -9,7 +10,6 @@
from blivet.size import Size
from blivet import Blivet

FSTAB_WRITE_FILE = "/tmp/test-blivet-fstab2"


class FSTabTestCase(unittest.TestCase):
Expand All @@ -20,19 +20,17 @@ def setUpClass(cls):
raise unittest.SkipTest("Missing libmount support required for this test")

def setUp(self):
self._temp_dir = tempfile.TemporaryDirectory()
self.fstab = FSTabManager()
self.addCleanup(self._clean_up)

def _clean_up(self):
try:
os.remove(FSTAB_WRITE_FILE)
except FileNotFoundError:
pass
self._temp_dir.cleanup()

def test_fstab(self):

self.fstab.src_file = None
self.fstab.dest_file = FSTAB_WRITE_FILE
self.fstab.dest_file = os.path.join(self._temp_dir.name, "fstab")

entry = FSTabEntry("/dev/sda_dummy", "/media/wrongpath", "xfs", ["ro", "noatime"])

Expand Down Expand Up @@ -71,7 +69,7 @@ def test_fstab(self):
self.fstab.write()

# read the file and verify its contents
with open(FSTAB_WRITE_FILE, "r") as f:
with open(os.path.join(self._temp_dir.name, "fstab"), "r") as f:
contents = f.read()
self.assertEqual(contents, "/dev/sdb_dummy /media/newpath ext4 defaults 0 0\n")

Expand Down Expand Up @@ -233,3 +231,34 @@ def test_get_device(self):
dev2 = self.fstab.get_device(b.devicetree, "/dev/sda_dummy", "/mnt/mountpath", "xfs", ["defaults"])

self.assertEqual(dev1, dev2)

def test_read_fstab(self):
with open(os.path.join(self._temp_dir.name, "fstab"), "w+") as f:
f.write("#\n"
"# /etc/fstab\n"
"# Created by anaconda on Mon Feb 10 11:39:42 2025\n"
"#\n"
"# Accessible filesystems, by reference, are maintained under '/dev/disk/'.\n"
"# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.\n"
"#\n"
"# After editing this file, run 'systemctl daemon-reload' to update systemd\n"
"# units generated from this file.\n"
"#\n"
"UUID=bc08e185-280e-46d3-9ae3-c0cc62c486ff\t/\t\text4\tdefaults\t1 1\n"
"UUID=51d9dc40-d1a9-4fd9-b538-61d5bd020fda\t/boot\t\text4\tdefaults\t1 2\n"
"/dev/sda1\t/mnt/test\tntfs\tro,nosuid,users,nofail,noauto\t0 2\n")

self.fstab.src_file = os.path.join(self._temp_dir.name, "fstab")
self.fstab.read()

entry = self.fstab.find_entry(file="/")
self.assertIsNotNone(entry)
self.assertEqual(entry.spec, "UUID=bc08e185-280e-46d3-9ae3-c0cc62c486ff")
self.assertEqual(entry.vfstype, "ext4")
self.assertEqual(entry.mntopts, ["defaults"])

entry = self.fstab.find_entry("/dev/sda1")
self.assertIsNotNone(entry)
self.assertEqual(entry.file, "/mnt/test")
self.assertEqual(entry.vfstype, "ntfs")
self.assertEqual(entry.mntopts, ['ro', 'nosuid', 'users', 'nofail', 'noauto'])

0 comments on commit 9044d70

Please sign in to comment.