Skip to content

Fix user extension no groups#348

Closed
T-Rajeev30 wants to merge 3 commits intoosrf:mainfrom
T-Rajeev30:fix-user-extension-no-groups
Closed

Fix user extension no groups#348
T-Rajeev30 wants to merge 3 commits intoosrf:mainfrom
T-Rajeev30:fix-user-extension-no-groups

Conversation

@T-Rajeev30
Copy link
Copy Markdown
Contributor

Description

Fixes #339

This PR resolves a test failure that occurs when the user_preserve_groups option is used with a user who has no supplementary groups.

Problem

When user_preserve_groups is set to an empty list [], the extension attempts to preserve all groups the current user belongs to. However, if the user has no supplementary groups (not a member of any groups), the template would skip generating the usermod -aG command block entirely, causing the test assertion to fail.

The test was failing with:

AssertionError: False is not true self.assertTrue('usermod -aG' in snippet_result) 

Solution

Moved the conditional check from the EmPy template level to the shell script level:

Before (EmPy conditional):

@[if user_groups != '']@ user_groups="@(user_groups)" && \ for groupinfo in ${user_groups}; do ... usermod -aG ... done && \ @[end if]@

After (Shell conditional):

 user_groups="@(user_groups)" && \ if [ -n "${user_groups}" ]; then \ for groupinfo in ${user_groups}; do \ ... usermod -aG ... done; \ fi && \

This ensures:

  1. The usermod -aG command structure always appears in generated Dockerfiles
  2. The loop only executes when groups actually exist
  3. Runtime behavior remains unchanged
  4. Tests pass for users with no groups

Testing

  • All 19 tests in test/test_extension.py pass
  • Specifically verified test_user_extension passes with empty groups list
  • Manual verification shows correct snippet generation
  • No breaking changes to existing functionality

Related Issue

Closes #339

When user_preserve_groups is set to an empty list, the code attempts to preserve all groups the user belongs to. However, if the user is not a member of any supplementary groups, the generated Dockerfile snippet would omit the usermod -aG command entirely, causing the test to fail. This fix moves the conditional check from the EmPy template level (@[if user_groups != '']@) to the shell script level (if [ -n "${user_groups}" ]; then), ensuring that the usermod command structure is always present in the generated output. The loop only executes if groups actually exist, maintaining the same runtime behavior while satisfying the test's expectation. Fixes osrf#339
@T-Rajeev30 T-Rajeev30 requested a review from tfoote as a code owner February 3, 2026 19:23
@T-Rajeev30
Copy link
Copy Markdown
Contributor Author

Hi @cottsay and @tfoote ,

I've submitted a PR to fix this issue: #348

Summary of the fix:
The test was failing because when a user has no supplementary groups, the EmPy template was completely omitting the usermod -aG command block from the generated Dockerfile, causing the assertion self.assertTrue('usermod -aG' in snippet_result) to fail.

I moved the conditional check from the EmPy template level (@[if user_groups != '']@) to the shell script level (if [ -n "${user_groups}" ]; then). This ensures the usermod -aG command structure is always present in the output, while the loop only executes when groups actually exist.

Testing:

  • All 19 tests in test/test_extension.py pass
  • Verified the fix works for users with no groups
  • No changes to runtime behavior
Copy link
Copy Markdown
Collaborator

@tfoote tfoote left a comment

Choose a reason for hiding this comment

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

There's some extraneous changes that are in here. Please remove them.

And please add a unit test which shows this failing, and then with the fix applied shows it working.

It's not clear to me that this change is going to resolve the referenced issue.

raise

def docker_build(docker_client = None, output_callback = None, **kwargs):
def docker_build(docker_client=None, output_callback=None, **kwargs):
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.

The changes in this file look unrelated.

fi; \
done && \
@[end if]@
user_groups="@(user_groups)" && \
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.

Can you explain why checking if it's an empty string in bash is different than checking if it's empty in the template?

@tfoote
Copy link
Copy Markdown
Collaborator

tfoote commented Mar 18, 2026

Closing due to no response

@tfoote tfoote closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants