Add more test coverage on Hyper-V Provider actions

This commit is contained in:
Chris Roberts 2018-05-25 16:23:45 -07:00
parent d24b432273
commit eba552ea73
8 changed files with 342 additions and 0 deletions

View File

@ -0,0 +1,29 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/delete_vm")
describe VagrantPlugins::HyperV::Action::DeleteVM do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider) }
let(:subject){ described_class.new(app, env) }
before do
allow(app).to receive(:call)
allow(ui).to receive(:info)
allow(driver).to receive(:delete_vm)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should call the driver to delete the vm" do
expect(driver).to receive(:delete_vm)
subject.call(env)
end
end

View File

@ -0,0 +1,37 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/is_windows")
describe VagrantPlugins::HyperV::Action::IsWindows do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, config: config) }
let(:config){ double("config", vm: vm) }
let(:vm){ double("vm", guest: :windows) }
let(:subject){ described_class.new(app, env) }
before do
allow(app).to receive(:call)
allow(env).to receive(:[]=)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should update the env with the result" do
expect(env).to receive(:[]=).with(:result, true)
subject.call(env)
end
it "should set the result to false when not windows" do
expect(vm).to receive(:guest).and_return(:linux)
expect(env).to receive(:[]=).with(:result, false)
subject.call(env)
end
end

View File

@ -0,0 +1,40 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/net_set_mac")
describe VagrantPlugins::HyperV::Action::NetSetMac do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, provider_config: provider_config) }
let(:provider_config){ double("provider_config", mac: mac) }
let(:mac){ "ADDRESS" }
let(:subject){ described_class.new(app, env) }
before do
allow(ui).to receive(:info)
allow(driver).to receive(:net_set_mac)
allow(app).to receive(:call)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should call the driver to set the MAC address" do
expect(driver).to receive(:net_set_mac).with(mac)
subject.call(env)
end
context "with no MAC address provided" do
let(:mac){ nil }
it "should not call driver to set the MAC address" do
expect(driver).not_to receive(:net_set_mac)
subject.call(env)
end
end
end

View File

@ -0,0 +1,40 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/net_set_vlan")
describe VagrantPlugins::HyperV::Action::NetSetVLan do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, provider_config: provider_config) }
let(:provider_config){ double("provider_config", vlan_id: vlan_id) }
let(:vlan_id){ "VID" }
let(:subject){ described_class.new(app, env) }
before do
allow(ui).to receive(:info)
allow(driver).to receive(:net_set_vlan)
allow(app).to receive(:call)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should call the driver to set the vlan id" do
expect(driver).to receive(:net_set_vlan).with(vlan_id)
subject.call(env)
end
context "with no vlan id provided" do
let(:vlan_id){ nil }
it "should not call driver to set the vlan id" do
expect(driver).not_to receive(:net_set_vlan)
subject.call(env)
end
end
end

View File

@ -0,0 +1,39 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/read_guest_ip")
describe VagrantPlugins::HyperV::Action::ReadGuestIP do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider) }
let(:subject){ described_class.new(app, env) }
before do
allow(app).to receive(:call)
allow(env).to receive(:[]=)
allow(machine).to receive(:id)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
context "with machine ID set" do
before{ allow(machine).to receive(:id).and_return("VMID") }
it "should request guest IP from the driver" do
expect(driver).to receive(:read_guest_ip).and_return("ip" => "ADDRESS")
subject.call(env)
end
it "should set the host information into the env" do
expect(env).to receive(:[]=).with(:machine_ssh_info, host: "ADDRESS")
expect(driver).to receive(:read_guest_ip).and_return("ip" => "ADDRESS")
subject.call(env)
end
end
end

View File

@ -0,0 +1,56 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/read_state")
describe VagrantPlugins::HyperV::Action::ReadState do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine, machine_state_id: state_id} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider) }
let(:state_id){ nil }
let(:subject){ described_class.new(app, env) }
before do
allow(app).to receive(:call)
allow(env).to receive(:[]=)
allow(machine).to receive(:id)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should set machine state into the env as not created" do
expect(env).to receive(:[]=).with(:machine_state_id, :not_created)
subject.call(env)
end
context "with machine ID set" do
before{ allow(machine).to receive(:id).and_return("VMID") }
it "should request machine state from the driver" do
expect(driver).to receive(:get_current_state).and_return("state" => "running")
subject.call(env)
end
it "should set machine state into the env" do
expect(driver).to receive(:get_current_state).and_return("state" => "running")
expect(env).to receive(:[]=).with(:machine_state_id, :running)
subject.call(env)
end
context "with machine state ID as not_created" do
let(:state_id){ :not_created }
it "should clear the machine ID" do
expect(driver).to receive(:get_current_state).and_return("state" => "not_created")
expect(machine).to receive(:id=).with(nil)
subject.call(env)
end
end
end
end

View File

@ -0,0 +1,61 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/set_name")
describe VagrantPlugins::HyperV::Action::SetName do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine, root_path: Pathname.new("path")} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, provider_config: provider_config, data_dir: data_dir, name: "machname") }
let(:data_dir){ double("data_dir") }
let(:provider_config){ double("provider_config", vmname: vmname) }
let(:vmname){ "VMNAME" }
let(:sentinel){ double("sentinel") }
let(:subject){ described_class.new(app, env) }
before do
allow(ui).to receive(:info)
allow(driver).to receive(:set_name)
allow(app).to receive(:call)
allow(data_dir).to receive(:join).and_return(sentinel)
allow(sentinel).to receive(:file?).and_return(false)
allow(sentinel).to receive(:open)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should call the driver to set the name" do
expect(driver).to receive(:set_name)
subject.call(env)
end
it "should use the configured name when setting" do
expect(driver).to receive(:set_name).with(vmname)
subject.call(env)
end
it "should write sentinel after name is set" do
expect(sentinel).to receive(:open)
subject.call(env)
end
context "when no name is provided in the config" do
let(:vmname){ nil }
it "should generate a name based on path and machine" do
expect(driver).to receive(:set_name).with(/^#{env[:root_path].to_s}_#{machine.name}_.+/)
subject.call(env)
end
it "should not set name if sentinel exists" do
expect(sentinel).to receive(:file?).and_return(true)
subject.call(env)
end
end
end

View File

@ -0,0 +1,40 @@
require_relative "../../../../base"
require Vagrant.source_root.join("plugins/providers/hyperv/action/wait_for_ip_address")
describe VagrantPlugins::HyperV::Action::WaitForIPAddress do
let(:app){ double("app") }
let(:env){ {ui: ui, machine: machine} }
let(:ui){ double("ui") }
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, provider_config: provider_config) }
let(:provider_config){ double("provider_config", ip_address_timeout: ip_address_timeout) }
let(:ip_address_timeout){ 1 }
let(:subject){ described_class.new(app, env) }
before do
allow(ui).to receive(:output)
allow(ui).to receive(:detail)
allow(driver).to receive(:read_guest_ip).and_return("ip" => "127.0.0.1")
allow(app).to receive(:call)
end
it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end
it "should set a timeout for waiting" do
expect(Timeout).to receive(:timeout).with(ip_address_timeout)
subject.call(env)
end
it "should retry until it receives a valid address" do
expect(driver).to receive(:read_guest_ip).and_return("ip" => "ADDRESS")
expect(driver).to receive(:read_guest_ip).and_return("ip" => "127.0.0.1")
expect(subject).to receive(:sleep)
subject.call(env)
end
end