diff options
| author | Thiago da Silva <thiago@redhat.com> | 2015-02-19 17:00:11 -0500 | 
|---|---|---|
| committer | Thiago da Silva <thiago@redhat.com> | 2015-02-19 17:00:11 -0500 | 
| commit | fc318e921b073cdbe9fbe62b8c893634b057f0e8 (patch) | |
| tree | 62623b3d198d2393089846b0e3c0daed42cbe94a | |
| parent | ec407b4d61b15506e6ae5b3f28d3983af4f28457 (diff) | |
adding chmod and fchmod
Change-Id: Iba5f4e72a257adeb8ec78b267dfdef26a1ec66f1
Signed-off-by: Thiago da Silva <thiago@redhat.com>
| -rwxr-xr-x | glusterfs/api.py | 9 | ||||
| -rwxr-xr-x | glusterfs/gfapi.py | 27 | ||||
| -rw-r--r-- | test/functional/libgfapi-python-tests.py | 10 | ||||
| -rw-r--r-- | test/unit/gluster/test_gfapi.py | 30 | 
4 files changed, 76 insertions, 0 deletions
diff --git a/glusterfs/api.py b/glusterfs/api.py index 6bc19ea..1ecc34a 100755 --- a/glusterfs/api.py +++ b/glusterfs/api.py @@ -126,6 +126,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, diff --git a/glusterfs/gfapi.py b/glusterfs/gfapi.py index fc6bb27..2d51a96 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 @@ -196,6 +209,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 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..1608332 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 @@ -278,6 +293,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  | 
