Clean up tests
This commit is contained in:
parent
cf130d07a2
commit
9eb431b1bf
@ -11,6 +11,11 @@ module VagrantPlugins
|
||||
Guest
|
||||
end
|
||||
|
||||
guest_capability(:linux, :change_host_name) do
|
||||
require_relative "cap/change_host_name"
|
||||
Cap::ChangeHostName
|
||||
end
|
||||
|
||||
guest_capability(:linux, :choose_addressable_ip_addr) do
|
||||
require_relative "cap/choose_addressable_ip_addr"
|
||||
Cap::ChooseAddressableIPAddr
|
||||
|
||||
@ -8,7 +8,7 @@ module VagrantPlugins
|
||||
|
||||
def self.change_host_name?(comm, name)
|
||||
basename = name.split(".", 2)[0]
|
||||
!comm.test('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false)
|
||||
!comm.test("test \"$(hostnamectl --static status)\" = \"#{basename}\"", sudo: false)
|
||||
end
|
||||
|
||||
def self.change_name_command(name)
|
||||
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestALT::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/NEW_HOSTNAME_FULL='#{name}'/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestArch::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/hostnamectl set-hostname/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/hostnamectl set-hostname/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestGentoo::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/systemctl set-hostname '#{name}'/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
77
test/unit/plugins/guests/linux/cap/change_host_name_test.rb
Normal file
77
test/unit/plugins/guests/linux/cap/change_host_name_test.rb
Normal file
@ -0,0 +1,77 @@
|
||||
require_relative "../../../../base"
|
||||
|
||||
describe "VagrantPlugins::GuestLinux::Cap::ChangeHostName" do
|
||||
let(:described_class) do
|
||||
Class.new do
|
||||
extend VagrantPlugins::GuestLinux::Plugin
|
||||
.components
|
||||
.guest_capabilities[:linux]
|
||||
.get(:change_host_name)
|
||||
end
|
||||
end
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
let(:basename) { "banana-rama" }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
end
|
||||
|
||||
after do
|
||||
comm.verify_expectations!
|
||||
end
|
||||
|
||||
describe ".change_host_name" do
|
||||
context "minimal network config" do
|
||||
let(:networks) { [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]
|
||||
] }
|
||||
|
||||
before do
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
end
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm.received_commands[2]).to match(/hostname '#{name}'/)
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
|
||||
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm).to_not receive(:sudo).with(/hostname '#{name}'/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestPld::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/service network restart/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -45,31 +45,5 @@ describe "VagrantPlugins::GuestSlackware::Cap::ChangeHostName" do
|
||||
expect(comm).to_not receive(:sudo).with(/hostname -F \/etc\/hostname/)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -43,41 +43,7 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do
|
||||
comm.stub_command('test "$(hostnamectl --static status)" = "#{basename}"', exit_code: 0)
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context "multiple networks configured with hostname" do
|
||||
before do
|
||||
allow(comm).to receive(:test).with('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false).and_return(false)
|
||||
end
|
||||
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
|
||||
|
||||
expect(cap).to receive(:replace_host)
|
||||
expect(cap).to_not receive(:add_hostname_to_loopback_interface)
|
||||
cap.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
|
||||
|
||||
expect(cap).to_not receive(:replace_host)
|
||||
expect(cap).to receive(:add_hostname_to_loopback_interface).once
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user