Clean tests for plugin client refactor

This commit is contained in:
Chris Roberts 2019-04-09 16:56:17 -07:00
parent fe7ac740df
commit f4420f6b81
3 changed files with 127 additions and 166 deletions

View File

@ -1,19 +1,137 @@
require_relative "../../base"
describe Vagrant::GoPlugin::CapabilityPlugin do
let(:client) { double("client") }
describe Vagrant::GoPlugin::CapabilityPlugin::Capability do
it "should be a TypedGoPlugin" do
expect(described_class.ancestors).to include(Vagrant::GoPlugin::TypedGoPlugin)
it "should be a GRPCPlugin" do
expect(described_class.ancestors).to include(Vagrant::GoPlugin::GRPCPlugin)
end
end
describe ".interface" do
it "should create an interface instance" do
expect(described_class.interface).to be_a(Vagrant::GoPlugin::CapabilityPlugin::Interface)
describe ".generate_guest_capabilities" do
let(:caps) {
[{platform: "dummy", name: "stub_cap"},
{platform: "dummy", name: "other_cap"}]}
let(:cap_response) {
Vagrant::Proto::SystemCapabilityList.new(
capabilities: caps.map { |i|
Vagrant::Proto::SystemCapability.new(i)})}
let(:plugin_klass) { double("plugin_klass") }
let(:plugin_type) { :testing }
before do
allow(client).to receive(:guest_capabilities).
and_return(cap_response)
allow(plugin_klass).to receive(:guest_capability)
end
it "should cache generated interface" do
expect(described_class.interface).to be(described_class.interface)
it "should generate two new capability classes" do
expect(Class).to receive(:new).twice.
with(Vagrant::GoPlugin::CapabilityPlugin::Capability).
and_call_original
described_class.generate_guest_capabilities(client, plugin_klass, plugin_type)
end
it "should create capability name methods" do
c1 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
c2 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
expect(Class).to receive(:new).and_return(c1)
expect(Class).to receive(:new).and_return(c2)
described_class.generate_guest_capabilities(client, plugin_klass, plugin_type)
expect(c1).to respond_to(:stub_cap)
expect(c2).to respond_to(:other_cap)
end
it "should register guest capability" do
expect(plugin_klass).to receive(:guest_capability).with(:dummy, :stub_cap)
expect(plugin_klass).to receive(:guest_capability).with(:dummy, :other_cap)
described_class.generate_guest_capabilities(client, plugin_klass, plugin_type)
end
end
describe ".generate_host_capabilities" do
let(:caps) {
[{platform: "dummy", name: "stub_cap"},
{platform: "dummy", name: "other_cap"}]}
let(:cap_response) {
Vagrant::Proto::SystemCapabilityList.new(
capabilities: caps.map { |i|
Vagrant::Proto::SystemCapability.new(i)})}
let(:plugin_klass) { double("plugin_klass") }
let(:plugin_type) { :testing }
before do
allow(client).to receive(:host_capabilities).
and_return(cap_response)
allow(plugin_klass).to receive(:host_capability)
end
it "should generate two new capability classes" do
expect(Class).to receive(:new).twice.
with(Vagrant::GoPlugin::CapabilityPlugin::Capability).
and_call_original
described_class.generate_host_capabilities(client, plugin_klass, plugin_type)
end
it "should create capability name methods" do
c1 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
c2 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
expect(Class).to receive(:new).and_return(c1)
expect(Class).to receive(:new).and_return(c2)
described_class.generate_host_capabilities(client, plugin_klass, plugin_type)
expect(c1).to respond_to(:stub_cap)
expect(c2).to respond_to(:other_cap)
end
it "should register host capability" do
expect(plugin_klass).to receive(:host_capability).with(:dummy, :stub_cap)
expect(plugin_klass).to receive(:host_capability).with(:dummy, :other_cap)
described_class.generate_host_capabilities(client, plugin_klass, plugin_type)
end
end
describe ".generate_provider_capabilities" do
let(:caps) {
[{provider: "dummy", name: "stub_cap"},
{provider: "dummy", name: "other_cap"}]}
let(:cap_response) {
Vagrant::Proto::ProviderCapabilityList.new(
capabilities: caps.map { |i|
Vagrant::Proto::ProviderCapability.new(i)})}
let(:plugin_klass) { double("plugin_klass") }
let(:plugin_type) { :testing }
before do
allow(client).to receive(:provider_capabilities).
and_return(cap_response)
allow(plugin_klass).to receive(:provider_capability)
end
it "should generate two new capability classes" do
expect(Class).to receive(:new).twice.
with(Vagrant::GoPlugin::CapabilityPlugin::Capability).
and_call_original
described_class.generate_provider_capabilities(client, plugin_klass, plugin_type)
end
it "should create capability name methods" do
c1 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
c2 = Class.new(Vagrant::GoPlugin::CapabilityPlugin::Capability)
expect(Class).to receive(:new).and_return(c1)
expect(Class).to receive(:new).and_return(c2)
described_class.generate_provider_capabilities(client, plugin_klass, plugin_type)
expect(c1).to respond_to(:stub_cap)
expect(c2).to respond_to(:other_cap)
end
it "should register provider capability" do
expect(plugin_klass).to receive(:provider_capability).with(:dummy, :stub_cap)
expect(plugin_klass).to receive(:provider_capability).with(:dummy, :other_cap)
described_class.generate_provider_capabilities(client, plugin_klass, plugin_type)
end
end
end

View File

@ -54,161 +54,4 @@ describe Vagrant::GoPlugin::Core do
:created, "", ""))
end
end
describe "#dump_machine" do
it "should raise error when argument is not a Vagrant Machine" do
expect { subject.dump_machine(:value) }.to raise_error(TypeError)
end
it "should dump machine to JSON string" do
expect(subject.dump_machine(machine)).to be_a(String)
end
it "should dump machine information" do
val = subject.dump_machine(machine)
result = JSON.load(val)
expect(result["name"]).to eq(machine.name)
expect(result["provider_name"]).to eq(machine.provider_name.to_s)
expect(result["data_dir"]).to eq(machine.data_dir.to_s)
end
it "should dump box information" do
val = subject.dump_machine(machine)
result = JSON.load(val)
expect(result["box"]).to_not be_nil
expect(result["box"]["name"]).to eq("foo")
end
it "should dump environment information" do
val = subject.dump_machine(machine)
result = JSON.load(val)
expect(result["environment"]).to_not be_nil
expect(result["environment"]["cwd"]).to eq(machine.env.cwd.to_s)
end
end
describe "#dump_environment" do
it "should dump environment to Hash" do
expect(subject.dump_environment(env)).to be_a(Hash)
end
it "should include environment information" do
result = subject.dump_environment(env)
expect(result[:cwd]).to eq(env.cwd)
expect(result[:data_dir]).to eq(env.data_dir)
end
end
describe "#load_machine" do
it "should set ID if ID has changed" do
expect(machine).to receive(:id=).with("newid")
subject.load_machine({id: "newid"}, machine)
end
it "should not set ID if ID has not changed" do
expect(machine).not_to receive(:id=)
subject.load_machine({id: machine.id}, machine)
end
end
end
describe Vagrant::GoPlugin::DirectGoPlugin do
let(:subject_class) { Class.new.tap { |c| c.include(described_class) } }
let(:subject) { subject_class.new }
describe ".go_plugin_name" do
it "should return nil by default" do
expect(subject_class.go_plugin_name).to be_nil
end
it "should return assigned name when assigned" do
subject_class.go_plugin_name = :test
expect(subject_class.go_plugin_name).to eq(:test)
end
end
describe ".go_plugin_name=" do
it "should allow for setting the plugin name" do
subject_class.go_plugin_name = :test_plugin
expect(subject_class.go_plugin_name).to eq(:test_plugin)
end
it "should raise error when name has already been set" do
subject_class.go_plugin_name = :test_plugin
expect {
subject_class.go_plugin_name = :different_plugin
}.to raise_error(ArgumentError)
end
end
describe ".plugin_name" do
it "should proxy to .go_plugin_name" do
expect(subject_class).to receive(:go_plugin_name)
subject_class.plugin_name
end
end
describe ".name" do
it "should default to empty string" do
expect(subject_class.name).to eq("")
end
it "should camel case the go_plugin_name" do
subject_class.go_plugin_name = "test_vagrant_plugin"
expect(subject_class.name).to eq("TestVagrantPlugin")
end
end
describe "#plugin_name" do
it "should proxy to .go_plugin_name" do
expect(subject_class).to receive(:go_plugin_name)
subject.plugin_name
end
end
end
describe Vagrant::GoPlugin::TypedGoPlugin do
let(:subject_class) { Class.new.tap { |c| c.include(described_class) } }
let(:subject) { subject_class.new }
it "should include DirectGoPlugin" do
expect(subject_class.ancestors).to include(Vagrant::GoPlugin::DirectGoPlugin)
end
describe ".go_plugin_type" do
it "should be nil by default" do
expect(subject_class.go_plugin_type).to be_nil
end
it "should return assigned type when set" do
subject_class.go_plugin_type = "provider"
expect(subject_class.go_plugin_type).to eq("provider")
end
end
describe ".go_plugin_type=" do
it "should allow setting plugin type" do
subject_class.go_plugin_type = "test_type"
expect(subject_class.go_plugin_type).to eq("test_type")
end
it "should convert plugin type value to string" do
subject_class.go_plugin_type = :test_type
expect(subject_class.go_plugin_type).to eq("test_type")
end
it "should raise an error when type has already been set" do
subject_class.go_plugin_type = :test_type
expect {
subject_class.go_plugin_type = :different_type
}.to raise_error(ArgumentError)
end
end
describe "#plugin_type" do
it "should proxy to .go_plugin_type" do
expect(subject_class).to receive(:go_plugin_type)
subject.plugin_type
end
end
end

View File

@ -22,8 +22,8 @@ describe Vagrant::GoPlugin::Interface do
describe "#register_plugins" do
it "should load Provider and SyncedFolder plugins" do
expect(Vagrant::GoPlugin::ProviderPlugin).to receive_message_chain(:interface, :load!)
expect(Vagrant::GoPlugin::SyncedFolderPlugin).to receive_message_chain(:interface, :load!)
expect(subject).to receive(:load_providers)
expect(subject).to receive(:load_synced_folders)
subject.register_plugins
end
end