Hard-coded regression test

I need to extend python code that has a lot of hard-coded paths
In order not to damage everything, I want to create unit tests for the code before my modifications: it will run tests without regression with my new code (which will not have hard-coded paths)

But because of the hard coded system path, I will run my test inside the chroot tree (I do not want to pollute the system directory)
My problem is that I want to configure chroot only for the test, and this can be done with os.chroot only with root privileges (and I do not want to run test scripts with root privileges)

Actually, I just need a fake tree pointer, so when the code that open('/etc/resolv.conf)retrieves the fake resolv.conf file, not my system,

I obviously do not want to replace myself with a hard-coded path in the code, because this will not be a real regression test

Do you have any ideas how to achieve this?

thank

Please note that all access to the path is readable using custom accout

+3
source share
2 answers

In your test setup, you can override the built- openin in the module you are testing with your own version, which is read from your "fake" directory structure:

import mymod
import os.path

def chroot_open(filename,*args):
    filename = os.path.join("/home/you/fakeroot",filename)
    return open(filename,*args)

mymod.open = chroot_open
+5

, setuid root chroot; root. , , , - , (, ).

chroot , . ( open), ...

0

Source: https://habr.com/ru/post/1772637/


All Articles