Test has capability

This commit is contained in:
sophia 2021-08-27 16:19:42 -05:00 committed by Paul Hinze
parent 04222f0eb6
commit 434e613856
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 62 additions and 1 deletions

View File

@ -102,7 +102,7 @@ module VagrantPlugins
ServiceInfo.with_info(ctx) do |info|
cap_name = req.args.detect { |a|
a.type == "hashicorp.vagrant.sdk.Args.NamedCapability"
}&.value&.value.strip.gsub("\b", "")
}&.value&.value.strip.gsub("\b", "").gsub("\x05", "")
plugin_name = info.plugin_name
LOGGER.debug("checking for #{cap_name} capability in #{plugin_name}")

View File

@ -112,4 +112,65 @@ describe VagrantPlugins::CommandServe::Service::GuestService do
expect(d.detected).to be false
end
end
context "requesting has capability" do
before do
cap_guest = Class.new(Vagrant.plugin("2", :guest)) do
def detect?(machine)
true
end
end
register_plugin do |p|
p.guest(:cap_guest) { cap_guest }
p.guest_capability(:cap_guest, :mycap) do
"this is a capability"
end
end
end
let(:named_cap_request){
Hashicorp::Vagrant::Sdk::FuncSpec::Args.new(
args: [
Hashicorp::Vagrant::Sdk::FuncSpec::Value.new(
name: "",
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
value: Google::Protobuf::Any.pack(Hashicorp::Vagrant::Sdk::Args::NamedCapability.new(Capability:"mycap")))
]
)
}
let(:named_cap_bad_request){
Hashicorp::Vagrant::Sdk::FuncSpec::Args.new(
args: [
Hashicorp::Vagrant::Sdk::FuncSpec::Value.new(
name: "",
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
value: Google::Protobuf::Any.pack(Hashicorp::Vagrant::Sdk::Args::NamedCapability.new(Capability:"notacapability")))
]
)
}
it "generates a spec" do
spec = subject.has_capability_spec
expect(spec).not_to be_nil
end
it "raises an error for unknown plugins" do
ctx = DummyContext.new("idontexisthahaha")
expect { subject.has_capability(test_cap_name, ctx) }.to raise_error
end
it "returns true for plugin with capability" do
ctx = DummyContext.new("cap_guest")
d = subject.has_capability(named_cap_request, ctx)
expect(d.has_capability).to be true
end
it "returns false for plugin without capability" do
ctx = DummyContext.new("cap_guest")
d = subject.has_capability(named_cap_bad_request, ctx)
expect(d.has_capability).to be false
end
end
end