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

Add unit tests for unsafe_proxy #47887

Merged
merged 4 commits into from Nov 1, 2018
Merged

Conversation

sivel
Copy link
Member

@sivel sivel commented Oct 31, 2018

SUMMARY

Add unit tests for unsafe_proxy

Name                                Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------------------------------
lib/ansible/utils/unsafe_proxy.py      38      0     20      0   100%
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

test/units/utils/test_unsafe_proxy.py

ANSIBLE VERSION
2.8
ADDITIONAL INFORMATION

@sivel sivel requested a review from mattclay October 31, 2018 17:39
@ansibot
Copy link
Contributor

ansibot commented Oct 31, 2018

Hi @sivel, thank you for submitting this pull-request!

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 31, 2018
Copy link
Member

@mattclay mattclay left a comment

Choose a reason for hiding this comment

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

I recommend changing all is not assertions to assert what is expected. I've used the new Suggested Changes feature to mark all of those, along with removing an unused import.

I also recommend changing all assertions of AnsibleUnsafe to the more specific AnsibleUnsafeText, except for the last one in test_Wrap_var_unsafe. I didn't call those out individually though.

from ansible.module_utils.six import PY3
from ansible.utils.unsafe_proxy import AnsibleUnsafe, AnsibleUnsafeText, UnsafeProxy, wrap_var

import pytest
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import pytest

assert isinstance(wrap_var('foo'), AnsibleUnsafe)
assert isinstance(wrap_var(u'foo'), AnsibleUnsafe)
if PY3:
assert not isinstance(wrap_var(b'foo'), AnsibleUnsafe)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert not isinstance(wrap_var(b'foo'), AnsibleUnsafe)
assert isinstance(wrap_var(b'foo'), type(b''))

Copy link
Member Author

Choose a reason for hiding this comment

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

I used the not isinstance syntax, so that if someone were to implement something like:

class AnsibleUnsafeBytes(binary_type, AnsibleUnsafe):
    pass

The tests would have to be knowingly updated, because they would fail.

The change you recommend, would silently pass, due to making an assumption about the data.

The current method would fail, as it causes a change in behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that makes sense. It would still be good to add the assertions while keeping the existing ones. In line with that, should most of the isinstance assertions which aren't negated check for AnsibleUnsafeText instead of just AnsibleUnsafe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Feedback should be addressed.



def test_wrap_var_dict():
assert not isinstance(wrap_var(dict(foo='bar')), AnsibleUnsafe)

This comment was marked as resolved.



def test_wrap_var_dict_None():
assert not isinstance(wrap_var(dict(foo=None))['foo'], AnsibleUnsafe)

This comment was marked as resolved.



def test_wrap_var_None():
assert not isinstance(wrap_var(None), AnsibleUnsafe)

This comment was marked as resolved.



def test_wrap_var_list():
assert not isinstance(wrap_var(['foo']), AnsibleUnsafe)

This comment was marked as resolved.



def test_wrap_var_set():
assert not isinstance(wrap_var(set(['foo'])), AnsibleUnsafe)

This comment was marked as resolved.


def test_wrap_var_set_None():
for item in wrap_var(set([None])):
assert not isinstance(item, AnsibleUnsafe)

This comment was marked as resolved.



def test_wrap_var_tuple():
assert not isinstance(wrap_var(('foo',)), AnsibleUnsafe)

This comment was marked as resolved.


def test_wrap_var_tuple():
assert not isinstance(wrap_var(('foo',)), AnsibleUnsafe)
assert not isinstance(wrap_var(('foo',))[0], AnsibleUnsafe)

This comment was marked as resolved.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Oct 31, 2018
Copy link
Member

@mattclay mattclay left a comment

Choose a reason for hiding this comment

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

Looks good, although I wonder if there should be a test to assert that AnsibleUnsafeText inherits AnsibleUnsafe?

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Oct 31, 2018
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Nov 1, 2018
@sivel sivel merged commit 042a0cf into ansible:devel Nov 1, 2018
Tomorrow9 pushed a commit to Tomorrow9/ansible that referenced this pull request Dec 4, 2018
* Add unit tests for unsafe_proxy

* Remove unused import

* Address comments

* Add inheritance test
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants