Merge pull request #11863 from soapy1/include-index

Include index
This commit is contained in:
Sophia Castellarin 2020-09-15 15:47:35 -05:00 committed by GitHub
commit 6e8be9f9ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 1 deletions

View File

@ -263,6 +263,7 @@ module Vagrant
#
# @return [Hash]
def find_by_prefix(prefix)
return if !prefix
@machines.each do |uuid, data|
return data.merge("id" => uuid) if uuid.start_with?(prefix)
end

View File

@ -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

View File

@ -105,6 +105,7 @@ describe Vagrant::MachineIndex do
it "returns nil if the machine doesn't exist" do
expect(subject.get("foo")).to be_nil
expect(subject.get(nil)).to be_nil
end
it "returns a valid entry if the machine exists" do
@ -139,6 +140,11 @@ describe Vagrant::MachineIndex do
expect(subject.include?("b")).to be(true)
end
it "should return false if given nil input" do
expect(subject.include?(nil)).to be(false)
end
it "locks the entry so subsequent gets fail" do
result = subject.get("bar")
expect(result).to_not be_nil

View File

@ -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