def test_unlink_err(self): errs = [("/dir1", hw3.EISDIR), ("/bogus", hw3.ENOENT)] for name, err in errs: v = hw3.unlink(name) self.assertEqual(v, -err)
def test_8_unlink(self): print "Test 8a - unlink:" v, sfs = hw3.statfs() f_bfree = sfs.f_bfree path = 'to_be_delete' hw3.create(path, 0o100777) hw3.write(path, 10000 * 'K', 0) v = hw3.unlink(path) self.assertTrue(v == 0) v, sb = hw3.getattr(path) self.assertTrue(v == -hw3.ENOENT) v, sfs = hw3.statfs() self.assertTrue(sfs.f_bfree == f_bfree) print "Test 8b - cannot unlink a directory:" v = hw3.unlink('dir1/dir2') self.assertTrue(v == -hw3.EISDIR) self.fsck()
def test_create_write_concurrent(self): self.assertEqual(hw3.create("/", 0), -hw3.EEXIST) big_file = 512 * 1024 self.assertEqual(hw3.mkdir("/dir2", 0), 0) self.assertEqual(hw3.create("/dir2/.file", 0), 0) self.assertEqual(hw3.write("/dir2/.file", bytes("Hello, world", 'utf-8'), 0), 12) self.assertEqual(hw3.create("/dir2/file.2", 0), 0) self.assertEqual(hw3.write("/dir2/file.2", b'M' * big_file, 0), big_file) self.assertEqual(hw3.create("/dir2/.file", 0), -hw3.EEXIST) self.run_fsck() v, data = hw3.read("/dir2/.file", 1000, 7) self.assertEqual(v, 5) self.assertEqual(data.decode('utf-8'), "world") self.run_fsck() v, data = hw3.read("/dir2/file.2", big_file, 0) self.assertEqual(v, big_file) self.assertEqual(data, b'M' * big_file) self.run_fsck() self.assertEqual(hw3.unlink("/dir2/file.2"), 0) self.run_fsck() self.assertEqual(hw3.unlink("/dir2/.file"), 0) self.run_fsck() v, data = hw3.readdir("/dir2") self.assertEqual(v, 0) self.assertEqual(data, []) self.run_fsck() self.assertEqual(hw3.rmdir("/dir2"), 0)
def test_stress_write_unlink(self): v, res = hw3.statfs() self.assertEqual(v, 0) blocks_free = res.f_bavail sizes = [12, 577, 1011, 1024, 1025, 2001, 8099, 37000, 289150, 1024 * 1024] writes = [17, 100, 1000, 1024, 1970] cases = [(size, write, "/%s-%s" % (size, write)) for size in sizes for write in writes] bigdata = b'123456789' * 33333333 for size, write, name in cases: towrite = bigdata[0:size] v = hw3.create(name, 0) self.assertEqual(v, 0) idx = 0 while idx < size: maxwrite = min(size, idx + write) data = towrite[idx:maxwrite] v = hw3.write(name, data, idx) self.assertEqual(v, len(data)) idx += write v, data = hw3.read(name, size + 10000, 0) self.assertEqual(v, size) self.assertEqual(len(data), size) self.assertEqual(data, towrite, name) # Truncate before the unlink roughly half of the time if size % 2 == 0: v = hw3.truncate(name, 0) self.assertEqual(v, 0) v, attr = hw3.getattr(name) self.assertEqual(v, 0) self.assertEqual(attr.st_size, 0) v = hw3.unlink(name) self.assertEqual(v, 0) v, attr = hw3.getattr(name) self.assertEqual(v, -hw3.ENOENT) v, stat = hw3.statfs() self.assertEqual(v, 0) self.assertEqual(stat.f_bavail, blocks_free)
def test_9_rmdir(self): print "Test 9a - rmdir:" v = hw3.rmdir('dir1/dir2') v, sb = hw3.getattr('dir1/dir2') self.assertTrue(v == -hw3.ENOENT) print "Test 9b - cannot rmdir a file:" v = hw3.rmdir('dir1/file.A') self.assertTrue(v == -hw3.ENOTDIR) print "Test 9c - cannot rmdir a non-empty directory:" v = hw3.rmdir('dir1') self.assertTrue(v == -hw3.ENOTEMPTY) v = hw3.unlink('dir1/file.A') v = hw3.rmdir('dir1') self.assertTrue(v == 0) self.fsck()
def test_rmdir_inval(self): v = hw3.rmdir("/dir1") self.assertEqual(v, -hw3.ENOTEMPTY) v = hw3.rmdir("/dir2") self.assertEqual(v, -hw3.ENOENT) hw3.unlink("/dir1/file.2") hw3.unlink("/dir1/file.0") hw3.unlink("/dir1/file.270") v = hw3.unlink("/dir1") self.assertEqual(v, -hw3.EISDIR) v = hw3.rmdir("/dir1") self.assertEqual(v, 0)
def test_stress_write_truncate(self): v, res = hw3.statfs() self.assertEqual(v, 0) blocks_free = res.f_bfree sizes = [12, 577, 1011, 1024, 1025, 2001, 8099, 37000, 289150, 1024 * 1024] cases = [(size, "/%s" % (size)) for size in sizes] bigdata = b'123456789' * 33333333 for size, name in cases: towrite = bigdata[0:size] v = hw3.create(name, 0) self.assertEqual(v, 0) v = hw3.write(name, towrite, 0) self.assertEqual(v, size) v, data = hw3.read(name, size + 10000, 0) self.assertEqual(v, size) self.assertEqual(len(data), size) self.assertEqual(data, towrite, name) # Truncate before the unlink roughly half of the time v = hw3.truncate(name, 0) self.assertEqual(v, 0) v, attr = hw3.getattr(name) self.assertEqual(v, 0) self.assertEqual(attr.st_size, 0) v = hw3.unlink(name) self.assertEqual(v, 0) v, attr = hw3.getattr(name) self.assertEqual(v, -hw3.ENOENT) v, stat = hw3.statfs() self.assertEqual(v, 0) self.assertEqual(stat.f_bfree, blocks_free)
def test_complex_rmdir(self): v, stat = hw3.statfs() self.assertEqual(v, 0) free = stat.f_bfree self.assertTrue(free >= 900) actions = [ (hw3.mkdir("/test8", 0o777), 0), (hw3.mkdir("/test8/dir1", 0o777), 0), (hw3.mkdir("/test8/dir2", 0o777), 0), (hw3.mkdir("/test8/dir3", 0o777), 0), (hw3.mkdir("/test8/dir1/a", 0o777), 0), (hw3.mkdir("/test8/dir1/b", 0o777), 0), (hw3.create("/test8/dir2/c", 0o777), 0), (hw3.create("/test8/dir2/d", 0o777), 0), (hw3.rmdir("/test8/dir2/c"), -20), (hw3.rmdir("/test8/dir2"), -39), (hw3.rmdir("/test8/dir1"), -39), (hw3.unlink("/test8/dir3"), -21), (hw3.unlink("/test8/dir1/a"), -21), (hw3.rmdir("/test8/dir3"), 0), (hw3.rmdir("/test8/dir1/a"), 0), (hw3.rmdir("/test8/dir1/b"), 0), (hw3.unlink("/test8/dir2/c/foo"), -20), (hw3.unlink("/test8/dir1/q/foo"), -2), (hw3.rmdir("/test8/dir2/c/foo"), -20), (hw3.rmdir("/test8/dir1/q/foo"), -2), (hw3.unlink("/test8/dir1/q"), -2), (hw3.rmdir("/test8/dir1/q"), -2), (hw3.unlink("/test8/dir2/c"), 0), (hw3.unlink("/test8/dir2/d"), 0), (hw3.rmdir("/test8/dir2"), 0), (hw3.rmdir("/test8/dir1"), 0), (hw3.rmdir("/test8"), 0) ] actions = [(i, actions[i][0], actions[i][1]) for i in range(len(actions))] for i, result, expected in actions: self.assertEqual(result, expected, str(i) + ": " + hw3.strerr(result)) v, stat = hw3.statfs() self.assertEqual(v, 0) self.assertEqual(free, stat.f_bfree)
def test_unlink_big(self): v = hw3.unlink('/dir1/file.270') self.assertEqual(v, 0) v, data = hw3.read("/dir1/file.270", 1000, 0) self.assertEqual(v, -hw3.ENOENT)
def test_7_unlink(self): # print('Test 7 - Unlink a file') v = hw3.unlink('/file.A') self.assertEqual(v, 0) v, dirents = hw3.readdir("/file.A") self.assertEqual(v, - hw3.ENOENT)