Best Python code snippet using molecule_python
test_ansible.py
Source:test_ansible.py  
...484    _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)...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
