diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index c8754be9a..d4c1f9f71 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -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 diff --git a/plugins/guests/suse/cap/change_host_name.rb b/plugins/guests/suse/cap/change_host_name.rb index bd98e97d5..a5452c00c 100644 --- a/plugins/guests/suse/cap/change_host_name.rb +++ b/plugins/guests/suse/cap/change_host_name.rb @@ -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) diff --git a/test/unit/plugins/guests/alt/cap/change_host_name_test.rb b/test/unit/plugins/guests/alt/cap/change_host_name_test.rb index 7f2051e36..a79737e10 100644 --- a/test/unit/plugins/guests/alt/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/alt/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb index 173bc7f63..d7705227c 100644 --- a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb index ba80977a8..7ef35e517 100644 --- a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb b/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb index 3120fca2f..a39112751 100644 --- a/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/linux/cap/change_host_name_test.rb b/test/unit/plugins/guests/linux/cap/change_host_name_test.rb new file mode 100644 index 000000000..3a3b447ce --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/pld/cap/change_host_name_test.rb b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb index d1b6195e1..97192ab42 100644 --- a/test/unit/plugins/guests/pld/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb index 906dfdf02..bb64295c4 100644 --- a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb index 16c6041ea..6cae24d0f 100644 --- a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb @@ -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