apache 模拟没有可用磁盘空间情况的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2043999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:36:45  来源:igfitidea点击:

Easiest way to simulate no free disk space situation?

phpapachemacostestingdiskspace

提问by Ilya Birman

I need to test my web app in a scenario where there's no disk space remaining, i.e. I cannot write any more files. But I don't want to fill my hard drive with junk just to make sure there's really no space left. What I want is to simulate this situation withing a particular process (actually, a PHP app).

我需要在没有剩余磁盘空间的情况下测试我的网络应用程序,即我无法再写入任何文件。但我不想用垃圾填满我的硬盘,只是为了确保没有剩余空间。我想要的是用一个特定的进程(实际上是一个 PHP 应用程序)来模拟这种情况。

Indeed, temporarily prohibiting disk writes to a process must be enough.

实际上,暂时禁止对进程进行磁盘写入就足够了。

What's the easiest way to do this? I'm using Mac OS X 10.6.2 with built-in Apache/PHP bundle. Thanks.

什么是最简单的方法来做到这一点?我正在使用带有内置 Apache/PHP 包的 Mac OS X 10.6.2。谢谢。

Edit: Disk free space check is not going to be reliable since it can change any moment. Many pages are being served simultaneously. There can be enough free space when checking, but none by the moment you actually write something. Also, checking for disk free space will require changing the code everywhere I write a file, which is not what I want :-) Finally, this solution is exactly the opposite of what I'm trying to test: how my app will behave when it cannot write any more.

编辑:磁盘可用空间检查将不可靠,因为它可以随时更改。许多页面正在同时提供。检查时可以有足够的可用空间,但在您实际编写内容时却没有可用空间。此外,检查磁盘可用空间将需要在我编写文件的任何地方更改代码,这不是我想要的:-) 最后,此解决方案与我要测试的完全相反:我的应用程序在以下情况下的行为方式它不能再写了。

采纳答案by Hamish Grubijan

I bet you could also create your own .dmg file with file system of size ... say 2Mb and write to it. If this works, then it is super-easy for testing - you just mount it and switch the path for testing. If the dmg is small enough, you could probably even upload it to the source control.

我敢打赌,您也可以使用大小为 2Mb 的文件系统创建自己的 .dmg 文件并写入它。如果这有效,那么它就非常容易测试——您只需安装它并切换测试路径即可。如果 dmg 足够小,您甚至可以将其上传到源代码管理。

回答by ryeguy

When I needed to do this I created a virtual machine with limited space allocated to the virtual disk.

当我需要这样做时,我创建了一个虚拟机,分配给虚拟磁盘的空间有限。

回答by Gordon

No need to use a prefilled dummy filesystem.
Use disk_free_space()to mock the FileSystem

无需使用预填充的虚拟文件系统。
使用disk_free_space()嘲笑文件系统

disk_free_space() - Given a string containing a directory, this function will return the number of bytes available on the corresponding filesystem or disk partition.

disk_free_space() - 给定一个包含目录的字符串,此函数将返回相应文件系统或磁盘分区上可用的字节数。

To simulate, just wrap the function into a FileSystem Class. Then inject it to your class doing the saving as a dependency and check if the drive is full before you do the actual saving. In your UnitTest, just swap out the regular class with the class mocking a full file system and you're done. This way you don't have to recreate the full disk drive or keep the drive with your project files all the time whenever you want to rerun your test, e.g.

要进行模拟,只需将该函数包装到 FileSystem 类中即可。然后将其注入您的班级,将其作为依赖项进行保存,并在进行实际保存之前检查驱动器是否已满。在您的 UnitTest 中,只需将常规类替换为模拟完整文件系统的类即可。这样您就不必重新创建完整的磁盘驱动器或在您想要重新运行测试时始终将驱动器与您的项目文件一起保存,例如

class MyFileSystem
{
    public static function df($drive)
    {
        return disk_free_space($drive);
    }
}

and to simulate a full FileSystem do

并模拟一个完整的文件系统做

class MyFileSystemFull
{
    public static function df($drive)
    {
        return 0;
    }
}

If you want to overload the function to return 0 at all times, you could use the RunKit Pecl extension and do:

如果您想重载函数以始终返回 0,您可以使用 RunKit Pecl 扩展并执行以下操作:

runkit_function_redefine('disk_free_space','string','return 0;');

As an alternative look into vfsStream:

作为对vfsStream的另一种看法

vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream 是虚拟文件系统的流包装器,可能有助于单元测试以模拟真实文件系统。它可以与任何单元测试框架一起使用,例如 PHPUnit 或 SimpleTest。

回答by Cheeso

I used a thumb drive, as the volume for the process.

我使用了一个拇指驱动器,作为这个过程的音量。

回答by Noufal Ibrahim

I'm not sure of how to do it on OSX but on Linux, I'd probably put a disk quota on my test user and then run the app.

我不确定如何在 OSX 上执行此操作,但在 Linux 上,我可能会为我的测试用户设置磁盘配额,然后运行该应用程序。

Or maybe create a null file (a small one), format it as an ext3 partition, mount it using the loopback device and run the PHP app inside it. This would be closer to a physical disk that's short of space.

或者可能创建一个空文件(一个小文件),将其格式化为 ext3 分区,使用环回设备安装它并在其中运行 PHP 应用程序。这将更接近于空间不足的物理磁盘。

回答by Pekka

A quick and easy solution would be setting up a Quota for a specialized user account. Quota support on Mac OS X

一种快速简便的解决方案是为专门的用户帐户设置配额。Mac OS X 上的配额支持

If you don't mind the hassle to set it up, and the fact that you probably need a second license for your operating system, a Virtual Machine is probably the best idea with the most long-term possibilities.

如果您不介意设置它的麻烦,并且您可能需要为您的操作系统获得第二个许可证,那么虚拟机可能是具有最长期可能性的最佳主意。

回答by Norman Ramsey

Create a disk/filesystem image in a regular file (of limited size) and loop mount it.

在常规文件(大小有限)中创建磁盘/文件系统映像并循环挂载它。

But if you'll be doing this often I'd create a virtual machine—you'll find opportunity to reuse it.

但是如果你经常这样做,我会创建一个虚拟机——你会发现重用它的机会。

回答by Mathias

Can't you use a Mock, and substitute the part of your code which actually writes to disk, with a fake test replacement which will throw the exception(s) you expect to see?

您不能使用 Mock 并替换实际写入磁盘的代码部分,用一个假的测试替换来抛出您希望看到的异常吗?

回答by knittl

recursively remove all write permissions from your webfolder, folders and files your app is going to write to.

从您的应用程序要写入的 web 文件夹、文件夹和文件中递归删除所有写入权限。

回答by bbum

Bottom line; don't do that. Seriously -- there are so many thingsthat go horribly wrong when a volume runs out of space. Unless the volume targeted is notthe boot volume and has not one other application writing to it, the behavior as the disk fills will be out of your control anyway.

底线;不要那样做。说真的 -当卷空间不足时,有很多事情会发生可怕的错误。除非目标卷不是启动卷并且没有其他应用程序写入它否则磁盘填满时的行为将不受您的控制。

If it is the boot drive, the system will quite likely panic or crash upon full disk anyway. Or, if not, it'll behave erratically.

如果是引导驱动器,则系统很可能会在磁盘满时崩溃或崩溃。或者,如果没有,它会表现不正常。

If you are talking about a data volume, is yours the onlyapp that is writing to it? If any other app is writing, do you knowfor certain how they might fail?

如果您正在谈论数据量,那么您的应用程序是唯一写入数据的应用程序吗?如果有任何其他应用程序正在写,你知道肯定他们会怎么失败的?

Disk space is so dirt cheap these days that you are far better off ensuring that out of disk spacewill simply never happen. Drop a 2TB drive in and put an alarm in when it reaches 50% capacity. Far cheaper to implement (unless your time is free) and far more reliable.

如今,磁盘空间非常便宜,您最好确保永远不会发生磁盘空间不足的情况。放入 2TB 驱动器,并在容量达到 50% 时发出警报。实施起来要便宜得多(除非你有空闲时间)而且更可靠。