def test_inventory_file_simple(mocker): open_url = OpenUrlProxy([ OpenUrlCall('GET', 200).result_json([ { 'server': { 'server_ip': '1.2.3.4', 'dc': 'foo', }, }, { 'server': { 'server_ip': '1.2.3.5', 'server_name': 'test-server', 'dc': 'foo', }, }, { 'server': { 'server_ip': '1.2.3.6', 'server_name': 'test-server-2', 'dc': 'bar', }, }, ]).expect_url('{0}/server'.format(BASE_URL)), ]) mocker.patch( 'ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', lambda x: True) mocker.patch('os.access', lambda x, y: True) inventory_filename = "test.robot.yaml" C.INVENTORY_ENABLED = ['community.hrobot.robot'] inventory_file = { inventory_filename: textwrap.dedent("""\ --- plugin: community.hrobot.robot hetzner_user: test hetzner_password: hunter2 filters: dc: foo """) } im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert im._inventory.hosts assert '1.2.3.4' in im._inventory.hosts assert 'test-server' in im._inventory.hosts assert 'test-server-2' not in im._inventory.hosts assert im._inventory.get_host( '1.2.3.4') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host( 'test-server') in im._inventory.groups['ungrouped'].hosts assert len(im._inventory.groups['ungrouped'].hosts) == 2 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_file_error(mocker): inventory_filename = "test.hosttech_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hosttech_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hosttech_dns_records hosttech_token: foo zone_id: 42 """)} open_url = OpenUrlProxy([ OpenUrlCall('GET', 500) .expect_header('accept', 'application/json') .expect_header('authorization', 'Bearer foo') .expect_url('https://api.ns1.hosttech.eu/api/user/v1/zones/42') .result_json({}), ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_wrong_file(mocker): open_url = OpenUrlProxy([]) mocker.patch( 'ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', lambda x: True) mocker.patch('os.access', lambda x, y: True) inventory_filename = "test.bobot.yml" C.INVENTORY_ENABLED = ['community.hrobot.robot'] inventory_file = { inventory_filename: textwrap.dedent("""\ --- plugin: community.hrobot.robot hetzner_user: test hetzner_password: hunter2 """) } im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert not im._inventory.hosts assert '1.2.3.4' not in im._inventory.hosts assert 'test-server' not in im._inventory.hosts assert 'test-server-2' not in im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_file_unauthorized(mocker): inventory_filename = "test.hetzner_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hetzner_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hetzner_dns_records hetzner_token: foo zone_id: '23' """)} open_url = OpenUrlProxy([ OpenUrlCall('GET', 403) .expect_header('accept', 'application/json') .expect_header('auth-api-token', 'foo') .expect_url('https://dns.hetzner.com/api/v1/zones/23') .result_json(dict(message="")), ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() # TODO: make sure that the correct error was reported assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_file_collision(mocker): inventory_filename = "test.hetzner_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hetzner_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hetzner_dns_records hetzner_token: '{{ "foo" }}' zone_name: '{{ "example." ~ "com" }}' filters: type: - A - AAAA """)} open_url = OpenUrlProxy([ OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('auth-api-token', 'foo') .expect_url('https://dns.hetzner.com/api/v1/zones', without_query=True) .expect_query_values('name', 'example.com') .return_header('Content-Type', 'application/json') .result_json(HETZNER_JSON_ZONE_LIST_RESULT), OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('auth-api-token', 'foo') .expect_url('https://dns.hetzner.com/api/v1/records', without_query=True) .expect_query_values('zone_id', '42') .expect_query_values('page', '1') .expect_query_values('per_page', '100') .return_header('Content-Type', 'application/json') .result_json(HETZNER_JSON_ZONE_RECORDS_GET_RESULT), ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert im._inventory.hosts assert 'example.com' in im._inventory.hosts assert '*.example.com' in im._inventory.hosts assert 'foo.example.com' in im._inventory.hosts assert 'bar.example.com' not in im._inventory.hosts assert im._inventory.get_host('example.com') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host('*.example.com') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host('foo.example.com') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host('example.com').get_vars()['ansible_host'] == '2001:1:2::3' assert im._inventory.get_host('*.example.com').get_vars()['ansible_host'] == '1.2.3.5' assert im._inventory.get_host('foo.example.com').get_vars()['ansible_host'] == '2001:1:2::4' assert len(im._inventory.groups['ungrouped'].hosts) == 3 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_no_file(mocker): open_url = OpenUrlProxy([]) mocker.patch( 'ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', lambda x: False) inventory_filename = "test.robot.yml" C.INVENTORY_ENABLED = ['community.hrobot.robot'] with pytest.raises(AnsibleError): im = InventoryManager(loader=DictDataLoader({}), sources='test.robot.yml') open_url.assert_is_done()
def test_inventory_no_file(mocker): inventory_filename = "test.hosttech_dns.yml" C.INVENTORY_ENABLED = ['community.dns.hosttech_dns_records'] open_url = OpenUrlProxy([]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename, False)) mocker.patch('os.access', access_mock(inventory_filename, False)) im = InventoryManager(loader=DictDataLoader({}), sources=inventory_filename) open_url.assert_is_done() assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_file_simple(mocker): inventory_filename = "test.hosttech_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hosttech_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hosttech_dns_records hosttech_token: foo zone_name: example.com filters: type: A """)} open_url = OpenUrlProxy([ OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('authorization', 'Bearer foo') .expect_url('https://api.ns1.hosttech.eu/api/user/v1/zones', without_query=True) .expect_query_values('query', 'example.com') .return_header('Content-Type', 'application/json') .result_json(HOSTTECH_JSON_ZONE_LIST_RESULT), OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('authorization', 'Bearer foo') .expect_url('https://api.ns1.hosttech.eu/api/user/v1/zones/42') .return_header('Content-Type', 'application/json') .result_json(HOSTTECH_JSON_ZONE_GET_RESULT), ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert im._inventory.hosts assert 'example.com' in im._inventory.hosts assert '*.example.com' in im._inventory.hosts assert 'foo.example.com' not in im._inventory.hosts assert 'bar.example.com' not in im._inventory.hosts assert im._inventory.get_host('example.com') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host('*.example.com') in im._inventory.groups['ungrouped'].hosts assert im._inventory.get_host('example.com').get_vars()['ansible_host'] == '1.2.3.4' assert im._inventory.get_host('*.example.com').get_vars()['ansible_host'] == '1.2.3.5' assert len(im._inventory.groups['ungrouped'].hosts) == 2 assert len(im._inventory.groups['all'].hosts) == 0
def test_basic(self): open_url = OpenUrlProxy([ OpenUrlCall( 'GET', 200).result_str('hello').expect_form_value_absent('foo'). expect_header_unset('foo').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')
def test_error_in_test(self): open_url = OpenUrlProxy([ OpenUrlCall('GET', 204).expect_url( 'http://example.com#asdf', without_query=True).expect_query_values('foo', 'bar', 'baz'), ]) with patch( 'ansible_collections.community.internal_test_tools.plugins.lookup.open_url_test_lookup.open_url', open_url): with pytest.raises(AnsibleLookupError) as e: self.lookup.run( [ 'http://example.com?foo=bar&foo=baz#asdf', 'http://example.org' ], [], ) open_url.assert_is_done() print(e.value.message) assert e.value.message == 'Error while GETing http://example.com?foo=bar&foo=baz#asdf: OpenUrlCall data has neither body nor error data'
def test_inventory_file_record_conversion_error(mocker): inventory_filename = "test.hetzner_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hetzner_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hetzner_dns_records hetzner_token: foo zone_id: "{{ '42' }}" """)} open_url = OpenUrlProxy([ OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('auth-api-token', 'foo') .expect_url('https://dns.hetzner.com/api/v1/zones/42') .return_header('Content-Type', 'application/json') .result_json(HETZNER_JSON_ZONE_GET_RESULT), OpenUrlCall('GET', 200) .expect_header('accept', 'application/json') .expect_header('auth-api-token', 'foo') .expect_url('https://dns.hetzner.com/api/v1/records', without_query=True) .expect_query_values('zone_id', '42') .expect_query_values('page', '1') .expect_query_values('per_page', '100') .return_header('Content-Type', 'application/json') .result_json(HETZNER_JSON_ZONE_RECORDS_GET_RESULT_2), ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() # TODO: make sure that the correct error was reported assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_multiple(self): open_url = OpenUrlProxy([ OpenUrlCall('POST', 200).result_json({ '1': 2 }).return_header( 'content-type', 'application/json').expect_content( 'name=foo&[email protected]'.encode('utf-8')). expect_content_predicate(lambda content: True).expect_header( 'foo', 'bar').expect_header_unset('baz'), OpenUrlCall('POST', 500).result_error('Error!'.encode( 'utf-8')).expect_form_present('name').expect_form_value( 'email', '*****@*****.**').expect_form_value_absent( 'firstname').expect_url('http://example.org'), OpenUrlCall( 'POST', 400).result_error().expect_url('http://example.example'), ]) 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', 'http://example.org', 'http://example.example' ], [], method='POST', headers=dict(foo='bar'), data=base64.b64encode('name=foo&[email protected]'.encode( 'utf-8')).decode('utf-8'), ) open_url.assert_is_done() assert len(result) == 3 assert result[0]['status'] == 200 assert result[1]['status'] == 500 assert result[2]['status'] == 400 assert result[2]['content'] == ''
def test_inventory_file_invalid_zone_id(mocker): inventory_filename = "test.hosttech_dns.yaml" C.INVENTORY_ENABLED = ['community.dns.hosttech_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hosttech_dns_records hosttech_token: foo zone_id: invalid """)} open_url = OpenUrlProxy([ ]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
def test_inventory_wrong_file(mocker): inventory_filename = "test.hetznerdns.yml" C.INVENTORY_ENABLED = ['community.dns.hetzner_dns_records'] inventory_file = {inventory_filename: textwrap.dedent("""\ --- plugin: community.dns.hetzner_dns_records hetzner_token: foo """)} open_url = OpenUrlProxy([]) mocker.patch('ansible_collections.community.dns.plugins.module_utils.http.open_url', open_url) mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) mocker.patch('os.path.exists', exists_mock(inventory_filename)) mocker.patch('os.access', access_mock(inventory_filename)) im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) open_url.assert_is_done() # TODO: make sure that the correct error was reported assert not im._inventory.hosts assert len(im._inventory.groups['ungrouped'].hosts) == 0 assert len(im._inventory.groups['all'].hosts) == 0
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) inventory.get_option = mocker.MagicMock(side_effect=get_option) inventory.populate(inventory.get_servers()) open_url.assert_is_done() host_1 = inventory.inventory.get_host('1.2.3.4') host_2 = inventory.inventory.get_host('test-server') host_1_vars = host_1.get_vars() host_2_vars = host_2.get_vars() assert host_1_vars['ansible_host'] == '1.2.3.4' assert host_1_vars['hrobot_server_ip'] == '1.2.3.4' assert 'hrobot_server_name' not in host_1_vars assert host_2_vars['ansible_host'] == '1.2.3.5' assert host_2_vars['hrobot_server_ip'] == '1.2.3.5' assert host_2_vars['hrobot_server_name'] == 'test-server'
def test_error(self): open_url = OpenUrlProxy([ OpenUrlCall( 'PUT', 404).exception(lambda: Exception('foo bar!')).expect_header( 'foo', 'bar').expect_header_unset('baz').expect_url( 'http://example.com', without_query=True, without_fragment=True).expect_query_values('foo', ''), ]) with patch( 'ansible_collections.community.internal_test_tools.plugins.lookup.open_url_test_lookup.open_url', open_url): with pytest.raises(AnsibleLookupError) as e: self.lookup.run( ['http://example.com?foo', 'http://example.org'], [], method='PUT', headers=dict(foo='bar'), ) open_url.assert_is_done() print(e.value.message) assert e.value.message == 'Error while PUTing http://example.com?foo: foo bar!'