From fee0545b2344a18cf8bce4c162d94ee13b3f2f5b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 30 May 2016 23:51:11 -0400 Subject: [PATCH] guests/coreos: Do not use sudo for looking up hostname --- plugins/guests/coreos/cap/change_host_name.rb | 14 ++++++++++---- .../guests/coreos/cap/change_host_name_test.rb | 16 ++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/plugins/guests/coreos/cap/change_host_name.rb b/plugins/guests/coreos/cap/change_host_name.rb index f568beadb..f1aeb5bc4 100644 --- a/plugins/guests/coreos/cap/change_host_name.rb +++ b/plugins/guests/coreos/cap/change_host_name.rb @@ -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 diff --git a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb index d893384ab..e29d52aa5 100644 --- a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb @@ -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