| 1 |
# Copyright (c), Felix Fontein <[email protected]>, 2020 |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
import json |
| 10 |
import os |
| 11 |
import textwrap |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from ansible import constants as C |
| 16 |
from ansible.inventory.data import InventoryData |
| 17 |
from ansible.inventory.manager import InventoryManager |
| 18 |
from ansible.module_utils.common.text.converters import to_native |
| 19 |
from ansible.template import Templar |
| 20 |
|
| 21 |
from ansible_collections.community.internal_test_tools.tests.unit.mock.path import mock_unfrackpath_noop |
| 22 |
from ansible_collections.community.internal_test_tools.tests.unit.mock.loader import DictDataLoader |
| 23 |
from ansible_collections.community.internal_test_tools.tests.unit.utils.open_url_framework import ( |
| 24 |
OpenUrlCall, |
| 25 |
OpenUrlProxy, |
| 26 |
) |
| 27 |
from ansible_collections.community.internal_test_tools.tests.unit.utils.trust import ( |
| 28 |
SUPPORTS_DATA_TAGGING, |
| 29 |
is_trusted, |
| 30 |
) |
| 31 |
|
| 32 |
from ansible_collections.community.hrobot.plugins.inventory.robot import InventoryModule |
| 33 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL |
| 34 |
|
| 35 |
|
| 36 |
# The hashes involved are computed from the prefix and the plugin's name. |
| 37 |
# This no longer works with Data Tagging |
| 38 |
CACHE_PREFIX = 'prefix' |
| 39 |
CACHE_KEY = 'prefixcommunity.hrobot.robot_4e69bs_95733' |
| 40 |
|
| 41 |
|
| 42 |
original_exists = os.path.exists |
| 43 |
original_access = os.access |
| 44 |
|
| 45 |
|
| 46 |
def exists_mock(path, exists=True): |
| 47 |
def exists(f): |
| 48 |
if to_native(f) == path: |
| 49 |
return exists |
| 50 |
return original_exists(f) |
| 51 |
|
| 52 |
return exists |
| 53 |
|
| 54 |
|
| 55 |
def access_mock(path, can_access=True): |
| 56 |
def access(f, m, *args, **kwargs): |
| 57 |
if to_native(f) == path: |
| 58 |
return can_access |
| 59 |
return original_access(f, m, *args, **kwargs) # pragma: no cover |
| 60 |
|
| 61 |
return access |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture(scope="module") |
| 65 |
def inventory(): |
| 66 |
r = InventoryModule() |
| 67 |
r.inventory = InventoryData() |
| 68 |
r.templar = Templar(loader=DictDataLoader({})) |
| 69 |
return r |
| 70 |
|
| 71 |
|
| 72 |
def get_option(option): |
| 73 |
if option == 'simple_filters': |
| 74 |
return {} |
| 75 |
if option == 'filters': |
| 76 |
return [] |
| 77 |
if option == 'hetzner_user': |
| 78 |
return 'test' |
| 79 |
if option == 'hetzner_password': |
| 80 |
return 'hunter2' |
| 81 |
return False |
| 82 |
|
| 83 |
|
| 84 |
def test_populate(inventory, mocker): |
| 85 |
open_url = OpenUrlProxy([ |
| 86 |
OpenUrlCall('GET', 200) |
| 87 |
.result_json([ |
| 88 |
{ |
| 89 |
'server': { |
| 90 |
'server_ip': '1.2.3.4', |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
'server': { |
| 95 |
'server_ip': '1.2.3.5', |
| 96 |
'server_name': 'test-server', |
| 97 |
}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
'server': { |
| 101 |
'server_number': 5, |
| 102 |
}, |
| 103 |
}, |
| 104 |
]) |
| 105 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 106 |
]) |
| 107 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 108 |
|
| 109 |
inventory.get_option = mocker.MagicMock(side_effect=get_option) |
| 110 |
inventory.populate(inventory.get_servers()) |
| 111 |
|
| 112 |
open_url.assert_is_done() |
| 113 |
|
| 114 |
host_1 = inventory.inventory.get_host('1.2.3.4') |
| 115 |
host_2 = inventory.inventory.get_host('test-server') |
| 116 |
host_3 = inventory.inventory.get_host('5') |
| 117 |
|
| 118 |
host_1_vars = host_1.get_vars() |
| 119 |
host_2_vars = host_2.get_vars() |
| 120 |
host_3_vars = host_3.get_vars() |
| 121 |
|
| 122 |
assert host_1_vars['ansible_host'] == '1.2.3.4' |
| 123 |
assert host_1_vars['hrobot_server_ip'] == '1.2.3.4' |
| 124 |
assert 'hrobot_server_name' not in host_1_vars |
| 125 |
assert host_2_vars['ansible_host'] == '1.2.3.5' |
| 126 |
assert host_2_vars['hrobot_server_ip'] == '1.2.3.5' |
| 127 |
assert host_2_vars['hrobot_server_name'] == 'test-server' |
| 128 |
assert 'ansible_host' not in host_3_vars |
| 129 |
assert 'hrobot_server_ip' not in host_3_vars |
| 130 |
assert 'hrobot_server_name' not in host_3_vars |
| 131 |
|
| 132 |
|
| 133 |
def test_inventory_file_simple(mocker): |
| 134 |
open_url = OpenUrlProxy([ |
| 135 |
OpenUrlCall('GET', 200) |
| 136 |
.result_json([ |
| 137 |
{ |
| 138 |
'server': { |
| 139 |
'server_ip': '1.2.3.4', |
| 140 |
'dc': 'foo', |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
'server': { |
| 145 |
'server_ip': '1.2.3.5', |
| 146 |
'server_name': 'test-server', |
| 147 |
'dc': 'foo', |
| 148 |
}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
'server': { |
| 152 |
'server_ip': '1.2.3.6', |
| 153 |
'server_name': 'test-server-2', |
| 154 |
'dc': 'bar', |
| 155 |
}, |
| 156 |
}, |
| 157 |
]) |
| 158 |
.expect_basic_auth('test', 'hunter2') |
| 159 |
.expect_force_basic_auth(True) |
| 160 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 161 |
]) |
| 162 |
inventory_filename = "test.robot.yaml" |
| 163 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 164 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 165 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 166 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 167 |
|
| 168 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 169 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 170 |
--- |
| 171 |
plugin: community.hrobot.robot |
| 172 |
hetzner_user: test |
| 173 |
hetzner_password: hunter2 |
| 174 |
simple_filters: |
| 175 |
dc: foo |
| 176 |
""")} |
| 177 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 178 |
open_url.assert_is_done() |
| 179 |
|
| 180 |
assert im._inventory.hosts |
| 181 |
assert '1.2.3.4' in im._inventory.hosts |
| 182 |
assert 'test-server' in im._inventory.hosts |
| 183 |
assert 'test-server-2' not in im._inventory.hosts |
| 184 |
assert im._inventory.get_host('1.2.3.4') in im._inventory.groups['ungrouped'].hosts |
| 185 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 186 |
assert len(im._inventory.groups['ungrouped'].hosts) == 2 |
| 187 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 188 |
|
| 189 |
|
| 190 |
def test_inventory_file_simple_2(mocker): |
| 191 |
open_url = OpenUrlProxy([ |
| 192 |
OpenUrlCall('GET', 200) |
| 193 |
.result_json([ |
| 194 |
{ |
| 195 |
'server': { |
| 196 |
'server_ip': '1.2.3.4', |
| 197 |
'dc': 'foo', |
| 198 |
}, |
| 199 |
}, |
| 200 |
{ |
| 201 |
'server': { |
| 202 |
'server_ip': '1.2.3.5', |
| 203 |
'server_name': 'test-server', |
| 204 |
'dc': 'foo', |
| 205 |
}, |
| 206 |
}, |
| 207 |
{ |
| 208 |
'server': { |
| 209 |
'server_ip': '1.2.3.6', |
| 210 |
'server_name': 'test-server-2', |
| 211 |
'dc': 'bar', |
| 212 |
}, |
| 213 |
}, |
| 214 |
]) |
| 215 |
.expect_basic_auth('test', 'hunter2') |
| 216 |
.expect_force_basic_auth(True) |
| 217 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 218 |
]) |
| 219 |
inventory_filename = "test.robot.yaml" |
| 220 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 221 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 222 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 223 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 224 |
|
| 225 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 226 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 227 |
--- |
| 228 |
plugin: community.hrobot.robot |
| 229 |
hetzner_user: '{{ "test" }}' |
| 230 |
hetzner_password: '{{ "hunter2" }}' |
| 231 |
simple_filters: |
| 232 |
dc: foo |
| 233 |
""")} |
| 234 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 235 |
open_url.assert_is_done() |
| 236 |
|
| 237 |
assert im._inventory.hosts |
| 238 |
assert '1.2.3.4' in im._inventory.hosts |
| 239 |
assert 'test-server' in im._inventory.hosts |
| 240 |
assert 'test-server-2' not in im._inventory.hosts |
| 241 |
assert im._inventory.get_host('1.2.3.4') in im._inventory.groups['ungrouped'].hosts |
| 242 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 243 |
assert len(im._inventory.groups['ungrouped'].hosts) == 2 |
| 244 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 245 |
|
| 246 |
|
| 247 |
def test_inventory_file_filter(mocker): |
| 248 |
open_url = OpenUrlProxy([ |
| 249 |
OpenUrlCall('GET', 200) |
| 250 |
.result_json([ |
| 251 |
{ |
| 252 |
'server': { |
| 253 |
'server_ip': '1.2.3.4', |
| 254 |
'dc': 'foo', |
| 255 |
}, |
| 256 |
}, |
| 257 |
{ |
| 258 |
'server': { |
| 259 |
'server_ip': '1.2.3.5', |
| 260 |
'server_name': 'test-server', |
| 261 |
'dc': 'foo', |
| 262 |
}, |
| 263 |
}, |
| 264 |
{ |
| 265 |
'server': { |
| 266 |
'server_ip': '1.2.3.6', |
| 267 |
'server_name': 'test-server-2', |
| 268 |
'dc': 'bar', |
| 269 |
}, |
| 270 |
}, |
| 271 |
]) |
| 272 |
.expect_basic_auth('test', 'hunter2') |
| 273 |
.expect_force_basic_auth(True) |
| 274 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 275 |
]) |
| 276 |
inventory_filename = "test.robot.yaml" |
| 277 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 278 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 279 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 280 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 281 |
|
| 282 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 283 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 284 |
--- |
| 285 |
plugin: community.hrobot.robot |
| 286 |
hetzner_user: '{{ "test" }}' |
| 287 |
hetzner_password: '{{ "hunter2" }}' |
| 288 |
filters: |
| 289 |
- include: hrobot_dc == 'foo' |
| 290 |
- exclude: true |
| 291 |
""")} |
| 292 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 293 |
open_url.assert_is_done() |
| 294 |
|
| 295 |
assert im._inventory.hosts |
| 296 |
assert '1.2.3.4' in im._inventory.hosts |
| 297 |
assert 'test-server' in im._inventory.hosts |
| 298 |
assert 'test-server-2' not in im._inventory.hosts |
| 299 |
assert im._inventory.get_host('1.2.3.4') in im._inventory.groups['ungrouped'].hosts |
| 300 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 301 |
assert len(im._inventory.groups['ungrouped'].hosts) == 2 |
| 302 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 303 |
|
| 304 |
|
| 305 |
@pytest.mark.parametrize("error_result", [ |
| 306 |
None, |
| 307 |
json.dumps(dict( |
| 308 |
error=dict( |
| 309 |
code="foo", |
| 310 |
status=400, |
| 311 |
message="bar", |
| 312 |
), |
| 313 |
), sort_keys=True).encode('utf-8') |
| 314 |
]) |
| 315 |
def test_inventory_file_fail(mocker, error_result): |
| 316 |
open_url = OpenUrlProxy([ |
| 317 |
OpenUrlCall('GET', 200) |
| 318 |
.result_error(error_result) |
| 319 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 320 |
]) |
| 321 |
inventory_filename = "test.robot.yml" |
| 322 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 323 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 324 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 325 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 326 |
|
| 327 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 328 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 329 |
--- |
| 330 |
plugin: community.hrobot.robot |
| 331 |
hetzner_user: test |
| 332 |
hetzner_password: hunter2 |
| 333 |
simple_filters: |
| 334 |
dc: foo |
| 335 |
""")} |
| 336 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 337 |
open_url.assert_is_done() |
| 338 |
|
| 339 |
assert not im._inventory.hosts |
| 340 |
assert '1.2.3.4' not in im._inventory.hosts |
| 341 |
assert 'test-server' not in im._inventory.hosts |
| 342 |
assert 'test-server-2' not in im._inventory.hosts |
| 343 |
assert len(im._inventory.groups['ungrouped'].hosts) == 0 |
| 344 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 345 |
|
| 346 |
|
| 347 |
def test_inventory_wrong_file(mocker): |
| 348 |
open_url = OpenUrlProxy([]) |
| 349 |
inventory_filename = "test.bobot.yml" |
| 350 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 351 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 352 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 353 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 354 |
|
| 355 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 356 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 357 |
--- |
| 358 |
plugin: community.hrobot.robot |
| 359 |
hetzner_user: test |
| 360 |
hetzner_password: hunter2 |
| 361 |
""")} |
| 362 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 363 |
open_url.assert_is_done() |
| 364 |
|
| 365 |
assert not im._inventory.hosts |
| 366 |
assert '1.2.3.4' not in im._inventory.hosts |
| 367 |
assert 'test-server' not in im._inventory.hosts |
| 368 |
assert 'test-server-2' not in im._inventory.hosts |
| 369 |
assert len(im._inventory.groups['ungrouped'].hosts) == 0 |
| 370 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 371 |
|
| 372 |
|
| 373 |
def test_inventory_no_file(mocker): |
| 374 |
open_url = OpenUrlProxy([]) |
| 375 |
inventory_filename = "test.robot.yml" |
| 376 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 377 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 378 |
mocker.patch('os.path.exists', exists_mock(inventory_filename, False)) |
| 379 |
mocker.patch('os.access', access_mock(inventory_filename, False)) |
| 380 |
|
| 381 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 382 |
im = InventoryManager(loader=DictDataLoader({}), sources=inventory_filename) |
| 383 |
open_url.assert_is_done() |
| 384 |
|
| 385 |
assert not im._inventory.hosts |
| 386 |
assert len(im._inventory.groups['ungrouped'].hosts) == 0 |
| 387 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 388 |
|
| 389 |
|
| 390 |
def test_inventory_file_collision(mocker): |
| 391 |
open_url = OpenUrlProxy([ |
| 392 |
OpenUrlCall('GET', 200) |
| 393 |
.result_json([ |
| 394 |
{ |
| 395 |
'server': { |
| 396 |
'server_ip': '1.2.3.4', |
| 397 |
'server_name': 'test-server', |
| 398 |
}, |
| 399 |
}, |
| 400 |
{ |
| 401 |
'server': { |
| 402 |
'server_ip': '1.2.3.5', |
| 403 |
'server_name': 'test-server', |
| 404 |
}, |
| 405 |
}, |
| 406 |
]) |
| 407 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 408 |
]) |
| 409 |
inventory_filename = "test.robot.yaml" |
| 410 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 411 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 412 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 413 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 414 |
|
| 415 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 416 |
inventory_file = {inventory_filename: textwrap.dedent("""\ |
| 417 |
--- |
| 418 |
plugin: community.hrobot.robot |
| 419 |
hetzner_user: test |
| 420 |
hetzner_password: hunter2 |
| 421 |
""")} |
| 422 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 423 |
open_url.assert_is_done() |
| 424 |
|
| 425 |
assert im._inventory.hosts |
| 426 |
assert 'test-server' in im._inventory.hosts |
| 427 |
assert im._inventory.get_host('test-server').get_vars()['ansible_host'] == '1.2.3.4' |
| 428 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 429 |
assert len(im._inventory.groups['ungrouped'].hosts) == 1 |
| 430 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 431 |
# TODO: check for warning |
| 432 |
|
| 433 |
|
| 434 |
def test_unsafe(inventory, mocker): |
| 435 |
open_url = OpenUrlProxy([ |
| 436 |
OpenUrlCall('GET', 200) |
| 437 |
.result_json([ |
| 438 |
{ |
| 439 |
'server': { |
| 440 |
'server_ip': '1.2.{3.4', |
| 441 |
'dc': 'abc', |
| 442 |
}, |
| 443 |
}, |
| 444 |
{ |
| 445 |
'server': { |
| 446 |
'server_ip': '1.2.3.5', |
| 447 |
'server_name': 'fo{o', |
| 448 |
'dc': 'EVALU{{ "" }}ATED', |
| 449 |
}, |
| 450 |
}, |
| 451 |
]) |
| 452 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 453 |
]) |
| 454 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 455 |
|
| 456 |
inventory.get_option = mocker.MagicMock(side_effect=get_option) |
| 457 |
inventory.populate(inventory.get_servers()) |
| 458 |
|
| 459 |
open_url.assert_is_done() |
| 460 |
|
| 461 |
host_1 = inventory.inventory.get_host('1.2.{3.4') |
| 462 |
host_2 = inventory.inventory.get_host('fo{o') |
| 463 |
|
| 464 |
host_1_vars = host_1.get_vars() |
| 465 |
host_2_vars = host_2.get_vars() |
| 466 |
|
| 467 |
assert host_1_vars['ansible_host'] == '1.2.{3.4' |
| 468 |
assert host_1_vars['hrobot_server_ip'] == '1.2.{3.4' |
| 469 |
assert host_1_vars['hrobot_dc'] == 'abc' |
| 470 |
|
| 471 |
assert host_2_vars['ansible_host'] == '1.2.3.5' |
| 472 |
assert host_2_vars['hrobot_server_ip'] == '1.2.3.5' |
| 473 |
assert host_2_vars['hrobot_server_name'] == 'fo{o' |
| 474 |
assert host_2_vars['hrobot_dc'] == 'EVALU{{ "" }}ATED' |
| 475 |
|
| 476 |
# Make sure everything is unsafe |
| 477 |
assert not is_trusted(host_1_vars['ansible_host']) |
| 478 |
assert not is_trusted(host_1_vars['hrobot_server_ip']) |
| 479 |
if SUPPORTS_DATA_TAGGING: |
| 480 |
assert not is_trusted(host_1_vars['hrobot_dc']) |
| 481 |
else: |
| 482 |
assert is_trusted(host_1_vars['hrobot_dc']) |
| 483 |
|
| 484 |
if SUPPORTS_DATA_TAGGING: |
| 485 |
assert not is_trusted(host_2_vars['ansible_host']) |
| 486 |
assert not is_trusted(host_2_vars['hrobot_server_ip']) |
| 487 |
else: |
| 488 |
assert is_trusted(host_2_vars['ansible_host']) |
| 489 |
assert is_trusted(host_2_vars['hrobot_server_ip']) |
| 490 |
assert not is_trusted(host_2_vars['hrobot_server_name']) |
| 491 |
assert not is_trusted(host_2_vars['hrobot_dc']) |
| 492 |
|
| 493 |
|
| 494 |
@pytest.mark.skipif(SUPPORTS_DATA_TAGGING, reason="The Data Tagging PR changes how the on-disk cache looks like") |
| 495 |
def test_inventory_cache_empty(tmpdir, mocker): |
| 496 |
cache_directory = os.path.join(tmpdir, 'cache') |
| 497 |
open_url = OpenUrlProxy([ |
| 498 |
OpenUrlCall('GET', 200) |
| 499 |
.result_json([ |
| 500 |
{ |
| 501 |
'server': { |
| 502 |
'server_ip': '1.2.3.4', |
| 503 |
'dc': 'foo', |
| 504 |
}, |
| 505 |
}, |
| 506 |
{ |
| 507 |
'server': { |
| 508 |
'server_ip': '1.2.3.5', |
| 509 |
'server_name': 'test-server', |
| 510 |
'dc': 'foo', |
| 511 |
}, |
| 512 |
}, |
| 513 |
]) |
| 514 |
.expect_basic_auth('test', 'hunter2') |
| 515 |
.expect_force_basic_auth(True) |
| 516 |
.expect_url('{0}/server'.format(BASE_URL)), |
| 517 |
]) |
| 518 |
inventory_filename = "test.robot.yaml" |
| 519 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 520 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 521 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 522 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 523 |
|
| 524 |
assert not os.path.exists(cache_directory) |
| 525 |
|
| 526 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 527 |
inventory_file = {inventory_filename: textwrap.dedent(f"""\ |
| 528 |
--- |
| 529 |
plugin: community.hrobot.robot |
| 530 |
hetzner_user: test |
| 531 |
hetzner_password: hunter2 |
| 532 |
cache: true |
| 533 |
cache_plugin: ansible.builtin.jsonfile |
| 534 |
cache_connection: {cache_directory} |
| 535 |
cache_prefix: {CACHE_PREFIX} |
| 536 |
""")} |
| 537 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 538 |
open_url.assert_is_done() |
| 539 |
|
| 540 |
assert im._inventory.hosts |
| 541 |
assert '1.2.3.4' in im._inventory.hosts |
| 542 |
assert 'test-server' in im._inventory.hosts |
| 543 |
assert im._inventory.get_host('1.2.3.4') in im._inventory.groups['ungrouped'].hosts |
| 544 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 545 |
assert len(im._inventory.groups['ungrouped'].hosts) == 2 |
| 546 |
assert len(im._inventory.groups['all'].hosts) == 0 |
| 547 |
|
| 548 |
assert os.path.exists(cache_directory) |
| 549 |
assert os.path.isdir(cache_directory) |
| 550 |
print(os.listdir(cache_directory)) |
| 551 |
|
| 552 |
cache_filename = os.path.join(cache_directory, CACHE_KEY) |
| 553 |
assert os.path.exists(cache_filename) |
| 554 |
assert os.path.isfile(cache_filename) |
| 555 |
|
| 556 |
with open(cache_filename, 'rb') as f: |
| 557 |
data = json.load(f) |
| 558 |
print(data) |
| 559 |
assert data == [ |
| 560 |
{ |
| 561 |
'server': { |
| 562 |
'dc': 'foo', |
| 563 |
'server_ip': '1.2.3.4', |
| 564 |
}, |
| 565 |
}, |
| 566 |
{ |
| 567 |
'server': { |
| 568 |
'dc': 'foo', |
| 569 |
'server_ip': '1.2.3.5', |
| 570 |
'server_name': 'test-server', |
| 571 |
}, |
| 572 |
}, |
| 573 |
] |
| 574 |
|
| 575 |
|
| 576 |
@pytest.mark.skipif(SUPPORTS_DATA_TAGGING, reason="The Data Tagging PR changes how the on-disk cache looks like") |
| 577 |
def test_inventory_cache_full(tmpdir, mocker): |
| 578 |
cache_directory = os.path.join(tmpdir, 'cache') |
| 579 |
open_url = OpenUrlProxy([]) |
| 580 |
inventory_filename = "test.robot.yaml" |
| 581 |
mocker.patch('ansible_collections.community.hrobot.plugins.module_utils.robot.open_url', open_url) |
| 582 |
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop) |
| 583 |
mocker.patch('os.path.exists', exists_mock(inventory_filename)) |
| 584 |
mocker.patch('os.access', access_mock(inventory_filename)) |
| 585 |
|
| 586 |
os.mkdir(cache_directory) |
| 587 |
cache_filename = os.path.join(cache_directory, CACHE_KEY) |
| 588 |
with open(cache_filename, 'wt') as f: |
| 589 |
json.dump([ |
| 590 |
{ |
| 591 |
'server': { |
| 592 |
'dc': 'foo', |
| 593 |
'server_ip': '1.2.3.4', |
| 594 |
}, |
| 595 |
}, |
| 596 |
{ |
| 597 |
'server': { |
| 598 |
'dc': 'foo', |
| 599 |
'server_ip': '1.2.3.5', |
| 600 |
'server_name': 'test-server', |
| 601 |
}, |
| 602 |
}, |
| 603 |
], f) |
| 604 |
|
| 605 |
C.INVENTORY_ENABLED = ['community.hrobot.robot'] |
| 606 |
inventory_file = {inventory_filename: textwrap.dedent(f"""\ |
| 607 |
--- |
| 608 |
plugin: community.hrobot.robot |
| 609 |
hetzner_user: test |
| 610 |
hetzner_password: hunter2 |
| 611 |
cache: true |
| 612 |
cache_plugin: ansible.builtin.jsonfile |
| 613 |
cache_connection: {cache_directory} |
| 614 |
cache_prefix: {CACHE_PREFIX} |
| 615 |
""")} |
| 616 |
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename) |
| 617 |
open_url.assert_is_done() |
| 618 |
|
| 619 |
assert im._inventory.hosts |
| 620 |
assert '1.2.3.4' in im._inventory.hosts |
| 621 |
assert 'test-server' in im._inventory.hosts |
| 622 |
assert im._inventory.get_host('1.2.3.4') in im._inventory.groups['ungrouped'].hosts |
| 623 |
assert im._inventory.get_host('test-server') in im._inventory.groups['ungrouped'].hosts |
| 624 |
assert len(im._inventory.groups['ungrouped'].hosts) == 2 |
| 625 |
assert len(im._inventory.groups['all'].hosts) == 0 |