Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for --no-destroy-on-error in up() #129

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
TOX_PARALLEL_NO_SPINNER: 1
TOXENV: ${{ matrix.tox_env }}
FORCE_COLOR: 1
PYTEST_REQPASS: 24
PYTEST_REQPASS: 25

steps:
- name: Check vagrant presence
Expand Down
11 changes: 9 additions & 2 deletions src/vagrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,15 @@ def init(self, box_name=None, box_url=None) -> None:
"""
self._call_vagrant_command(["init", box_name, box_url])

def up(
def up( # pylint: disable-msg=too-many-locals
self,
vm_name=None,
no_provision=False,
provider=None,
provision=None,
provision_with=None,
stream_output=False,
destroy_on_error=True,
) -> Optional[Iterator[str]]:
"""
Invoke `vagrant up` to start a box or boxes, possibly streaming the
Expand All @@ -332,6 +333,8 @@ def up(
subprocess might hang. if False, None is returned and the command
is run to completion without streaming the output. Defaults to
False.
destroy_on_error: if True, the newly created will be destroyed if there
was any error in the provisioning.
Note: If provision and no_provision are not None, no_provision will be
ignored.
returns: None or a generator yielding lines of output.
Expand All @@ -352,7 +355,10 @@ def up(
if provision
else "--no-provision"
)

if destroy_on_error:
destroy_on_error_arg = "--destroy-on-error"
else:
destroy_on_error_arg = "--no-destroy-on-error"
args = [
"up",
vm_name,
Expand All @@ -361,6 +367,7 @@ def up(
provider_arg,
prov_with_arg,
providers_arg,
destroy_on_error_arg,
]
if stream_output:
generator = self._stream_vagrant_command(args)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def get_provider() -> str:
"vagrantfiles",
"shell_provision_Vagrantfile",
)
SHELL_PROVISION_FAIL_VAGRANTFILE = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"vagrantfiles",
"shell_provision_fail_Vagrantfile",
)
# the names of the vms from the multi-vm Vagrantfile.
VM_1 = "web"
VM_2 = "db"
Expand Down Expand Up @@ -604,6 +609,25 @@ def test_provisioning(vm_dir):
assert test_file_contents == "foo", "The test file should contain 'foo'"


@pytest.mark.parametrize("vm_dir", (SHELL_PROVISION_FAIL_VAGRANTFILE,), indirect=True)
def test_provisioning_no_destory_on_error(vm_dir):
eighthave marked this conversation as resolved.
Show resolved Hide resolved
"""
Test support for 'vagrant up --no-destroy-on-error'.
"""
v = vagrant.Vagrant(vm_dir)
try:
v.up(destroy_on_error=True)
except subprocess.CalledProcessError:
pass
assert v.status()[0].state == v.NOT_CREATED

try:
v.up(destroy_on_error=False)
except subprocess.CalledProcessError:
pass
assert v.status()[0].state == v.RUNNING


@pytest.mark.parametrize("vm_dir", (MULTIVM_VAGRANTFILE,), indirect=True)
def test_multivm_lifecycle(vm_dir):
v = vagrant.Vagrant(vm_dir)
Expand Down
5 changes: 5 additions & 0 deletions tests/vagrantfiles/shell_provision_fail_Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Vagrant.configure("2") do |config|
config.vbguest.auto_update = false if Vagrant.has_plugin?("vagrant-vbguest")
config.vm.box = "generic/alpine315"
config.vm.provision :shell, :inline => "exit 1"
end