Skip to content

Conversation

pondrejk
Copy link
Contributor

Problem Statement

to cover theforeman/foreman#10719

Solution

Related Issues

@pondrejk pondrejk self-assigned this Oct 14, 2025
@pondrejk pondrejk requested a review from a team as a code owner October 14, 2025 10:02
@pondrejk pondrejk added the No-CherryPick PR doesnt need CherryPick to previous branches label Oct 14, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Consider capturing and restoring the original host_owner setting in teardown to ensure test isolation and avoid side effects on other tests.
  • Rename test_positive_host_owner to something more descriptive (e.g., test_host_owner_setting_cleared_on_user_deletion) to clearly convey the behavior under test.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider capturing and restoring the original host_owner setting in teardown to ensure test isolation and avoid side effects on other tests.
- Rename test_positive_host_owner to something more descriptive (e.g., test_host_owner_setting_cleared_on_user_deletion) to clearly convey the behavior under test.

## Individual Comments

### Comment 1
<location> `tests/foreman/cli/test_user.py:585-588` </location>
<code_context>
+        :expectedresults: host_owner is cleared when the assigned user is deleted
+
+        """
+        user = target_sat.cli_factory.user()
+        target_sat.cli.Settings.set({'name': "host_owner", 'value': user['login']})
+        host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
+        assert host_owner['value'] == user['login']
+        target_sat.cli.User.delete({'id': user['id']})
+        host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
</code_context>

<issue_to_address>
**suggestion (testing):** Add cleanup for created user and settings to avoid test pollution.

Please add teardown steps or use fixtures to remove the created user and reset the host_owner setting after the test completes to prevent residual state from affecting other tests.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 585 to 588
user = target_sat.cli_factory.user()
target_sat.cli.Settings.set({'name': "host_owner", 'value': user['login']})
host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
assert host_owner['value'] == user['login']
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add cleanup for created user and settings to avoid test pollution.

Please add teardown steps or use fixtures to remove the created user and reset the host_owner setting after the test completes to prevent residual state from affecting other tests.

"""
user = target_sat.cli_factory.user()
target_sat.cli.Settings.set({'name': "host_owner", 'value': user['login']})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to set the type, whether it's Users or Usergroups. Something like:

Suggested change
target_sat.cli.Settings.set({'name': "host_owner", 'value': user['login']})
target_sat.cli.Settings.set({'name': "host_owner", 'value': f'{user['login']}-Users'})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

seems it must be user['id']

@pondrejk
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/cli/test_user.py -k test_positive_host_owner
theforeman:
    foreman: 10719

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 13162
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/cli/test_user.py -k test_positive_host_owner --external-logging
Test Result : ========== 1 passed, 25 deselected, 5 warnings in 1219.59s (0:20:19) ===========

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Oct 14, 2025
@Satellite-QE Satellite-QE removed the PRT-Passed Indicates that latest PRT run is passed for the PR label Oct 15, 2025
Copy link
Member

@chris1984 chris1984 left a comment

Choose a reason for hiding this comment

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

Looks fine based on the PR, left a suggestion but i'm not an expert with Python.

host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
assert host_owner['value'] == f'{user["id"]}-Users'
target_sat.cli.User.delete({'id': user['id']})
host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
Copy link
Member

Choose a reason for hiding this comment

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

What do you think of:

host_owner = target_sat.cli.Settings.list({'search': 'name=host_owner'})[0]
assert host_owner['value'] == '', f'Host owner is not cleared: {host_owner["value"]}'

Copy link
Contributor Author

@pondrejk pondrejk Oct 20, 2025

Choose a reason for hiding this comment

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

this is perfectly fine, but I'd like to clear the value if the assertion fails, hence the try except block

try:
assert host_owner['value'] == ''
except AssertionError:
target_sat.cli.Settings.set({'name': "host_owner", 'value': ''})
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be under a finally so it's cleared regardless of whether an exception is raised or not?

Copy link
Contributor Author

@pondrejk pondrejk Oct 20, 2025

Choose a reason for hiding this comment

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

hm, should be fine if it's not raised, though if there's some other exception kind finally is indeed better

:expectedresults: host_owner is cleared when the assigned user is deleted
"""
user = target_sat.cli_factory.user()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is using the CLI, but wouldn't using the plain API be easier and faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would be faster to execute, not sure if easier as the factory creates names for us. There are no strict rules for this, but since we are in the cli module for user, I opt to create user through cli :)

@adamruzicka
Copy link
Contributor

trigger: test-robottelo
pytest: tests/foreman/cli/test_user.py -k test_positive_host_owner
theforeman:
  foreman: 10719

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 13216
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/cli/test_user.py -k test_positive_host_owner --external-logging
Test Result : ========== 1 passed, 25 deselected, 5 warnings in 1151.44s (0:19:11) ===========

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Oct 16, 2025
@Satellite-QE Satellite-QE removed the PRT-Passed Indicates that latest PRT run is passed for the PR label Oct 20, 2025
@pondrejk
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/cli/test_user.py -k test_positive_host_owner

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 13233
Build Status: UNSTABLE
PRT Comment: pytest tests/foreman/cli/test_user.py -k test_positive_host_owner --external-logging
Test Result : =========== 1 failed, 25 deselected, 5 warnings in 790.30s (0:13:10) ===========

@Satellite-QE Satellite-QE added the PRT-Failed Indicates that latest PRT run is failed for the PR label Oct 20, 2025
@pondrejk
Copy link
Contributor Author

ah, prt won't pass until snap is out

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

Labels

No-CherryPick PR doesnt need CherryPick to previous branches PRT-Failed Indicates that latest PRT run is failed for the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants