Skip to content

Fixes non-virtualbox provider #113

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Mar 30, 2022
Merged
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
18 changes: 16 additions & 2 deletions tests/test_vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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"
)
Expand Down Expand Up @@ -459,7 +473,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():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion vagrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down