Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to run code as non-root #474

Closed
FelixSchwarz opened this issue Apr 9, 2019 · 3 comments
Closed

ability to run code as non-root #474

FelixSchwarz opened this issue Apr 9, 2019 · 3 comments

Comments

@FelixSchwarz
Copy link

Some CI services (e.g. shippable) run code under the "root" user. Since commit c16a508 (PR #455, pyfakefs 3.5.4) pyfakefs uses the value from os.getuid() to emulate root permissions.

Some of my tests broke with that version as I tried to test for non-accessible files (using os.chmod(..., 0). Of course root can access all files.

Describe the solution you'd like
Ideally I'd like to tell pyfakefs to emulate a non-root user even though the Unix user might be "root". The setting should only be active for a given test (maybe bound to the "Patcher()" instance?).

I use the "Patcher" manually anyway so adding a parameter to the constructor or the .setUp() call (e.g. root_user=False) would be pretty easy.

    patcher = Patcher()
    patcher.setUp()
    self.addCleanup(patcher.tearDown)

Describe alternatives you've considered
I could do the same as some pyfakefs tests (e.g. FakeOsModuleTest) which means calling set_uid(1) manually. However that means additional boilerplate code (remembering the old value, using tearDown() to restore it, dealing with Windows).

class FakeOsModuleTest(FakeOsModuleTestBase):
    def setUp(self):
        super(FakeOsModuleTest, self).setUp()
        # ...
        set_uid(1)
@mrbean-bremen
Copy link
Member

This makes sense. I'm a bit reluctant to add another argument to Patcher, but this is probably the best way. I will sleep on the matter, and if nothing else comes to mind, will add this.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Apr 10, 2019
- added argument allow_root_user to Patcher and UnitTest
- see pytest-dev#474
@mrbean-bremen
Copy link
Member

Ok, I added something - please check if that is what you need!

@FelixSchwarz
Copy link
Author

Yes, that code works fine :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants