From 5607428a66cb6ba5c4ef4c520045f4359aecf27e Mon Sep 17 00:00:00 2001 From: sophia Date: Fri, 28 Aug 2020 14:42:37 -0500 Subject: [PATCH] Don't try to recover machine without a uuid --- lib/vagrant/machine_index.rb | 2 +- lib/vagrant/plugin/v2/command.rb | 3 +- test/unit/vagrant/plugin/v2/command_test.rb | 35 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/machine_index.rb b/lib/vagrant/machine_index.rb index 047650a76..02fd13db9 100644 --- a/lib/vagrant/machine_index.rb +++ b/lib/vagrant/machine_index.rb @@ -263,7 +263,7 @@ module Vagrant # # @return [Hash] def find_by_prefix(prefix) - return nil if prefix.nil? + return if !prefix @machines.each do |uuid, data| return data.merge("id" => uuid) if uuid.start_with?(prefix) end diff --git a/lib/vagrant/plugin/v2/command.rb b/lib/vagrant/plugin/v2/command.rb index a338b7902..e158ed2e4 100644 --- a/lib/vagrant/plugin/v2/command.rb +++ b/lib/vagrant/plugin/v2/command.rb @@ -230,7 +230,8 @@ module Vagrant color_index = 0 machines.each do |machine| - if machine.state && machine.state.id != :not_created && !@env.machine_index.include?(machine.index_uuid) + if (machine.state && machine.state.id != :not_created && + !machine.index_uuid.nil? && !@env.machine_index.include?(machine.index_uuid)) machine.recover_machine(machine.state.id) end diff --git a/test/unit/vagrant/plugin/v2/command_test.rb b/test/unit/vagrant/plugin/v2/command_test.rb index add43f963..72d77b7a6 100644 --- a/test/unit/vagrant/plugin/v2/command_test.rb +++ b/test/unit/vagrant/plugin/v2/command_test.rb @@ -1,5 +1,6 @@ require File.expand_path("../../../../base", __FILE__) require 'optparse' +require 'vagrant/machine_index' describe Vagrant::Plugin::V2::Command do include_context "unit" @@ -151,6 +152,40 @@ describe Vagrant::Plugin::V2::Command do instance.with_target_vms("foo") { |vm| vms << vm } end + it "does not recover the vm if it has no uuid" do + foo_vm = double("foo") + provider = :foobarbaz + state_id = :some_state + allow(foo_vm).to receive(:name).and_return("foo") + allow(foo_vm).to receive(:provider).and_return(provider) + allow(foo_vm).to receive(:ui).and_return(Vagrant::UI::Silent.new) + allow(foo_vm).to receive(:state).and_return(double("state", id: state_id)) + allow(foo_vm).to receive(:index_uuid).and_return(nil) + allow(environment).to receive(:machine).with(:foo, provider).and_return(foo_vm) + + expect(foo_vm).not_to receive(:recover_machine).with(state_id) + vms = [] + instance.with_target_vms("foo", provider: provider) { |vm| vms << vm } + expect(vms).to eq([foo_vm]) + end + + it "recovers the vm" do + foo_vm = double("foo") + provider = :foobarbaz + state_id = :some_state + allow(foo_vm).to receive(:name).and_return("foo") + allow(foo_vm).to receive(:provider).and_return(provider) + allow(foo_vm).to receive(:ui).and_return(Vagrant::UI::Silent.new) + allow(foo_vm).to receive(:state).and_return(double("state", id: state_id)) + allow(foo_vm).to receive(:index_uuid).and_return("someuuid") + allow(environment).to receive(:machine).with(:foo, provider).and_return(foo_vm) + + expect(foo_vm).to receive(:recover_machine).with(state_id) + vms = [] + instance.with_target_vms("foo", provider: provider) { |vm| vms << vm } + expect(vms).to eq([foo_vm]) + end + it "yields the given VM with proper provider if given" do foo_vm = double("foo") provider = :foobarbaz