模拟没有可用磁盘空间情况的最简单方法?
我需要在没有剩余磁盘空间的情况下测试我的网络应用程序,即我无法写入更多文件。但我不想只是为了确保确实没有剩余空间而在硬盘中塞满垃圾。我想要的是用一个特定的进程(实际上是一个 PHP 应用程序)来模拟这种情况。
事实上,暂时禁止进程的磁盘写入就足够了。
做到这一点最简单的方法是什么?我使用的是带有内置 Apache/PHP 包的 Mac OS X 10.6.2。谢谢。
编辑:磁盘可用空间检查并不可靠,因为它随时可能发生变化。许多页面同时被提供。检查时可能有足够的可用空间,但在您实际写入内容时却没有足够的可用空间。另外,检查磁盘可用空间将需要在我写入文件的所有地方更改代码,这不是我想要的:-)最后,这个解决方案与我试图测试的完全相反:我的应用程序在以下情况下的行为方式:它不能再写了。
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).
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我打赌您也可以创建自己的 .dmg 文件,其文件系统大小为...比如 2Mb 并写入它。如果这有效,那么测试就非常容易了——你只需安装它并切换测试路径即可。如果 dmg 足够小,您甚至可以将其上传到源代码管理。
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.
当我需要执行此操作时,我创建了一个虚拟机,并为虚拟磁盘分配了有限的空间。
When I needed to do this I created a virtual machine with limited space allocated to the virtual disk.
无需使用预填充的虚拟文件系统。
使用
disk_free_space()
模拟文件系统要进行模拟,只需将函数包装到 FileSystem 类中即可。然后将其注入到您的类中,作为依赖项进行保存,并在进行实际保存之前检查驱动器是否已满。在您的 UnitTest 中,只需将常规类替换为模拟完整文件系统的类即可。这样,每当您想要重新运行测试时,您就不必重新创建完整的磁盘驱动器或始终将驱动器与项目文件一起保存,例如
模拟完整的文件系统,
如果您想重载该函数以返回 0在任何时候,您都可以使用 RunKit Pecl 扩展并执行以下操作:
作为替代方案,请查看 vfsStream :
No need to use a prefilled dummy filesystem.
Use
disk_free_space()
to mock the FileSystemTo 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.
and to simulate a full FileSystem do
If you want to overload the function to return 0 at all times, you could use the RunKit Pecl extension and do:
As an alternative look into vfsStream:
我使用拇指驱动器作为该过程的卷。
I used a thumb drive, as the volume for the process.
我不确定如何在 OSX 上执行此操作,但在 Linux 上,我可能会为测试用户设置磁盘配额,然后运行该应用程序。
或者创建一个空文件(一个小文件),将其格式化为 ext3 分区,使用环回设备安装它并在其中运行 PHP 应用程序。这更接近于空间不足的物理磁盘。
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.
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.
一个快速而简单的解决方案是为专门的用户帐户设置配额。 Mac OS X 上的配额支持
如果您不介意由于设置起来很麻烦,而且您可能需要操作系统的第二个许可证,因此虚拟机可能是具有最长期可能性的最佳想法。
A quick and easy solution would be setting up a Quota for a specialized user account. Quota support on 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.
在常规文件(大小有限)中创建磁盘/文件系统映像并循环挂载它。
但如果您经常这样做,我会创建一个虚拟机 - 您会找到重用它的机会。
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.
难道你不能使用模拟,并用一个会抛出你期望看到的异常的假测试替换来替换实际写入磁盘的代码部分吗?
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?
递归地从您的应用程序要写入的网络文件夹、文件夹和文件中删除所有写入权限。
recursively remove all write permissions from your webfolder, folders and files your app is going to write to.
底线;不要那样做。说真的——当卷空间不足时,很多事情都会出现严重错误。除非目标卷不是启动卷并且没有其他应用程序写入该卷,否则磁盘填充时的行为无论如何都将超出您的控制。
如果它是引导驱动器,系统很可能会在磁盘已满时出现恐慌或崩溃。或者,如果没有,它的行为就会不稳定。
如果您谈论的是数据卷,那么您是唯一正在写入数据的应用程序吗?如果任何其他应用程序正在写入,您知道它们可能会如何失败吗?
如今,磁盘空间非常便宜,您最好确保永远不会发生磁盘空间不足的情况。放入 2TB 驱动器,并在达到 50% 容量时发出警报。实施起来便宜得多(除非您的时间是空闲的)并且更加可靠。
Bottom line; don't do that. Seriously -- there are so many things that go horribly wrong when a volume runs out of space. Unless the volume targeted is not the 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 only app that is writing to it? If any other app is writing, do you know for certain how they might fail?
Disk space is so dirt cheap these days that you are far better off ensuring that out of disk space will 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.
您是否尝试过使用 -f -r 挂载?这并不是真正的磁盘空间不足,但它应该会抛出同一级别的错误。
Have you tried mount with -f -r ? It's not really low disk space, but it should throw an error from the same level.
我认为模拟课的想法是正确的方向。我通常也这样测试我的代码。如果可能的话,我会使用一个框架,而不是自己编写这些类。我不太了解 PHP(更多地使用 C# 和 Java 编程),但这似乎很好。
https://github.com/padraic/mockery
I think the idea with the mock class is the right direction. I usually test my code that way too. If possible I use a framework for that though, instead of writing those classes myself. I don't know PHP very well (more programming with C# and Java) , but this seems to be nice.
https://github.com/padraic/mockery
无论您在何处获得剩余磁盘空间,只需将其注释掉并使用替换值(例如 0.1、0、-1)运行您的应用程序
Wherever you obtain the remaining disk space, just comment it out and run your app with a replacement values such as 0.1, 0, -1