From 74c89e5ec146408e9f9df10ec32437c55b881bbf Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 4 Jun 2019 14:14:06 -0700 Subject: [PATCH] Add virtualbox provider rspec tests --- .../providers/virtualbox/provider_test.rb | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/unit/plugins/providers/virtualbox/provider_test.rb diff --git a/test/unit/plugins/providers/virtualbox/provider_test.rb b/test/unit/plugins/providers/virtualbox/provider_test.rb new file mode 100644 index 000000000..410873e24 --- /dev/null +++ b/test/unit/plugins/providers/virtualbox/provider_test.rb @@ -0,0 +1,93 @@ +require_relative "../../../base" + +require Vagrant.source_root.join("plugins/providers/virtualbox/provider") + +describe VagrantPlugins::ProviderVirtualBox::Provider do + let(:driver){ double("driver") } + let(:provider){ double("provider", driver: driver) } + let(:provider_config){ double("provider_config") } + let(:machine){ double("machine", uid: "1000", provider: provider, provider_config: provider_config) } + + let(:platform) { double("platform") } + + subject { described_class.new(machine) } + + before do + stub_const("Vagrant::Util::Platform", platform) + allow(platform).to receive(:windows?).and_return(false) + allow(platform).to receive(:cygwin?).and_return(false) + allow(platform).to receive(:wsl?).and_return(false) + allow(platform).to receive(:wsl_windows_access_bypass?).and_return(false) + allow(machine).to receive(:id).and_return("foo") + end + + describe ".usable?" do + subject { described_class } + + it "returns true if usable" do + allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).and_return(driver) + expect(subject).to be_usable + end + + it "raises an exception if virtualbox is not available" do + allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new). + and_raise(Vagrant::Errors::VirtualBoxNotDetected) + + expect { subject.usable?(true) }. + to raise_error(Vagrant::Errors::VirtualBoxNotDetected) + end + + it "raises an exception if virtualbox is the wrong version" do + allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new). + and_raise(Vagrant::Errors::VirtualBoxInvalidVersion, supported_versions: "1,2,3") + + expect { subject.usable?(true) }. + to raise_error(Vagrant::Errors::VirtualBoxInvalidVersion) + end + end + + describe "#driver" do + it "is initialized" do + expect(subject.driver).to be_kind_of(VagrantPlugins::ProviderVirtualBox::Driver::Meta) + end + end + + describe "#state" do + it "returns not_created if no ID" do + allow(machine).to receive(:id).and_return(nil) + allow(machine).to receive(:data_dir).and_return(".vagrant") + + expect(subject.state.id).to eq(:not_created) + end + end + + describe "#ssh_info" do + let(:result) { "127.0.0.1" } + let(:exit_code) { 0 } + let(:ssh_info) {{:host=>result,:port=>22}} + let(:ssh) { double("ssh", guest_port: 22) } + let(:config) { double("config", ssh: ssh) } + + before do + allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).and_return(driver) + allow(machine).to receive(:action).with(:read_state).and_return(machine_state_id: :running) + allow(machine).to receive(:data_dir).and_return(".vagrant") + allow(driver).to receive(:uuid).and_return("1234") + allow(driver).to receive(:read_state).and_return(:running) + allow(driver).to receive(:ssh_port).and_return(22) + allow(machine).to receive(:config).and_return(config) + end + + it "returns nil if machine state is not running" do + allow(driver).to receive(:read_state).and_return(:not_created) + expect(subject.ssh_info).to eq(nil) + end + + it "should receive a valid address" do + allow(driver).to receive(:execute).with(:get_network_config).and_return(result) + + allow(driver).to receive(:read_guest_ip).and_return({"ip" => "127.0.0.1"}) + expect(subject.ssh_info).to eq(ssh_info) + end + end +end