Remove existing common tests to shared examples. Add custom import test.

This commit is contained in:
Chris Roberts 2018-11-15 11:06:30 -08:00
parent e86824e098
commit c5bc8aba46
1 changed files with 68 additions and 110 deletions

View File

@ -1,3 +1,4 @@
require "pathname"
require_relative "../base"
describe VagrantPlugins::ProviderVirtualBox::Driver::Version_5_0 do
@ -8,127 +9,84 @@ describe VagrantPlugins::ProviderVirtualBox::Driver::Version_5_0 do
subject { VagrantPlugins::ProviderVirtualBox::Driver::Version_5_0.new(uuid) }
it_behaves_like "a version 4.x virtualbox driver"
it_behaves_like "a version 5.x virtualbox driver"
describe "#shared_folders" do
let(:folders) { [{:name=>"folder",
:hostpath=>"/Users/brian/vagrant-folder",
:transient=>false,
:SharedFoldersEnableSymlinksCreate=>true}]}
describe "#import" do
let(:ovf) { double("ovf") }
let(:machine_id) { double("machine_id") }
let(:output) {<<-OUTPUT
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interpreting /home/user/.vagrant.d/boxes/hashicorp-VAGRANTSLASH-precise64/1.1.0/virtualbox/box.ovf...
OK.
Disks:
vmdisk1 85899345920 -1 http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized box-disk1.vmdk -1 -1
let(:folders_automount) { [{:name=>"folder",
:hostpath=>"/Users/brian/vagrant-folder",
:transient=>false,
:automount=>true,
:SharedFoldersEnableSymlinksCreate=>true}]}
let(:folders_disabled) { [{:name=>"folder",
:hostpath=>"/Users/brian/vagrant-folder",
:transient=>false,
:SharedFoldersEnableSymlinksCreate=>false}]}
it "enables SharedFoldersEnableSymlinksCreate if true" do
expect(subprocess).to receive(:execute).
with("VBoxManage", "setextradata", anything, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/folder", "1", {:notify=>[:stdout, :stderr]}).
and_return(subprocess_result(exit_code: 0))
expect(subprocess).to receive(:execute).
with("VBoxManage", "sharedfolder", "add", anything, "--name", "folder", "--hostpath", "/Users/brian/vagrant-folder", {:notify=>[:stdout, :stderr]}).
and_return(subprocess_result(exit_code: 0))
subject.share_folders(folders)
end
it "enables automount if option is true" do
expect(subprocess).to receive(:execute).
with("VBoxManage", "setextradata", anything, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/folder", "1", {:notify=>[:stdout, :stderr]}).
and_return(subprocess_result(exit_code: 0))
expect(subprocess).to receive(:execute).
with("VBoxManage", "sharedfolder", "add", anything, "--name", "folder", "--hostpath", "/Users/brian/vagrant-folder", "--automount", {:notify=>[:stdout, :stderr]}).
and_return(subprocess_result(exit_code: 0))
subject.share_folders(folders_automount)
end
it "disables SharedFoldersEnableSymlinksCreate if false" do
expect(subprocess).to receive(:execute).
with("VBoxManage", "sharedfolder", "add", anything, "--name", "folder", "--hostpath", "/Users/brian/vagrant-folder", {:notify=>[:stdout, :stderr]}).
and_return(subprocess_result(exit_code: 0))
subject.share_folders(folders_disabled)
end
end
describe "#set_mac_address" do
let(:mac) { "00:00:00:00:00:00" }
after { subject.set_mac_address(mac) }
it "should modify vm and set mac address" do
expect(subprocess).to receive(:execute).with("VBoxManage", "modifyvm", anything, "--macaddress1", mac, anything).
and_return(subprocess_result(exit_code: 0))
end
context "when mac address is falsey" do
let(:mac) { nil }
it "should modify vm and set mac address to automatic value" do
expect(subprocess).to receive(:execute).with("VBoxManage", "modifyvm", anything, "--macaddress1", "auto", anything).
and_return(subprocess_result(exit_code: 0))
end
end
end
describe "#ssh_port" do
let(:forwards) {
[[1, "ssh", 2222, 22, "127.0.0.1"],
[1, "ssh", 8080, 80, ""]]
Virtual system 0:
0: Suggested OS type: "Ubuntu_64"
(change with "--vsys 0 --ostype <type>"; use "list ostypes" to list all possible values)
1: Suggested VM name "precise64"
(change with "--vsys 0 --vmname <name>")
2: Number of CPUs: 2
(change with "--vsys 0 --cpus <n>")
3: Guest memory: 384 MB
(change with "--vsys 0 --memory <MB>")
4: Network adapter: orig NAT, config 3, extra slot=0;type=NAT
5: CD-ROM
(disable with "--vsys 0 --unit 5 --ignore")
6: IDE controller, type PIIX4
(disable with "--vsys 0 --unit 6 --ignore")
7: IDE controller, type PIIX4
(disable with "--vsys 0 --unit 7 --ignore")
8: SATA controller, type AHCI
(disable with "--vsys 0 --unit 8 --ignore")
9: Hard disk image: source image=box-disk1.vmdk, target path=/home/user/VirtualBox VMs/precise64/box-disk1.vmdk, controller=8;channel=0
(change target path with "--vsys 0 --unit 9 --disk path";
disable with "--vsys 0 --unit 9 --ignore")
OUTPUT
}
before { allow(subject).to receive(:read_forwarded_ports).and_return(forwards) }
it "should return the host port" do
expect(subject.ssh_port(22)).to eq(2222)
before do
allow(Vagrant::Util::Platform).to receive(:windows_path).
with(ovf).and_return(ovf)
allow(subject).to receive(:execute).with("import", "-n", ovf).
and_return(output)
allow(subject).to receive(:execute).with("import", ovf, any_args)
allow(subject).to receive(:get_machine_id).and_return(machine_id)
end
context "when multiple matches are available" do
let(:forwards) {
[[1, "ssh", 2222, 22, "127.0.0.1"],
[1, "", 2221, 22, ""]]
}
it "should choose localhost port forward" do
expect(subject.ssh_port(22)).to eq(2222)
end
context "when multiple named matches are available" do
let(:forwards) {
[[1, "ssh", 2222, 22, "127.0.0.1"],
[1, "SSH", 2221, 22, "127.0.0.1"]]
}
it "should choose lowercased name forward" do
expect(subject.ssh_port(22)).to eq(2222)
end
end
it "should return the machine id" do
expect(subject).to receive(:get_machine_id).and_return(machine_id)
expect(subject.import(ovf)).to eq(machine_id)
end
context "when only ports are defined" do
let(:forwards) {
[[1, "", 2222, 22, ""]]
}
it "should return the host port" do
expect(subject.ssh_port(22)).to eq(2222)
end
it "should return machine id using custom name" do
expect(subject).to receive(:get_machine_id).with(/.*precise64_.+/).and_return(machine_id)
expect(subject.import(ovf)).to eq(machine_id)
end
context "when no matches are available" do
let(:forwards) { [] }
it "should include disk image on import" do
expect(subject).to receive(:execute).with("import", "-n", ovf).and_return(output)
expect(subject).to receive(:execute) do |*args|
match = args[3, args.size].detect { |a| a.include?("disk1.vmdk") }
expect(match).to include("disk1.vmdk")
end
expect(subject.import(ovf)).to eq(machine_id)
end
it "should return nil" do
expect(subject.ssh_port(22)).to be_nil
it "should include full path for disk image on import" do
expect(subject).to receive(:execute).with("import", "-n", ovf).and_return(output)
expect(subject).to receive(:execute) do |*args|
dpath = args[3, args.size].detect { |a| a.include?("disk1.vmdk") }
expect(Pathname.new(dpath).absolute?).to be_truthy
end
expect(subject.import(ovf)).to eq(machine_id)
end
context "suggested name is not provided" do
before { output.sub!(/Suggested VM name/, "") }
it "should raise an error" do
expect { subject.import(ovf) }.to raise_error(Vagrant::Errors::VirtualBoxNoName)
end
end
end