Skip to content

Tests: Add a new user with home directory creation#1611

Open
aborah-sudo wants to merge 2 commits intoshadow-maint:masterfrom
aborah-sudo:useradd
Open

Tests: Add a new user with home directory creation#1611
aborah-sudo wants to merge 2 commits intoshadow-maint:masterfrom
aborah-sudo:useradd

Conversation

@aborah-sudo
Copy link
Copy Markdown
Contributor

This is the transformation to Python of the test located in tests/usertools/01/17_useradd_create_homedir.test which checks that useradd can add a new user with --create-home

Implement CREATE_HOME is set to no

Copy link
Copy Markdown
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Anuj,

First, let’s get those framework changes moved into their own separate commit! We need to keep the framework evolution isolated from the test logic to keep our git history clean and easy to audit.

On the technical side:

  • Simplify the logic by removing self.host.discard_file() for /etc/login.defs. Changes to that specific file don’t actually impact the test results, so there's no need for the extra overhead.
  • The current implementation relies on some hacks that won't scale well. We need a more robust approach that handles its own cleanup. Check out the SSSD implementation here for inspiration. I’m expecting a similar pattern where we can call something like shadow.login_defs("CREATE_HOME") = "no". This will keep the code clean.
  • Right now, your configuration changes are leaking into the next tests. Please ensure the implementation includes a proper teardown mechanism (like the SSSD framework does) so that every test starts with a fresh, predictable environment.

@aborah-sudo aborah-sudo force-pushed the useradd branch 5 times, most recently from 610adce to 70d60e1 Compare April 20, 2026 13:52
@aborah-sudo aborah-sudo requested a review from ikerexxe April 20, 2026 16:02
Copy link
Copy Markdown
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation creates LoginDefsConfig in a way that bypasses pytest-mh's automatic lifecycle management.

The LoginDefsConfig is stored as self._login_defs (with leading underscore) and created lazily via @property. This means:

  • pytest-mh's metaclass ignores it (leading underscore signals "private")
  • setup() is never called automatically → no backup is created
  • teardown() must be called manually → error-prone and inconsistent

Changes needed:

  1. Remove the @property and _login_defs attribute
  2. Create the utility in __init__ with no leading underscore
  3. Use .postpone_setup() for lazy initialization -> self.login_defs: LoginDefsConfig = LoginDefsConfig(self.host).postpone_setup()
  4. Remove the manual self._login_defs.teardown() call from Shadow.teardown()

This gives you the same clean API (shadow.login_defs["key"] = "value") but with automatic framework-managed lifecycle. Look at how other utilities like self.fs, self.firewall etc. are integrated in BaseLinuxRole.__init__.

The SSSD framework uses this same pattern - utilities are discovered and managed automatically by the framework, not manually by each role.

Comment thread tests/system/framework/hosts/shadow.py Outdated
@aborah-sudo aborah-sudo force-pushed the useradd branch 3 times, most recently from 727eee2 to bc835f7 Compare April 27, 2026 14:15
@aborah-sudo aborah-sudo requested a review from ikerexxe April 28, 2026 04:38
Copy link
Copy Markdown
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some comments inline

Comment thread tests/system/framework/hosts/shadow.py Outdated
Comment thread tests/system/framework/hosts/shadow.py Outdated
Comment thread tests/system/framework/hosts/shadow.py Outdated
Comment thread tests/system/framework/roles/shadow.py Outdated
Copy link
Copy Markdown
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are almost there. Apart from the inline comment you should also improve the commit message for the LoginDefsConfig implementation. What about:

tests: implement login.defs configuration utility                                                 
                                                                                                    
Introduce LoginDefsConfig class for /etc/login.defs manipulation.
It supports getting, setting, and removing configuration options
with automatic backup and restoration.

In addition, let's invert the commits as the test requires the framework changes to be there before.

Comment thread tests/system/tests/test_useradd.py Outdated
assert group_entry.name == "test1", "Incorrect group name"

home_dir = "/home/test1"
assert shadow.fs.exists(home_dir), f"Home directory {home_dir} should exist even with CREATE_HOME=no"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is a bit misleading as we are focusing on the creation with --create-home. What about using Home directory {home_dir} should exist when using --create-home flag or Expected home directory {home_dir} to be created by --create-home?

This is the transformation to Python of the test located in
`tests/usertools/01/17_useradd_create_homedir.test`
which checks that `useradd` can add a new user with --create-home
Introduce LoginDefsConfig class for /etc/login.defs manipulation.
It supports getting, setting, and removing configuration options
with automatic backup and restoration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants