From 0e2ddc7033e60f876e2eee49fce0ec963e942ae1 Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Wed, 30 Mar 2022 15:22:12 +0200 Subject: [PATCH 1/3] tests/test_vagrant.py: test_boxesvm: Fix hardcoded provider The function is hardcoding the provider name while there's a variable for that. So use it. Signed-off-by: Arnaud Patard --- tests/test_vagrant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py index c9788cc..f9897c7 100644 --- a/tests/test_vagrant.py +++ b/tests/test_vagrant.py @@ -459,7 +459,7 @@ def test_boxesvm(test_dir): """ v = vagrant.Vagrant(test_dir) box_name = "python-vagrant-dummy-box" - provider = "virtualbox" + provider = f"{TEST_PROVIDER}" # Start fresh with no dummy box if box_name in list_box_names(): From e21a3496c54105ccde0e9258d778ef15f3063187 Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Wed, 30 Mar 2022 15:23:09 +0200 Subject: [PATCH 2/3] vagrant/__init__.py: Normalize status between providers The machine status returned by vagrant may change between providers so add code to deal with the differences. For now, it's coded inside a function but can be replaced with a dict() if more differences are found. Signed-off-by: Arnaud Patard --- vagrant/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/vagrant/__init__.py b/vagrant/__init__.py index 9a8dcd4..23d1afb 100644 --- a/vagrant/__init__.py +++ b/vagrant/__init__.py @@ -524,6 +524,22 @@ def status(self, vm_name=None): output = self._run_vagrant_command(["status", "--machine-readable", vm_name]) return self._parse_status(output) + def _normalize_status(self, status, provider): + """ + Normalise VM status to cope with state name being different + between providers + """ + if provider == "virtualbox": + return status + + if provider == "libvirt": + if status == "shutoff": + return self.POWEROFF + if status == "paused": + return self.SAVED + + return status + def _parse_status(self, output): """ Unit testing is so much easier when Vagrant is removed from the @@ -537,8 +553,9 @@ def _parse_status(self, output): for target, tuples in itertools.groupby(parsed, lambda tup: tup[1]): # transform tuples into a dict mapping "type" to "data" info = {kind: data for timestamp, _, kind, data in tuples} + state = self._normalize_status(info.get("state"), info.get("provider-name")) status = Status( - name=target, state=info.get("state"), provider=info.get("provider-name") + name=target, state=state, provider=info.get("provider-name") ) statuses.append(status) From 59df8fdf9851b2e4a5a54552732106fd41a75be0 Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Wed, 30 Mar 2022 16:09:14 +0200 Subject: [PATCH 3/3] tests/test_vagrant.py: Allow to set provider from env variables Current provider is hardcoded inside the test file. We may want to switch easily between test providers (local testing, using something else than GHA, whatever...) so add a new function used to return the current test provider. Changing the test provider is done by setting an environment variable since any code / logic to detect the provider to use will always be broken. Signed-off-by: Arnaud Patard --- tests/test_vagrant.py | 16 +++++++++++++++- tox.ini | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py index f9897c7..d131a02 100644 --- a/tests/test_vagrant.py +++ b/tests/test_vagrant.py @@ -30,6 +30,20 @@ import vagrant from vagrant import compat + +# must be defined before TEST_PROVIDER. +def get_provider() -> str: + """ + Return the provider to use for testing and allow to set it + with PYTHON_VAGRANT_TEST_PROVIDER environment variable is set. + Defauts to virtualbox + """ + my_prov = "virtualbox" + if "PYTHON_VAGRANT_TEST_PROVIDER" in os.environ: + my_prov = os.environ["PYTHON_VAGRANT_TEST_PROVIDER"] + return my_prov + + # location of Vagrant executable VAGRANT_EXE = vagrant.get_vagrant_executable() @@ -53,7 +67,7 @@ # name of the base box used for testing TEST_BOX_URL = "generic/alpine315" TEST_BOX_NAME = TEST_BOX_URL -TEST_PROVIDER = "virtualbox" +TEST_PROVIDER = get_provider() TEST_DUMMY_BOX_URL = os.path.join( os.path.dirname(os.path.abspath(__file__)), "tools", f"dummy-{TEST_PROVIDER}.box" ) diff --git a/tox.ini b/tox.ini index e7fb963..a364fe9 100644 --- a/tox.ini +++ b/tox.ini @@ -26,6 +26,7 @@ commands = --no-cov-on-fail \ } passenv = + PYTHON_VAGRANT_TEST_PROVIDER # Pass HOME to the test environment as it is required by # vagrant. Otherwise error happens due to missing HOME env variable. HOME