Exemplo n.º 1
0
def write_metadata(path_or_fd, metadata):
    """
    Helper function to write serialized metadata for a File/Directory.

    :param path_or_fd: File/Directory path or fd to write the metadata
    :param metadata: dictionary of metadata write
    """
    assert isinstance(metadata, dict)
    metastr = serialize_metadata(metadata)
    key = 0
    while metastr:
        try:
            do_setxattr(path_or_fd,
                        '%s%s' % (METADATA_KEY, key or ''),
                        metastr[:MAX_XATTR_SIZE])
        except IOError as err:
            if err.errno in (errno.ENOSPC, errno.EDQUOT):
                if isinstance(path_or_fd, int):
                    filename = get_filename_from_fd(path_or_fd)
                    do_log_rl("write_metadata(%d, metadata) failed: %s : %s",
                              path_or_fd, err, filename)
                else:
                    do_log_rl("write_metadata(%s, metadata) failed: %s",
                              path_or_fd, err)
                raise DiskFileNoSpace()
            else:
                raise SwiftOnFileSystemIOError(
                    err.errno,
                    '%s, setxattr("%s", %s, metastr)' % (err.strerror,
                                                         path_or_fd, key))
        metastr = metastr[MAX_XATTR_SIZE:]
        key += 1
 def test_do_log_rl_err(self):
     _mock = Mock()
     pid = os.getpid()
     sleep(1.1)
     with patch("logging.error", _mock):
         fs.do_log_rl("Hello %s", "world", log_level="blah")
     _mock.assert_called_once_with('[PID:' + str(pid) + '][RateLimitedLog;'
                                   'Count:1] Hello %s', 'world')
    def test_do_log_rl(self):
        _mock = Mock()
        pid = os.getpid()
        with patch("logging.error", _mock):
            # The first call always invokes logging.error
            fs.do_log_rl("Hello %s", "world")
            _mock.reset_mock()
            # We call do_log_rl 3 times. Twice in immediate succession and
            # then after an interval of 1.1 second. Thus, the last call will
            # invoke logging.error
            for i in range(2):
                fs.do_log_rl("Hello %s", "world")
            sleep(1.1)
            fs.do_log_rl("Hello %s", "world")

        # We check if logging.error was called exactly once even though
        # do_log_rl was called 3 times.
        _mock.assert_called_once_with('[PID:' + str(pid) + '][RateLimitedLog;'
                                      'Count:3] Hello %s', 'world')