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

Meraki - Initial unit tests #55251

Merged
merged 9 commits into from
Aug 14, 2019
Merged

Conversation

kbreit
Copy link
Contributor

@kbreit kbreit commented Apr 13, 2019

SUMMARY

This PR adds a few unit tests for Meraki module utility. It is bare bones but will be used on current and future PRs.

ISSUE TYPE
  • Test Pull Request
COMPONENT NAME

lib/ansible/module_utils/network/meraki/meraki.py

ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 feature This issue/PR relates to a feature request. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. 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 Apr 13, 2019
@jillr jillr removed the needs_triage Needs a first human triage before being processed. label Apr 18, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 26, 2019
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. core_review In order to be merged, this PR must follow the core review workflow. labels May 19, 2019
@ansibot
Copy link
Contributor

ansibot commented May 22, 2019

The test ansible-test sanity --test pylint [explain] failed with 2 errors:

test/units/module_utils/network/meraki/test_meraki.py:68:20: undefined-variable Undefined variable 'Mock'
test/units/module_utils/network/meraki/test_meraki.py:69:7: undefined-variable Undefined variable 'args'

click here for bot help

@mattclay mattclay added the ci_verified Changes made in this PR are causing tests to fail. label May 23, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 31, 2019


def test_fetch_url_404(module, mocker):
mocker.patch('ansible.module_utils.urls.fetch_url')
Copy link
Member

Choose a reason for hiding this comment

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

The patch needs to be applied where name is used, not where it is defined:

Suggested change
mocker.patch('ansible.module_utils.urls.fetch_url')
mocker.patch('ansible.module_utils.network.meraki.meraki.fetch_url')

See https://docs.python.org/3/library/unittest.mock.html#where-to-patch for details.

Copy link
Member

Choose a reason for hiding this comment

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

To apply the side effect, use this:

Suggested change
mocker.patch('ansible.module_utils.urls.fetch_url')
mocker.patch('ansible.module_utils.network.meraki.meraki.fetch_url', side_effect=mocked_fetch_url)

You'll also need to update mocked_fetch_url to accept args, like:

def mocked_fetch_url(*args, **kwargs):



def mocked_fetch_url():
response_mock = Mock()
Copy link
Member

Choose a reason for hiding this comment

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

Remove this since it doesn't do anything and Mock isn't defined.

info = {'status': 404,
'body': "404 - Page is missing",
}
return (None, info)
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to define info for both code paths.


def mocked_fetch_url():
response_mock = Mock()
if args[0] == 'https://api.meraki.com/404':
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't match because your test uses http:// instead of https://.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, and args[0] is the module, so you want to look at args[1] for the url.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the url I'm seeing from the test is: https://api.meraki.com/api/v0/http://api.meraki.com/404

@ansibot ansibot removed ci_verified Changes made in this PR are causing tests to fail. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Aug 6, 2019
@ansibot
Copy link
Contributor

ansibot commented Aug 6, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:128:0: missing-final-newline Final newline missing

The test ansible-test sanity --test future-import-boilerplate [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:0:0: missing: from __future__ import (absolute_import, division, print_function)

The test ansible-test sanity --test metaclass-boilerplate [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:0:0: missing: __metaclass__ = type

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:128:26: W292 no newline at end of file

click here for bot help

1 similar comment
@ansibot
Copy link
Contributor

ansibot commented Aug 6, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:128:0: missing-final-newline Final newline missing

The test ansible-test sanity --test future-import-boilerplate [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:0:0: missing: from __future__ import (absolute_import, division, print_function)

The test ansible-test sanity --test metaclass-boilerplate [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:0:0: missing: __metaclass__ = type

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:128:26: W292 no newline at end of file

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Aug 7, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:24:0: misplaced-future __future__ import is not the first non docstring statement

click here for bot help

1 similar comment
@ansibot
Copy link
Contributor

ansibot commented Aug 7, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

test/units/module_utils/network/meraki/test_meraki.py:24:0: misplaced-future __future__ import is not the first non docstring statement

click here for bot help

@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 Aug 9, 2019
@Qalthos Qalthos added test This PR relates to tests. and removed feature This issue/PR relates to a feature request. labels Aug 14, 2019
@Qalthos Qalthos merged commit f52657f into ansible:devel Aug 14, 2019
@kbreit kbreit deleted the meraki/initial_unit_tests branch August 16, 2019 17:35
@ansible ansible locked and limited conversation to collaborators Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 core_review In order to be merged, this PR must follow the core review workflow. has_issue support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants