From eba552ea73135fa460c73c7c97b1152573504918 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 25 May 2018 16:23:45 -0700 Subject: [PATCH] Add more test coverage on Hyper-V Provider actions --- .../providers/hyperv/action/delete_vm_test.rb | 29 +++++++++ .../hyperv/action/is_windows_test.rb | 37 +++++++++++ .../hyperv/action/net_set_mac_test.rb | 40 ++++++++++++ .../hyperv/action/net_set_vlan_test.rb | 40 ++++++++++++ .../hyperv/action/read_guest_ip_test.rb | 39 ++++++++++++ .../hyperv/action/read_state_test.rb | 56 +++++++++++++++++ .../providers/hyperv/action/set_name_test.rb | 61 +++++++++++++++++++ .../hyperv/action/wait_for_ip_address_test.rb | 40 ++++++++++++ 8 files changed, 342 insertions(+) create mode 100644 test/unit/plugins/providers/hyperv/action/delete_vm_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/is_windows_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/net_set_mac_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/net_set_vlan_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/read_state_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/set_name_test.rb create mode 100644 test/unit/plugins/providers/hyperv/action/wait_for_ip_address_test.rb diff --git a/test/unit/plugins/providers/hyperv/action/delete_vm_test.rb b/test/unit/plugins/providers/hyperv/action/delete_vm_test.rb new file mode 100644 index 000000000..dda24c90c --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/delete_vm_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/is_windows_test.rb b/test/unit/plugins/providers/hyperv/action/is_windows_test.rb new file mode 100644 index 000000000..6ed9aaf68 --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/is_windows_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/net_set_mac_test.rb b/test/unit/plugins/providers/hyperv/action/net_set_mac_test.rb new file mode 100644 index 000000000..01bc3d6ef --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/net_set_mac_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/net_set_vlan_test.rb b/test/unit/plugins/providers/hyperv/action/net_set_vlan_test.rb new file mode 100644 index 000000000..c8b128d73 --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/net_set_vlan_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb b/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb new file mode 100644 index 000000000..1ec91639a --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/read_state_test.rb b/test/unit/plugins/providers/hyperv/action/read_state_test.rb new file mode 100644 index 000000000..50602f67d --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/read_state_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/set_name_test.rb b/test/unit/plugins/providers/hyperv/action/set_name_test.rb new file mode 100644 index 000000000..9708140e8 --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/set_name_test.rb @@ -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 diff --git a/test/unit/plugins/providers/hyperv/action/wait_for_ip_address_test.rb b/test/unit/plugins/providers/hyperv/action/wait_for_ip_address_test.rb new file mode 100644 index 000000000..4d5173236 --- /dev/null +++ b/test/unit/plugins/providers/hyperv/action/wait_for_ip_address_test.rb @@ -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