[apparmor] [patch] Add tempdir and tempfile handling to AATest

Steve Beattie steve at nxnw.org
Thu May 28 23:43:24 UTC 2015


On Sun, May 17, 2015 at 10:33:47PM +0200, Christian Boltz wrote:
> this patch adds writeTmpfile() to AATest to write a file into the tmpdir.
> If no tmpdir exists yet, automatically create one.
> 
> createTmpdir() is a separate function so that it's possible to manually
> create the tmpdir (for example, if a test needs an empty tmpdir).
> 
> Also add a tearDown() function to delete the tmpdir again. This function
> calls self.AATeardown() to avoid the need for super() in child classes.
> 
> Finally, simplify AaTestWithTempdir in test-aa.py to use createTmpdir()
> and add an example for AATeardown() to test-example.py.
> 
> 
> [ 10-tests-tempdir.diff ]
> 
> === modified file utils/test/common_test.py
> --- utils/test/common_test.py   2015-05-17 22:22:19.266683216 +0200
> +++ utils/test/common_test.py   2015-05-17 22:24:41.240395607 +0200
> @@ -16,7 +16,9 @@
>  import inspect
>  import os
>  import re
> +import shutil
>  import sys
> +import tempfile
>  
>  import apparmor.common
>  import apparmor.config
> @@ -47,7 +49,27 @@
>          '''override this function if a test needs additional setup steps (instead of overriding setUp())'''
>          pass
>  
> +    def tearDown(self):
> +        if self.tmpdir:
> +            if os.path.exists(self.tmpdir):
> +                shutil.rmtree(self.tmpdir)

No need to nest this deep, the following will work.

        if self.tmpdir and os.path.exists(self.tmpdir):
            shutil.rmtree(self.tmpdir)

With that change, Acked-by: Steve Beattie <steve at nxnw.org>.

A feature to add would be a .debug field defaulting to False, and which
if True doesn't delete the directory tree, leaving it behind for after
the fact inspection.

(There's a way to do it such that cleanup like that is automatically
blocked on test case failure, but the way I found to do it requires
metaclasses for which the syntax differs greatly between python 2
and python 3. Therefore, a feature for the future.)

-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150528/13393199/attachment-0001.pgp>


More information about the AppArmor mailing list