3

I'm trying to write a unittest to test the behaviour of a function when the disk is full. I need file access functions to behave normally while most of the test runs, so that the file I'm creating is actually created, then at one point I need the disk to be 'full'. I can't find a way to do this using mock_open(), since the file object created by this doesn't seem to be persist between function calls. I've tried to use pyfakefs and setting the disk size using self.fs.set_disk_usage(MAX_FS_SIZE) but when I try to run this in my tests, it allows used_size to go negative, meaning there is always free space (though oddly, their example code works correctly).

Is there a way to either simulate a disk-out-space error at a particular point in my code? Mocking the write function to have a side-effect would be my immediate thought, but I can't access the file object that I'm writing to in my test code, as it's buried deep inside function calls.

Edit: looks like I've found a bug in pyfakefs

Edit2: bug in pyfakefs has been fixed; now works as expected. Still interested to know if there's a way to get f.write() to throw an OSError with a simple mock.

askvictor
  • 3,621
  • 4
  • 32
  • 45
  • 2
    Maybe add the tests you've got + the example code? – 0x263A Jan 28 '22 at 00:26
  • 1
    Maybe you can use this question to get ideas https://stackoverflow.com/questions/16044204/testing-out-of-disk-space-in-linux – Gonzalo Odiard Jan 28 '22 at 02:54
  • @GonzaloOdiard thanks; I had something like this set up initially, but it doesn't play well with our continuous integration server, so I'm looking for something at the python level, not the OS level. – askvictor Jan 28 '22 at 03:53
  • @askvictor Check 0x263A's comment. – aaron Jan 30 '22 at 08:55

0 Answers0