-
Notifications
You must be signed in to change notification settings - Fork 115
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
Fix omitting credentials when CLI has already been instantiated #14449
Conversation
trigger: test-robottelo |
PRT Result
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-reviewed with @ogajduse
robottelo/hosts.py
Outdated
getattr(self._cli, name).omitting_credentials = True | ||
except AttributeError: | ||
# not everything has an mro method, we don't care about them | ||
pass | ||
yield | ||
self.omitting_credentials = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lhellebr should we check self._cli._configured
is configured in the teardown phase too? what happens if we don't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if someone does
with parametrized_enrolled_sat.omit_credentials():
pass
Then CLI won't get instantiated at all (because parametrized_enrolled_sat.cli
) hasn't been called. I haven't tested that though. In any case, I employed this just as a defensive programming technique to avoid error in cases we may not even think about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
co-reviewed with @jameerpathan111
robottelo/hosts.py
Outdated
for file in Path('robottelo/cli/').iterdir(): | ||
if file.suffix == '.py' and not file.name.startswith('_'): | ||
cli_module = importlib.import_module(f'robottelo.cli.{file.stem}') | ||
for name, obj in cli_module.__dict__.items(): | ||
try: | ||
if Base in obj.mro(): | ||
getattr(self._cli, name).omitting_credentials = True | ||
except AttributeError: | ||
# not everything has an mro method, we don't care about them | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, @jameerpathan111 and I had some concerns about code repetition. But looking at it further, we concluded that it could be possibly remade into the following:
for file in Path('robottelo/cli/').iterdir(): | |
if file.suffix == '.py' and not file.name.startswith('_'): | |
cli_module = importlib.import_module(f'robottelo.cli.{file.stem}') | |
for name, obj in cli_module.__dict__.items(): | |
try: | |
if Base in obj.mro(): | |
getattr(self._cli, name).omitting_credentials = True | |
except AttributeError: | |
# not everything has an mro method, we don't care about them | |
pass | |
for name, obj in self._cli.__dict__.items(): | |
with contextlib.suppress(AttributeError): # not everything has an mro method, we don't care about them | |
if Base in obj.mro(): | |
getattr(self._cli, name).omitting_credentials = True |
With this change, it does not seem that much repetitive. 😃 We are not sure if it would work, though. But if the omit_credentials
property assumes self._cli._configured
, then we can rely on the classes being already populated there instead of going over robottelo/cli/
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cycling through robottelo/cli
ensures we won't run into some attribute of cli
that is for some reason descendant of Base
- but it's not an entity we should instantiate cli
for. I don't know if any such exists and more so, I don't know if any will exist in the future. I understand your point about repetition. Please consider what I just wrote and if you don't change your opinion, I can test your version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best person to address the
I don't know if any such exists and more so, I don't know if any will exist in the future.
part would be @JacobCallahan since he created the initial implementation of cli
.
I do not see how your comment relates to the changes I suggested. The implementation of cli
property ensures (through calling mro()
) that only descendants of Base
will be attributes of cli
. If cli
contains something that should not have been assigned as its attribute, then implementation of cli
is the one to blame.
robottelo/hosts.py
Outdated
getattr(self._cli, name).omitting_credentials = True | ||
except AttributeError: | ||
# not everything has an mro method, we don't care about them | ||
pass | ||
yield | ||
self.omitting_credentials = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Sat object attribute is not necessarily False
every time. One can initialize Satellite with True
(by explicitly setting it in kwargs
). In such scenario, this wouldn't reset the attribute to the correct original version.
Perhaps this contextmanager function could check if the ommiting_credentials
is set to False
first, otherwise, it has no reason to run and could be skipped straight away.
trigger: test-robottelo |
PRT Result
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can likely simplify the reversion by adding all modified objects into a list called modified
, then iterate over that list post-yield to update omitting_credentials
again.
robottelo/hosts.py
Outdated
AttributeError | ||
): # not everything has an mro method, we don't care about them | ||
if Base in obj.mro(): | ||
getattr(self._cli, name).omitting_credentials = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be True or False, if it's a reversion of the previous action?
trigger: test-robottelo |
PRT Result
|
trigger: test-robottelo |
PRT Result
|
@lhellebr if you need help with my recommended implementation, let me know. |
Let's just merge. This PR is over a month old. |
@lhellebr I don't see any priority labels on this PR, so would still like you to consider my suggestion. Here it is in code form: def omit_credentials(self):
change = not self.omitting_credentials # if not already set to omit
changed = []
if change:
self.omitting_credentials = True
# if CLI is already created
if self._cli._configured:
for name, obj in self._cli.__dict__.items():
with contextlib.suppress(
AttributeError
): # not everything has an mro method, we don't care about them
if Base in obj.mro():
attr = getattr(self._cli, name)
attr.omitting_credentials = True
changed.append(attr)
yield
if change:
self.omitting_credentials = False
for attr in changed:
attr.omitting_credentials = False |
* Fix omitting credentials when CLI has already been instantiated * Rework based on comments * Set to False after yield (cherry picked from commit 845ef03)
…ted (#14880) Fix omitting credentials when CLI has already been instantiated (#14449) * Fix omitting credentials when CLI has already been instantiated * Rework based on comments * Set to False after yield (cherry picked from commit 845ef03) Co-authored-by: Lukáš Hellebrandt <[email protected]>
…lliteQE#14449) * Fix omitting credentials when CLI has already been instantiated * Rework based on comments * Set to False after yield
Problem Statement
Omitting credentials doesn't work when CLI has already been instantiated
Solution
If it's been instantiated, set its omitting attribute to
True
for all the classes