Skip to content

Commit cfbf530

Browse files
committed
vagrant: Improve network interfaces support
Current code doesn't ensure that the network name is valid. According to current Vagrant documentation, it can only be: - public_network - private_network - forwarded_port Moreover, the same documentation is saying that public network configuration may be reduced to: config.vm.network "public_network" So add needed code to support interfaces without any options. On the test side: - modify the network test to check that the generated Vagrantfile is correct when no option is specified - add a test to ensure invalid network name are caught. Fixes: #99 Signed-off-by: Arnaud Patard <apatard@hupstream.com>
1 parent 5d86a8b commit cfbf530

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

src/molecule_plugins/vagrant/modules/vagrant.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
See doc/source/configuration.rst
182182
"""
183183

184+
VAGRANT_VALID_NETNAMES = ["private_network", "public_network", "forwarded_port"]
184185
VAGRANTFILE_TEMPLATE = """
185186
{%- macro ruby_format(value) -%}
186187
{%- if value is boolean -%}
@@ -247,7 +248,7 @@
247248
# Network
248249
##
249250
{% for n in instance.networks %}
250-
c.vm.network "{{ n.name }}", {{ dict2args(n.options) }}
251+
c.vm.network "{{ n.name }}"{% if 'options' in n %}, {{ dict2args(n.options) }}{% endif %}
251252
{% endfor %}
252253
253254
##
@@ -596,10 +597,16 @@ def _get_instance_vagrant_config_dict(self, instance):
596597
networks = []
597598
if "interfaces" in instance:
598599
for iface in instance["interfaces"]:
600+
net_name = iface["network_name"]
601+
if net_name not in VAGRANT_VALID_NETNAMES:
602+
self._module.fail_json(
603+
msg=f"Invalid network_name value {net_name}."
604+
)
599605
net = dict()
600-
net["name"] = iface["network_name"]
606+
net["name"] = net_name
601607
iface.pop("network_name")
602-
net["options"] = iface
608+
if len(iface) > 0:
609+
net["options"] = iface
603610
networks.append(net)
604611

605612
# compat

test/vagrant/functional/test_func.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ def test_invalid_settings(temp_dir):
9494
assert "Failed to validate generated Vagrantfile" in result.stdout
9595

9696

97+
@pytest.mark.skipif(
98+
not is_vagrant_supported(), reason="vagrant not supported on this machine"
99+
)
100+
def test_invalid_network_name(temp_dir):
101+
scenario_directory = os.path.join(
102+
os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios"
103+
)
104+
105+
with change_dir_to(scenario_directory):
106+
cmd = ["molecule", "create", "--scenario-name", "invalid_net"]
107+
result = run_command(cmd)
108+
assert result.returncode == 2
109+
110+
assert "Invalid network_name value my_network." in result.stdout
111+
112+
97113
@pytest.mark.skipif(
98114
not is_vagrant_supported(), reason="vagrant not supported on this machine"
99115
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
gather_facts: false
5+
become: true
6+
tasks:
7+
- name: Sample task # noqa command-instead-of-shell
8+
ansible.builtin.shell:
9+
cmd: uname
10+
changed_when: false
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
dependency:
3+
name: galaxy
4+
driver:
5+
name: vagrant
6+
platforms:
7+
- name: instance/foo
8+
interfaces:
9+
- network_name: my_network
10+
provisioner:
11+
name: ansible

test/vagrant/scenarios/molecule/network/molecule.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ platforms:
1515
- network_name: private_network
1616
ip: 192.168.56.4
1717
auto_config: true
18+
- network_name: public_network
1819
provisioner:
1920
name: ansible
2021
verifier:

test/vagrant/scenarios/molecule/network/verify.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
gather_subset:
66
- network
77
tasks:
8-
- name: Check that there are 3 interfaces
8+
- name: Check that there are 4 interfaces
99
ansible.builtin.assert:
1010
that:
11-
- "{{ ansible_interfaces | length == 3 }}"
11+
- "{{ ansible_interfaces | length == 4 }}"

0 commit comments

Comments
 (0)