diff options
-rwxr-xr-x | glusterfs/api.py | 15 | ||||
-rwxr-xr-x | glusterfs/gfapi.py | 62 | ||||
-rw-r--r-- | test/functional/libgfapi-python-tests.py | 10 | ||||
-rw-r--r-- | test/unit/gluster/test_gfapi.py | 72 |
4 files changed, 155 insertions, 4 deletions
diff --git a/glusterfs/api.py b/glusterfs/api.py index 7f22b4e..803a778 100755 --- a/glusterfs/api.py +++ b/glusterfs/api.py @@ -269,6 +269,15 @@ glfs_stat = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_char_p, glfs_fstat = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.POINTER( Stat))(('glfs_fstat', client)) +glfs_chmod = ctypes.CFUNCTYPE(ctypes.c_int, + ctypes.c_void_p, + ctypes.c_char_p, + ctypes.c_ushort)(('glfs_chmod', client)) + +glfs_fchmod = ctypes.CFUNCTYPE(ctypes.c_int, + ctypes.c_void_p, + ctypes.c_ushort)(('glfs_fchmod', client)) + glfs_chown = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_char_p, @@ -366,6 +375,12 @@ glfs_rmdir = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_char_p)(('glfs_rmdir', client)) +glfs_setfsuid = ctypes.CFUNCTYPE(ctypes.c_int, + ctypes.c_uint)(('glfs_setfsuid', client)) + +glfs_setfsgid = ctypes.CFUNCTYPE(ctypes.c_int, + ctypes.c_uint)(('glfs_setfsgid', client)) + # TODO: creat and open fails on test_create_file_already_exists & test_open_file_not_exist functional testing, # noqa # when defined via function prototype.. Need to find RCA. For time being, using it from 'api.glfs_' # noqa diff --git a/glusterfs/gfapi.py b/glusterfs/gfapi.py index fc6bb27..42c4aef 100755 --- a/glusterfs/gfapi.py +++ b/glusterfs/gfapi.py @@ -65,6 +65,19 @@ class File(object): raise OSError(err, os.strerror(err)) return ret + def fchmod(self, mode): + """ + Change this file's mode + + :param mode: new mode + :returns: 0 if success, raises OSError if it fails + """ + ret = api.glfs_fchmod(self.fd, mode) + if ret < 0: + err = ctypes.get_errno() + raise OSError(err, os.strerror(err)) + return ret + def fchown(self, uid, gid): """ Change this file's owner and group id @@ -91,6 +104,12 @@ class File(object): raise OSError(err, os.strerror(err)) return ret + def fgetsize(self): + """ + Return the size of a file, reported by fstat() + """ + return self.fstat().st_size + def fstat(self): """ Returns Stat object for this file. @@ -124,9 +143,18 @@ class File(object): """ return api.glfs_lseek(self.fd, pos, how) - def read(self, buflen, flags=0): + def read(self, buflen=-1): + """ + read file + + :param buflen: length of read buffer. If less than 0, then whole + file is read. Default is -1. + :returns: buffer of size buflen + """ + if buflen < 0: + buflen = self.fgetsize() rbuf = ctypes.create_string_buffer(buflen) - ret = api.glfs_read(self.fd, rbuf, buflen, flags) + ret = api.glfs_read(self.fd, rbuf, buflen, 0) if ret > 0: return rbuf elif ret < 0: @@ -177,8 +205,6 @@ class Dir(object): class Volume(object): - # Housekeeping functions. - def __init__(self, host, volid, proto="tcp", port=24007): # Add a reference so the module-level variable "api" doesn't # get yanked out from under us (see comment above File def'n). @@ -196,6 +222,20 @@ class Volume(object): def mount(self): return api.glfs_init(self.fs) + def chmod(self, path, mode): + """ + Change mode of path + + :param path: the item to be modified + :mode: new mode + :returns: 0 if success, raises OSError if it fails + """ + ret = api.glfs_chmod(self.fs, path, mode) + if ret < 0: + err = ctypes.get_errno() + raise OSError(err, os.strerror(err)) + return ret + def chown(self, path, uid, gid): """ Change owner and group id of path @@ -441,6 +481,20 @@ class Volume(object): except OSError as e: onerror(self.rmdir, path, e) + def setfsuid(self, uid): + ret = api.glfs_setfsuid(uid) + if ret < 0: + err = ctypes.get_errno() + raise OSError(err, os.strerror(err)) + return ret + + def setfsgid(self, gid): + ret = api.glfs_setfsgid(gid) + if ret < 0: + err = ctypes.get_errno() + raise OSError(err, os.strerror(err)) + return ret + def setxattr(self, path, key, value, vlen): ret = api.glfs_setxattr(self.fs, path, key, value, vlen, 0) if ret < 0: diff --git a/test/functional/libgfapi-python-tests.py b/test/functional/libgfapi-python-tests.py index 8bcbf45..4dbdf2d 100644 --- a/test/functional/libgfapi-python-tests.py +++ b/test/functional/libgfapi-python-tests.py @@ -163,6 +163,16 @@ class FileOpsTest(unittest.TestCase): except OSError as e: self.fail(e.message) + def test_chmod(self): + stat = self.vol.stat(self.path) + orig_mode = oct(stat.st_mode & 0777) + self.assertEqual(orig_mode, '0644L') + ret = self.vol.chmod(self.path, 0600) + self.assertEqual(ret, 0) + stat = self.vol.stat(self.path) + new_mode = oct(stat.st_mode & 0777) + self.assertEqual(new_mode, '0600L') + def test_exists(self): e = self.vol.exists(self.path) self.assertTrue(e) diff --git a/test/unit/gluster/test_gfapi.py b/test/unit/gluster/test_gfapi.py index 550ba79..8451b51 100644 --- a/test/unit/gluster/test_gfapi.py +++ b/test/unit/gluster/test_gfapi.py @@ -63,6 +63,21 @@ class TestFile(unittest.TestCase): def tearDown(self): glusterfs.gfapi.api.glfs_close = self._saved_glfs_close + def test_fchmod_success(self): + mock_glfs_fchmod = Mock() + mock_glfs_fchmod.return_value = 0 + + with patch("glusterfs.gfapi.api.glfs_fchmod", mock_glfs_fchmod): + ret = self.fd.fchmod(0600) + self.assertEquals(ret, 0) + + def test_fchmod_fail_exception(self): + mock_glfs_fchmod = Mock() + mock_glfs_fchmod.return_value = -1 + + with patch("glusterfs.gfapi.api.glfs_fchmod", mock_glfs_fchmod): + self.assertRaises(OSError, self.fd.fchmod, 0600) + def test_fchown_success(self): mock_glfs_fchown = Mock() mock_glfs_fchown.return_value = 0 @@ -165,6 +180,18 @@ class TestFile(unittest.TestCase): b = self.fd.read(5) self.assertEqual(b, 0) + def test_read_buflen_negative(self): + _mock_fgetsize = Mock(return_value=12345) + + def _mock_glfs_read(fd, rbuf, buflen, flags): + self.assertEqual(buflen, 12345) + return buflen + + for buflen in (-1,-2,-999): + with patch("glusterfs.gfapi.api.glfs_read", _mock_glfs_read): + with patch("glusterfs.gfapi.File.fgetsize", _mock_fgetsize): + b = self.fd.read(buflen) + def test_write_success(self): mock_glfs_write = Mock() mock_glfs_write.return_value = 5 @@ -278,6 +305,21 @@ class TestVolume(unittest.TestCase): glusterfs.gfapi.api.glfs_close = cls._saved_glfs_close glusterfs.gfapi.api.glfs_closedir = cls._saved_glfs_closedir + def test_chmod_success(self): + mock_glfs_chmod = Mock() + mock_glfs_chmod.return_value = 0 + + with patch("glusterfs.gfapi.api.glfs_chmod", mock_glfs_chmod): + ret = self.vol.chmod("file.txt", 0600) + self.assertEquals(ret, 0) + + def test_chmod_fail_exception(self): + mock_glfs_chmod = Mock() + mock_glfs_chmod.return_value = -1 + + with patch("glusterfs.gfapi.api.glfs_chmod", mock_glfs_chmod): + self.assertRaises(OSError, self.vol.chmod, "file.txt", 0600) + def test_chown_success(self): mock_glfs_chown = Mock() mock_glfs_chown.return_value = 0 @@ -761,6 +803,36 @@ class TestVolume(unittest.TestCase): mock_unlink.assert_called_once_with("dir1/file") mock_rmdir.assert_called_with("dir1") + def test_setfsuid_success(self): + mock_glfs_setfsuid = Mock() + mock_glfs_setfsuid.return_value = 0 + + with patch("glusterfs.gfapi.api.glfs_setfsuid", mock_glfs_setfsuid): + ret = self.vol.setfsuid(1000) + self.assertEquals(ret, 0) + + def test_setfsuid_fail(self): + mock_glfs_setfsuid = Mock() + mock_glfs_setfsuid.return_value = -1 + + with patch("glusterfs.gfapi.api.glfs_setfsuid", mock_glfs_setfsuid): + self.assertRaises(OSError, self.vol.setfsuid, 1001) + + def test_setfsgid_success(self): + mock_glfs_setfsgid = Mock() + mock_glfs_setfsgid.return_value = 0 + + with patch("glusterfs.gfapi.api.glfs_setfsgid", mock_glfs_setfsgid): + ret = self.vol.setfsgid(1000) + self.assertEquals(ret, 0) + + def test_setfsgid_fail(self): + mock_glfs_setfsgid = Mock() + mock_glfs_setfsgid.return_value = -1 + + with patch("glusterfs.gfapi.api.glfs_setfsgid", mock_glfs_setfsgid): + self.assertRaises(OSError, self.vol.setfsgid, 1001) + def test_setxattr_success(self): mock_glfs_setxattr = Mock() mock_glfs_setxattr.return_value = 0 |