19

I have to write a bat script for a test scenario where the software that we are testing fails to write to file due to a disk full error. The test script must be automated, so that we can run it on overnight tests, for example. The test script must also work at different computers, so installing a software like a virtual machine wouldn't be the best solution in this case.

How can I simulate that error in a Windows environment?

Alceu Costa
  • 9,733
  • 19
  • 65
  • 83
  • People seem to overlook that you wrote "test scenario". That means for me, that you seek a solution that works regardless of the environment (testing done on a different computer). – Leonidas Feb 09 '09 at 15:30
  • Leonidas: Thanks for the comments. I have updated the question. – Alceu Costa Feb 09 '09 at 15:47
  • Related: [Easiest way to simulate no free disk space situation?](http://stackoverflow.com/q/2043999) – tchrist Feb 15 '13 at 21:51

12 Answers12

13

You could try writing to a full floppy disk.

Edit:

In light of your edited question, you could set up a network share with no disk space quota and write to that. The error will then be produced regardless of the logged on user or machine.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • 8
    Or, from this century, a full USB disk. Repartition it, if needed. – gregmac Feb 09 '09 at 15:24
  • 1
    +1 for the USB-disk/stick/whatever. Who would spend hours and hours waiting for a floppy to react in 2009? – Arve Systad Feb 09 '09 at 15:28
  • 4
    Who even has floppy disks anymore? The intentions were good though, apologies for living in 1990! :) – Patrick McDonald Feb 09 '09 at 15:38
  • Floppies are outrageously expensive now that no one needs them anymore. –  Feb 09 '09 at 16:55
  • We ended up using the floppy disk solution because of the constraints of our testing enviroment. However, I think that there must be a more elegant solution for this problem. – Alceu Costa Jun 15 '09 at 02:30
  • The network share with no disk space quota sounds like a more elegant solution. Meanwhile, in this century who needs floppy drives to install Windows 2003 on a server with a SCSI RAID card whose driver isn't built into Windows. Us silly programmers, we should know better. – Windows programmer Jun 15 '09 at 02:54
13

For Windows XP or later:

This command can get the amount of free space for the c:\ drive:

for /f "usebackq tokens=1-5" %%A in (`dir c:\ ^| find "bytes free"`) do (
    set FREE_SPACE=%%C
)

Replace c:\ with your drive, as needed.

You can then take some space away from this value so you have a little room to work with:

set /a FREE_SPACE=FREE_SPACE-1024

or however much space you want to keep free.

You can use the fsutil command to create a file to fill up the free space on the disk:

fsutil file createnew c:\spacehog.dat %FREE_SPACE%

Run your test, writing to the drive. After you write 1024 bytes or so you should run out of space.

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • This is what I used, and it worked great. My XP install is a VM, so I did a snapshot before, fsutil, and executed my test. – Jared Oberhaus Jun 03 '09 at 19:22
  • This will only wok if the free space is less than 2 GiB, though. `cmd`'s arithmetic is restricted to 32-bit signed integers. I guess in the last decade it would be kinda hard to find that little free space for this to work. – Joey Apr 27 '11 at 15:56
10

Download and install TrueCrypt. You can then create a virtual partition of whatever size you want (a couple of megabytes), mount it and then fill it with a couple of documents.

Rad
  • 8,336
  • 4
  • 46
  • 45
8

Best Option: Microsoft's consume program

Reasons:

  • It tests the system disk (vs a separate drive)
  • It's fast - run the program to fill the disk instantly, stop when no longer needed
  • It's easy - No creating and deleting files. No extra test partition hanging around. Installation is required, but you can use a simple command afterward.
  • It's scriptable

Steps:

  1. Install the Windows Server 2003 Resource Kit Tools (Works fine on Windows 7)
  2. cd "%ProgramFiles(x86)%\Windows Resource Kits\Tools" (or whereever it's installed)
  3. consume.exe -disk-space

Command output:

C:\Program Files (x86)\Windows Resource Kits\Tools>consume.exe
Universal Resource Consumer - Just an innocent stress program, v 0.1.0
Copyright (c) 1998, 1999, Microsoft Corporation

    consume RESOURCE [-time SECONDS]

RESOURCE can be one of the following:

    -physical-memory
    -page-file
    -disk-space
    -cpu-time
    -kernel-pool


C:\Program Files (x86)\Windows Resource Kits\Tools>consume.exe -disk-space
Consume: Message: Total disk space:         96049 Mb
Consume: Message: Free disk space:          14705 Mb
Consume: Message: Free per user space:      14705 Mb
Consume: Message: Attempting to use:        14705 Mb
Consume: Message: Reattempting to use:      14705 Mb
Consume: Message: Sleeping ...

Other Options:

  • Windows 7 has a virtual hard drive feature. Basically do the following: Computer Management > Disk Management > Action Menu > Create VHD > Right click disk and Initialize > Right click
  • Generate large files (should be instant) until your disk is full using a shell command or Dummy File Generator program. Another prorgram: SpaceHog.
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
  • 1
    The virtual hard drive feature in Windows 7 did the trick for me. I just needed to test for the error to create the appropriate handler logic. I created a 10 MB VHD. WIndows bookkeeping takes up around 7 MB on the VHD, so when I try to write anything bigger than 3 MB file to the disk, it spits out the error. – Snapman Apr 13 '15 at 21:18
7

It might seem like a bit much, but one thing I can think of is to use a virtual machine, and set its virtual disk to just big enough to fit the OS on. Fill it with some garbage files to tip it over the edge, then run your program.

ryeguy
  • 65,519
  • 58
  • 198
  • 260
5

Create a secondary partition, fill it with junk and then run your program there.

Mizipzor
  • 51,151
  • 22
  • 97
  • 138
4

You could setup a small ramdisk and write to that. See this page for some free ramdisk products.

M4N
  • 94,805
  • 45
  • 217
  • 260
2

Best thing that works on every computer (as testing is not neceessarily done on a dedicated machine) would be a ramdrive/ramdisk that could be set up on the fly.

Only found a Virtual Disk SDK so far see here that maybe could be included in your buzild process.

Different idea: maybe your testing computers could be set up to write to a shared network folder (that is full) mount as a drive?

Leonidas
  • 2,440
  • 15
  • 22
2

Create a new user accout, set a quota for it, and use runas to run your app as that user. (not exactly the same as disk full, but should have similar consequences.)

2

The operating system will respond differently to it's system drive filling than to other drives filling and as such your application will do so too, surely? Simply filling a drive irrespective of what the physical media is used isn't going to be a accurate test.

Can't you mock the file system event for a full disk? Why would you want to wait until the disk is full? Wouldn't you want to monitor disk space periodically and warn the user when the disk is with a percentage margin of filling? Rather than wait until the disk space is terminal simply prevent your application from working until the issue is resolved, not doing so could effect any data IO and be unrecoverable!

If the test has to be a hard integration test then automating a virtual machine, deploying the application and then fill the remaining space with a recursive script is feasible.

Ed Blackburn
  • 551
  • 1
  • 4
  • 11
2

I have made a modification to the above script to make it compatiable with Windows 7... Essentially adding the switch "/-c" to the for statement. This removes the thousands seperator as fsutil does not like it in the statement.

for /f "usebackq tokens=1-5" %%A in (`dir /-c d:\ ^| find "bytes free"`) do (set FREE_SPACE=%%C)
fsutil file createnew d:\largefile.txt %FREE_SPACE%
Derek
  • 21
  • 1
1

use a very small iscsi target

johnny
  • 19,272
  • 52
  • 157
  • 259