From cbb66ed67f177bd4869070762f7ecc73f6b9cbe2 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 6 Jul 2020 14:27:57 -0500 Subject: [PATCH] Update FreeBSD guest change hostname cap --- .../guests/freebsd/cap/change_host_name.rb | 16 ++++-- .../freebsd/cap/change_host_name_test.rb | 55 +++++++++++++++---- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index 9ddd0a614..0f707bbe4 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -1,7 +1,11 @@ +require 'vagrant/util/guest_hosts' + module VagrantPlugins module GuestFreeBSD module Cap class ChangeHostName + extend Vagrant::Util::GuestHosts::BSD + def self.change_host_name(machine, name) comm = machine.communicate @@ -11,15 +15,15 @@ module VagrantPlugins # Set the hostname hostname '#{name}' sed -i '' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf - - # Prepend ourselves to /etc/hosts - grep -w '#{name}' /etc/hosts || { - echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts - mv /tmp/tmp-hosts /etc/hosts - } EOH comm.sudo(command, shell: "sh") end + network_with_hostname = machine.config.vm.networks.map {|_, c| c if c[:hostname] }.compact[0] + if network_with_hostname + replace_host(comm, name, network_with_hostname[:ip]) + else + add_hostname_to_loopback_interface(comm, name) + end end end end diff --git a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb index bdd9b4226..4a3f3258d 100644 --- a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb @@ -10,9 +10,13 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do let(:machine) { double("machine") } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:name) { "banana-rama.example.com" } + let(:basename) { "banana-rama" } + let(:networks) {} before do allow(machine).to receive(:communicate).and_return(comm) + allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks) end after do @@ -20,21 +24,50 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do end describe ".change_host_name" do - let(:name) { "banana-rama.example.com" } + 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"}] + ] } - it "sets the hostname and /etc/hosts" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - described_class.change_host_name(machine, name) + 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[1]).to match(/hostname '#{name}'/) + expect(described_class).to_not receive(:add_hostname_to_loopback_interface) + end - expect(comm.received_commands[1]).to match(/hostname '#{name}'/) - expect(comm.received_commands[1]).to match(/grep -w '#{name}' \/etc\/hosts/) - expect(comm.received_commands[1]).to match(/echo -e '127.0.0.1\\t#{name}\\tbanana-rama'/) + 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}'/) + expect(described_class).to_not receive(:add_hostname_to_loopback_interface) + end end - it "does nothing if the hostname is already set" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) - described_class.change_host_name(machine, name) - expect(comm.received_commands.size).to eq(1) + 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