From b0b9d044b57dc766424edf5d378fe6fc91bb5d4d Mon Sep 17 00:00:00 2001 From: James Nugent Date: Fri, 16 Jun 2017 16:58:07 -0400 Subject: [PATCH] guests/smartos: Add/fix various guest capabilities This commit adds a variety of fixes for SmartOS guest support: - Host name setting now works in the global zone and in non-global zones - NFS now works in the global zone and the non-global zone. - Tests are updated and moved to the (apparently) more modern style --- .../guests/smartos/cap/change_host_name.rb | 19 +++++++--- plugins/guests/smartos/cap/mount_nfs.rb | 13 ++++++- .../smartos/cap/change_host_name_test.rb | 38 +++++++++++-------- .../smartos/cap/insert_public_key_test.rb | 2 +- .../guests/smartos/cap/mount_nfs_test.rb | 34 ++++++++++------- 5 files changed, 69 insertions(+), 37 deletions(-) diff --git a/plugins/guests/smartos/cap/change_host_name.rb b/plugins/guests/smartos/cap/change_host_name.rb index 3e9313dc8..7a5f6a351 100644 --- a/plugins/guests/smartos/cap/change_host_name.rb +++ b/plugins/guests/smartos/cap/change_host_name.rb @@ -3,12 +3,21 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - su_cmd = machine.config.smartos.suexec_cmd + sudo = machine.config.smartos.suexec_cmd - # Only do this if the hostname is not already set - if !machine.communicate.test("hostname | grep '#{name}'") - machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"") - machine.communicate.execute("#{su_cmd} hostname #{name}") + machine.communicate.tap do |comm| + comm.execute <<-EOH.sub(/^ */, '') + if hostname | grep '#{name}' ; then + exit 0 + fi + + if [ -d /usbkey ] && [ "$(zonename)" == "global" ] ; then + #{sudo} sed -i '' 's/hostname=.*/hostname=#{name}/' /usbkey/config + fi + + #{sudo} echo '#{name}' > /etc/nodename + #{sudo} hostname #{name} + EOH end end end diff --git a/plugins/guests/smartos/cap/mount_nfs.rb b/plugins/guests/smartos/cap/mount_nfs.rb index d23bbcf5d..26aab038e 100644 --- a/plugins/guests/smartos/cap/mount_nfs.rb +++ b/plugins/guests/smartos/cap/mount_nfs.rb @@ -7,8 +7,17 @@ module VagrantPlugins folders.each do |name, opts| machine.communicate.tap do |comm| - comm.execute("#{sudo} mkdir -p #{opts[:guestpath]}", {shell: "sh"}) - comm.execute("#{sudo} /usr/sbin/mount -F nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {shell: "sh"}) + nfsDescription = "#{ip}:#{opts[:hostpath]}:#{opts[:guestpath]}" + + comm.execute <<-EOH.sub(/^ */, '') + if [ -d /usbkey ] && [ "$(zonename)" == "global" ] ; then + #{sudo} mkdir -p /usbkey/config.inc + printf '#{nfsDescription}\\n' | #{sudo} tee -a /usbkey/config.inc/nfs_mounts + fi + + #{sudo} mkdir -p #{opts[:guestpath]} + #{sudo} /usr/sbin/mount -F nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}' + EOH end end end diff --git a/test/unit/plugins/guests/smartos/cap/change_host_name_test.rb b/test/unit/plugins/guests/smartos/cap/change_host_name_test.rb index 3c91edf92..c51dfb9eb 100644 --- a/test/unit/plugins/guests/smartos/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/smartos/cap/change_host_name_test.rb @@ -1,33 +1,39 @@ require_relative "../../../../base" -require_relative "../../../../../../plugins/guests/smartos/config" -describe "VagrantPlugins::VagrantPlugins::Cap::ChangeHostName" do - let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:change_host_name) } +describe "VagrantPlugins::GuestSmartos::Cap::ChangeHostName" do + let(:caps) do + VagrantPlugins::GuestSmartos::Plugin + .components + .guest_capabilities[:smartos] + end + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) } - let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - let(:old_hostname) { 'oldhostname.olddomain.tld' } - let(:new_hostname) { 'newhostname.olddomain.tld' } before do - machine.stub(:communicate).and_return(communicator) + machine.stub(:communicate).and_return(comm) machine.stub(:config).and_return(config) - communicator.stub_command("hostname | grep '#{old_hostname}'", stdout: old_hostname) end after do - communicator.verify_expectations! + comm.verify_expectations! end describe ".change_host_name" do - it "refreshes the hostname service with the hostname command" do - communicator.expect_command(%Q(pfexec hostname #{new_hostname})) - plugin.change_host_name(machine, new_hostname) - end + let(:cap) { caps.get(:change_host_name) } - it "writes the hostname into /etc/nodename" do - communicator.expect_command(%Q(pfexec sh -c "echo '#{new_hostname}' > /etc/nodename")) - plugin.change_host_name(machine, new_hostname) + it "changes the hostname if appropriate" do + cap.change_host_name(machine, "testhost") + + expect(comm.received_commands[0]).to match(/if hostname | grep 'testhost' ; then/) + expect(comm.received_commands[0]).to match(/exit 0/) + expect(comm.received_commands[0]).to match(/fi/) + expect(comm.received_commands[0]).to match(/if \[ -d \/usbkey \] && \[ "\$\(zonename\)" == "global" \] ; then/) + expect(comm.received_commands[0]).to match(/pfexec sed -i '' 's\/hostname=\.\*\/hostname=testhost\/' \/usbkey\/config/) + expect(comm.received_commands[0]).to match(/fi/) + expect(comm.received_commands[0]).to match(/pfexec echo 'testhost' > \/etc\/nodename/) + expect(comm.received_commands[0]).to match(/pfexec hostname testhost/) end end end diff --git a/test/unit/plugins/guests/smartos/cap/insert_public_key_test.rb b/test/unit/plugins/guests/smartos/cap/insert_public_key_test.rb index 917f0baa9..b404412ab 100644 --- a/test/unit/plugins/guests/smartos/cap/insert_public_key_test.rb +++ b/test/unit/plugins/guests/smartos/cap/insert_public_key_test.rb @@ -11,7 +11,7 @@ describe "VagrantPlugins::GuestSmartos::Cap::InsertPublicKey" do let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do - allow(machine).to receive(:communicate).and_return(comm) + machine.stub(:communicate).and_return(comm) end after do diff --git a/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb b/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb index d41dcd808..5dee28a09 100644 --- a/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb +++ b/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb @@ -1,30 +1,38 @@ require_relative "../../../../base" +require_relative "../../../../../../plugins/guests/smartos/config" + +describe "VagrantPlugins::GuestSmartos::Cap::MountNFS" do + let(:caps) do + VagrantPlugins::GuestSmartos::Plugin + .components + .guest_capabilities[:smartos] + end -describe "VagrantPlugins::VagrantPlugins::Cap::MountNFS" do - let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:mount_nfs_folder) } let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) } - let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do - machine.stub(:communicate).and_return(communicator) + machine.stub(:communicate).and_return(comm) machine.stub(:config).and_return(config) end after do - communicator.verify_expectations! + comm.verify_expectations! end describe ".mount_nfs_folder" do - it "creates the directory mount point" do - communicator.expect_command(%Q(pfexec mkdir -p /mountpoint)) - plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint'}}) - end + let(:cap) { caps.get(:mount_nfs_folder) } - it "mounts the NFS share" do - communicator.expect_command(%Q(pfexec /usr/sbin/mount -F nfs '1.1.1.1:/some/share' '/mountpoint')) - plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint', hostpath: '/some/share'}}) + it "mounts the folder" do + cap.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint', hostpath: '/some/share'}}) + + expect(comm.received_commands[0]).to match(/if \[ -d \/usbkey \] && \[ "\$\(zonename\)" == "global" \] ; then/) + expect(comm.received_commands[0]).to match(/pfexec mkdir -p \/usbkey\/config.inc/) + expect(comm.received_commands[0]).to match(/printf '1\.1\.1\.1:\/some\/share:\/mountpoint' | pfexec tee -a \/usbkey\/config.inc\/nfs_mounts/) + expect(comm.received_commands[0]).to match(/fi/) + expect(comm.received_commands[0]).to match(/pfexec mkdir -p \/mountpoint/) + expect(comm.received_commands[0]).to match(/pfexec \/usr\/sbin\/mount -F nfs '1\.1\.1\.1:\/some\/share' '\/mountpoint'/) end end end -