From 0b1ef52bd9ccb85728ff4daa54b00c717ab41ee2 Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Wed, 5 Jan 2022 11:23:44 +0100 Subject: [PATCH] libvirt: automatic fallback to qemu (#158) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea Co-authored-by: anadahz Co-authored-by: Gonéri Le Bouder --- molecule_vagrant/modules/vagrant.py | 8 ++++++++ molecule_vagrant/test/functional/test_func.py | 15 ++++----------- .../molecule/config_options/molecule.yml | 2 -- .../scenarios/molecule/multi-node/molecule.yml | 2 -- .../test/scenarios/molecule/network/molecule.yml | 1 - .../molecule/provider_config_options/molecule.yml | 1 - .../scenarios/molecule/vagrant_root/molecule.yml | 2 -- 7 files changed, 12 insertions(+), 19 deletions(-) diff --git a/molecule_vagrant/modules/vagrant.py b/molecule_vagrant/modules/vagrant.py index 7ac94a5..5a48736 100644 --- a/molecule_vagrant/modules/vagrant.py +++ b/molecule_vagrant/modules/vagrant.py @@ -285,7 +285,14 @@ {% endif %} {% endif %} {% if instance.provider == 'libvirt' %} + {% if no_kvm is sameas true and 'driver' not in instance.provider_options %} + libvirt.driver='qemu' + {% endif %} + {% set libvirt_use_qemu = no_kvm %} {% if 'driver' in instance.provider_options and 'qemu' in instance.provider_options['driver'] %} + {% set libvirt_use_qemu = true %} + {% endif %} + {% if libvirt_use_qemu is sameas true %} {% if 'cpu_mode' not in instance.provider_options %} # When using qemu instead of kvm, some libvirt systems # will use EPYC as vCPU model inside the new VM. @@ -543,6 +550,7 @@ def _write_vagrantfile(self): VAGRANTFILE_TEMPLATE, instances=instances, cachier=self.cachier, + no_kvm=not os.path.exists("/dev/kvm"), ) molecule.util.write_file(self._vagrantfile, template) diff --git a/molecule_vagrant/test/functional/test_func.py b/molecule_vagrant/test/functional/test_func.py index c77d457..deb9f9b 100644 --- a/molecule_vagrant/test/functional/test_func.py +++ b/molecule_vagrant/test/functional/test_func.py @@ -21,6 +21,7 @@ import pytest import os +import vagrant from molecule import util from molecule import logger @@ -53,10 +54,10 @@ def test_command_init_scenario(temp_dir): env = os.environ if "TESTBOX" in env: conf["platforms"][0]["box"] = env["TESTBOX"] - if not os.path.exists("/dev/kvm"): + if "vagrant-libvirt" in list( + map(lambda x: x.name, vagrant.Vagrant().plugin_list()) + ): conf["driver"]["provider"] = {"name": "libvirt"} - for p in conf["platforms"]: - p["provider_options"] = {"driver": '"qemu"'} util.write_file(confpath, util.safe_dump(conf)) cmd = ["molecule", "--debug", "test", "-s", "test-scenario"] @@ -77,10 +78,6 @@ def test_command_init_scenario(temp_dir): ) def test_vagrant_root(temp_dir, scenario): - env = os.environ - if not os.path.exists("/dev/kvm"): - env.update({"VIRT_DRIVER": "'qemu'"}) - scenario_directory = os.path.join( os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios" ) @@ -93,10 +90,6 @@ def test_vagrant_root(temp_dir, scenario): def test_multi_node(temp_dir): - env = os.environ - if not os.path.exists("/dev/kvm"): - env.update({"VIRT_DRIVER": "'qemu'"}) - scenario_directory = os.path.join( os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios" ) diff --git a/molecule_vagrant/test/scenarios/molecule/config_options/molecule.yml b/molecule_vagrant/test/scenarios/molecule/config_options/molecule.yml index ce98961..6e66a31 100644 --- a/molecule_vagrant/test/scenarios/molecule/config_options/molecule.yml +++ b/molecule_vagrant/test/scenarios/molecule/config_options/molecule.yml @@ -9,8 +9,6 @@ platforms: - name: instance config_options: synced_folder: true - provider_options: - driver: ${VIRT_DRIVER:-kvm} box: ${TESTBOX:-centos/7} instance_raw_config_args: - 'vm.synced_folder ".", "/vagrant", type: "rsync"' diff --git a/molecule_vagrant/test/scenarios/molecule/multi-node/molecule.yml b/molecule_vagrant/test/scenarios/molecule/multi-node/molecule.yml index 320ea56..aa3c31d 100644 --- a/molecule_vagrant/test/scenarios/molecule/multi-node/molecule.yml +++ b/molecule_vagrant/test/scenarios/molecule/multi-node/molecule.yml @@ -17,7 +17,6 @@ platforms: memory: 256 cpus: 1 provider_options: - driver: ${VIRT_DRIVER:-kvm} # using session with network leads to troubles qemu_use_session: false config_options: @@ -35,7 +34,6 @@ platforms: memory: 256 cpus: 2 provider_options: - driver: ${VIRT_DRIVER:-kvm} # using session with network leads to troubles qemu_use_session: false instance_raw_config_args: diff --git a/molecule_vagrant/test/scenarios/molecule/network/molecule.yml b/molecule_vagrant/test/scenarios/molecule/network/molecule.yml index 6265823..beda094 100644 --- a/molecule_vagrant/test/scenarios/molecule/network/molecule.yml +++ b/molecule_vagrant/test/scenarios/molecule/network/molecule.yml @@ -9,7 +9,6 @@ platforms: - name: instance box: ${TESTBOX:-centos/7} provider_options: - driver: ${VIRT_DRIVER:-kvm} # using session with network leads to troubles qemu_use_session: false interfaces: diff --git a/molecule_vagrant/test/scenarios/molecule/provider_config_options/molecule.yml b/molecule_vagrant/test/scenarios/molecule/provider_config_options/molecule.yml index 028e82c..a70e6af 100644 --- a/molecule_vagrant/test/scenarios/molecule/provider_config_options/molecule.yml +++ b/molecule_vagrant/test/scenarios/molecule/provider_config_options/molecule.yml @@ -9,7 +9,6 @@ platforms: - name: instance provider_options: nic_model_type: e1000 - driver: ${VIRT_DRIVER:-kvm} box: ${TESTBOX:-centos/7} provisioner: name: ansible diff --git a/molecule_vagrant/test/scenarios/molecule/vagrant_root/molecule.yml b/molecule_vagrant/test/scenarios/molecule/vagrant_root/molecule.yml index 5c8689e..92540b9 100644 --- a/molecule_vagrant/test/scenarios/molecule/vagrant_root/molecule.yml +++ b/molecule_vagrant/test/scenarios/molecule/vagrant_root/molecule.yml @@ -9,8 +9,6 @@ driver: platforms: - name: instance box: ${TESTBOX:-centos/7} - provider_options: - driver: ${VIRT_DRIVER:-kvm} instance_raw_config_args: - "vm.provision :shell, inline: \"echo #{Dir.pwd} > /tmp/workdir\"" provisioner: