diff --git a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb index ce18667ef..726b2926b 100644 --- a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb @@ -1,38 +1,41 @@ require_relative "../../../../base" -require_relative "../../support/shared/debian_like_host_name_examples" describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do - let(:described_class) do + let(:caps) do VagrantPlugins::GuestDebian::Plugin .components .guest_capabilities[:debian] - .get(:change_host_name) end let(:machine) { double("machine") } - let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - let(:old_hostname) { 'oldhostname.olddomain.tld' } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do - allow(machine).to receive(:communicate).and_return(communicator) - communicator.stub_command('hostname -f', stdout: old_hostname) + allow(machine).to receive(:communicate).and_return(comm) end after do - communicator.verify_expectations! + comm.verify_expectations! end describe ".change_host_name" do - it_behaves_like "a debian-like host name change" + let(:cap) { caps.get(:change_host_name) } - it "refreshes the hostname service with the hostname command" do - communicator.expect_command(%q(hostname -F /etc/hostname)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') + let(:name) { 'banana-rama.example.com' } + + it "sets the hostname if not set" 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(/hostname -F \/etc\/hostname/) + expect(comm.received_commands[1]).to match(/invoke-rc.d hostname.sh start/) + expect(comm.received_commands[1]).to match(/invoke-rc.d networking force-reload/) + expect(comm.received_commands[1]).to match(/invoke-rc.d network-manager force-reload/) end - it "renews dhcp on the system with the new hostname" do - communicator.expect_command(%q(ifdown -a; ifup -a; ifup eth0)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') + it "does not set the hostname if unset" 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 diff --git a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb index 9045f6af8..bde388906 100644 --- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb @@ -1,11 +1,10 @@ require_relative "../../../../base" describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do - let(:described_class) do + let(:caps) do VagrantPlugins::GuestDebian::Plugin .components .guest_capabilities[:debian] - .get(:configure_networks) end let(:machine) { double("machine") } @@ -13,6 +12,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do before do allow(machine).to receive(:communicate).and_return(comm) + comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", + stdout: "eth1\neth2") end after do @@ -20,6 +21,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + let(:network_0) do { interface: 0, @@ -38,14 +41,14 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - described_class.configure_networks(machine, [network_0, network_1]) + cap.configure_networks(machine, [network_0, network_1]) - expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth0' 2> /dev/null || true") - expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth0' 2> /dev/null") - expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth1' 2> /dev/null || true") - expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth1' 2> /dev/null") - expect(comm.received_commands[0]).to match("/sbin/ifup 'eth0'") - expect(comm.received_commands[0]).to match("/sbin/ifup 'eth1'") + expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth1' || true") + expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth1'") + expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth2' || true") + expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth2'") + expect(comm.received_commands[1]).to match("/sbin/ifup 'eth1'") + expect(comm.received_commands[1]).to match("/sbin/ifup 'eth2'") end end end diff --git a/test/unit/plugins/guests/support/shared/debian_like_host_name_examples.rb b/test/unit/plugins/guests/support/shared/debian_like_host_name_examples.rb deleted file mode 100644 index 94d2478a7..000000000 --- a/test/unit/plugins/guests/support/shared/debian_like_host_name_examples.rb +++ /dev/null @@ -1,106 +0,0 @@ -shared_examples "a debian-like host name change" do - it "updates /etc/hostname on the machine" do - communicator.expect_command(%q(echo 'newhostname' > /etc/hostname)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') - end - - it "updates mailname to prevent problems with the default mailer" do - communicator.expect_command(%q(hostname --fqdn > /etc/mailname)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') - end - - it "does nothing when the provided hostname is not different" do - described_class.change_host_name(machine, 'oldhostname.olddomain.tld') - expect(communicator.received_commands).to eq(['hostname -f']) - end - - describe "flipping out the old hostname in /etc/hosts" do - let(:sed_command) do - # Here we run the change_host_name through and extract the recorded sed - # command from the dummy communicator - described_class.change_host_name(machine, 'newhostname.newdomain.tld') - communicator.received_commands.find { |cmd| cmd =~ /^sed/ } - end - - # Now we extract the regexp from that sed command so we can do some - # verification on it - let(:expression) { sed_command.sub(%r{^sed -ri '\(.*\)' /etc/hosts$}, "\1") } - let(:search) { Regexp.new(expression.split('@')[1], Regexp::EXTENDED) } - let(:replace) { expression.split('@')[2] } - - let(:grep_command) { "grep '#{old_hostname}' /etc/hosts" } - - before do - communicator.stub_command(grep_command, exit_code: 0) - end - - it "works on an simple /etc/hosts file" do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 oldhostname.olddomain.tld oldhostname - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 newhostname.newdomain.tld newhostname - RESULT - end - - it "does not modify lines which contain similar hostnames" do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 oldhostname.olddomain.tld oldhostname - - # common prefix, but different fqdn - 192.168.12.34 oldhostname.olddomain.tld.different - - # different characters at the dot - 192.168.34.56 oldhostname-olddomain.tld - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 newhostname.newdomain.tld newhostname - - # common prefix, but different fqdn - 192.168.12.34 oldhostname.olddomain.tld.different - - # different characters at the dot - 192.168.34.56 oldhostname-olddomain.tld - RESULT - end - - it "appends 127.0.1.1 if it isn't there" do - communicator.stub_command(grep_command, exit_code: 1) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') - - sed = communicator.received_commands.find { |cmd| cmd =~ /^sed/ } - expect(sed).to be_nil - - echo = communicator.received_commands.find { |cmd| cmd =~ /^echo/ } - expect(echo).to_not be_nil - end - - context "when the old fqdn has a trailing dot" do - let(:old_hostname) { 'oldhostname.withtrailing.dot.' } - - it "modifies /etc/hosts properly" do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 oldhostname.withtrailing.dot. oldhostname - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 localhost - 127.0.1.1 newhostname.newdomain.tld newhostname - RESULT - end - end - end -end diff --git a/test/unit/plugins/guests/support/shared/redhat_like_host_name_examples.rb b/test/unit/plugins/guests/support/shared/redhat_like_host_name_examples.rb deleted file mode 100644 index 1b4959449..000000000 --- a/test/unit/plugins/guests/support/shared/redhat_like_host_name_examples.rb +++ /dev/null @@ -1,252 +0,0 @@ -shared_examples 'a partial redhat-like host name change' do - shared_examples 'shared between newhostname styles' do - - it 'sets dhcp_hostname with the provided short hostname' do - communicator.expect_command(%q(sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1"newhostname"/' /etc/sysconfig/network-scripts/ifcfg-*)) - described_class.change_host_name(machine, new_hostname) - end - - it 'restarts networking' do - communicator.expect_command(%q(service network restart)) - described_class.change_host_name(machine, new_hostname) - end - end - - context 'when newhostname is qualified' do - let(:new_hostname) {'newhostname.newdomain.tld'} - - include_examples 'shared between newhostname styles' - - it 'updates sysconfig with the provided full hostname' do - communicator.expect_command(%q(sed -i 's/\\(HOSTNAME=\\).*/\\1newhostname.newdomain.tld/' /etc/sysconfig/network)) - described_class.change_host_name(machine, new_hostname) - end - - it 'updates hostname on the machine with the new hostname' do - communicator.expect_command(%q(hostname newhostname.newdomain.tld)) - described_class.change_host_name(machine, new_hostname) - end - end - - context 'when newhostname is simple' do - let(:new_hostname) {'newhostname'} - - include_examples 'shared between newhostname styles' - - it 'updates sysconfig with as much hostname as is available' do - communicator.expect_command(%q(sed -i 's/\\(HOSTNAME=\\).*/\\1newhostname/' /etc/sysconfig/network)) - described_class.change_host_name(machine, new_hostname) - end - - it 'updates hostname on the machine with the new hostname' do - communicator.expect_command(%q(hostname newhostname)) - described_class.change_host_name(machine, new_hostname) - end - - end -end - -shared_examples 'a full redhat-like host name change' do - include_examples 'a partial redhat-like host name change' - - it "does nothing when the provided hostname is not different" do - described_class.change_host_name(machine, old_hostname) - expect(communicator.received_commands.to_set).to eq(communicator.expected_commands.keys.to_set) - end - - it "does more when the provided hostname is a similar version" do - described_class.change_host_name(machine, similar_hostname) - expect(communicator.received_commands.to_set).not_to eq(communicator.expected_commands.keys.to_set) - end -end - -shared_examples 'mutating /etc/hosts helpers' do - let(:sed_command) do - # Here we run the change_host_name through and extract the recorded sed - # command from the dummy communicator - described_class.change_host_name(machine, new_hostname) - communicator.received_commands.find { |cmd| cmd =~ %r(^sed .* /etc/hosts$) } - end - - # Now we extract the regexp from that sed command so we can do some - # verification on it - let(:expression) { sed_command.sub(%r{^sed -i '\(.*\)' /etc/hosts$}, "\1") } - let(:search) { Regexp.new(expression.split('@')[1].gsub(/\\/,'')) } - let(:replace) { expression.split('@')[2] } -end - -shared_examples 'inserting hostname in /etc/hosts' do - include_examples 'mutating /etc/hosts helpers' - - context 'when target hostname is qualified' do - let(:new_hostname) {'newhostname.newdomain.tld'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname.newdomain.tld newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end - - context 'when target hostname is simple' do - let(:new_hostname) {'newhostname'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end -end - -shared_examples 'swapping simple hostname in /etc/hosts' do - include_examples 'mutating /etc/hosts helpers' - - context 'when target hostname is qualified' do - let(:new_hostname) {'newhostname.newdomain.tld'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname.newdomain.tld newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - - it 'does not touch suffixed hosts' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname.newdomain.tld newhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end - - context 'when target hostname is simple' do - let(:new_hostname) {'newhostname'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - - it 'does not touch suffixed hosts' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end -end - -shared_examples 'swapping qualified hostname in /etc/hosts' do - include_examples 'mutating /etc/hosts helpers' - - context 'when target hostname is qualified' do - let(:new_hostname) {'newhostname.newdomain.tld'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname.olddomain.tld oldhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname.newdomain.tld newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - - it 'does not touch suffixed hosts' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname.olddomain.tld oldhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname.newdomain.tld newhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end - - context 'when target hostname is simple' do - let(:new_hostname) {'newhostname'} - - it 'works with a basic file' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname.olddomain.tld oldhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - - it 'does not touch suffixed hosts' do - original_etc_hosts = <<-ETC_HOSTS.gsub(/^ */, '') - 127.0.0.1 oldhostname.olddomain.tld oldhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - ETC_HOSTS - - modified_etc_hosts = original_etc_hosts.gsub(search, replace) - - expect(modified_etc_hosts).to eq <<-RESULT.gsub(/^ */, '') - 127.0.0.1 newhostname oldhostname.nope localhost.localdomain localhost - ::1 localhost6.localdomain6 localhost6 - RESULT - end - end -end diff --git a/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb b/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb index 15473ea3c..d184ad9cb 100644 --- a/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb @@ -1,34 +1,42 @@ -require File.expand_path("../../../../../base", __FILE__) -require File.expand_path("../../../support/shared/debian_like_host_name_examples", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::GuestUbuntu::Cap::ChangeHostName" do - let(:described_class) do - VagrantPlugins::GuestUbuntu::Plugin.components.guest_capabilities[:ubuntu].get(:change_host_name) + let(:caps) do + VagrantPlugins::GuestUbuntu::Plugin + .components + .guest_capabilities[:ubuntu] end + let(:machine) { double("machine") } - let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - let(:old_hostname) {'oldhostname.olddomain.tld' } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do - allow(machine).to receive(:communicate).and_return(communicator) - communicator.stub_command('hostname -f', stdout: old_hostname) + allow(machine).to receive(:communicate).and_return(comm) end after do - communicator.verify_expectations! + comm.verify_expectations! end describe ".change_host_name" do - it_behaves_like "a debian-like host name change" + let(:cap) { caps.get(:change_host_name) } - it "refreshes the hostname service with upstart" do - communicator.expect_command(%q(service hostname start)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') + let(:name) { 'banana-rama.example.com' } + + it "sets the hostname if not set" 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(/hostname -F \/etc\/hostname/) + expect(comm.received_commands[1]).to match(/\/etc\/init.d\/hostname.sh start/) + expect(comm.received_commands[1]).to match(/\/etc\/init.d\/hostname start/) + expect(comm.received_commands[1]).to match(/\/etc\/init.d\/networking force-reload/) + expect(comm.received_commands[1]).to match(/\/etc\/init.d\/network-manager force-reload/) end - it "renews dhcp on the system with the new hostname (with hotplug allowed)" do - communicator.expect_command(%q(ifdown -a; ifup -a; ifup -a --allow=hotplug)) - described_class.change_host_name(machine, 'newhostname.newdomain.tld') + it "does not set the hostname if unset" 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