def test_open_file_not_exist(self): try: f = File(self.vol.open("filenotexist", os.O_WRONLY)) except OSError as e: self.assertEqual(e.errno, errno.ENOENT) else: f.close() self.fail("Expected a OSError with errno.ENOENT")
def test_ftruncate(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.write(b"123456789") f.ftruncate(5) f.fsync() with File(self.vol.open(name, os.O_RDONLY)) as f: # The size should be reduced self.assertEqual(f.fgetsize(), 5) # So should be the content. self.assertEqual(f.read(), b"12345")
def test_bin_open_and_read(self): # Write binary data data = "Gluster is so awesome" payload = bytearray(data, "ascii") path = self._testMethodName + ".io" with File( self.vol.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o644)) as f: f.write(payload) # Read binary data with File(self.vol.open(path, os.O_RDONLY)) as f: buf = f.read() self.assertEqual(bytearray(buf), payload) self.assertEqual(buf.decode("ascii"), data)
class BinFileOpsTest(unittest.TestCase): vol = None path = None data = None @classmethod def setUpClass(cls): cls.vol = Volume(HOST, VOLNAME) cls.vol.mount() # Cleanup volume cls.vol.rmtree("/", ignore_errors=True) @classmethod def tearDownClass(cls): cls.vol.rmtree("/", ignore_errors=True) cls.vol = None def test_bin_open_and_read(self): # Write binary data data = "Gluster is so awesome" payload = bytearray(data, "ascii") path = self._testMethodName + ".io" with File(self.vol.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0644)) as f: f.write(payload) # Read binary data with File(self.vol.open(path, os.O_RDONLY)) as f: buf = f.read() self.assertEqual(bytearray(buf), payload) self.assertEqual(buf.decode("ascii"), data)
def test_fallocate(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.fallocate(0, 0, 10) f.fsync() # Stat information should now show the allocated size. self.assertEqual(f.fstat().st_size, 10)
def test_readinto(self): file_name = uuid4().hex with File(self.vol.open(file_name, os.O_WRONLY | os.O_CREAT)) as f: s = ''.join([str(i) for i in xrange(10)]) f.write(s) f.fsync() buf = bytearray(1) with File(self.vol.open(file_name, os.O_RDONLY)) as f: for i in xrange(10): # Read one character at a time into buf f.readinto(buf) self.assertEqual(len(buf), 1) self.assertEqual(buf, bytearray(str(i))) with File(self.vol.open(file_name, os.O_RDONLY)) as f: self.assertRaises(TypeError, f.readinto, str("buf"))
def test_create_file_already_exists(self): try: f = File(self.vol.open("newfile", os.O_CREAT)) f.close() g = File(self.vol.open("newfile", os.O_CREAT | os.O_EXCL)) except OSError as e: self.assertEqual(e.errno, errno.EEXIST) else: g.close() self.fail("Expected a OSError with errno.EEXIST")
def test_fremovexattr(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.fsetxattr("user.gluster", "awesome") f.fremovexattr("user.gluster") # The xattr now shouldn't exist self.assertRaises(OSError, f.fgetxattr, "user.gluster") # Removing an xattr that does not exist self.assertRaises(OSError, f.fremovexattr, "user.gluster")
def setUp(self): self.data = b"gluster is awesome" self.path = self._testMethodName + ".io" with File(self.vol.open(self.path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o644), path=self.path) as f: rc = f.write(self.data) self.assertEqual(rc, len(self.data)) f.fsync() self.assertEqual(f.originalpath, self.path)
def test_open_direct_success(self): mock_glfs_open = Mock() mock_glfs_open.return_value = 2 with patch("gluster.api.glfs_open", mock_glfs_open): f = File(self.vol.open("file.txt", os.O_WRONLY)) self.assertTrue(isinstance(f, File)) self.assertEqual(mock_glfs_open.call_count, 1) mock_glfs_open.assert_called_once_with(12345, "file.txt", os.O_WRONLY)
def test_creat_success(self): mock_glfs_creat = Mock() mock_glfs_creat.return_value = 2 with patch("gluster.gfapi.api.glfs_creat", mock_glfs_creat): with File(self.vol.open("file.txt", os.O_CREAT, 0o644)) as f: self.assertTrue(isinstance(f, File)) self.assertEqual(mock_glfs_creat.call_count, 1) mock_glfs_creat.assert_called_once_with( 12345, b"file.txt", os.O_CREAT, 0o644)
def test_readlink(self): file_name = uuid4().hex with File(self.vol.open(file_name, os.O_WRONLY | os.O_CREAT)) as f: f.write("It's not who I am underneath," "but what I do that defines me.") f.fsync() # Create a symlink link_name = uuid4().hex self.vol.symlink(file_name, link_name) self.assertEqual(self.vol.readlink(link_name), file_name)
def test_validate_init(self): self.assertRaises(ValueError, File, None) self.assertRaises(ValueError, File, "not_int") try: with File(None) as f: pass except ValueError: pass else: self.fail("Expecting ValueError") try: with File("not_int") as f: pass except ValueError: pass else: self.fail("Expecting ValueError")
def test_discard(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.fallocate(0, 0, 10) f.fsync() self.assertEqual(f.fstat().st_size, 10) # We can't really know if the blocks were actually returned # to filesystem. This functional test only tests if glfs_discard # interfacing is proper and that it returns successfully. f.discard(4, 5)
def test_zerofill(self): name = uuid4().hex with File(self.vol.open(name, os.O_RDWR | os.O_CREAT)) as f: f.write(b'0123456789') f.fsync() self.assertEqual(f.fstat().st_size, 10) f.lseek(0, os.SEEK_SET) self.assertEqual(f.read(), b'0123456789') f.zerofill(3, 6) f.lseek(0, os.SEEK_SET) data = f.read() self.assertEqual(data, b'012\x00\x00\x00\x00\x00\x009') self.assertEqual(len(data), 10)
def test_fgetxattr(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.fsetxattr("user.gluster", "awesome") # user does not know the size of value beforehand self.assertEqual(f.fgetxattr("user.gluster"), "awesome") # user knows the size of value beforehand self.assertEqual(f.fgetxattr("user.gluster", 7), "awesome") # size is larger self.assertEqual(f.fgetxattr("user.gluster", 70), "awesome") # size is smaller self.assertRaises(OSError, f.fgetxattr, "user.gluster", size=1) # size is negative self.assertRaises(ValueError, f.fgetxattr, "user.gluster", size=-7)
def test_access(self): file_name = uuid4().hex with File(self.vol.open(file_name, os.O_WRONLY | os.O_CREAT)) as f: f.write("I'm whatever Gotham needs me to be") f.fsync() # Check that file exists self.assertTrue(self.vol.access(file_name, os.F_OK)) # Check that file does not exist self.assertFalse(self.vol.access("nonexistentfile", os.F_OK)) dir_name = uuid4().hex self.vol.mkdir(dir_name) # Check that directory exists self.assertTrue(self.vol.access(dir_name, os.F_OK)) # Check if there is execute and write permission self.assertTrue(self.vol.access(file_name, os.W_OK | os.X_OK))
def test_fsetxattr(self): name = uuid4().hex with File(self.vol.open(name, os.O_WRONLY | os.O_CREAT)) as f: f.fsetxattr("user.gluster", "awesome") self.assertEqual(f.fgetxattr("user.gluster"), "awesome") # flag = 1 behavior: fail if xattr exists self.assertRaises(OSError, f.fsetxattr, "user.gluster", "more awesome", flags=1) # flag = 1 behavior: pass if xattr does not exist f.fsetxattr("user.gluster2", "awesome2", flags=1) self.assertEqual(f.fgetxattr("user.gluster2"), "awesome2") # flag = 2 behavior: fail if xattr does not exist self.assertRaises(OSError, f.fsetxattr, "user.whatever", "awesome", flags=2) # flag = 2 behavior: pass if xattr exists f.fsetxattr("user.gluster", "more awesome", flags=2) self.assertEqual(f.fgetxattr("user.gluster"), "more awesome")
def test_flistxattr(self): name = uuid4().hex with File(self.vol.open(name, os.O_RDWR | os.O_CREAT)) as f: f.fsetxattr("user.gluster", "awesome") f.fsetxattr("user.gluster2", "awesome2") xattrs = f.flistxattr() self.assertTrue("user.gluster" in xattrs) self.assertTrue("user.gluster2" in xattrs) # Test passing of size # larger size - should pass xattrs = f.flistxattr(size=512) self.assertTrue("user.gluster" in xattrs) self.assertTrue("user.gluster2" in xattrs) # smaller size - should fail self.assertRaises(OSError, f.flistxattr, size=1) # invalid size - should fail self.assertRaises(ValueError, f.flistxattr, size=-1)
def test_write_file_dup_lseek_read(self): try: f = File(self.vol.open("dune", os.O_CREAT | os.O_EXCL | os.O_RDWR)) f.write(b"I must not fear. Fear is the mind-killer.") fdup = f.dup() self.assertTrue(isinstance(fdup, File)) f.close() ret = fdup.lseek(0, os.SEEK_SET) self.assertEqual(ret, 0) buf = fdup.read(15) self.assertEqual(buf, b"I must not fear") ret = fdup.lseek(29, os.SEEK_SET) self.assertEqual(ret, 29) buf = fdup.read(11) self.assertEqual(buf, b"mind-killer") fdup.close() except OSError as e: self.fail(e.message)
def test_write_file_dup_lseek_read(self): try: f = File(self.vol.open("dune", os.O_CREAT | os.O_EXCL | os.O_RDWR)) f.write("I must not fear. Fear is the mind-killer.") fdup = f.dup() self.assertTrue(isinstance(fdup, File)) f.close() ret = fdup.lseek(0, os.SEEK_SET) self.assertEqual(ret, 0) buf = fdup.read(15) self.assertEqual(buf, "I must not fear") ret = fdup.lseek(29, os.SEEK_SET) self.assertEqual(ret, 29) buf = fdup.read(11) self.assertEqual(buf, "mind-killer") fdup.close() except OSError as e: self.fail(e.message)
def setUpClass(cls): cls.fd = File(2, 'fakefile')
def test_open_and_read(self): with File(self.vol.open(self.path, os.O_RDONLY)) as f: self.assertTrue(isinstance(f, File)) buf = f.read(len(self.data)) self.assertFalse(isinstance(buf, int)) self.assertEqual(buf, self.data)