Get parents from guest plugin

This commit is contained in:
sophia 2021-08-27 10:33:45 -05:00 committed by Paul Hinze
parent 493343abb6
commit 7477455380
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
4 changed files with 31 additions and 16 deletions

View File

@ -60,6 +60,7 @@ module Vagrant
#
# @return [Symbol]
def name
# TODO: this can probably be deleted
client.parents[0]
end

View File

@ -17,6 +17,25 @@ module Vagrant
def detect?(machine)
false
end
# Returns list of parents for
# this guest
#
# @return [Array<Symbol>]
def parents
guests = Vagrant.plugin("2").manager.guests.to_hash
ancestors = []
n, entry = guests.detect { |_, v| v.first == self.class }
while n
n = nil
if entry.last
ancestors << entry.last
entry = guests[entry.last]
n = entry.last
end
end
ancestors
end
end
end
end

View File

@ -64,26 +64,15 @@ module VagrantPlugins
)
end
def get_parent_chain(plugin_name)
chain = [plugin_name]
plugins = Vagrant.plugin("2").manager.guests[plugin_name.to_s.to_sym].to_a
if plugins.last.nil?
return chain
else
chain + get_parent_chain(plugins.last)
end
end
def parents(req, ctx)
ServiceInfo.with_info(ctx) do |info|
plugin_name = info.plugin_name
plugin = Vagrant.plugin("2").manager.guests[plugin_name.to_s.to_sym].to_a
if !plugin.first
plugin = Vagrant.plugin("2").manager.guests[plugin_name.to_s.to_sym].to_a.first
if !plugin
raise "Failed to locate guest plugin for: #{plugin_name.inspect}"
end
guest_chain = get_parent_chain(plugin_name.to_s.to_sym)
SDK::Platform::ParentsResp.new(
parents: guest_chain
parents: plugin.new.parents
)
end
end

View File

@ -1,6 +1,7 @@
require File.expand_path("../../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/commands/serve/command")
require Vagrant.source_root.join("plugins/commands/serve/broker").to_s
class DummyContext
attr_reader :metadata
@ -34,6 +35,12 @@ describe VagrantPlugins::CommandServe::Service::GuestService do
subject { described_class.new(broker: broker) }
before(:each) do
parent_test_guest = Class.new(Vagrant.plugin("2", :guest)) do
def detect?(machine)
true
end
end
test_guest = Class.new(Vagrant.plugin("2", :guest)) do
def detect?(machine)
true
@ -41,6 +48,7 @@ describe VagrantPlugins::CommandServe::Service::GuestService do
end
register_plugin do |p|
p.guest(:parent_test) { parent_test_guest }
p.guest(:test, :parent_test) { test_guest }
end
end
@ -60,9 +68,7 @@ describe VagrantPlugins::CommandServe::Service::GuestService do
ctx = DummyContext.new("test")
parents = subject.parents("", ctx)
expect(parents).not_to be_nil
expect(parents.parents).to include("test")
expect(parents.parents).to include("parent_test")
end
end