How to use _link_or_update_vars method in molecule

Best Python code snippet using molecule_python

test_ansible.py

Source:test_ansible.py Github

copy

Full Screen

...36@pytest.fixture37def _patched_remove_vars(mocker):38 return mocker.patch("molecule.provisioner.ansible.Ansible._remove_vars")39@pytest.fixture40def _patched_link_or_update_vars(mocker):41 return mocker.patch("molecule.provisioner.ansible.Ansible._link_or_update_vars")42@pytest.fixture43def _provisioner_section_data():44 return {45 "provisioner": {46 "name": "ansible",47 "config_options": {"defaults": {"foo": "bar"}},48 "connection_options": {"foo": "bar"},49 "options": {"foo": "bar", "become": True, "v": True},50 "env": {51 "FOO": "bar",52 "ANSIBLE_ROLES_PATH": "foo/bar",53 "ANSIBLE_LIBRARY": "foo/bar",54 "ANSIBLE_FILTER_PLUGINS": "foo/bar",55 },56 "inventory": {57 "hosts": {58 "all": {59 "hosts": {"extra-host-01": {}},60 "children": {"extra-group": {"hosts": ["extra-host-01"]}},61 }62 },63 "host_vars": {64 "instance-1": [{"foo": "bar"}],65 "localhost": [{"foo": "baz"}],66 },67 "group_vars": {68 "example_group1": [{"foo": "bar"}],69 "example_group2": [{"foo": "bar"}],70 },71 },72 }73 }74@pytest.fixture75def _instance(_provisioner_section_data, config_instance):76 return ansible.Ansible(config_instance)77def test_config_private_member(_instance):78 assert isinstance(_instance._config, config.Config)79def test_default_config_options_property(_instance):80 x = {81 "defaults": {82 "ansible_managed": "Ansible managed: Do NOT edit this file manually!",83 "display_failed_stderr": True,84 "forks": 50,85 "host_key_checking": False,86 "interpreter_python": "auto",87 "nocows": 1,88 "retry_files_enabled": False,89 },90 "ssh_connection": {91 "control_path": "%(directory)s/%%h-%%p-%%r",92 "scp_if_ssh": True,93 },94 }95 assert x == _instance.default_config_options96def test_default_options_property(_instance):97 assert {"skip-tags": "molecule-notest,notest"} == _instance.default_options98def test_default_env_property(_instance):99 x = _instance._config.provisioner.config_file100 assert x == _instance.default_env["ANSIBLE_CONFIG"]101 assert "MOLECULE_FILE" in _instance.default_env102 assert "MOLECULE_INVENTORY_FILE" in _instance.default_env103 assert "MOLECULE_SCENARIO_DIRECTORY" in _instance.default_env104 assert "MOLECULE_INSTANCE_CONFIG" in _instance.default_env105 assert "ANSIBLE_CONFIG" in _instance.env106 assert "ANSIBLE_ROLES_PATH" in _instance.env107 assert "ANSIBLE_LIBRARY" in _instance.env108 assert "ANSIBLE_FILTER_PLUGINS" in _instance.env109def test_name_property(_instance):110 assert "ansible" == _instance.name111@pytest.mark.parametrize(112 "config_instance", ["_provisioner_section_data"], indirect=True113)114def test_config_options_property(_instance):115 x = {116 "defaults": {117 "ansible_managed": "Ansible managed: Do NOT edit this file manually!",118 "display_failed_stderr": True,119 "foo": "bar",120 "forks": 50,121 "host_key_checking": False,122 "interpreter_python": "auto",123 "nocows": 1,124 "retry_files_enabled": False,125 },126 "ssh_connection": {127 "control_path": "%(directory)s/%%h-%%p-%%r",128 "scp_if_ssh": True,129 },130 }131 assert x == _instance.config_options132@pytest.mark.parametrize(133 "config_instance", ["_provisioner_section_data"], indirect=True134)135def test_options_property(_instance):136 x = {"become": True, "foo": "bar", "v": True, "skip-tags": "molecule-notest,notest"}137 assert x == _instance.options138def test_options_property_does_not_merge(_instance):139 for action in ["create", "destroy"]:140 _instance._config.action = action141 assert {"skip-tags": "molecule-notest,notest"} == _instance.options142def test_options_property_handles_cli_args(_instance):143 _instance._config.args = {"debug": True}144 x = {145 "vvv": True,146 "become": True,147 "diff": True,148 "skip-tags": "molecule-notest,notest",149 }150 assert x == _instance.options151@pytest.mark.parametrize(152 "config_instance", ["_provisioner_section_data"], indirect=True153)154def test_env_property(_instance):155 x = _instance._config.provisioner.config_file156 assert x == _instance.env["ANSIBLE_CONFIG"]157 assert "bar" == _instance.env["FOO"]158@pytest.mark.parametrize(159 "config_instance", ["_provisioner_section_data"], indirect=True160)161def test_env_appends_env_property(_instance):162 x = [163 util.abs_path(164 os.path.join(_instance._config.scenario.ephemeral_directory, "roles")165 ),166 util.abs_path(167 os.path.join(_instance._config.project_directory, os.path.pardir)168 ),169 util.abs_path(os.path.join(os.path.expanduser("~"), ".ansible", "roles")),170 "/usr/share/ansible/roles",171 "/etc/ansible/roles",172 util.abs_path(os.path.join(_instance._config.scenario.directory, "foo", "bar")),173 ]174 assert x == _instance.env["ANSIBLE_ROLES_PATH"].split(":")175 x = _instance._get_modules_directories()176 x.append(177 util.abs_path(os.path.join(_instance._config.scenario.directory, "foo", "bar"))178 )179 assert x == _instance.env["ANSIBLE_LIBRARY"].split(":")180 x = [181 _instance._get_filter_plugin_directory(),182 util.abs_path(183 os.path.join(184 _instance._config.scenario.ephemeral_directory, "plugins", "filter"185 )186 ),187 util.abs_path(188 os.path.join(_instance._config.project_directory, "plugins", "filter")189 ),190 util.abs_path(191 os.path.join(os.path.expanduser("~"), ".ansible", "plugins", "filter")192 ),193 "/usr/share/ansible/plugins/filter",194 util.abs_path(os.path.join(_instance._config.scenario.directory, "foo", "bar")),195 ]196 assert x == _instance.env["ANSIBLE_FILTER_PLUGINS"].split(":")197@pytest.mark.parametrize(198 "config_instance", ["_provisioner_section_data"], indirect=True199)200def test_host_vars_property(_instance):201 x = {"instance-1": [{"foo": "bar"}], "localhost": [{"foo": "baz"}]}202 assert x == _instance.host_vars203@pytest.mark.parametrize(204 "config_instance", ["_provisioner_section_data"], indirect=True205)206def test_group_vars_property(_instance):207 x = {"example_group1": [{"foo": "bar"}], "example_group2": [{"foo": "bar"}]}208 assert x == _instance.group_vars209@pytest.mark.parametrize(210 "config_instance", ["_provisioner_section_data"], indirect=True211)212def test_hosts_property(_instance):213 hosts = {214 "all": {215 "hosts": {"extra-host-01": {}},216 "children": {"extra-group": {"hosts": ["extra-host-01"]}},217 }218 }219 assert hosts == _instance.hosts220def test_links_property(_instance):221 assert {} == _instance.links222@pytest.mark.parametrize(223 "config_instance", ["_provisioner_section_data"], indirect=True224)225def test_inventory_property(_instance):226 x = {227 "ungrouped": {"vars": {}},228 "bar": {229 "hosts": {"instance-1": {"foo": "bar", "ansible_connection": "docker"}},230 "children": {231 "child1": {232 "hosts": {233 "instance-1": {"foo": "bar", "ansible_connection": "docker"}234 }235 }236 },237 "vars": {238 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",239 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",240 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",241 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",242 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",243 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "244 "molecule_yml.provisioner.log|default(False) | bool }}",245 },246 },247 "all": {248 "hosts": {249 "instance-1": {"foo": "bar", "ansible_connection": "docker"},250 "instance-2": {"foo": "bar", "ansible_connection": "docker"},251 },252 "vars": {253 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",254 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",255 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",256 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",257 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",258 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "259 "molecule_yml.provisioner.log|default(False) | bool }}",260 },261 },262 "foo": {263 "hosts": {264 "instance-1": {"foo": "bar", "ansible_connection": "docker"},265 "instance-2": {"foo": "bar", "ansible_connection": "docker"},266 },267 "children": {268 "child1": {269 "hosts": {270 "instance-1": {"foo": "bar", "ansible_connection": "docker"}271 }272 },273 "child2": {274 "hosts": {275 "instance-2": {"foo": "bar", "ansible_connection": "docker"}276 }277 },278 },279 "vars": {280 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",281 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",282 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",283 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",284 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",285 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "286 "molecule_yml.provisioner.log|default(False) | bool }}",287 },288 },289 "baz": {290 "hosts": {"instance-2": {"foo": "bar", "ansible_connection": "docker"}},291 "children": {292 "child2": {293 "hosts": {294 "instance-2": {"foo": "bar", "ansible_connection": "docker"}295 }296 }297 },298 "vars": {299 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",300 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",301 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",302 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",303 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",304 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "305 "molecule_yml.provisioner.log|default(False) | bool }}",306 },307 },308 }309 assert is_subset(x, _instance.inventory)310@pytest.mark.parametrize(311 "config_instance", ["_provisioner_section_data"], indirect=True312)313def test_inventory_property_handles_missing_groups(temp_dir, _instance):314 platforms = [{"name": "instance-1"}, {"name": "instance-2"}]315 _instance._config.config["platforms"] = platforms316 x = {317 "ungrouped": {318 "hosts": {319 "instance-1": {"foo": "bar", "ansible_connection": "docker"},320 "instance-2": {"foo": "bar", "ansible_connection": "docker"},321 },322 "vars": {},323 },324 "all": {325 "hosts": {326 "instance-1": {"foo": "bar", "ansible_connection": "docker"},327 "instance-2": {"foo": "bar", "ansible_connection": "docker"},328 },329 "vars": {330 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",331 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",332 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",333 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",334 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",335 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "336 "molecule_yml.provisioner.log|default(False) | bool }}",337 },338 },339 }340 assert is_subset(x, _instance.inventory)341def test_inventory_directory_property(_instance):342 x = os.path.join(_instance._config.scenario.ephemeral_directory, "inventory")343 assert x == _instance.inventory_directory344def test_inventory_file_property(_instance):345 x = os.path.join(346 _instance._config.scenario.inventory_directory, "ansible_inventory.yml"347 )348 assert x == _instance.inventory_file349def test_config_file_property(_instance):350 x = os.path.join(_instance._config.scenario.ephemeral_directory, "ansible.cfg")351 assert x == _instance.config_file352def test_playbooks_property(_instance):353 assert isinstance(_instance.playbooks, ansible_playbooks.AnsiblePlaybooks)354def test_directory_property(_instance):355 result = _instance.directory356 parts = pytest.helpers.os_split(result)357 assert ("molecule", "provisioner", "ansible") == parts[-3:]358def test_playbooks_cleaned_property_is_optional(_instance):359 assert _instance.playbooks.cleanup is None360def test_playbooks_create_property(_instance):361 x = os.path.join(362 _instance._config.provisioner.playbooks._get_playbook_directory(),363 "docker",364 "create.yml",365 )366 assert x == _instance.playbooks.create367def test_playbooks_converge_property(_instance):368 x = os.path.join(_instance._config.scenario.directory, "converge.yml")369 assert x == _instance.playbooks.converge370def test_playbooks_destroy_property(_instance):371 x = os.path.join(372 _instance._config.provisioner.playbooks._get_playbook_directory(),373 "docker",374 "destroy.yml",375 )376 assert x == _instance.playbooks.destroy377def test_playbooks_side_effect_property(_instance):378 assert _instance.playbooks.side_effect is None379@pytest.mark.parametrize(380 "config_instance", ["_provisioner_section_data"], indirect=True381)382def test_connection_options(_instance):383 x = {"ansible_connection": "docker", "foo": "bar"}384 assert is_subset(x, _instance.connection_options("foo"))385def test_check(_instance, mocker, _patched_ansible_playbook):386 _instance.check()387 _patched_ansible_playbook.assert_called_once_with(388 _instance._config.provisioner.playbooks.converge, _instance._config389 )390 _patched_ansible_playbook.return_value.add_cli_arg.assert_called_once_with(391 "check", True392 )393 _patched_ansible_playbook.return_value.execute.assert_called_once_with()394def test_converge(_instance, mocker, _patched_ansible_playbook):395 result = _instance.converge()396 _patched_ansible_playbook.assert_called_once_with(397 _instance._config.provisioner.playbooks.converge, _instance._config398 )399 # NOTE(retr0h): This is not the true return type. This is a mock return400 # which didn't go through str.decode().401 assert result == b"patched-ansible-playbook-stdout"402 _patched_ansible_playbook.return_value.execute.assert_called_once_with()403def test_converge_with_playbook(_instance, mocker, _patched_ansible_playbook):404 result = _instance.converge("playbook")405 _patched_ansible_playbook.assert_called_once_with("playbook", _instance._config)406 # NOTE(retr0h): This is not the true return type. This is a mock return407 # which didn't go through str.decode().408 assert result == b"patched-ansible-playbook-stdout"409 _patched_ansible_playbook.return_value.execute.assert_called_once_with()410def test_cleanup(_instance, mocker, _patched_ansible_playbook):411 _instance.cleanup()412 _patched_ansible_playbook.assert_called_once_with(413 _instance._config.provisioner.playbooks.cleanup, _instance._config414 )415 _patched_ansible_playbook.return_value.execute.assert_called_once_with()416def test_destroy(_instance, mocker, _patched_ansible_playbook):417 _instance.destroy()418 _patched_ansible_playbook.assert_called_once_with(419 _instance._config.provisioner.playbooks.destroy, _instance._config420 )421 _patched_ansible_playbook.return_value.execute.assert_called_once_with()422def test_side_effect(_instance, mocker, _patched_ansible_playbook):423 _instance.side_effect()424 _patched_ansible_playbook.assert_called_once_with(425 _instance._config.provisioner.playbooks.side_effect, _instance._config426 )427 _patched_ansible_playbook.return_value.execute.assert_called_once_with()428def test_create(_instance, mocker, _patched_ansible_playbook):429 _instance.create()430 _patched_ansible_playbook.assert_called_once_with(431 _instance._config.provisioner.playbooks.create, _instance._config432 )433 _patched_ansible_playbook.return_value.execute.assert_called_once_with()434def test_prepare(_instance, mocker, _patched_ansible_playbook):435 _instance.prepare()436 _patched_ansible_playbook.assert_called_once_with(437 _instance._config.provisioner.playbooks.prepare, _instance._config438 )439 _patched_ansible_playbook.return_value.execute.assert_called_once_with()440def test_syntax(_instance, mocker, _patched_ansible_playbook):441 _instance.syntax()442 _patched_ansible_playbook.assert_called_once_with(443 _instance._config.provisioner.playbooks.converge, _instance._config444 )445 _patched_ansible_playbook.return_value.add_cli_arg.assert_called_once_with(446 "syntax-check", True447 )448 _patched_ansible_playbook.return_value.execute.assert_called_once_with()449def test_verify(_instance, mocker, _patched_ansible_playbook):450 _instance.verify()451 if _instance._config.provisioner.playbooks.verify:452 _patched_ansible_playbook.assert_called_once_with(453 _instance._config.provisioner.playbooks.verify, _instance._config454 )455 _patched_ansible_playbook.return_value.execute.assert_called_once_with()456def test_write_config(temp_dir, _instance):457 _instance.write_config()458 assert os.path.isfile(_instance.config_file)459def test_manage_inventory(460 _instance,461 _patched_write_inventory,462 _patched_remove_vars,463 patched_add_or_update_vars,464 _patched_link_or_update_vars,465):466 _instance.manage_inventory()467 _patched_write_inventory.assert_called_once_with()468 _patched_remove_vars.assert_called_once_with()469 patched_add_or_update_vars.assert_called_once_with()470 assert not _patched_link_or_update_vars.called471def test_manage_inventory_with_links(472 _instance,473 _patched_write_inventory,474 _patched_remove_vars,475 patched_add_or_update_vars,476 _patched_link_or_update_vars,477):478 c = _instance._config.config479 c["provisioner"]["inventory"]["links"] = {"foo": "bar"}480 _instance.manage_inventory()481 _patched_write_inventory.assert_called_once_with()482 _patched_remove_vars.assert_called_once_with()483 assert not patched_add_or_update_vars.called484 _patched_link_or_update_vars.assert_called_once_with()485@pytest.mark.parametrize(486 "config_instance", ["_provisioner_section_data"], indirect=True487)488def test_add_or_update_vars(_instance):489 inventory_dir = _instance._config.scenario.inventory_directory490 host_vars_directory = os.path.join(inventory_dir, "host_vars")491 host_vars = os.path.join(host_vars_directory, "instance-1")492 _instance._add_or_update_vars()493 assert os.path.isdir(host_vars_directory)494 assert os.path.isfile(host_vars)495 host_vars_localhost = os.path.join(host_vars_directory, "localhost")496 assert os.path.isfile(host_vars_localhost)497 group_vars_directory = os.path.join(inventory_dir, "group_vars")498 group_vars_1 = os.path.join(group_vars_directory, "example_group1")499 group_vars_2 = os.path.join(group_vars_directory, "example_group2")500 assert os.path.isdir(group_vars_directory)501 assert os.path.isfile(group_vars_1)502 assert os.path.isfile(group_vars_2)503 hosts = os.path.join(inventory_dir, "hosts")504 assert os.path.isfile(hosts)505 assert util.safe_load_file(hosts) == _instance.hosts506@pytest.mark.parametrize(507 "config_instance", ["_provisioner_section_data"], indirect=True508)509def test_add_or_update_vars_without_host_vars(_instance):510 c = _instance._config.config511 c["provisioner"]["inventory"]["host_vars"] = {}512 inventory_dir = _instance._config.scenario.inventory_directory513 host_vars_directory = os.path.join(inventory_dir, "host_vars")514 host_vars = os.path.join(host_vars_directory, "instance-1")515 _instance._add_or_update_vars()516 assert not os.path.isdir(host_vars_directory)517 assert not os.path.isfile(host_vars)518 host_vars_localhost = os.path.join(host_vars_directory, "localhost")519 assert not os.path.isfile(host_vars_localhost)520 group_vars_directory = os.path.join(inventory_dir, "group_vars")521 group_vars_1 = os.path.join(group_vars_directory, "example_group1")522 group_vars_2 = os.path.join(group_vars_directory, "example_group2")523 assert os.path.isdir(group_vars_directory)524 assert os.path.isfile(group_vars_1)525 assert os.path.isfile(group_vars_2)526 hosts = os.path.join(inventory_dir, "hosts")527 assert os.path.isfile(hosts)528 assert util.safe_load_file(hosts) == _instance.hosts529def test_add_or_update_vars_does_not_create_vars(_instance):530 c = _instance._config.config531 c["provisioner"]["inventory"]["hosts"] = {}532 c["provisioner"]["inventory"]["host_vars"] = {}533 c["provisioner"]["inventory"]["group_vars"] = {}534 inventory_dir = _instance._config.scenario.inventory_directory535 hosts = os.path.join(inventory_dir, "hosts")536 host_vars_directory = os.path.join(inventory_dir, "host_vars")537 group_vars_directory = os.path.join(inventory_dir, "group_vars")538 _instance._add_or_update_vars()539 assert not os.path.isdir(host_vars_directory)540 assert not os.path.isdir(group_vars_directory)541 assert not os.path.isfile(hosts)542@pytest.mark.parametrize(543 "config_instance", ["_provisioner_section_data"], indirect=True544)545def test_write_inventory(temp_dir, _instance):546 _instance._write_inventory()547 assert os.path.isfile(_instance.inventory_file)548 data = util.safe_load_file(_instance.inventory_file)549 x = {550 "ungrouped": {"vars": {}},551 "bar": {552 "hosts": {"instance-1": {"foo": "bar", "ansible_connection": "docker"}},553 "children": {554 "child1": {555 "hosts": {556 "instance-1": {"foo": "bar", "ansible_connection": "docker"}557 }558 }559 },560 "vars": {561 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",562 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",563 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",564 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",565 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",566 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "567 "molecule_yml.provisioner.log|default(False) | bool }}",568 },569 },570 "all": {571 "hosts": {572 "instance-1": {"foo": "bar", "ansible_connection": "docker"},573 "instance-2": {"foo": "bar", "ansible_connection": "docker"},574 },575 "vars": {576 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",577 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",578 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",579 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",580 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",581 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "582 "molecule_yml.provisioner.log|default(False) | bool }}",583 },584 },585 "foo": {586 "hosts": {587 "instance-1": {"foo": "bar", "ansible_connection": "docker"},588 "instance-2": {"foo": "bar", "ansible_connection": "docker"},589 },590 "children": {591 "child1": {592 "hosts": {593 "instance-1": {"foo": "bar", "ansible_connection": "docker"}594 }595 },596 "child2": {597 "hosts": {598 "instance-2": {"foo": "bar", "ansible_connection": "docker"}599 }600 },601 },602 "vars": {603 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",604 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",605 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",606 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",607 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",608 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "609 "molecule_yml.provisioner.log|default(False) | bool }}",610 },611 },612 "baz": {613 "hosts": {"instance-2": {"foo": "bar", "ansible_connection": "docker"}},614 "children": {615 "child2": {616 "hosts": {617 "instance-2": {"foo": "bar", "ansible_connection": "docker"}618 }619 }620 },621 "vars": {622 "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}",623 "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}",624 "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}",625 "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}",626 "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}",627 "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not "628 "molecule_yml.provisioner.log|default(False) | bool }}",629 },630 },631 }632 assert is_subset(x, data)633@pytest.mark.parametrize(634 "config_instance", ["_provisioner_section_data"], indirect=True635)636def test_remove_vars(_instance):637 inventory_dir = _instance._config.scenario.inventory_directory638 hosts = os.path.join(inventory_dir, "hosts")639 host_vars_directory = os.path.join(inventory_dir, "host_vars")640 host_vars = os.path.join(host_vars_directory, "instance-1")641 _instance._add_or_update_vars()642 assert os.path.isfile(hosts)643 assert os.path.isdir(host_vars_directory)644 assert os.path.isfile(host_vars)645 host_vars_localhost = os.path.join(host_vars_directory, "localhost")646 assert os.path.isfile(host_vars_localhost)647 group_vars_directory = os.path.join(inventory_dir, "group_vars")648 group_vars_1 = os.path.join(group_vars_directory, "example_group1")649 group_vars_2 = os.path.join(group_vars_directory, "example_group2")650 assert os.path.isdir(group_vars_directory)651 assert os.path.isfile(group_vars_1)652 assert os.path.isfile(group_vars_2)653 _instance._remove_vars()654 assert not os.path.isfile(hosts)655 assert not os.path.isdir(host_vars_directory)656 assert not os.path.isdir(group_vars_directory)657def test_remove_vars_symlinks(_instance):658 inventory_dir = _instance._config.scenario.inventory_directory659 source_group_vars = os.path.join(inventory_dir, os.path.pardir, "group_vars")660 target_group_vars = os.path.join(inventory_dir, "group_vars")661 os.mkdir(source_group_vars)662 os.symlink(source_group_vars, target_group_vars)663 _instance._remove_vars()664 assert not os.path.lexists(target_group_vars)665def test_link_vars(_instance):666 c = _instance._config.config667 c["provisioner"]["inventory"]["links"] = {668 "hosts": "../hosts",669 "group_vars": "../group_vars",670 "host_vars": "../host_vars",671 }672 inventory_dir = _instance._config.scenario.inventory_directory673 scenario_dir = _instance._config.scenario.directory674 source_hosts = os.path.join(scenario_dir, os.path.pardir, "hosts")675 target_hosts = os.path.join(inventory_dir, "hosts")676 source_group_vars = os.path.join(scenario_dir, os.path.pardir, "group_vars")677 target_group_vars = os.path.join(inventory_dir, "group_vars")678 source_host_vars = os.path.join(scenario_dir, os.path.pardir, "host_vars")679 target_host_vars = os.path.join(inventory_dir, "host_vars")680 open(source_hosts, "w").close()681 os.mkdir(source_group_vars)682 os.mkdir(source_host_vars)683 _instance._link_or_update_vars()684 assert os.path.lexists(target_hosts)685 assert os.path.lexists(target_group_vars)686 assert os.path.lexists(target_host_vars)687def test_link_vars_raises_when_source_not_found(_instance, patched_logger_critical):688 c = _instance._config.config689 c["provisioner"]["inventory"]["links"] = {"foo": "../bar"}690 with pytest.raises(SystemExit) as e:691 _instance._link_or_update_vars()692 assert 1 == e.value.code693 source = os.path.join(_instance._config.scenario.directory, os.path.pardir, "bar")694 msg = "The source path '{}' does not exist.".format(source)695 patched_logger_critical.assert_called_once_with(msg)696def test_verify_inventory(_instance):697 _instance._verify_inventory()698def test_verify_inventory_raises_when_missing_hosts(699 temp_dir, patched_logger_critical, _instance700):701 _instance._config.config["platforms"] = []702 with pytest.raises(SystemExit) as e:703 _instance._verify_inventory()704 assert 1 == e.value.code705 msg = "Instances missing from the 'platform' section of molecule.yml."...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful