diff --git a/plugins/commands/snapshot/command/save.rb b/plugins/commands/snapshot/command/save.rb index e380c228e..7ec71f9c6 100644 --- a/plugins/commands/snapshot/command/save.rb +++ b/plugins/commands/snapshot/command/save.rb @@ -31,7 +31,15 @@ module VagrantPlugins help: opts.help.chomp end - name = argv.pop + # 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 + with_target_vms(argv) do |vm| if !vm.provider.capability?(:snapshot_list) raise Vagrant::Errors::SnapshotNotSupported diff --git a/test/unit/plugins/commands/snapshot/command/save_test.rb b/test/unit/plugins/commands/snapshot/command/save_test.rb index 3ed2f1ffb..942102c6b 100644 --- a/test/unit/plugins/commands/snapshot/command/save_test.rb +++ b/test/unit/plugins/commands/snapshot/command/save_test.rb @@ -76,6 +76,29 @@ describe VagrantPlugins::CommandSnapshot::Command::Save do end end + context "with a snapshot guest and name given" do + let(:argv) { ["foo", "backup"] } + it "calls snapshot_save with a snapshot name" do + machine.id = "foo" + + expect(machine).to receive(:action) do |name, opts| + expect(name).to eq(:snapshot_save) + expect(opts[:snapshot_name]).to eq("backup") + end + + expect(subject.execute).to eq(0) + end + + 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 + end + context "with a duplicate snapshot name given and no force flag" do let(:argv) { ["test"] }