guests/coreos: Do not use sudo for looking up hostname
This commit is contained in:
parent
0b2804fbb0
commit
fee0545b23
|
@ -3,10 +3,16 @@ module VagrantPlugins
|
||||||
module Cap
|
module Cap
|
||||||
class ChangeHostName
|
class ChangeHostName
|
||||||
def self.change_host_name(machine, name)
|
def self.change_host_name(machine, name)
|
||||||
machine.communicate.tap do |comm|
|
comm = machine.communicate
|
||||||
if !comm.test("sudo hostname --fqdn | grep '#{name}'")
|
|
||||||
comm.sudo("hostname #{name.split('.')[0]}")
|
if !comm.test("hostname --fqdn | grep -w '#{name}'")
|
||||||
end
|
basename = name.split(".", 2)[0]
|
||||||
|
comm.sudo("hostname '#{basename}'")
|
||||||
|
|
||||||
|
# Note that when working with CoreOS, we explicitly do not add the
|
||||||
|
# entry to /etc/hosts because this file does not exist on CoreOS.
|
||||||
|
# We could create it, but the recommended approach on CoreOS is to
|
||||||
|
# use Fleet to manage /etc/hosts files.
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,29 +9,29 @@ describe "VagrantPlugins::GuestCoreOS::Cap::ChangeHostName" do
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(machine).to receive(:communicate).and_return(communicator)
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
communicator.verify_expectations!
|
comm.verify_expectations!
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".change_host_name" do
|
describe ".change_host_name" do
|
||||||
let(:hostname) { "example.com" }
|
let(:hostname) { "banana-rama.example.com" }
|
||||||
|
|
||||||
it "sets the hostname" do
|
it "sets the hostname" do
|
||||||
communicator.stub_command("sudo hostname --fqdn | grep '#{hostname}'", exit_code: 1)
|
comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 1)
|
||||||
communicator.expect_command("hostname example")
|
comm.expect_command("hostname 'banana-rama'")
|
||||||
described_class.change_host_name(machine, hostname)
|
described_class.change_host_name(machine, hostname)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not change the hostname if already set" do
|
it "does not change the hostname if already set" do
|
||||||
communicator.stub_command("sudo hostname --fqdn | grep '#{hostname}'", exit_code: 0)
|
comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 0)
|
||||||
described_class.change_host_name(machine, hostname)
|
described_class.change_host_name(machine, hostname)
|
||||||
expect(communicator.received_commands.size).to eq(1)
|
expect(comm.received_commands.size).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue