import datetime import os import subprocess import time import unittest class Lab4TestCase(unittest.TestCase): @classmethod def setUpClass(cls): cls.year = 2021 subprocess.run(['make'], capture_output=True) subprocess.run(['./ext2-create'], capture_output=True) p = subprocess.run(['dumpe2fs', 'cs111-base.img'], capture_output=True, text=True) cls.dump_lines = p.stdout.splitlines() p = subprocess.run(['fsck.ext2', '-f', '-n', 'cs111-base.img'], capture_output=True, text=True) cls.fsck_lines = p.stdout.splitlines() p = subprocess.run(['ls', '-f', '-n', 'cs111-base.img'], capture_output=True, text=True) cls.ls_lines = p.stdout.splitlines() subprocess.run(['mkdir', 'mnt'], capture_output=True) subprocess.run(['sudo', 'mount', '-o', 'loop', 'cs111-base.img', 'mnt']) cls.root_stat = os.lstat('mnt') if os.path.exists('mnt') else None cls.hello_world_stat = os.lstat('mnt/hello-world') if os.path.exists('mnt/hello-world') else None cls.hello_stat = os.lstat('mnt/hello') if os.path.exists('mnt/hello') else None @classmethod def tearDownClass(cls): subprocess.run(['sudo', 'umount', 'mnt']) subprocess.run(['rmdir', 'mnt'], capture_output=True) subprocess.run(['make', 'clean'], capture_output=True) def test_hello(self): self.assertEqual(os.readlink('mnt/hello'), 'hello-world') print("{:<40}{:>30}".format("Test: Symlink check", "| Passed")) def test_hello_world(self): with open('mnt/hello-world') as f: self.assertEqual(f.read(), "Hello world\n") print("{:<40}{:>30}".format("Test: hello-world file content check", "| Passed")) def test_hello_world_text_cat(self): p = subprocess.run(['cat', 'mnt/hello-world'], capture_output=True) self.assertEqual(p.stdout.decode(), 'Hello world\n') print("{:<40}{:>30}".format("Test: cat mnt/hello-world (Regular file)", "| Passed")) def test_hello_link_text_cat(self): p = subprocess.run(['cat', 'mnt/hello'], capture_output=True) self.assertEqual(p.stdout.decode(), 'Hello world\n') print("{:<40}{:>30}".format("Test: cat mnt/hello (Symlink)", "| Passed")) def test_fsck(self): p = subprocess.run(['fsck.ext2', '-f', '-n', 'cs111-base.img'], capture_output=True, text=True) fsck_lines = p.stdout.splitlines() self.assertEqual(fsck_lines[0], 'Pass 1: Checking inodes, blocks, and sizes') self.assertEqual(fsck_lines[1], 'Pass 2: Checking directory structure') self.assertEqual(fsck_lines[2], 'Pass 3: Checking directory connectivity') self.assertEqual(fsck_lines[3], 'Pass 4: Checking reference counts') self.assertEqual(fsck_lines[4], 'Pass 5: Checking group summary information') self.assertEqual(fsck_lines[5], 'cs111-base: 13/128 files (0.0% non-contiguous), 24/1024 blocks') print("{:<40}{:>30}".format("Test: fsck.ext2 -f -n cs111-base.img", "| Passed")) def test_dump(self): p = subprocess.run(['dumpe2fs', 'cs111-base.img'], capture_output=True, text=True) dump_lines = p.stdout.splitlines() self.assertEqual(dump_lines[-3], ' 1000 free blocks, 115 free inodes, 2 directories') self.assertEqual(dump_lines[-2], ' Free blocks: 24-1023') self.assertEqual(dump_lines[-1], ' Free inodes: 14-128') print("{:<40}{:>30}".format("Test: dumpe2fs cs111-base.img", "| Passed"))