Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensuring a test case can delete the temporary directory it created

(Platform: Linux, specifically Fedora and Red Hat Enterprise Linux 6)

I have an integration test written in Python that does the following:

  • creates a temporary directory
  • tells a web service (running under apache) to run an rsync job that copies files into that directory
  • checks the files have been copied correctly (i.e. the configuration was correctly passed from the client through to an rsync invocation via the web service)
  • (tries to) delete the temporary directory

At the moment, the last step is failing because rsync is creating the files with their ownership set to that of the apache user, and so the test case doesn't have the necessary permissions to delete the files.

This Server Fault question provides a good explanation for why the cleanup step currently fails given the situation the integration test sets up.

What I currently do: I just don't delete the temporary directory in the test cleanup, so these integration tests leave dummy files around that need to be cleared out of /tmp manually.

The main solution I am currently considering is to add a setuid script specifically to handle the cleanup operation for the test suite. This should work, but I'm hoping someone else can suggest a more elegant solution. Specifically, I'd really like it if nothing in the integration test client needed to care about the uid of the apache process.

Approaches I have considered but rejected for various reasons:

  • Run the test case as root. This actually works, but needing to run the test suite as root is rather ugly.
  • Set the sticky bit on the directory created by the test suite. As near as I can tell, rsync is ignoring this because it's set to copy the flags from the remote server. However, even tweaking the settings to only copy the execute bit didn't seem to help, so I'm still not really sure why this didn't work.
  • Adding the test user to the apache group. As rsync is creating the files without group write permission, this didn't help.
  • Running up an Apache instance as the test user and testing against that. This has some advantages (in that the integration tests won't require that apache be already running), but has the downside that I won't be able to run the integration tests against an Apache instance that has been preconfigured with the production settings to make sure those are correct. So even though I'll likely add this capability to the test suite eventually, it won't be as a replacement for solving the current problem more directly.

One other thing I really don't want to do is change the settings passed to rsync just so the test suite can correctly clean up the temporary directory. This is an integration test for the service daemon, so I want to use a configuration as close to production as I can get.

like image 436
ncoghlan Avatar asked Dec 06 '25 09:12

ncoghlan


1 Answers

Add the test user to the apache group (or httpd group, whichever has group ownership on the files).

like image 65
stark Avatar answered Dec 08 '25 23:12

stark