guests/photon: Change host name in one command
This commit is contained in:
parent
a9fb66b3e3
commit
2e943428a9
|
@ -3,10 +3,23 @@ module VagrantPlugins
|
|||
module Cap
|
||||
class ChangeHostName
|
||||
def self.change_host_name(machine, name)
|
||||
machine.communicate.tap do |comm|
|
||||
unless comm.test("sudo hostname --fqdn | grep '#{name}'")
|
||||
comm.sudo("hostname #{name.split('.')[0]}")
|
||||
end
|
||||
comm = machine.communicate
|
||||
|
||||
if !comm.test("hostname -f | grep -w '#{name}'", sudo: false)
|
||||
basename = name.split(".", 2)[0]
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||
# Set the hostname
|
||||
echo '#{name}' > /etc/hostname
|
||||
hostname '#{name}'
|
||||
|
||||
# Remove comments and blank lines from /etc/hosts
|
||||
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
|
||||
|
||||
# Prepend ourselves to /etc/hosts
|
||||
grep -w '#{name}' /etc/hosts || {
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||
}
|
||||
EOH
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,34 +1,42 @@
|
|||
# encoding: UTF-8
|
||||
# Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
||||
|
||||
require File.expand_path("../../../../../base", __FILE__)
|
||||
require_relative "../../../../base"
|
||||
|
||||
describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do
|
||||
let(:described_class) do
|
||||
VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:change_host_name)
|
||||
let(:caps) do
|
||||
VagrantPlugins::GuestPhoton::Plugin
|
||||
.components
|
||||
.guest_capabilities[:photon]
|
||||
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
|
||||
|
||||
it 'should change hostname when hostname is differ from current' do
|
||||
hostname = 'vagrant-photon'
|
||||
expect(communicator).to receive(:test).with("sudo hostname --fqdn | grep 'vagrant-photon'")
|
||||
communicator.should_receive(:sudo).with("hostname #{hostname.split('.')[0]}")
|
||||
described_class.change_host_name(machine, hostname)
|
||||
end
|
||||
describe ".change_host_name" do
|
||||
let(:cap) { caps.get(:change_host_name) }
|
||||
|
||||
it 'should not change hostname when hostname equals current' do
|
||||
hostname = 'vagrant-photon'
|
||||
communicator.stub(:test).and_return(true)
|
||||
communicator.should_not_receive(:sudo)
|
||||
described_class.change_host_name(machine, hostname)
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1)
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/hostname/)
|
||||
expect(comm.received_commands[1]).to match(/hostname '#{name}'/)
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0)
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue