Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Dec 9, 2020
1 parent fcfcaef commit 42d3803
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
3 changes: 2 additions & 1 deletion tests/unit/plugins/lookup/test_open_url_test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
OpenUrlProxy,
)

from ansible_collections.community.internal_test_tools.plugins.lookup import open_url_test_lookup
# This import is needed so patching below works
from ansible_collections.community.internal_test_tools.plugins.lookup import open_url_test_lookup # noqa

from ansible_collections.community.internal_test_tools.tests.unit.compat.unittest import TestCase
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import (
Expand Down
77 changes: 28 additions & 49 deletions tests/unit/utils/open_url_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,45 @@
An example test could look as follows::
import pytest
import base64
from mock import MagicMock
from ansible.inventory.data import InventoryData
from ansible.plugins.loader import lookup_loader
from ansible_collections.community.internal_test_tools.tests.unit.utils.open_url_framework import (
OpenUrlCall,
OpenUrlProxy,
)
from ansible_collections.community.hrobot.plugins.inventory.robot import InventoryModule
from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL
@pytest.fixture(scope="module")
def inventory():
r = InventoryModule()
r.inventory = InventoryData()
return r
def get_option(option):
if option == 'filters':
return {}
return False
def test_populate(inventory, mocker):
open_url = OpenUrlProxy([
OpenUrlCall('GET', 200)
.result_json([
{
'server': {
'server_ip': '1.2.3.4',
},
},
{
'server': {
'server_ip': '1.2.3.5',
'server_name': 'test-server',
},
},
])
.expect_url('{0}/server'.format(BASE_URL)),
])
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url)
# This import is needed so patching below works
from ansible_collections.community.internal_test_tools.plugins.lookup import open_url_test_lookup # noqa
inventory.get_option = mocker.MagicMock(side_effect=get_option)
inventory.populate(inventory.get_servers())
from ansible_collections.community.internal_test_tools.tests.unit.compat.unittest import TestCase
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch
open_url.assert_is_done()
host_1 = inventory.inventory.get_host('1.2.3.4')
host_2 = inventory.inventory.get_host('test-server')
class TestLookupModule(TestCase):
def setUp(self):
self.lookup = lookup_loader.get("community.internal_test_tools.open_url_test_lookup")
This test makes sure that if the ``community.hrobot.robot`` inventory plugin's
get_servers() and populate() methods are called, one ``open_url()`` call is made
to ``GET`` the URL, and if this call is returned with a ``200 OK`` and the
given JSON object, that two hosts are added to the inventory.
def test_basic(self):
open_url = OpenUrlProxy([
OpenUrlCall('GET', 200)
.result_str('hello')
.expect_url('http://example.com'),
])
with patch('ansible_collections.community.internal_test_tools.plugins.lookup.open_url_test_lookup.open_url', open_url):
result = self.lookup.run(
['http://example.com'],
[],
)
open_url.assert_is_done()
assert len(result) == 1
assert result[0]['status'] == 200
assert result[0]['content'] == base64.b64encode('hello'.encode('utf-8')).decode('utf-8')
This test makes sure that if the ``community.internal_test_tools.open_url_test_lookup`` lookup
makes the requested ``open_url()`` call.
"""

from __future__ import (absolute_import, division, print_function)
Expand Down

0 comments on commit 42d3803

Please sign in to comment.