How to simulate limited disk space condition via Xcode.

QA af!
5 min readFeb 5, 2021

--

How to simulate limited disk space condition via Xcode.

The Intro

We received reports from one of our medical clients, that their app was crashing to the device menu during the export of a bigger number of measurements. When we initially tried to analyze this behaviour in a simulator, we were not able to reproduce it. We then noticed that the pdf file created in the process could have the size of a couple hundred megabytes, as every measurement adds about 800 kb to the document. Meaning a series of one measurement per day for a full year results in a monstrous file of around 290 mb. Therefore, we assumed the export could cause either the app or the entire phone to crash due to filling up the hard drive. This behaviour sounds like a rare edge case, but being able to produce and export a large number of measurements is a vital functionality for medical software, as the effects of therapy can sometimes manifest over a very long period of time.

This led us to look for a method to test how the app would behave in that situation in a test environment and as usual, Stackoverflow was a helper in our times of need.

The possibility to export measurements as a pdf file was explicitly mentioned in the acceptance criteria. Aside from analyzing the issue at hand, we also forwarded the underlying problem of this feature to the client.

The steps presented are specific to the scenario we were trying to test. If you are trying this in a different scenario, the commands will stay the same, yet you can mount your disk image whereever you need it or give it any size, name or format you might require. You also do not have to download SimSim, if you know other ways of finding the directories for your test.

The Setup

Get the repo, open your project in XCode and run a successful debug build. You need your app to be running on a simulator of your choice.

The next step is somewhat tricky and anticipatory, but is probably the most important one in this scenario:
You need to figure out where the simulator is running and fetch the path. For this purpose I used SimSim. Download and start and you will see a small bat in the status bar. Click on it and it will show you an assortment of simulators. You have the option to navigate directly to the chosen simulator or to copy its path to the clipboard.

There is one problem with SimSim: You can not clear its history and the simulators given in the menu do not have any sort of identification. If you ran multiple simulators over a period of time, you have no opportunity to identify the “right” one. To work around this I used a file management program called Commander One. Navigate to the path of the simulators using SimSim, then sort by date to see which simulator was started recently et voila, you found the right one.

Now you need to open a terminal and do the actual simulation. Type the command
hdiutil create -size 2m -fs HFS+ /tmp/2meg.dmg and hit enter. This will create a 2 mb disk image named 2meg.dmg with the file system HFS+. You can of course change the size or name of the image according to your scenario.

Next you want to mount the disk image to the simulator you are running. This will cause the simulator to be limited to a disk size we defined. For this we will use hdiutil attach /tmp/2meg.dmg -mountpoint and the path to the current simulator. You can copy this path from SimSim or the file management system.
It should look like this: /Users/*USERNAME*/Library/Developer/CoreSimulator/Devices/*SIMULATOR ID/data/Containers/Data/Application/*SIMULATOR ID*/Library/Caches.

The full command in my scenario looks like this:

hdiutil attach /tmp/2meg.dmg -mountpoint /Users/peterwohlfarth/Library/Developer/CoreSimulator/Devices/E1E5FA60-FC8D-4594-BD1D-2CA6275B22B8/data/Containers/Data/Application/D1D719AA-92B2–40BF-ABED-ED6765A1680A/Library/Caches
This will mount the disk image to the path of the simulator.

The Simulation

Compared to the epic setup, the simulation itself is very simple to do. As mentioned above we were trying to find out if limited disk space has anything to do with the crash of the app. Due to NDA reasons I cannot show you the app in detail, but I will run the scenario and check the results in the log menu of XCode.

First, we export 250 measurements via pdf. This should be sufficient to fill up the limited disk space pretty quickly.

Then we check the results.

As we see, the app does not crash to the device menu, it “only” freezes and can continue to be used once the menu is exited through the x-button on the top right. The log menu tells us exactly what happened:
failed: No space left on device.

The Aftermath

So while this is an unhandled behaviour, it sadly is not the cause for the behaviour mentioned in the introduction. After we commenced further testing we found out that the crash was caused due to insufficient RAM, quite unspectacular by reading the stack trace.

But we are not finished yet, we still need to clean up the test suite, as we can experience very confusing simulator behaviour when we forget about the disk space limitation. For this we use the command hdiutil detach /Users/*USERNAME*/Library/Developer/CoreSimulator/Devices/*SIMULATOR ID*/data/Applications/*SIMULATOR ID*/Library/Caches to detach the disk image from the simulator.

Peter Wohlfarth is an Agile Tester at the Appsfactory-Gmbh
If you are interested in joining our team, please visit our website!

--

--