Finish up test coverage and cleanup
This commit is contained in:
parent
f4420f6b81
commit
0d9ad25d69
|
@ -9,6 +9,7 @@ module Vagrant
|
|||
autoload :CapabilityPlugin, "vagrant/go_plugin/capability_plugin"
|
||||
autoload :ConfigPlugin, "vagrant/go_plugin/config_plugin"
|
||||
autoload :Core, "vagrant/go_plugin/core"
|
||||
autoload :GRPCPlugin, "vagrant/go_plugin/core"
|
||||
autoload :Interface, "vagrant/go_plugin/interface"
|
||||
autoload :Manager, "vagrant/go_plugin/manager"
|
||||
autoload :ProviderPlugin, "vagrant/go_plugin/provider_plugin"
|
||||
|
|
|
@ -24,14 +24,21 @@ module Vagrant
|
|||
logger.debug("guest capabilities support detected in #{plugin_type} plugin #{plugin_klass}")
|
||||
result.capabilities.each do |cap|
|
||||
cap_klass = Class.new(Capability).tap do |k|
|
||||
k.class_eval("def self.#{cap.name}(machine, *args){ plugin_client.guest_capability(" \
|
||||
"Vagrant::Proto::GuestCapabilityRequest.new(" \
|
||||
"machine: JSON.dump(machine), arguments: JSON.dump(args)," \
|
||||
"capability: Vagrant::Proto::SystemCapability.new(" \
|
||||
"name: '#{cap.name}', platform: '#{cap.platform}'))) }")
|
||||
k.define_singleton_method(cap.name) { |machine, *args|
|
||||
response = plugin_client.guest_capability(
|
||||
Vagrant::Proto::GuestCapabilityRequest.new(
|
||||
machine: JSON.dump(machine), arguments: JSON.dump(args),
|
||||
capability: Vagrant::Proto::SystemCapability.new(
|
||||
name: cap.name, platform: cap.platform))).result
|
||||
result = JSON.load(response)
|
||||
if result.is_a?(Hash)
|
||||
result = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
end
|
||||
result
|
||||
}
|
||||
end
|
||||
cap_klass.plugin_client = client
|
||||
plugin_klass.guest_capability(cap.platform, cap.name) { cap_klass }
|
||||
plugin_klass.guest_capability(cap.platform.to_sym, cap.name.to_sym) { cap_klass }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -48,14 +55,21 @@ module Vagrant
|
|||
logger.debug("host capabilities support detected in #{plugin_type} plugin #{plugin_klass}")
|
||||
result.capabilities.each do |cap|
|
||||
cap_klass = Class.new(Capability).tap do |k|
|
||||
k.class_eval("def self.#{cap.name}(environment, *args){ plugin_client.host_capability(" \
|
||||
"Vagrant::Proto::HostCapabilityRequest.new(" \
|
||||
"environment: JSON.dump(environment), arguments: JSON.dump(args)," \
|
||||
"capability: Vagrant::Proto::SystemCapability.new(" \
|
||||
"name: '#{cap.name}', platform: '#{cap.platform}'))) }")
|
||||
k.define_singleton_method(cap.name) { |environment, *args|
|
||||
response = plugin_client.host_capability(
|
||||
Vagrant::Proto::HostCapabilityRequest.new(
|
||||
environment: JSON.dump(environment), arguments: JSON.dump(args),
|
||||
capability: Vagrant::Proto::SystemCapability.new(
|
||||
name: cap.name, platform: cap.platform))).result
|
||||
result = JSON.load(response)
|
||||
if result.is_a?(Hash)
|
||||
result = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
end
|
||||
result
|
||||
}
|
||||
end
|
||||
cap_klass.plugin_client = client
|
||||
plugin_klass.host_capability(cap.platform, cap.name) { cap_klass }
|
||||
plugin_klass.host_capability(cap.platform.to_sym, cap.name.to_sym) { cap_klass }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -72,14 +86,21 @@ module Vagrant
|
|||
logger.debug("provider capabilities support detected in #{plugin_type} plugin #{plugin_klass}")
|
||||
result.capabilities.each do |cap|
|
||||
cap_klass = Class.new(Capability).tap do |k|
|
||||
k.class_eval("def self.#{cap.name}(machine, *args){ plugin_client.provider_capability(" \
|
||||
"Vagrant::Proto::ProviderCapabilityRequest.new(" \
|
||||
"machine: JSON.dump(machine), arguments: JSON.dump(args)," \
|
||||
"capability: Vagrant::Proto::ProviderCapability.new(" \
|
||||
"name: '#{cap.name}', provider: '#{cap.provider}'))) }")
|
||||
k.define_singleton_method(cap.name) { |machine, *args|
|
||||
response = plugin_client.provider_capability(
|
||||
Vagrant::Proto::ProviderCapabilityRequest.new(
|
||||
machine: JSON.dump(machine), arguments: JSON.dump(args),
|
||||
capability: Vagrant::Proto::ProviderCapability.new(
|
||||
name: cap.name, provider: cap.provider))).result
|
||||
result = JSON.load(response)
|
||||
if result.is_a?(Hash)
|
||||
result = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
end
|
||||
result
|
||||
}
|
||||
end
|
||||
cap_klass.plugin_client = client
|
||||
plugin_klass.provider_capability(cap.provider, cap.name) { cap_klass }
|
||||
plugin_klass.provider_capability(cap.provider.to_sym, cap.name.to_sym) { cap_klass }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,14 +27,17 @@ module Vagrant
|
|||
# Finalize the current configuration
|
||||
def finalize!
|
||||
data = local_data
|
||||
result = plugin_client.config_finalize(Vagrant::Proto::Configuration.new(
|
||||
response = plugin_client.config_finalize(Vagrant::Proto::Configuration.new(
|
||||
data: JSON.dump(data)))
|
||||
new_data = Vagrant::Util::HashWithIndifferentAccess.new(JSON.load(result.data))
|
||||
new_data.each do |key, value|
|
||||
next if data[key] == value
|
||||
instance_variable_set("@#{key}", value)
|
||||
if !self.respond_to?(key)
|
||||
self.define_singleton_method(key) { instance_variable_get("@#{key}") }
|
||||
result = JSON.load(response.data)
|
||||
if result && result.is_a?(Hash)
|
||||
new_data = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
new_data.each do |key, value|
|
||||
next if data[key] == value
|
||||
instance_variable_set("@#{key}", value)
|
||||
if !key.start_with?("_") && !self.respond_to?(key)
|
||||
self.define_singleton_method(key) { instance_variable_get("@#{key}") }
|
||||
end
|
||||
end
|
||||
end
|
||||
self
|
||||
|
@ -47,18 +50,14 @@ module Vagrant
|
|||
def validate(machine)
|
||||
result = plugin_client.config_validate(Vagrant::Proto::Configuration.new(
|
||||
machine: JSON.dump(machine),
|
||||
data: local_data))
|
||||
data: JSON.dump(local_data)))
|
||||
result.items
|
||||
end
|
||||
|
||||
# @return [Hash] currently defined instance variables
|
||||
def local_data
|
||||
data = Vagrant::Util::HashWithIndifferentAccess.
|
||||
Vagrant::Util::HashWithIndifferentAccess.
|
||||
new(instance_variables_hash)
|
||||
data.delete_if { |k,v|
|
||||
k.start_with?("_")
|
||||
}
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
require "vagrant/go_plugin/core"
|
||||
|
||||
module Vagrant
|
||||
module GoPlugin
|
||||
class ProviderClient
|
||||
end
|
||||
end
|
||||
end
|
|
@ -31,14 +31,21 @@ module Vagrant
|
|||
|
||||
# Run the action
|
||||
def call(env)
|
||||
result = plugin_client.run_action(
|
||||
if env.is_a?(Hash) && !env.is_a?(Vagrant::Util::HashWithIndifferentAccess)
|
||||
env = Vagrant::Util::HashWithIndifferentAccess.new(env)
|
||||
end
|
||||
machine = env.fetch(:machine, {})
|
||||
response = plugin_client.run_action(
|
||||
Vagrant::Proto::ExecuteAction.new(
|
||||
name: self.class.action_name,
|
||||
data: JSON.dump(env),
|
||||
machine: JSON.dump(machine)))
|
||||
response = JSON.load(result)
|
||||
response.each_pair do |k, v|
|
||||
env[k] = v
|
||||
result = JSON.load(response.result)
|
||||
if result.is_a?(Hash)
|
||||
result = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
result.each_pair do |k, v|
|
||||
env[k] = v
|
||||
end
|
||||
end
|
||||
@app.call(env)
|
||||
end
|
||||
|
@ -58,7 +65,7 @@ module Vagrant
|
|||
end
|
||||
|
||||
# @return [String] name of the provider plugin for this class
|
||||
def provider_name
|
||||
def name
|
||||
if !@_name
|
||||
@_name = plugin_client.name(Vagrant::Proto::Empty.new).name
|
||||
end
|
||||
|
@ -105,20 +112,24 @@ module Vagrant
|
|||
|
||||
# Execute capability with given name
|
||||
#
|
||||
# @param [Symbol] name Name of the capability
|
||||
# @param [Symbol] cap_name Name of the capability
|
||||
# @return [Object]
|
||||
def capability(name, *args)
|
||||
def capability(cap_name, *args)
|
||||
r = plugin_client.provider_capability(
|
||||
Vagrant::Proto::ProviderCapabilityRequest.new(
|
||||
capability: Vagrant::Proto::ProviderCapability.new(
|
||||
name: name.to_s,
|
||||
provider: provider_name
|
||||
name: cap_name.to_s,
|
||||
provider: name
|
||||
),
|
||||
machine: JSON.dump(machine),
|
||||
arguments: JSON.dump(args)
|
||||
)
|
||||
)
|
||||
JSON.load(r.result)
|
||||
result = JSON.load(r.result)
|
||||
if result.is_a?(Hash)
|
||||
result = Vagrant::Util::HashWithIndifferentAccess.new(result)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# @return [Boolean] provider is installed
|
||||
|
|
|
@ -49,6 +49,32 @@ describe Vagrant::GoPlugin::CapabilityPlugin do
|
|||
expect(plugin_klass).to receive(:guest_capability).with(:dummy, :other_cap)
|
||||
described_class.generate_guest_capabilities(client, plugin_klass, plugin_type)
|
||||
end
|
||||
|
||||
context "generated capability" do
|
||||
let(:cap_class) { Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability) }
|
||||
let(:response) { double(result: result) }
|
||||
let(:result) { nil }
|
||||
|
||||
before {
|
||||
expect(Class).to receive(:new).and_return(cap_class)
|
||||
allow(Class).to receive(:new).and_call_original
|
||||
described_class.generate_guest_capabilities(client, plugin_klass, plugin_type)
|
||||
}
|
||||
|
||||
it "should call guest_capability on the plugin client" do
|
||||
expect(client).to receive(:guest_capability).and_return(response)
|
||||
cap_class.stub_cap(nil, nil)
|
||||
end
|
||||
|
||||
context "when result is a hash" do
|
||||
let(:result) { "{}" }
|
||||
|
||||
it "should convert hash to hash with indifferent access" do
|
||||
expect(client).to receive(:guest_capability).and_return(response)
|
||||
expect(cap_class.stub_cap(nil, nil)).to be_a(Vagrant::Util::HashWithIndifferentAccess)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".generate_host_capabilities" do
|
||||
|
@ -91,6 +117,32 @@ describe Vagrant::GoPlugin::CapabilityPlugin do
|
|||
expect(plugin_klass).to receive(:host_capability).with(:dummy, :other_cap)
|
||||
described_class.generate_host_capabilities(client, plugin_klass, plugin_type)
|
||||
end
|
||||
|
||||
context "generated capability" do
|
||||
let(:cap_class) { Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability) }
|
||||
let(:response) { double(result: result) }
|
||||
let(:result) { nil }
|
||||
|
||||
before {
|
||||
expect(Class).to receive(:new).and_return(cap_class)
|
||||
allow(Class).to receive(:new).and_call_original
|
||||
described_class.generate_host_capabilities(client, plugin_klass, plugin_type)
|
||||
}
|
||||
|
||||
it "should call guest_capability on the plugin client" do
|
||||
expect(client).to receive(:host_capability).and_return(response)
|
||||
cap_class.stub_cap(nil, nil)
|
||||
end
|
||||
|
||||
context "when result is a hash" do
|
||||
let(:result) { "{}" }
|
||||
|
||||
it "should convert hash to hash with indifferent access" do
|
||||
expect(client).to receive(:host_capability).and_return(response)
|
||||
expect(cap_class.stub_cap(nil, nil)).to be_a(Vagrant::Util::HashWithIndifferentAccess)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".generate_provider_capabilities" do
|
||||
|
@ -133,5 +185,31 @@ describe Vagrant::GoPlugin::CapabilityPlugin do
|
|||
expect(plugin_klass).to receive(:provider_capability).with(:dummy, :other_cap)
|
||||
described_class.generate_provider_capabilities(client, plugin_klass, plugin_type)
|
||||
end
|
||||
|
||||
context "generated capability" do
|
||||
let(:cap_class) { Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability) }
|
||||
let(:response) { double(result: result) }
|
||||
let(:result) { nil }
|
||||
|
||||
before {
|
||||
expect(Class).to receive(:new).and_return(cap_class)
|
||||
allow(Class).to receive(:new).and_call_original
|
||||
described_class.generate_provider_capabilities(client, plugin_klass, plugin_type)
|
||||
}
|
||||
|
||||
it "should call guest_capability on the plugin client" do
|
||||
expect(client).to receive(:provider_capability).and_return(response)
|
||||
cap_class.stub_cap(nil, nil)
|
||||
end
|
||||
|
||||
context "when result is a hash" do
|
||||
let(:result) { "{}" }
|
||||
|
||||
it "should convert hash to hash with indifferent access" do
|
||||
expect(client).to receive(:provider_capability).and_return(response)
|
||||
expect(cap_class.stub_cap(nil, nil)).to be_a(Vagrant::Util::HashWithIndifferentAccess)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
require_relative "../../base"
|
||||
|
||||
describe Vagrant::GoPlugin::ConfigPlugin do
|
||||
let(:client) { double("client") }
|
||||
|
||||
describe ".generate_config" do
|
||||
let(:parent_name) { "test" }
|
||||
let(:parent_klass) { double("parent_klass") }
|
||||
let(:parent_type) { "dummy" }
|
||||
let(:attributes_response) {
|
||||
double("attributes_response",
|
||||
items: attributes)
|
||||
}
|
||||
let(:attributes) { [] }
|
||||
|
||||
before do
|
||||
allow(parent_klass).to receive(:config)
|
||||
allow(client).to receive(:config_attributes).
|
||||
and_return(attributes_response)
|
||||
end
|
||||
|
||||
it "should register a new config class" do
|
||||
expect(parent_klass).to receive(:config).
|
||||
with(parent_name, parent_type) { |&block|
|
||||
expect(block.call.ancestors).to include(Vagrant::GoPlugin::ConfigPlugin::Config)
|
||||
}
|
||||
described_class.generate_config(client, parent_name, parent_klass, parent_type)
|
||||
end
|
||||
|
||||
it "should register the client within the class" do
|
||||
expect(parent_klass).to receive(:config).
|
||||
with(parent_name, parent_type) { |&block|
|
||||
expect(block.call.plugin_client).to eq(client)
|
||||
}
|
||||
described_class.generate_config(client, parent_name, parent_klass, parent_type)
|
||||
end
|
||||
|
||||
context "when attributes are provided" do
|
||||
let(:attributes) { ["a_one", "a_two"] }
|
||||
|
||||
it "should add accessor instance methods to the class" do
|
||||
expect(parent_klass).to receive(:config).
|
||||
with(parent_name, parent_type) { |&block|
|
||||
expect(block.call.instance_methods).to include(:a_one)
|
||||
expect(block.call.instance_methods).to include(:a_two)
|
||||
}
|
||||
described_class.generate_config(client, parent_name, parent_klass, parent_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Vagrant::GoPlugin::ConfigPlugin::Config do
|
||||
let(:machine) { double("machine") }
|
||||
let(:subject) {
|
||||
c = Class.new(described_class)
|
||||
c.plugin_client = client
|
||||
c.new
|
||||
}
|
||||
|
||||
describe "#validate" do
|
||||
let(:response) { double("response", items: errors) }
|
||||
let(:errors) { ["errors"] }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:to_json).and_return("{}")
|
||||
allow(client).to receive(:config_validate).and_return(response)
|
||||
end
|
||||
|
||||
it "should call client to validate" do
|
||||
expect(client).to receive(:config_validate).
|
||||
with(instance_of(Vagrant::Proto::Configuration)).
|
||||
and_return(response)
|
||||
subject.validate(machine)
|
||||
end
|
||||
|
||||
it "should return list of validation errors" do
|
||||
expect(subject.validate(machine)).to eq(errors)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#finalize!" do
|
||||
let(:response) { double("response", data: result) }
|
||||
let(:result) { "null" }
|
||||
|
||||
before do
|
||||
allow(client).to receive(:config_finalize).and_return(response)
|
||||
end
|
||||
|
||||
it "should return the config instance" do
|
||||
expect(subject.finalize!).to eq(subject)
|
||||
end
|
||||
|
||||
it "should call client to finalize" do
|
||||
expect(client).to receive(:config_finalize).
|
||||
with(instance_of(Vagrant::Proto::Configuration)).
|
||||
and_return(response)
|
||||
subject.finalize!
|
||||
end
|
||||
|
||||
context "when configuration data is returned" do
|
||||
let(:result) {
|
||||
{attr1: true, attr2: "value"}.to_json
|
||||
}
|
||||
|
||||
it "should create accessor methods to configuration data" do
|
||||
subject.finalize!
|
||||
expect(subject).to respond_to(:attr1)
|
||||
expect(subject).to respond_to(:attr2)
|
||||
end
|
||||
|
||||
it "should return data from reader methods" do
|
||||
subject.finalize!
|
||||
expect(subject.attr1).to eq(true)
|
||||
expect(subject.attr2).to eq("value")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#local_data" do
|
||||
it "should return an empty hash" do
|
||||
expect(subject.local_data).to be_empty
|
||||
end
|
||||
|
||||
context "with config attributes set" do
|
||||
let(:response) { double("response", data: result) }
|
||||
let(:result) { {attr1: true, attr2: "value"}.to_json }
|
||||
|
||||
before do
|
||||
allow(client).to receive(:config_finalize).and_return(response)
|
||||
subject.finalize!
|
||||
end
|
||||
|
||||
it "should return data values" do
|
||||
result = subject.local_data
|
||||
expect(result[:attr1]).to eq(true)
|
||||
expect(result[:attr2]).to eq("value")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,57 +1,41 @@
|
|||
require_relative "../../base"
|
||||
|
||||
describe Vagrant::GoPlugin::Core do
|
||||
include_context "unit"
|
||||
describe Vagrant::GoPlugin::GRPCPlugin do
|
||||
let(:client) { double("client") }
|
||||
let(:subject) { Class.new.tap { |c| c.include(described_class) } }
|
||||
|
||||
let(:subject_class) { Class.new.tap { |c| c.include(described_class) } }
|
||||
let(:subject) { subject_class.new }
|
||||
describe ".plugin_client=" do
|
||||
it "should set the plugin client" do
|
||||
expect(subject.plugin_client = client).to eq(client)
|
||||
expect(subject.plugin_client).to eq(client)
|
||||
end
|
||||
|
||||
let(:name) { "foo" }
|
||||
let(:provider) { new_provider_mock }
|
||||
let(:provider_cls) do
|
||||
obj = double("provider_cls")
|
||||
allow(obj).to receive(:new).and_return(provider)
|
||||
obj
|
||||
end
|
||||
let(:provider_config) { Object.new }
|
||||
let(:provider_name) { :test }
|
||||
let(:provider_options) { {} }
|
||||
let(:base) { false }
|
||||
let(:box) do
|
||||
double("box",
|
||||
name: "foo",
|
||||
provider: :dummy,
|
||||
version: "1.0",
|
||||
directory: "box dir",
|
||||
metadata: nil,
|
||||
metadata_url: nil)
|
||||
it "should error if plugin is already set" do
|
||||
subject.plugin_client = client
|
||||
expect { subject.plugin_client = client }.
|
||||
to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
let(:config) { env.vagrantfile.config }
|
||||
let(:data_dir) { Pathname.new(Dir.mktmpdir("vagrant-machine-data-dir")) }
|
||||
let(:env) do
|
||||
# We need to create a Vagrantfile so that this test environment
|
||||
# has a proper root path
|
||||
test_env.vagrantfile("")
|
||||
describe ".plugin_client" do
|
||||
it "should return nil when client has not been set" do
|
||||
expect(subject.plugin_client).to be_nil
|
||||
end
|
||||
|
||||
# Create the Vagrant::Environment instance
|
||||
test_env.create_vagrant_env
|
||||
it "should return client when it has been set" do
|
||||
subject.plugin_client = client
|
||||
expect(subject.plugin_client).to eq(client)
|
||||
end
|
||||
end
|
||||
|
||||
let(:test_env) { isolated_environment }
|
||||
let(:machine) {
|
||||
Vagrant::Machine.new(name, provider_name, provider_cls, provider_config,
|
||||
provider_options, config, data_dir, box,
|
||||
env, env.vagrantfile, base)
|
||||
}
|
||||
describe "#plugin_client" do
|
||||
it "should be nil when client has not been set" do
|
||||
expect(subject.new.plugin_client).to be_nil
|
||||
end
|
||||
|
||||
def new_provider_mock
|
||||
double("provider").tap do |obj|
|
||||
allow(obj).to receive(:_initialize)
|
||||
.with(provider_name, anything).and_return(nil)
|
||||
allow(obj).to receive(:machine_id_changed).and_return(nil)
|
||||
allow(obj).to receive(:state).and_return(Vagrant::MachineState.new(
|
||||
:created, "", ""))
|
||||
it "should return client when client has been set" do
|
||||
subject.plugin_client = client
|
||||
expect(subject.new.plugin_client).to eq(client)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,273 @@
|
|||
require_relative "../../base"
|
||||
|
||||
describe Vagrant::GoPlugin::ProviderPlugin do
|
||||
let(:client) { double("client") }
|
||||
|
||||
describe Vagrant::GoPlugin::ProviderPlugin::Action do
|
||||
let(:described_class) { Class.new(Vagrant::GoPlugin::ProviderPlugin::Action) }
|
||||
|
||||
it "should be a GRPCPlugin" do
|
||||
expect(described_class.ancestors).to include(Vagrant::GoPlugin::GRPCPlugin)
|
||||
end
|
||||
|
||||
describe ".action_name" do
|
||||
it "should return nil when unset" do
|
||||
expect(described_class.action_name).to be_nil
|
||||
end
|
||||
|
||||
it "should return defined action name when set" do
|
||||
described_class.action_name = "test_action"
|
||||
expect(described_class.action_name).to eq("test_action")
|
||||
end
|
||||
end
|
||||
|
||||
describe ".action_name=" do
|
||||
it "should convert action name to string" do
|
||||
described_class.action_name = :test_action
|
||||
expect(described_class.action_name).to be_a(String)
|
||||
end
|
||||
|
||||
it "should set the action name" do
|
||||
described_class.action_name = "test_action"
|
||||
expect(described_class.action_name).to eq("test_action")
|
||||
end
|
||||
|
||||
it "should error if action name is already set" do
|
||||
described_class.action_name = "test_action"
|
||||
expect { described_class.action_name = "test_action" }.
|
||||
to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should freeze action name" do
|
||||
described_class.action_name = "test_action"
|
||||
expect(described_class.action_name).to be_frozen
|
||||
end
|
||||
end
|
||||
|
||||
describe "#call" do
|
||||
let(:app) { double("app") }
|
||||
let(:env) { {"key1" => "value1", "key2" => "value2"} }
|
||||
let(:response_env) {
|
||||
{"key1" => "value1", "key2" => "value2"}
|
||||
}
|
||||
let(:response) { double("response", result: response_env.to_json) }
|
||||
|
||||
let(:subject) {
|
||||
described_class.plugin_client = client
|
||||
described_class.new(app, {})
|
||||
}
|
||||
|
||||
before do
|
||||
described_class.action_name = "test_action"
|
||||
allow(app).to receive(:call)
|
||||
allow(client).to receive(:run_action).and_return(response)
|
||||
end
|
||||
|
||||
it "should call the next item in the middleware" do
|
||||
expect(app).to receive(:call)
|
||||
subject.call(env)
|
||||
end
|
||||
|
||||
it "should call the client" do
|
||||
expect(client).to receive(:run_action).and_return(response)
|
||||
subject.call(env)
|
||||
end
|
||||
|
||||
context "when new data is provided in result" do
|
||||
let(:response_env) { {"new_data" => "value"} }
|
||||
|
||||
it "should include new data when calling next action" do
|
||||
expect(app).to receive(:call).
|
||||
with(hash_including("new_data" => "value", "key1" => "value1"))
|
||||
subject.call(env)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Vagrant::GoPlugin::ProviderPlugin::Provider do
|
||||
let(:described_class) {
|
||||
Class.new(Vagrant::GoPlugin::ProviderPlugin::Provider).tap { |c|
|
||||
c.plugin_client = client
|
||||
}
|
||||
}
|
||||
let(:machine) { double("machine") }
|
||||
let(:subject) { described_class.new(machine) }
|
||||
|
||||
it "should be a GRPCPlugin" do
|
||||
expect(described_class.ancestors).to include(Vagrant::GoPlugin::GRPCPlugin)
|
||||
end
|
||||
|
||||
describe "#name" do
|
||||
let(:name) { double("name") }
|
||||
let(:response) { double("response", name: name) }
|
||||
|
||||
before { allow(client).to receive(:name).and_return(response) }
|
||||
|
||||
it "should request name from the client" do
|
||||
expect(client).to receive(:name).and_return(response)
|
||||
subject.name
|
||||
end
|
||||
|
||||
it "should return the plugin name" do
|
||||
expect(subject.name).to eq(name)
|
||||
end
|
||||
|
||||
it "should only call the client once" do
|
||||
expect(client).to receive(:name).once.and_return(response)
|
||||
subject.name
|
||||
end
|
||||
end
|
||||
|
||||
describe "#action" do
|
||||
let(:action_name) { "test_action" }
|
||||
let(:response) { double("response", items: actions) }
|
||||
let(:actions) { ["self::TestAction"] }
|
||||
|
||||
before do
|
||||
allow(client).to receive(:action).and_return(response)
|
||||
end
|
||||
|
||||
it "should return a builder instance" do
|
||||
expect(subject.action(action_name)).to be_a(Vagrant::Action::Builder)
|
||||
end
|
||||
|
||||
it "should call the plugin client" do
|
||||
expect(client).to receive(:action).
|
||||
with(instance_of(Vagrant::Proto::GenericAction)).
|
||||
and_return(response)
|
||||
subject.action(action_name)
|
||||
end
|
||||
|
||||
it "should create a new custom action class" do
|
||||
builder = subject.action(action_name)
|
||||
action = builder.stack.first.first
|
||||
expect(action.ancestors).to include(Vagrant::GoPlugin::ProviderPlugin::Action)
|
||||
expect(action.action_name).to eq("TestAction")
|
||||
end
|
||||
|
||||
context "when given non-local action name" do
|
||||
let(:actions) { ["Vagrant::Action::Builtin::Call"] }
|
||||
|
||||
it "should load the existing action class" do
|
||||
builder = subject.action(action_name)
|
||||
action = builder.stack.first.first
|
||||
expect(action).to eq(Vagrant::Action::Builtin::Call)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#capability" do
|
||||
let(:cap_name) { "test_cap" }
|
||||
let(:response) { double("response", result: result.to_json) }
|
||||
let(:result) { nil }
|
||||
|
||||
before do
|
||||
allow(subject).to receive(:name).and_return("dummy_provider")
|
||||
allow(client).to receive(:provider_capability).and_return(response)
|
||||
end
|
||||
|
||||
it "should call the plugin client" do
|
||||
expect(client).to receive(:provider_capability).
|
||||
with(instance_of(Vagrant::Proto::ProviderCapabilityRequest))
|
||||
.and_return(response)
|
||||
subject.capability(cap_name)
|
||||
end
|
||||
|
||||
it "should deserialize result" do
|
||||
expect(subject.capability(cap_name)).to eq(result)
|
||||
end
|
||||
|
||||
context "when hash value is returned" do
|
||||
let(:result) { {key: "value"} }
|
||||
|
||||
it "should return indifferent access hash" do
|
||||
r = subject.capability(cap_name)
|
||||
expect(r[:key]).to eq(result[:key])
|
||||
expect(r).to be_a(Vagrant::Util::HashWithIndifferentAccess)
|
||||
end
|
||||
end
|
||||
|
||||
context "when arguments are provided" do
|
||||
let(:args) { ["arg1", {"key" => "value"}] }
|
||||
|
||||
it "should serialize arguments when sent to plugin client" do
|
||||
expect(client).to receive(:provider_capability) do |req|
|
||||
expect(req.arguments).to eq(args.to_json)
|
||||
response
|
||||
end
|
||||
subject.capability(cap_name, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#is_installed?" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:is_installed).and_return(double("response", result: true))
|
||||
expect(subject.is_installed?).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#is_usable?" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:is_usable).and_return(double("response", result: true))
|
||||
expect(subject.is_usable?).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#machine_id_changed" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:machine_id_changed).and_return(double("response", result: true))
|
||||
expect(subject.machine_id_changed).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#ssh_info" do
|
||||
let(:response) {
|
||||
Vagrant::Proto::MachineSshInfo.new(
|
||||
host: "localhost",
|
||||
port: 2222,
|
||||
private_key_path: "/key/path",
|
||||
username: "vagrant"
|
||||
)
|
||||
}
|
||||
|
||||
before { allow(client).to receive(:ssh_info).and_return(response) }
|
||||
|
||||
it "should return hash with indifferent access result" do
|
||||
expect(subject.ssh_info).to be_a(Vagrant::Util::HashWithIndifferentAccess)
|
||||
end
|
||||
|
||||
it "should include ssh information" do
|
||||
result = subject.ssh_info
|
||||
expect(result[:host]).to eq(response.host)
|
||||
expect(result[:port]).to eq(response.port)
|
||||
expect(result[:private_key_path]).to eq(response.private_key_path)
|
||||
expect(result[:username]).to eq(response.username)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#state" do
|
||||
let(:response) {
|
||||
Vagrant::Proto::MachineState.new(
|
||||
id: "machine-id",
|
||||
short_description: "running",
|
||||
long_description: "it's really running"
|
||||
)
|
||||
}
|
||||
|
||||
before { allow(client).to receive(:state).and_return(response) }
|
||||
|
||||
it "should return a MachineState instance" do
|
||||
expect(subject.state).to be_a(Vagrant::MachineState)
|
||||
end
|
||||
|
||||
it "should set the state attributes" do
|
||||
result = subject.state
|
||||
expect(result.id).to eq(response.id)
|
||||
expect(result.short_description).to eq(response.short_description)
|
||||
expect(result.long_description).to eq(response.long_description)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,73 @@
|
|||
require_relative "../../base"
|
||||
|
||||
describe Vagrant::GoPlugin::SyncedFolderPlugin::SyncedFolder do
|
||||
let(:subject) {
|
||||
Class.new(described_class).tap { |c|
|
||||
c.plugin_client = client } }
|
||||
let(:client) { double("client") }
|
||||
let(:machine) { double("machine", to_json: "{}") }
|
||||
let(:folders) { double("folders", to_json: "{}") }
|
||||
let(:options) { double("options", to_json: "{}") }
|
||||
|
||||
it "should be a GRPCPlugin" do
|
||||
expect(subject).to be_a(Vagrant::GoPlugin::GRPCPlugin)
|
||||
end
|
||||
|
||||
describe "#cleanup" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:cleanup).
|
||||
with(instance_of(Vagrant::Proto::SyncedFolders))
|
||||
subject.cleanup(machine, options)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#disable" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:disable).
|
||||
with(instance_of(Vagrant::Proto::SyncedFolders))
|
||||
subject.disable(machine, folders, options)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#enable" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:enable).
|
||||
with(instance_of(Vagrant::Proto::SyncedFolders))
|
||||
subject.enable(machine, folders, options)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#prepare" do
|
||||
it "should call plugin client" do
|
||||
expect(client).to receive(:prepare).
|
||||
with(instance_of(Vagrant::Proto::SyncedFolders))
|
||||
subject.prepare(machine, folders, options)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#usable?" do
|
||||
let(:response) { Vagrant::Proto::Valid.new(result: true) }
|
||||
|
||||
it "should call the plugin client" do
|
||||
expect(client).to receive(:is_usable).
|
||||
with(instance_of(Vagrant::Proto::Machine)).
|
||||
and_return(response)
|
||||
expect(subject.usable?).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#name" do
|
||||
let(:response) { Vagrnat::Proto::Identifier.new(name: "dummy") }
|
||||
|
||||
it "should call the plugin client" do
|
||||
expect(client).to receive(:name).and_return(response)
|
||||
expect(subject.name).to eq(response.name)
|
||||
end
|
||||
|
||||
it "should only call the plugin client once" do
|
||||
expect(client).to receive(:name).once.and_return(response)
|
||||
expect(subject.name).to eq(response.name)
|
||||
expect(subject.name).to eq(response.name)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue