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_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_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_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_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