Example #1
0
    def test_release(self):
        file = "/dir1/dir2/file"

        def mockRelease():
            if mockRelease.callCount == 0:
                mockRelease.callCount = 1
                raise CacheRetry("Exception")
            else:
                return

        mockRelease.callCount = 0

        # Raise a timeout exception
        basefh = Object
        basefh.release = Mock(side_effect=mockRelease)
        basefh.fileModified = True
        basefh.path = file
        fh = HandleWrapper(basefh, False)
        myVofs = vofs.VOFS("vos:", self.testCacheDir, opt)
        with self.assertRaises(CacheRetry):
            myVofs.release(file, fh.get_id())

        # Raise an IO error.
        basefh.release = Mock(side_effect=Exception("Exception"))
        basefh.fileModified = True
        basefh.path = file
        fh = HandleWrapper(basefh, False)
        with self.assertRaises(Exception):
            myVofs.release(file, fh.get_id())

        # Release an invalid file descriptor
        # TODO should this throw a FuseOSError?
        with self.assertRaises(KeyError):
            myVofs.release(file, -1)
Example #2
0
    def test_fsync2(self):
        file = "/dir1/dir2/file"
        testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, opt)

        testfs.client = Object()
        node = Mock(spec=vos.Node)
        node.isdir = Mock(return_value=False)
        node.props = Object
        node.props.get = Mock(side_effect=SideEffect({
            ('islocked', False): False,
            ('length',): 10,
            ('MD5',): 12354,
        }, name="node.props.get"))
        node.type = "vos:DataNode"
        testfs.client.get_node = Mock(return_value=node)
        # Try flushing on a read-only file.
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            fh = testfs.open(file, os.O_RDONLY, None)
            self.assertFalse(HandleWrapper.file_handle(fh).cache_file_handle.
                             fileModified)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(fh).cache_file_handle.
                     fsync)
            with self.assertRaises(FuseOSError) as e:
                testfs.fsync(file, False, fh)
            self.assertEqual(e.exception.errno, EPERM)
            self.assertEqual(HandleWrapper.file_handle(fh).cache_file_handle.
                             fsync.call_count, 0)
            self.assertFalse(HandleWrapper.file_handle(fh).cache_file_handle.
                             fileModified)

            testfs.release(file, fh)

        # Try with an invalid file descriptor
        with self.assertRaises(FuseOSError) as e:
            testfs.fsync(file, False, -1)
        self.assertEqual(e.exception.errno, EIO)

        # Try flushing on a read-only file system.
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            myopt = copy.copy(opt)
            testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, myopt)
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            myopt.readonly = True
            testfs.client = Object()
            testfs.client.get_node = Mock(return_value=node)
            fh = testfs.open(file, os.O_RDONLY, None)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(fh).cache_file_handle.
                     fsync)
            testfs.fsync(file, False, fh)
            self.assertEqual(
                HandleWrapper.file_handle(fh).cache_file_handle.fsync.
                call_count, 0)
            testfs.release(file, fh)
Example #3
0
    def test_fsync2(self):
        file = "/dir1/dir2/file"
        testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, opt)

        testfs.client = Object()
        node = Mock(spec=vos.Node)
        node.isdir = Mock(return_value=False)
        node.props = Object
        node.props.get = Mock(side_effect=SideEffect({
            ('islocked', False): False,
            ('length',): 10,
            ('MD5',): 12354,
        }, name="node.props.get"))
        node.type = "vos:DataNode"
        testfs.client.get_node = Mock(return_value=node)
        # Try flushing on a read-only file.
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            fh = testfs.open(file, os.O_RDONLY, None)
            self.assertFalse(HandleWrapper.file_handle(fh).cache_file_handle.
                             fileModified)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(fh).cache_file_handle.
                     fsync)
            with self.assertRaises(FuseOSError) as e:
                testfs.fsync(file, False, fh)
            self.assertEqual(e.exception.errno, EPERM)
            self.assertEqual(HandleWrapper.file_handle(fh).cache_file_handle.
                             fsync.call_count, 0)
            self.assertFalse(HandleWrapper.file_handle(fh).cache_file_handle.
                             fileModified)

            testfs.release(file, fh)

        # Try with an invalid file descriptor
        with self.assertRaises(FuseOSError) as e:
            testfs.fsync(file, False, -1)
        self.assertEqual(e.exception.errno, EIO)

        # Try flushing on a read-only file system.
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            myopt = copy.copy(opt)
            testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, myopt)
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            myopt.readonly = True
            testfs.client = Object()
            testfs.client.get_node = Mock(return_value=node)
            fh = testfs.open(file, os.O_RDONLY, None)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(fh).cache_file_handle.
                     fsync)
            testfs.fsync(file, False, fh)
            self.assertEqual(
                HandleWrapper.file_handle(fh).cache_file_handle.fsync.
                call_count, 0)
            testfs.release(file, fh)
Example #4
0
    def test_fsync(self):

        file = "/dir1/dir2/file"
        testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, opt)

        testfs.client = Object()
        node = Mock(spec=vos.Node)
        node.isdir = Mock(return_value=False)
        node.props = Object
        node.props.get = Mock(side_effect=SideEffect({
            ('islocked', False): False,
            ('length',): 10,
            ('MD5',): 12354,
        }, name="node.props.get"))
        node.type = "vos:DataNode"
        node.name = "testNode"
        testfs.client.get_node = Mock(return_value=node)
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            fh = testfs.open(file, os.O_RDWR | os.O_CREAT, None)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(
                    fh).cache_file_handle.fsync)
            testfs.fsync(file, False, fh)
            HandleWrapper.file_handle(
                fh).cache_file_handle.fsync.assert_called_once_with()
            HandleWrapper.file_handle(
                fh).cache_file_handle.fsync.assert_called_once_with()
Example #5
0
    def test_release(self):
        file = "/dir1/dir2/file"

        def mockRelease():
            if mockRelease.callCount == 0:
                mockRelease.callCount = 1
                raise CacheRetry("Exception")
            else:
                return

        mockRelease.callCount = 0

        # Raise a timeout exception
        basefh = Object
        basefh.release = Mock(side_effect=mockRelease)
        basefh.fileModified = True
        basefh.path = file
        fh = HandleWrapper(basefh, False)
        myVofs = vofs.VOFS("vos:", self.testCacheDir, opt)
        with self.assertRaises(CacheRetry):
            myVofs.release(file, fh.get_id())

        # Raise an IO error.
        basefh.release = Mock(side_effect=Exception("Exception"))
        basefh.fileModified = True
        basefh.path = file
        fh = HandleWrapper(basefh, False)
        with self.assertRaises(Exception):
            myVofs.release(file, fh.get_id())

        # Release an invalid file descriptor
        # TODO should this throw a FuseOSError?
        with self.assertRaises(KeyError):
            myVofs.release(file, -1)
Example #6
0
    def test_fsync(self):

        file = "/dir1/dir2/file"
        testfs = vofs.VOFS(self.testMountPoint, self.testCacheDir, opt)

        testfs.client = Object()
        node = Mock(spec=vos.Node)
        node.isdir = Mock(return_value=False)
        node.props = Object
        node.props.get = Mock(side_effect=SideEffect({
            ('islocked', False): False,
            ('length',): 10,
            ('MD5',): 12354,
        }, name="node.props.get"))
        node.type = "vos:DataNode"
        node.name = "testNode"
        testfs.client.get_node = Mock(return_value=node)
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, testfs.cache, None)
            fh = testfs.open(file, os.O_RDWR | os.O_CREAT, None)
            HandleWrapper.file_handle(fh).cache_file_handle.fsync = \
                Mock(wraps=HandleWrapper.file_handle(
                    fh).cache_file_handle.fsync)
            testfs.fsync(file, False, fh)
            HandleWrapper.file_handle(
                fh).cache_file_handle.fsync.assert_called_once_with()
            HandleWrapper.file_handle(
                fh).cache_file_handle.fsync.assert_called_once_with()
Example #7
0
    def test_open(self):
        myVofs = vofs.VOFS("vos:", self.testCacheDir, opt)
        file = "/dir1/dir2/file"
        file2 = "/dir1/dir2/file2"
        file3 = "/dir1/dir2/file2"
        myVofs.cache.getAttr = Mock()
        myVofs.cache.getAttr.return_value = None

        # getNode return not found
        myVofs.get_node = Mock()
        myVofs.get_node.side_effect = OSError(404, "NoFile")
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle2(file, myVofs.cache,
                                                        None)
            mockFileHandle.return_value.readData = Mock(
                wraps=mockFileHandle.return_value.readData)
            fh = myVofs.open(file, os.O_RDWR | os.O_CREAT, None)
            self.assertEqual(self.testCacheDir + "/data" + file,
                             HandleWrapper.file_handle(
                                 fh).cache_file_handle.cacheDataFile)
            self.assertEqual(self.testCacheDir + "/metaData" + file,
                             HandleWrapper.file_handle(fh).cache_file_handle.
                             cacheMetaDataFile)
            self.assertFalse(HandleWrapper.file_handle(fh).read_only)
            self.assertEqual(
                mockFileHandle.return_value.readData.call_count, 1)
            myVofs.release(file, fh)

            # Try to open a file which doesn't exist.
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file2, os.O_RDWR, None)

            myVofs.get_node.side_effect = OSError(ENOENT, "no file")
            # Open where getNode returns an error
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file2, os.O_RDWR, None)

        # test file in the cache already
        myVofs.cache.getAttr = Mock()
        myVofs.cache.getAttr.return_value = Mock()
        # fhMock = Mock()
        # myVofs.cache.open = Mock()
        # myVofs.cache.open.return_value = fhMock
        with patch('vofs.vofs.MyIOProxy') as myIOProxy, patch(
                'vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, myVofs.cache, None)
            mockFileHandle.return_value.readData = Mock(
                wraps=mockFileHandle.return_value.readData)
            myMockIOObject = Object()
            myIOProxy.return_value = myMockIOObject
            fh = myVofs.open(file, os.O_RDWR, None)
            mockFileHandle.return_value.readData.assert_called_once_with(
                0, 0, None)
            self.assertFalse(HandleWrapper.file_handle(
                fh).cache_file_handle.fileModified)
            # TODO fullyCached = True or False?
            # self.assertFalse(
            # HandleWrapper.file_handle(fh).cache_file_handle.fullyCached)

            # test a read-only file
            mockFileHandle.return_value.readData.reset_mock()
            myVofs.cache.getAttr = Mock()
            myVofs.cache.getAttr.return_value = Mock()
            HandleWrapper.file_handle(
                fh).cache_file_handle.readData.reset_mock()
            fh = myVofs.open(file, os.O_RDONLY, None)
            self.assertTrue(HandleWrapper.file_handle(fh).read_only)
            self.assertEqual(
                mockFileHandle.return_value.readData.call_count, 0)

            # test a truncated file
            myVofs.cache.open = Mock(wraps=myVofs.cache.open)
            fh = myVofs.open(file, os.O_TRUNC, None)
            myVofs.cache.open.assert_called_once_with(file, False, True,
                                                      myMockIOObject, False)
            myVofs.cache.open.reset_mock()

            # Test a file with a locked parent opened read/write.
            myVofs.cache.getAttr.return_value = None
            nodeLocked = Object()
            nodeLocked.props = {'islocked': True, 'parent': True}
            nodeLocked.type = "vos:ContainerNode"
            nodeUnlocked = Object()
            nodeUnlocked.props = {'islocked': False, 'child': True}
            nodeUnlocked.type = "vos:DataNode"
            myVofs.getNode = Mock(side_effect=SideEffect({
                (os.path.dirname(file3),): nodeLocked,
                (file3,): nodeUnlocked}, name="myVofs.getNode", default=None))
            myMockIOObject.setSize = Mock()
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file3, os.O_RDWR, None)
Example #8
0
    def test_open(self):
        myVofs = vofs.VOFS("vos:", self.testCacheDir, opt)
        file = "/dir1/dir2/file"
        file2 = "/dir1/dir2/file2"
        file3 = "/dir1/dir2/file2"
        myVofs.cache.getAttr = Mock()
        myVofs.cache.getAttr.return_value = None

        # getNode return not found
        myVofs.get_node = Mock()
        myVofs.get_node.side_effect = OSError(404, "NoFile")
        with patch('vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle2(file, myVofs.cache,
                                                        None)
            mockFileHandle.return_value.readData = Mock(
                wraps=mockFileHandle.return_value.readData)
            fh = myVofs.open(file, os.O_RDWR | os.O_CREAT, None)
            self.assertEqual(self.testCacheDir + "/data" + file,
                             HandleWrapper.file_handle(
                                 fh).cache_file_handle.cacheDataFile)
            self.assertEqual(self.testCacheDir + "/metaData" + file,
                             HandleWrapper.file_handle(fh).cache_file_handle.
                             cacheMetaDataFile)
            self.assertFalse(HandleWrapper.file_handle(fh).read_only)
            self.assertEqual(
                mockFileHandle.return_value.readData.call_count, 1)
            myVofs.release(file, fh)

            # Try to open a file which doesn't exist.
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file2, os.O_RDWR, None)

            myVofs.get_node.side_effect = OSError(ENOENT, "no file")
            # Open where getNode returns an error
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file2, os.O_RDWR, None)

        # test file in the cache already
        myVofs.cache.getAttr = Mock()
        myVofs.cache.getAttr.return_value = Mock()
        # fhMock = Mock()
        # myVofs.cache.open = Mock()
        # myVofs.cache.open.return_value = fhMock
        with patch('vofs.vofs.MyIOProxy') as myIOProxy, patch(
                'vofs.CadcCache.FileHandle') as mockFileHandle:
            mockFileHandle.return_value = MyFileHandle(
                file, myVofs.cache, None)
            mockFileHandle.return_value.readData = Mock(
                wraps=mockFileHandle.return_value.readData)
            myMockIOObject = Object()
            myIOProxy.return_value = myMockIOObject
            fh = myVofs.open(file, os.O_RDWR, None)
            mockFileHandle.return_value.readData.assert_called_once_with(
                0, 0, None)
            self.assertFalse(HandleWrapper.file_handle(
                fh).cache_file_handle.fileModified)
            # TODO fullyCached = True or False?
            # self.assertFalse(
            # HandleWrapper.file_handle(fh).cache_file_handle.fullyCached)

            # test a read-only file
            mockFileHandle.return_value.readData.reset_mock()
            myVofs.cache.getAttr = Mock()
            myVofs.cache.getAttr.return_value = Mock()
            HandleWrapper.file_handle(
                fh).cache_file_handle.readData.reset_mock()
            fh = myVofs.open(file, os.O_RDONLY, None)
            self.assertTrue(HandleWrapper.file_handle(fh).read_only)
            self.assertEqual(
                mockFileHandle.return_value.readData.call_count, 0)

            # test a truncated file
            myVofs.cache.open = Mock(wraps=myVofs.cache.open)
            fh = myVofs.open(file, os.O_TRUNC, None)
            myVofs.cache.open.assert_called_once_with(file, False, True,
                                                      myMockIOObject, False)
            myVofs.cache.open.reset_mock()

            # Test a file with a locked parent opened read/write.
            myVofs.cache.getAttr.return_value = None
            nodeLocked = Object()
            nodeLocked.props = {'islocked': True, 'parent': True}
            nodeLocked.type = "vos:ContainerNode"
            nodeUnlocked = Object()
            nodeUnlocked.props = {'islocked': False, 'child': True}
            nodeUnlocked.type = "vos:DataNode"
            myVofs.getNode = Mock(side_effect=SideEffect({
                (os.path.dirname(file3),): nodeLocked,
                (file3,): nodeUnlocked}, name="myVofs.getNode", default=None))
            myMockIOObject.setSize = Mock()
            with self.assertRaises(FuseOSError):
                fh = myVofs.open(file3, os.O_RDWR, None)