Skip to main content

Easiest way to simulate no free disk space situation?


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.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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.

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

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


    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.


    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.

    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:

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


    As an alternative look into 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.

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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?

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

    ReplyDelete
  10. 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.

    ReplyDelete
  11. Have you tried mount with -f -r ? It's not really low disk space, but it should throw an error from the same level.

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex