Fixes #11139: Skip machines that haven't been created for snapshot save

This commit fixes the original #11027 fix, which assumed that the
hyper-v provider just wasn't properly getting a VM id when it listed
snapshots. In reality, it was just that if you invoke the
`with_target_vm` method with no arguments, it runs on the entire environment.
This meant that the capability snapshot_list attempted to be invoked on
machines that didn't exist yet, which is the original cause for why the
list_snapshot method did not recieve a vm ID. This commit fixes that by
simply skipping the machine if it does not yet exist.
This commit is contained in:
Brian Cain 2019-10-24 13:33:25 -07:00
parent f6503462f6
commit f998e535ce
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
3 changed files with 16 additions and 10 deletions

View File

@ -15,6 +15,9 @@ module VagrantPlugins
o.separator "can be restored via `vagrant snapshot restore` at any point in the"
o.separator "future to get back to this exact machine state."
o.separator ""
o.separator "If no vm-name is given, Vagrant will take a snapshot of"
o.separator "the entire environment with the same snapshot name."
o.separator ""
o.separator "Snapshots are useful for experimenting in a machine and being able"
o.separator "to rollback quickly."
@ -31,20 +34,22 @@ module VagrantPlugins
help: opts.help.chomp
end
# If no snapshot name is given, the backup name is the same as the machine name.
# If there is a name given, we need to remove it and save it as `name`. Otherwise
# `with_target_vms` will treat the snapshot name as a guest name.
if argv.size < 2
name = argv.first
else
name = argv.pop
end
name = argv.pop
with_target_vms(argv) do |vm|
if !vm.provider.capability?(:snapshot_list)
raise Vagrant::Errors::SnapshotNotSupported
end
# In this case, no vm name was given, and we are iterating over the
# entire environment. If a vm hasn't been created yet, we can't list
# its snapshots
if vm.id.nil?
@env.ui.warn(I18n.t("vagrant.commands.snapshot.save.vm_not_created",
name: vm.name))
next
end
snapshot_list = vm.provider.capability(:snapshot_list)
if !snapshot_list.include? name

View File

@ -2053,6 +2053,9 @@ en:
No pushed snapshot found!
Use `vagrant snapshot push` to push a snapshot to restore to.
save:
vm_not_created: |-
Machine '%{name}' has not been created yet, and therefore cannot save snapshots. Skipping...
status:
aborted: |-
The VM is in an aborted state. This means that it was abruptly

View File

@ -92,8 +92,6 @@ describe VagrantPlugins::CommandSnapshot::Command::Save do
it "doesn't snapshot a non-existent machine" do
machine.id = nil
expect(subject).to receive(:with_target_vms){}
expect(machine).to_not receive(:action)
expect(subject.execute).to eq(0)
end