From 31da3860e5a0b31b3cc4c671a1192c4992fbcc98 Mon Sep 17 00:00:00 2001 From: Eric Saxby Date: Mon, 10 Mar 2014 18:52:20 -0700 Subject: [PATCH] Add unit tests for smartos compatibility --- .../guests/smartos/cap/change_host_name.rb | 2 +- .../guests/smartos/cap/configure_networks.rb | 3 +- plugins/guests/smartos/cap/rsync.rb | 2 +- plugins/guests/smartos/plugin.rb | 2 +- .../smartos/cap/change_host_name_test.rb | 33 ++++++++++ .../smartos/cap/configure_networks_test.rb | 61 +++++++++++++++++++ .../plugins/guests/smartos/cap/halt_test.rb | 25 ++++++++ .../guests/smartos/cap/mount_nfs_test.rb | 30 +++++++++ .../plugins/guests/smartos/cap/rsync_test.rb | 52 ++++++++++++++++ 9 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 test/unit/plugins/guests/smartos/cap/change_host_name_test.rb create mode 100644 test/unit/plugins/guests/smartos/cap/configure_networks_test.rb create mode 100644 test/unit/plugins/guests/smartos/cap/halt_test.rb create mode 100644 test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb create mode 100644 test/unit/plugins/guests/smartos/cap/rsync_test.rb diff --git a/plugins/guests/smartos/cap/change_host_name.rb b/plugins/guests/smartos/cap/change_host_name.rb index 9c5e6977c..3e9313dc8 100644 --- a/plugins/guests/smartos/cap/change_host_name.rb +++ b/plugins/guests/smartos/cap/change_host_name.rb @@ -6,7 +6,7 @@ module VagrantPlugins su_cmd = machine.config.smartos.suexec_cmd # Only do this if the hostname is not already set - if !machine.communicate.test("#{su_cmd} hostname | grep '#{name}'") + 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}") end diff --git a/plugins/guests/smartos/cap/configure_networks.rb b/plugins/guests/smartos/cap/configure_networks.rb index 2e660c3d9..dac941dab 100644 --- a/plugins/guests/smartos/cap/configure_networks.rb +++ b/plugins/guests/smartos/cap/configure_networks.rb @@ -3,9 +3,10 @@ module VagrantPlugins module Cap class ConfigureNetworks def self.configure_networks(machine, networks) + su_cmd = machine.config.smartos.suexec_cmd + networks.each do |network| device = "#{machine.config.smartos.device}#{network[:interface]}" - su_cmd = machine.config.smartos.suexec_cmd ifconfig_cmd = "#{su_cmd} /sbin/ifconfig #{device}" machine.communicate.execute("#{ifconfig_cmd} plumb") diff --git a/plugins/guests/smartos/cap/rsync.rb b/plugins/guests/smartos/cap/rsync.rb index 0217c4dcc..870de8001 100644 --- a/plugins/guests/smartos/cap/rsync.rb +++ b/plugins/guests/smartos/cap/rsync.rb @@ -6,7 +6,7 @@ module VagrantPlugins machine.communicate.test("which rsync") end - def self.rsync_pre(machine, folder_opts) + def self.rsync_install(machine, folder_opts) username = machine.ssh_info[:username] sudo = machine.config.smartos.suexec_cmd diff --git a/plugins/guests/smartos/plugin.rb b/plugins/guests/smartos/plugin.rb index f99f7e498..b88312d29 100644 --- a/plugins/guests/smartos/plugin.rb +++ b/plugins/guests/smartos/plugin.rb @@ -41,7 +41,7 @@ module VagrantPlugins Cap::RSync end - guest_capability("smartos", "rsync_pre") do + guest_capability("smartos", "rsync_install") do require_relative "cap/rsync" Cap::RSync 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 new file mode 100644 index 000000000..1d9a84692 --- /dev/null +++ b/test/unit/plugins/guests/smartos/cap/change_host_name_test.rb @@ -0,0 +1,33 @@ +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::VagrantPlugins::Cap::ChangeHostName" do + let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:change_host_name) } + let(:machine) { double("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(:config).and_return(config) + communicator.stub_command("hostname | grep '#{old_hostname}'", stdout: old_hostname) + end + + after do + communicator.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 + + 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) + end + end +end + diff --git a/test/unit/plugins/guests/smartos/cap/configure_networks_test.rb b/test/unit/plugins/guests/smartos/cap/configure_networks_test.rb new file mode 100644 index 000000000..711875573 --- /dev/null +++ b/test/unit/plugins/guests/smartos/cap/configure_networks_test.rb @@ -0,0 +1,61 @@ +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::VagrantPlugins::Cap::ConfigureNetworks" do + let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:configure_networks) } + let(:machine) { double("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(:config).and_return(config) + end + + after do + communicator.verify_expectations! + end + + describe ".configure_networks" do + let(:interface) { "eth0" } + let(:device) { "e1000g#{interface}" } + + describe 'dhcp' do + let(:network) { {:interface => interface, :type => :dhcp} } + + it "plumbs the device" do + communicator.expect_command(%Q(pfexec /sbin/ifconfig #{device} plumb)) + plugin.configure_networks(machine, [network]) + end + + it "starts dhcp for the device" do + communicator.expect_command(%Q(pfexec /sbin/ifconfig #{device} dhcp start)) + plugin.configure_networks(machine, [network]) + end + end + + describe 'static' do + let(:network) { {:interface => interface, :type => :static, :ip => '1.1.1.1', :netmask => '255.255.255.0'} } + + it "plumbs the network" do + communicator.expect_command(%Q(pfexec /sbin/ifconfig #{device} plumb)) + plugin.configure_networks(machine, [network]) + end + + it "starts sets netmask and IP for the device" do + communicator.expect_command(%Q(pfexec /sbin/ifconfig #{device} inet 1.1.1.1 netmask 255.255.255.0)) + plugin.configure_networks(machine, [network]) + end + + it "starts enables the device" do + communicator.expect_command(%Q(pfexec /sbin/ifconfig #{device} up)) + plugin.configure_networks(machine, [network]) + end + + it "starts writes out a hostname file" do + communicator.expect_command(%Q(pfexec sh -c "echo '1.1.1.1' > /etc/hostname.#{device}")) + plugin.configure_networks(machine, [network]) + end + end + end +end + diff --git a/test/unit/plugins/guests/smartos/cap/halt_test.rb b/test/unit/plugins/guests/smartos/cap/halt_test.rb new file mode 100644 index 000000000..782fc6b09 --- /dev/null +++ b/test/unit/plugins/guests/smartos/cap/halt_test.rb @@ -0,0 +1,25 @@ +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::VagrantPlugins::Cap::Halt" do + let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:halt) } + let(:machine) { double("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(:config).and_return(config) + end + + after do + communicator.verify_expectations! + end + + describe ".halt" do + it "sends a shutdown signal" do + communicator.expect_command(%Q(pfexec /usr/sbin/shutdown -y -i5 -g0)) + plugin.halt(machine) + end + end +end + diff --git a/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb b/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb new file mode 100644 index 000000000..cbf0b875a --- /dev/null +++ b/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb @@ -0,0 +1,30 @@ +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::VagrantPlugins::Cap::MountNFS" do + let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:mount_nfs_folder) } + let(:machine) { double("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(:config).and_return(config) + end + + after do + communicator.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 + + 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'}}) + end + end +end + diff --git a/test/unit/plugins/guests/smartos/cap/rsync_test.rb b/test/unit/plugins/guests/smartos/cap/rsync_test.rb new file mode 100644 index 000000000..d51910b7c --- /dev/null +++ b/test/unit/plugins/guests/smartos/cap/rsync_test.rb @@ -0,0 +1,52 @@ +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::VagrantPlugins::Cap::Rsync" do + let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:rsync_install) } + let(:machine) { double("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(:config).and_return(config) + end + + after do + communicator.verify_expectations! + end + + describe ".rsync_installed" do + describe "when rsync is in the path" do + it "is true" do + communicator.stub_command("which rsync", stdout: '/usr/bin/rsync', exit_code: 0) + expect(plugin.rsync_installed(machine)).to be true + end + end + + describe "when rsync is not in the path" do + it "is false" do + communicator.stub_command("which rsync", stdout: '', exit_code: 1) + expect(plugin.rsync_installed(machine)).to be false + end + end + end + + describe ".rsync_install" do + let(:username) { "some_user" } + + before do + machine.stub(:ssh_info).and_return({username: username}) + end + + it "creates a local directory" do + communicator.expect_command(%Q(pfexec mkdir -p '/mountpoint')) + plugin.rsync_install(machine, {guestpath: '/mountpoint'}) + end + + it "chowns local directory to ssh user" do + communicator.expect_command(%Q(pfexec chown -R #{username} '/mountpoint')) + plugin.rsync_install(machine, {guestpath: '/mountpoint'}) + end + end +end +