Update OpenBSD guest change hostname cap

This commit is contained in:
sophia 2020-06-29 17:02:43 -05:00
parent db6d1b4aa6
commit 8bad23d176
6 changed files with 95 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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