Skip to content

Commit

Permalink
Pass --provider to vagrant when provider name specified
Browse files Browse the repository at this point in the history
Currently, vagrant is still choosing the provider to use on its own
liking. This may cause troubles like VM memory/cpus or provider
options ignored if the provider use is not the one specified.

See:

- ansible-community#174
- https://github.com/ansible-community/molecule-vagrant/issues/181

So, pass --provider when the provider is set in the molecule.yml.
If the test doesn't care about the provider, it should remove the
provider name setting in the molecule.yml.

Note: While this may break some setup, I believe this will prevent
new reports/issues like the one previously noted. In case one
molecule.yml file for multiple providers is wanted, use the new
providers_options, as done in our CI scenario.

Signed-off-by: Arnaud Patard <[email protected]>
  • Loading branch information
apatard committed Oct 18, 2022
1 parent f058b22 commit 4b325ee
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ jobs:
platform: ubuntu-latest
skip_vagrant: true
- tox_env: py38,py38-devel
PREFIX: PYTEST_REQPASS=12
PREFIX: PYTEST_REQPASS=13
platform: macos-12
python_version: "3.8"
- tox_env: py39,py39-devel
PREFIX: PYTEST_REQPASS=12
PREFIX: PYTEST_REQPASS=13
platform: macos-12
python_version: "3.9"
- tox_env: py310,py310-devel
PREFIX: PYTEST_REQPASS=12
PREFIX: PYTEST_REQPASS=13
platform: macos-12
python_version: "3.10"
- tox_env: packaging
Expand Down
14 changes: 11 additions & 3 deletions molecule_vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@
provider_name:
description:
- Name of the Vagrant provider to use.
- If not set, let vagrant choose the provider and settings are done for virtualbox.
required: False
default: virtualbox
default: None
provider_memory:
description:
- Amount of memory to allocate to the instance.
Expand Down Expand Up @@ -348,6 +349,10 @@ def __init__(self, module):
self.provision = self._module.params["provision"]
self.cachier = self._module.params["cachier"]
self.provider_name = self._module.params["provider_name"]
# Keep historic behaviour and use virtualbox as "default".
# It's important in order to be able to set vm memory/cpus.
if self.provider_name is None:
self.provider_name = "virtualbox"

# compat
if self._module.params["instance_name"] is not None:
Expand Down Expand Up @@ -439,7 +444,10 @@ def up(self):
changed = True
provision = self.provision
try:
self._vagrant.up(provision=provision)
if self._module.params["provider_name"] is None:
self._vagrant.up(provision=provision)
else:
self._vagrant.up(provider=self.provider_name, provision=provision)
except Exception:
# NOTE(retr0h): Ignore the exception since python-vagrant
# passes the actual error as a no-argument ContextManager.
Expand Down Expand Up @@ -702,7 +710,7 @@ def main():
provider_options=dict(type="dict", default={}),
provider_override_args=dict(type="list", default=None),
provider_raw_config_args=dict(type="list", default=None),
provider_name=dict(type="str", default="virtualbox"),
provider_name=dict(type="str", default=None),
default_box=dict(type="str", default=None),
provision=dict(type="bool", default=False),
force_stop=dict(type="bool", default=False),
Expand Down
13 changes: 10 additions & 3 deletions molecule_vagrant/test/functional/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,25 @@ def test_command_init_scenario(temp_dir):
assert result.returncode == 0


def test_invalide_settings(temp_dir):
@pytest.mark.parametrize(
"scenario,check",
[
("invalid", "Failed to validate generated Vagrantfile"),
("invalid_provider", "The provider 'vmware' could not be found"),
],
)
def test_invalide_settings(temp_dir, scenario, check):

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"]
cmd = ["molecule", "create", "--scenario-name", scenario]
result = run_command(cmd)
assert result.returncode == 2

assert "Failed to validate generated Vagrantfile" in result.stdout
assert check in result.stdout


def patch_molecule(molecule_yaml):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Converge
hosts: all
gather_facts: false
become: true
tasks:
- name: Sample task # noqa command-instead-of-shell
ansible.builtin.shell:
cmd: uname
warn: false
changed_when: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
dependency:
name: galaxy
driver:
name: vagrant
provider:
name: vmware
force_provider: true
platforms:
- name: instance
provisioner:
name: ansible

0 comments on commit 4b325ee

Please sign in to comment.