From cf130d07a267a1486c7bc33bacf96d270551e987 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 20 Jul 2020 13:34:02 -0500 Subject: [PATCH] Update omnios cap for setting hostname --- plugins/guests/omnios/cap/change_host_name.rb | 26 ++++++-------- .../omnios/cap/change_host_name_test.rb | 36 +++++++++++-------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/plugins/guests/omnios/cap/change_host_name.rb b/plugins/guests/omnios/cap/change_host_name.rb index 1fdcdd4b7..42572befd 100644 --- a/plugins/guests/omnios/cap/change_host_name.rb +++ b/plugins/guests/omnios/cap/change_host_name.rb @@ -1,24 +1,18 @@ +require_relative '../../linux/cap/change_host_name' + module VagrantPlugins module GuestOmniOS module Cap class ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Set hostname - echo '#{name}' > /etc/nodename - hostname '#{name}' - - # Prepend ourselves to /etc/hosts - grep -w '#{name}' /etc/hosts || { - echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts - mv /tmp/tmp-hosts /etc/hosts - } - EOH - end + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, "") + # Set hostname + echo '#{name}' > /etc/nodename + hostname '#{name}' + EOH end end end diff --git a/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb b/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb index c4afed849..b5a66a820 100644 --- a/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb @@ -1,14 +1,17 @@ require_relative "../../../../base" -describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do - let(:caps) do +describe "VagrantPlugins::GuestOmniOS::Cap::ChangeHostName" do + let(:described_class) do VagrantPlugins::GuestOmniOS::Plugin .components .guest_capabilities[:omnios] + .get(:change_host_name) end let(:machine) { double("machine") } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:name) { "banana-rama.example.com" } + let(:basename) { "banana-rama" } before do allow(machine).to receive(:communicate).and_return(comm) @@ -19,23 +22,28 @@ describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do end describe ".change_host_name" do - let(:cap) { caps.get(:change_host_name) } + context "minimal network config" do + let(:networks) { [ + [:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}] + ] } - let(:name) { "banana-rama.example.com" } + before do + allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks) + end - it "sets the hostname if unset" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - cap.change_host_name(machine, name) + it "sets the hostname" do + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/nodename/) - expect(comm.received_commands[1]).to match(/hostname '#{name}'/) - end + described_class.change_host_name(machine, name) + expect(comm.received_commands[2]).to match(/hostname '#{name}'/) + end - it "does not set the hostname if set" do - comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) - cap.change_host_name(machine, name) + it "does not change the hostname if already set" do + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) - expect(comm.received_commands.size).to eq(1) + described_class.change_host_name(machine, name) + expect(comm).to_not receive(:sudo).with(/hostname '#{name}'/) + end end end end