From 8bad23d176b6cc7d7562860f5df6aed0f3b89cba Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 29 Jun 2020 17:02:43 -0500 Subject: [PATCH] Update OpenBSD guest change hostname cap --- lib/vagrant/util/guest_hosts.rb | 8 +- plugins/guests/darwin/cap/change_host_name.rb | 2 +- .../guests/openbsd/cap/change_host_name.rb | 16 ++-- .../darwin/cap/change_host_name_test.rb | 8 +- .../openbsd/cap/change_host_name_test.rb | 73 +++++++++++++++++++ test/unit/vagrant/util/guest_hosts_test.rb | 6 +- 6 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 test/unit/plugins/guests/openbsd/cap/change_host_name_test.rb diff --git a/lib/vagrant/util/guest_hosts.rb b/lib/vagrant/util/guest_hosts.rb index 4daa55c26..c0c16c098 100644 --- a/lib/vagrant/util/guest_hosts.rb +++ b/lib/vagrant/util/guest_hosts.rb @@ -46,8 +46,8 @@ module Vagrant end end - # Darwin specific inspection helpers - module Darwin + # BSD specific inspection helpers + module BSD include Unix # Remove any line in /etc/hosts that contains hostname, # then add hostname with associated ip @@ -58,8 +58,8 @@ module Vagrant def replace_host(comm, name, ip) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') - sed -i '' '/#{name}/d' /etc/hosts - sed -i '' '1i\\\n#{ip}\t#{name}\t#{basename}\n' /etc/hosts + sed -i.bak '/#{name}/d' /etc/hosts + sed -i.bak '1i\\\n#{ip}\t#{name}\t#{basename}\n' /etc/hosts EOH end end diff --git a/plugins/guests/darwin/cap/change_host_name.rb b/plugins/guests/darwin/cap/change_host_name.rb index c17ffff1a..37d549570 100644 --- a/plugins/guests/darwin/cap/change_host_name.rb +++ b/plugins/guests/darwin/cap/change_host_name.rb @@ -4,7 +4,7 @@ module VagrantPlugins module GuestDarwin module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Darwin + extend Vagrant::Util::GuestHosts::BSD def self.change_host_name(machine, name) comm = machine.communicate diff --git a/plugins/guests/openbsd/cap/change_host_name.rb b/plugins/guests/openbsd/cap/change_host_name.rb index 131c8fc45..7661ad1c9 100644 --- a/plugins/guests/openbsd/cap/change_host_name.rb +++ b/plugins/guests/openbsd/cap/change_host_name.rb @@ -1,7 +1,11 @@ +require 'vagrant/util/guest_hosts' + module VagrantPlugins module GuestOpenBSD module Cap class ChangeHostName + extend Vagrant::Util::GuestHosts::BSD + def self.change_host_name(machine, name) comm = machine.communicate @@ -12,15 +16,15 @@ module VagrantPlugins hostname '#{name}' sed -i'' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf echo '#{name}' > /etc/myname - - # 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/darwin/cap/change_host_name_test.rb b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb index b59f9c354..45b8896da 100644 --- a/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb @@ -32,9 +32,9 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do 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(/scutil --set ComputerName 'banana-rama.example.com'/) - expect(comm.received_commands[1]).to match(/scutil --set HostName 'banana-rama.example.com'/) - expect(comm.received_commands[1]).to match(/scutil --set LocalHostName 'banana-rama'/) + expect(comm.received_commands[1]).to match(/scutil --set ComputerName '#{name}'/) + expect(comm.received_commands[1]).to match(/scutil --set HostName '#{name}'/) + expect(comm.received_commands[1]).to match(/scutil --set LocalHostName '#{basename}'/) expect(comm.received_commands[1]).to match(/hostname 'banana-rama.example.com'/) expect(described_class).to_not receive(:add_hostname_to_loopback_interface) end @@ -42,7 +42,7 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do 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(/scutil --set ComputerName 'banana-rama.example.com'/) + expect(comm).to_not receive(:sudo).with(/scutil --set ComputerName '#{name}/) expect(described_class).to_not receive(:add_hostname_to_loopback_interface) end end diff --git a/test/unit/plugins/guests/openbsd/cap/change_host_name_test.rb b/test/unit/plugins/guests/openbsd/cap/change_host_name_test.rb new file mode 100644 index 000000000..a629371cd --- /dev/null +++ b/test/unit/plugins/guests/openbsd/cap/change_host_name_test.rb @@ -0,0 +1,73 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestOpenBSD::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestOpenBSD::Plugin + .components + .guest_capabilities[:openbsd] + .get(:change_host_name) + end + + 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 + 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"}] + ] } + + 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 + + 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 + + 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/vagrant/util/guest_hosts_test.rb b/test/unit/vagrant/util/guest_hosts_test.rb index d64aa85ed..bc48dc81f 100644 --- a/test/unit/vagrant/util/guest_hosts_test.rb +++ b/test/unit/vagrant/util/guest_hosts_test.rb @@ -26,12 +26,12 @@ describe "Vagrant::Util::GuestHosts" do end end - describe "Darwin" do - subject{ Class.new { extend Vagrant::Util::GuestHosts::Darwin } } + describe "BSD" do + subject{ Class.new { extend Vagrant::Util::GuestHosts::BSD } } it "can add replace hostname" do subject.replace_host(comm, "test.end", "192.186.4.2") - expect(comm.received_commands[0]).to match(/sed -i '' '\/test.end\/d' \/etc\/hosts/) + expect(comm.received_commands[0]).to match(/sed -i.bak '\/test.end\/d' \/etc\/hosts/) end it "can add hostname to loopback interface" do