Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vagrant: Improve network interfaces support #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/molecule_plugins/vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
See doc/source/configuration.rst
"""

VAGRANT_VALID_NETNAMES = ["private_network", "public_network", "forwarded_port"]
VAGRANTFILE_TEMPLATE = """
{%- macro ruby_format(value) -%}
{%- if value is boolean -%}
Expand Down Expand Up @@ -244,7 +245,7 @@
# Network
##
{% for n in instance.networks %}
c.vm.network "{{ n.name }}", {{ dict2args(n.options) }}
c.vm.network "{{ n.name }}"{% if 'options' in n %}, {{ dict2args(n.options) }}{% endif %}
{% endfor %}

##
Expand Down Expand Up @@ -617,10 +618,16 @@ def _get_instance_vagrant_config_dict(self, instance):
networks = []
if "interfaces" in instance:
for iface in instance["interfaces"]:
net_name = iface["network_name"]
if net_name not in VAGRANT_VALID_NETNAMES:
self._module.fail_json(
msg=f"Invalid network_name value {net_name}.",
)
net = {}
net["name"] = iface["network_name"]
net["name"] = net_name
iface.pop("network_name")
net["options"] = iface
if len(iface) > 0:
net["options"] = iface
networks.append(net)

# compat
Expand Down
16 changes: 16 additions & 0 deletions test/vagrant/functional/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ def test_invalid_settings(temp_dir):
not is_vagrant_supported(),
reason="vagrant not supported on this machine",
)
def test_invalid_network_name(temp_dir):
scenario_directory = os.path.join(
os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios",
)

with change_dir_to(scenario_directory):
cmd = ["molecule", "create", "--scenario-name", "invalid_net"]
result = run_command(cmd)
assert result.returncode == 2

assert "Invalid network_name value my_network." in result.stdout


@pytest.mark.skipif(
not is_vagrant_supported(), reason="vagrant not supported on this machine",
)
@pytest.mark.parametrize(
"scenario",
[
Expand Down
10 changes: 10 additions & 0 deletions test/vagrant/scenarios/molecule/invalid_net/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Converge
hosts: all
gather_facts: false
become: true
tasks:
- name: Sample task # noqa command-instead-of-shell
ansible.builtin.shell:
cmd: uname
changed_when: false
11 changes: 11 additions & 0 deletions test/vagrant/scenarios/molecule/invalid_net/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
dependency:
name: galaxy
driver:
name: vagrant
platforms:
- name: instance/foo
interfaces:
- network_name: my_network
provisioner:
name: ansible
1 change: 1 addition & 0 deletions test/vagrant/scenarios/molecule/network/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ platforms:
- network_name: private_network
ip: 192.168.56.4
auto_config: true
- network_name: public_network
provisioner:
name: ansible
verifier:
Expand Down
4 changes: 2 additions & 2 deletions test/vagrant/scenarios/molecule/network/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
gather_subset:
- network
tasks:
- name: Check that there are 3 interfaces
- name: Check that there are 4 interfaces
ansible.builtin.assert:
that:
- "{{ ansible_interfaces | length == 3 }}"
- "{{ ansible_interfaces | length == 4 }}"