0

我有一个应用程序,它创建几个非常小的临时文件作为单元测试或特定功能的一部分。我开始经常遇到“随机”错误OSError: [Errno 27] File too large. 随机我的意思是问题有时会在重新启动时消失,或者如果我在一段时间后重新运行测试。我手动检查了 Mac 上的临时文件夹是否已清理并且有足够的内存/空间来创建这样的小文件。(几个 GB 可用)此上下文中的小文件例如大小为 16384、58330、26502(以字节为单位)甚至更小。Shutil.copyfile 用于创建这些文件,但在执行 os.link 时也会出现相同的错误,这应该占用磁盘上的最小空间。我用 os.link 替换了 shutil.copfile(如果可能)来测试它是否解决了问题,但效果是一样的。在 Mac OS 上,当我进行开发时,经常在密集运行大量测试的随机时间后抛出此错误。但是,在 docker 映像中运行时,该错误始终存在。

错误片段:

    @pytest.fixture(scope="module")
    def simulate_mirror(fixtures):
        """
        Thjs fixture creates a file/directory structure that simulates an offline PyPI mirror
        Used to test the `mirror://` bandersnatch integration for scanning the whole PyPI repository
        """
        from aura import mirror as amirror

        with tempfile.TemporaryDirectory(prefix="aura_test_mirror_") as mirror:
            pmirror = Path(mirror)
            assert pmirror.is_dir()
            os.mkdir(pmirror / "json")

            for pkg, pkg_files in MIRROR_FILES.items():
                # copy the package JSON metadata
                os.link(
                    fixtures.path(f"mirror/{pkg}.json"),
                    pmirror / "json" / pkg
                )

                for p in pkg_files:
                    os.makedirs(pmirror / p["path"])
                    os.link(
                        fixtures.path(f"mirror/{p['name']}"),
>                       os.fspath(pmirror / p["path"] / p["name"])
                    )
E                   OSError: [Errno 27] File too large: '/analyzer/tests/files/mirror/wheel-0.34.2-py2.py3-none-any.whl' -> '/tmp/aura_test_mirror_a6o5p8fn/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl'

tests/conftest.py:204: OSError
    def test_apip():
        # Test package taken from pip tests
        # https://github.com/pypa/pip/tree/master/tests/data/packages
        whl = Path(__file__).parent /'files' / 'simplewheel-1.0-py2.py3-none-any.whl'

        venv_dir = tempfile.mkdtemp(suffix="_pytest_aura_apip")
        # print(f'Virtualenv created in {venv_dir}')
        try:
            # Create virtualenv
            venv.create(
                env_dir=venv_dir,
>               with_pip=True,
                # symlinks=True
            )

tests/test_apip.py:56:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.7/venv/__init__.py:390: in create
    builder.create(env_dir)
/usr/local/lib/python3.7/venv/__init__.py:66: in create
    self.setup_python(context)
/usr/local/lib/python3.7/venv/__init__.py:233: in setup_python
    copier(context.executable, path)
/usr/local/lib/python3.7/venv/__init__.py:176: in symlink_or_copy
    shutil.copyfile(src, dst)
/usr/local/lib/python3.7/shutil.py:122: in copyfile
    copyfileobj(fsrc, fdst)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

fsrc = <_io.BufferedReader name='/usr/local/bin/python'>, fdst = <_io.BufferedWriter name='/tmp/tmpw5gbckwv_pytest_aura_apip/bin/python'>, length = 16384

    def copyfileobj(fsrc, fdst, length=16*1024):
        """copy data from file-like object fsrc to file-like object fdst"""
        while 1:
            buf = fsrc.read(length)
            if not buf:
                break
>           fdst.write(buf)
E           OSError: [Errno 27] File too large

/usr/local/lib/python3.7/shutil.py:82: OSError

使用创建 virtualenv 时有时也会抛出这些错误venv.create。我也总是收到 sqlite3.OperationalError: docker 映像中的磁盘 I/O 错误,这可能与同一问题有关。更多技术信息:Mac OS Catalina,完全升级,通过 brew 重新安装 python 到最新的 3.7.7 + 重新创建了所有 virtualenv 并重新安装了所有依赖项。基于其他 SO 问题(File too Large python),我已经检查了文件系统是否支持限制范围内的文件大小以及目录中允许的最大文件数。包含问题的最新提交(包括因错误而失败的 dockerfile):

https://github.com/RootLUG/aura/commit/b4c730693e8f7fd36ab2acc78997694002c4e345

触发错误的代码位置:

https://github.com/RootLUG/aura/blob/dev/tests/conftest.py#L181

https://github.com/RootLUG/aura/blob/dev/tests/test_aip.py#L54

来自单元测试的 Travis 日志:

https://travis-ci.org/github/RootLUG/aura/builds/671722230

4

1 回答 1

0

Does it work outside of Docker? I'm not familiar with how they do it on Macs but on Windows at least, I believe Docker runs in a VM with its own disk space limitations, so perhaps Docker is out of space and that's the issue you're running up against. May be worth looking into that.

于 2020-04-06T17:43:00.493 回答