guests/atomic: Update hostname capability
This commit does a few things: 1. Make the hostname update idempotent with `grep -w` 2. Add the given hostname to `/etc/hosts` as recommended by the docs 3. Add missing tests
This commit is contained in:
parent
3a9ac19f7e
commit
4f0c3474f2
|
@ -3,7 +3,20 @@ module VagrantPlugins
|
|||
module Cap
|
||||
class ChangeHostName
|
||||
def self.change_host_name(machine, name)
|
||||
machine.communicate.sudo("hostnamectl set-hostname #{name}")
|
||||
comm = machine.communicate
|
||||
|
||||
if !comm.test("hostname | grep -w '#{name}'", sudo: true)
|
||||
basename = name.split(".", 2)[0]
|
||||
comm.sudo <<-EOH
|
||||
hostnamectl set-hostname '#{name}'
|
||||
|
||||
# Remove comments and blank lines from /etc/hosts
|
||||
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
|
||||
|
||||
# Prepend ourselves to /etc/hosts
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||
EOH
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
require_relative "../../../../base"
|
||||
|
||||
describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do
|
||||
let(:described_class) do
|
||||
VagrantPlugins::GuestAtomic::Plugin
|
||||
.components
|
||||
.guest_capabilities[:atomic]
|
||||
.get(:change_host_name)
|
||||
end
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
end
|
||||
|
||||
after do
|
||||
comm.verify_expectations!
|
||||
end
|
||||
|
||||
describe ".change_host_name" do
|
||||
let(:hostname) { "banana-rama.example.com" }
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1)
|
||||
|
||||
described_class.change_host_name(machine, hostname)
|
||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/)
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0)
|
||||
described_class.change_host_name(machine, hostname)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue