guests/coreos: Do not use sudo for looking up hostname

This commit is contained in:
Seth Vargo 2016-05-30 23:51:11 -04:00
parent 0b2804fbb0
commit fee0545b23
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
2 changed files with 18 additions and 12 deletions

View File

@ -3,10 +3,16 @@ module VagrantPlugins
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
if !comm.test("sudo hostname --fqdn | grep '#{name}'")
comm.sudo("hostname #{name.split('.')[0]}")
end
comm = machine.communicate
if !comm.test("hostname --fqdn | grep -w '#{name}'")
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

View File

@ -9,29 +9,29 @@ describe "VagrantPlugins::GuestCoreOS::Cap::ChangeHostName" do
end
let(:machine) { double("machine") }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:communicate).and_return(comm)
end
after do
communicator.verify_expectations!
comm.verify_expectations!
end
describe ".change_host_name" do
let(:hostname) { "example.com" }
let(:hostname) { "banana-rama.example.com" }
it "sets the hostname" do
communicator.stub_command("sudo hostname --fqdn | grep '#{hostname}'", exit_code: 1)
communicator.expect_command("hostname example")
comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 1)
comm.expect_command("hostname 'banana-rama'")
described_class.change_host_name(machine, hostname)
end
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)
expect(communicator.received_commands.size).to eq(1)
expect(comm.received_commands.size).to eq(1)
end
end
end