diff --git a/plugins/guests/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index be0fd3079..fc3ec4e3e 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -3,12 +3,21 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.tap do |comm| - # Only do this if the hostname is not already set - if !comm.test("sudo hostname | grep '#{name}'") - comm.sudo("hostnamectl set-hostname #{name}") - comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} @' /etc/hosts") - end + comm = machine.communicate + + if !comm.test("hostname | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH +hostnamectl set-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 diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index e029c0732..9915b4a32 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- require "tempfile" require_relative "../../../../lib/vagrant/util/template_renderer" @@ -10,32 +9,45 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - tempfiles = [] + comm = machine.communicate + + commands = [] interfaces = [] - machine.communicate.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result| - interfaces = result.split("\n") + # The result will be something like: + # eth0\nenp0s8\nenp0s9 + comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| + interfaces = stdout.split("\n") end networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}", - options: network) + options: network, + ) - remote_path = "/tmp/vagrant-network-#{Time.now.to_i}-#{i}" + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" Tempfile.open("vagrant-arch-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, remote_path) + comm.upload(f.path, remote_path) end - machine.communicate.sudo("mv #{remote_path} /etc/netctl/#{network[:device]}") - machine.communicate.sudo("ip link set #{network[:device]} down && netctl restart #{network[:device]} && netctl enable #{network[:device]}") + commands << <<-EOH.gsub(/^ {14}/, '') + # Configure #{network[:device]} + mv '#{remote_path}' '/etc/netctl/#{network[:device]}' + ip link set '#{network[:device]}' down + netctl restart '#{network[:device]}' + netctl enable '#{network[:device]}' + EOH end + + # Run all the network modification commands in one communicator call. + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/arch/plugin.rb b/plugins/guests/arch/plugin.rb index ab3a0b016..4040bb382 100644 --- a/plugins/guests/arch/plugin.rb +++ b/plugins/guests/arch/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Arch guest support." guest("arch", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index aecec88e2..70e936321 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -3,7 +3,22 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.sudo("hostnamectl set-hostname #{name}") + comm = machine.communicate + + if !comm.test("hostname | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH +hostnamectl set-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 end diff --git a/plugins/guests/atomic/plugin.rb b/plugins/guests/atomic/plugin.rb index a410ed2fc..ea0f5e454 100644 --- a/plugins/guests/atomic/plugin.rb +++ b/plugins/guests/atomic/plugin.rb @@ -1,4 +1,4 @@ -require 'vagrant' +require "vagrant" module VagrantPlugins module GuestAtomic @@ -7,7 +7,7 @@ module VagrantPlugins description "Atomic Host guest support." guest("atomic", "fedora") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/coreos/cap/change_host_name.rb b/plugins/guests/coreos/cap/change_host_name.rb index f568beadb..f1aeb5bc4 100644 --- a/plugins/guests/coreos/cap/change_host_name.rb +++ b/plugins/guests/coreos/cap/change_host_name.rb @@ -3,10 +3,16 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.tap do |comm| - if !comm.test("sudo hostname --fqdn | grep '#{name}'") - comm.sudo("hostname #{name.split('.')[0]}") - end + comm = machine.communicate + + if !comm.test("hostname --fqdn | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo("hostname '#{basename}'") + + # Note that when working with CoreOS, we explicitly do not add the + # entry to /etc/hosts because this file does not exist on CoreOS. + # We could create it, but the recommended approach on CoreOS is to + # use Fleet to manage /etc/hosts files. end end end diff --git a/plugins/guests/coreos/cap/configure_networks.rb b/plugins/guests/coreos/cap/configure_networks.rb index 887d68c8c..9aff20837 100644 --- a/plugins/guests/coreos/cap/configure_networks.rb +++ b/plugins/guests/coreos/cap/configure_networks.rb @@ -10,47 +10,26 @@ module VagrantPlugins def self.configure_networks(machine, networks) machine.communicate.tap do |comm| - # Disable default etcd - comm.sudo("systemctl stop etcd") - # Read network interface names interfaces = [] comm.sudo("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:") do |_, result| interfaces = result.split("\n") end - # Configure interfaces - # FIXME: fix matching of interfaces with IP adresses - networks.each do |network| - comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}") - end - primary_machine_config = machine.env.active_machines.first primary_machine = machine.env.machine(*primary_machine_config, true) - get_ip = lambda do |machine| - ip = nil - machine.config.vm.networks.each do |type, opts| - if type == :private_network && opts[:ip] - ip = opts[:ip] - break - end - end - - ip - end - - primary_machine_ip = get_ip.(primary_machine) - current_ip = get_ip.(machine) + primary_machine_ip = get_ip(primary_machine) + current_ip = get_ip(machine) if current_ip == primary_machine_ip entry = TemplateRenderer.render("guests/coreos/etcd.service", options: { - my_ip: current_ip - }) + my_ip: current_ip, + }) else connection_string = "#{primary_machine_ip}:7001" entry = TemplateRenderer.render("guests/coreos/etcd.service", options: { connection_string: connection_string, - my_ip: current_ip + my_ip: current_ip, }) end @@ -62,13 +41,45 @@ module VagrantPlugins comm.upload(f.path, "/tmp/etcd-cluster.service") end - comm.sudo("mv /tmp/etcd-cluster.service /media/state/units/") - comm.sudo("systemctl restart local-enable.service") + # Build a list of commands + commands = [] - # Restart default etcd - comm.sudo("systemctl start etcd") + # Stop default systemd + commands << "systemctl stop etcd" + + # Configure interfaces + # FIXME: fix matching of interfaces with IP adresses + networks.each do |network| + iface = interfaces[network[:interface].to_i] + commands << "ifconfig #{iface} #{network[:ip]} netmask #{network[:netmask]}".squeeze(" ") + end + + commands << <<-EOH.gsub(/^ {14}/, '') + mv /tmp/etcd-cluster.service /media/state/units/ + systemctl restart local-enable.service + + # Restart default etcd + systemctl start etcd + EOH + + # Run all network configuration commands in one communicator session. + comm.sudo(commands.join("\n")) end end + + private + + def self.get_ip(machine) + ip = nil + machine.config.vm.networks.each do |type, opts| + if type == :private_network && opts[:ip] + ip = opts[:ip] + break + end + end + + ip + end end end end diff --git a/plugins/guests/coreos/guest.rb b/plugins/guests/coreos/guest.rb index cd7e76064..2fe718a54 100644 --- a/plugins/guests/coreos/guest.rb +++ b/plugins/guests/coreos/guest.rb @@ -1,3 +1,5 @@ +require "vagrant" + module VagrantPlugins module GuestCoreOS class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/coreos/plugin.rb b/plugins/guests/coreos/plugin.rb index 880f5bf4d..a81c047a0 100644 --- a/plugins/guests/coreos/plugin.rb +++ b/plugins/guests/coreos/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "CoreOS guest support." guest("coreos", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/darwin/cap/change_host_name.rb b/plugins/guests/darwin/cap/change_host_name.rb index c65c32642..b7b69faa7 100644 --- a/plugins/guests/darwin/cap/change_host_name.rb +++ b/plugins/guests/darwin/cap/change_host_name.rb @@ -3,13 +3,31 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'") - machine.communicate.sudo("scutil --set ComputerName #{name}") - machine.communicate.sudo("scutil --set HostName #{name}") - # LocalHostName shouldn't contain dots. - # It is used by Bonjour and visible through file sharing services. - machine.communicate.sudo("scutil --set LocalHostName #{name.gsub(/\.+/, '')}") - machine.communicate.sudo("hostname #{name}") + comm = machine.communicate + + if !comm.test("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'") + basename = name.split(".", 2)[0] + + comm.sudo <<-EOH.gsub(/^ {14}/, '') + scutil --set ComputerName '#{name}' + scutil --set HostName '#{name}' + + # LocalHostName should not contain dots - it is used by Bonjour and + # visible through file sharing services. + scutil --set LocalHostName '#{basename}' + + hostname '#{name}' + + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' /etc/hosts + sed -i'' -e '/^$/d' /etc/hosts + + # Prepend ourselves to /etc/hosts - sed on bsd is sad + 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 end end diff --git a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb index f5b578ebd..d7327d6f7 100644 --- a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb +++ b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb @@ -3,16 +3,16 @@ module VagrantPlugins module Cap module ChooseAddressableIPAddr def self.choose_addressable_ip_addr(machine, possible) - machine.communicate.tap do |comm| - possible.each do |ip| - command = "ping -c1 -t1 #{ip}" - if comm.test(command) - return ip - end + comm = machine.communicate + + possible.each do |ip| + if comm.test("ping -c1 -t1 #{ip}") + return ip end end - nil + # If we got this far, there are no addressable IPs + return nil end end end diff --git a/plugins/guests/darwin/cap/insert_public_key.rb b/plugins/guests/darwin/cap/insert_public_key.rb index 59e5dd58c..bd40279ab 100644 --- a/plugins/guests/darwin/cap/insert_public_key.rb +++ b/plugins/guests/darwin/cap/insert_public_key.rb @@ -1,19 +1,31 @@ -require "vagrant/util/shell_quote" +require "tempfile" module VagrantPlugins module GuestDarwin module Cap class InsertPublicKey def self.insert_public_key(machine, contents) + comm = machine.communicate contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - machine.communicate.tap do |comm| - comm.execute("mkdir -p ~/.ssh") - comm.execute("chmod 0700 ~/.ssh") - comm.execute("printf '#{contents}\\n' >> ~/.ssh/authorized_keys") - comm.execute("chmod 0600 ~/.ssh/authorized_keys") + remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" + Tempfile.open("vagrant-darwin-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) end + + comm.execute <<-EOH.gsub(/^ {12}/, '') + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + # Remove the temporary file + rm -f '#{remote_path}' + EOH end end end diff --git a/plugins/guests/darwin/guest.rb b/plugins/guests/darwin/guest.rb index 0e8113e0a..22af43aee 100644 --- a/plugins/guests/darwin/guest.rb +++ b/plugins/guests/darwin/guest.rb @@ -1,4 +1,4 @@ -require 'vagrant/util/template_renderer' +require "vagrant" module VagrantPlugins module GuestDarwin diff --git a/plugins/guests/darwin/plugin.rb b/plugins/guests/darwin/plugin.rb index 8e6dc279b..83f92110c 100644 --- a/plugins/guests/darwin/plugin.rb +++ b/plugins/guests/darwin/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Darwin guest support." guest("darwin") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index 8cb781b12..a80d05516 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -2,92 +2,45 @@ module VagrantPlugins module GuestDebian module Cap class ChangeHostName + # For more information, please see: + # + # https://wiki.debian.org/HowTo/ChangeHostname + # def self.change_host_name(machine, name) - new(machine, name).change! - end + comm = machine.communicate - attr_reader :machine, :new_hostname + if !comm.test("hostname -f | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + echo '#{name}' > /etc/hostname + hostname -F /etc/hostname - def initialize(machine, new_hostname) - @machine = machine - @new_hostname = new_hostname - end + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts - def change! - return unless should_change? + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts + } - update_etc_hostname - update_etc_hosts - refresh_hostname_service - update_mailname - renew_dhcp - end + # Update mailname + echo '#{name}' > /etc/mailname - def should_change? - new_hostname != current_hostname - end + # Restart networking and force new DHCP + if [ test -f /etc/init.d/hostname.sh ]; then + invoke-rc.d hostname.sh start + fi - def current_hostname - @current_hostname ||= get_current_hostname - end + if [ test -f /etc/init.d/networking ]; then + invoke-rc.d networking force-reload + fi - def get_current_hostname - hostname = "" - sudo "hostname -f" do |type, data| - hostname = data.chomp if type == :stdout && hostname.empty? + if [ test -f /etc/init.d/network-manager ]; then + invoke-rc.d network-manager force-reload + fi + EOH end - - hostname - end - - def update_etc_hostname - sudo("echo '#{short_hostname}' > /etc/hostname") - end - - # /etc/hosts should resemble: - # 127.0.0.1 localhost - # 127.0.1.1 host.fqdn.com host.fqdn host - def update_etc_hosts - if test("grep '#{current_hostname}' /etc/hosts") - # Current hostname entry is in /etc/hosts - ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}' - search = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$" - replace = "\\1 #{fqdn} #{short_hostname}" - expression = ['s', search, replace, 'g'].join('@') - - sudo("sed -ri '#{expression}' /etc/hosts") - else - # Current hostname entry isn't in /etc/hosts, just append it - sudo("echo '127.0.1.1 #{fqdn} #{short_hostname}' >>/etc/hosts") - end - end - - def refresh_hostname_service - sudo("hostname -F /etc/hostname") - end - - def update_mailname - sudo("hostname --fqdn > /etc/mailname") - end - - def renew_dhcp - sudo("ifdown -a; ifup -a; ifup eth0") - end - - def fqdn - new_hostname - end - - def short_hostname - new_hostname.split('.').first - end - - def sudo(cmd, &block) - machine.communicate.sudo(cmd, &block) - end - - def test(cmd) - machine.communicate.test(cmd) end end end diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index a80a13563..e7fabcb68 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -1,4 +1,3 @@ -require "set" require "tempfile" require_relative "../../../../lib/vagrant/util/template_renderer" @@ -10,54 +9,66 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - machine.communicate.tap do |comm| - # First, remove any previous network modifications - # from the interface file. - comm.sudo("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre") - comm.sudo("sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post") + comm = machine.communicate - # Accumulate the configurations to add to the interfaces file as - # well as what interfaces we're actually configuring since we use that - # later. - interfaces = Set.new - entries = [] - networks.each do |network| - interfaces.add(network[:interface]) - entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}", - options: network) + commands = [] + entries = [] + interfaces = [] - entries << entry - end - - # Perform the careful dance necessary to reconfigure the network - # interfaces. - Tempfile.open("vagrant-debian-configure-networks") do |f| - f.binmode - f.write(entries.join("\n")) - f.fsync - f.close - comm.upload(f.path, "/tmp/vagrant-network-entry") - end - - # Bring down all the interfaces we're reconfiguring. By bringing down - # each specifically, we avoid reconfiguring eth0 (the NAT interface) so - # SSH never dies. - interfaces.each do |interface| - # Ubuntu 16.04+ returns an error when downing an interface that - # does not exist. The `|| true` preserves the behavior that older - # Ubuntu versions exhibit and Vagrant expects (GH-7155) - comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null || true") - comm.sudo("/sbin/ip addr flush dev eth#{interface} 2> /dev/null") - end - - comm.sudo('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces') - comm.sudo('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post') - - # Bring back up each network interface, reconfigured - interfaces.each do |interface| - comm.sudo("/sbin/ifup eth#{interface}") - end + comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| + interfaces = stdout.split("\n") end + + networks.each do |network| + network[:device] = interfaces[network[:interface]] + + entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}", + options: network, + ) + entries << entry + end + + Tempfile.open("vagrant-debian-configure-networks") do |f| + f.binmode + f.write(entries.join("\n")) + f.fsync + f.close + comm.upload(f.path, "/tmp/vagrant-network-entry") + end + + networks.each do |network| + # Ubuntu 16.04+ returns an error when downing an interface that + # does not exist. The `|| true` preserves the behavior that older + # Ubuntu versions exhibit and Vagrant expects (GH-7155) + commands << "/sbin/ifdown '#{network[:device]}' || true" + commands << "/sbin/ip addr flush dev '#{network[:device]}'" + end + + # Reconfigure /etc/network/interfaces. + commands << <<-EOH.gsub(/^ {12}/, "") + # Remove any previous network modifications from the interfaces file + sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre + sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post + + cat \\ + /tmp/vagrant-network-interfaces.pre \\ + /tmp/vagrant-network-entry \\ + /tmp/vagrant-network-interfaces.post \\ + > /etc/network/interfaces + + rm -f /tmp/vagrant-network-interfaces.pre + rm -f /tmp/vagrant-network-entry + rm -f /tmp/vagrant-network-interfaces.post + EOH + + # Bring back up each network interface, reconfigured. + networks.each do |network| + commands << "/sbin/ifup '#{network[:device]}'" + end + + # Run all the commands in one session to prevent partial configuration + # due to a severed network. + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/debian/cap/nfs_client.rb b/plugins/guests/debian/cap/nfs_client.rb index 8a15ec5b5..d8107565d 100644 --- a/plugins/guests/debian/cap/nfs_client.rb +++ b/plugins/guests/debian/cap/nfs_client.rb @@ -3,10 +3,11 @@ module VagrantPlugins module Cap class NFSClient def self.nfs_client_install(machine) - machine.communicate.tap do |comm| - comm.sudo("apt-get -y update") - comm.sudo("apt-get -y install nfs-common portmap") - end + comm = machine.communicate + comm.sudo <<-EOH.gsub(/^ {12}/, '') + apt-get -yqq update + apt-get -yqq install nfs-common portmap + EOH end end end diff --git a/plugins/guests/debian/cap/rsync.rb b/plugins/guests/debian/cap/rsync.rb index 80735cc9e..d8371bf86 100644 --- a/plugins/guests/debian/cap/rsync.rb +++ b/plugins/guests/debian/cap/rsync.rb @@ -3,9 +3,12 @@ module VagrantPlugins module Cap class RSync def self.rsync_install(machine) - machine.communicate.tap do |comm| - comm.sudo("apt-get -y update") - comm.sudo("apt-get -y install rsync") + comm = machine.communicate + if !comm.test("command -v rsync") + comm.sudo <<-EOH.gsub(/^ {14}/, '') + apt-get -yqq update + apt-get -yqq install rsync + EOH end end end diff --git a/plugins/guests/debian/cap/smb.rb b/plugins/guests/debian/cap/smb.rb index fcc97af36..e1f3b7000 100644 --- a/plugins/guests/debian/cap/smb.rb +++ b/plugins/guests/debian/cap/smb.rb @@ -3,13 +3,12 @@ module VagrantPlugins module Cap class SMB def self.smb_install(machine) - # Deb/Ubuntu require mount.cifs which doesn't come by default. - machine.communicate.tap do |comm| - if !comm.test("test -f /sbin/mount.cifs") - machine.ui.detail(I18n.t("vagrant.guest_deb_installing_smb")) - comm.sudo("apt-get -y update") - comm.sudo("apt-get -y install cifs-utils") - end + comm = machine.communicate + if !comm.test("test -f /sbin/mount.cifs") + comm.sudo <<-EOH.gsub(/^ {14}/, '') + apt-get -yqq update + apt-get -yqq install cifs-utils + EOH end end end diff --git a/plugins/guests/debian/guest.rb b/plugins/guests/debian/guest.rb index a7f3d65de..6631bda6c 100644 --- a/plugins/guests/debian/guest.rb +++ b/plugins/guests/debian/guest.rb @@ -1,3 +1,5 @@ +require "vagrant" + module VagrantPlugins module GuestDebian class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/debian/plugin.rb b/plugins/guests/debian/plugin.rb index 5d00583e8..bcfdad05c 100644 --- a/plugins/guests/debian/plugin.rb +++ b/plugins/guests/debian/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Debian guest support." guest("debian", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb index f0c95eec6..82e1da99a 100644 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ b/plugins/guests/fedora/cap/change_host_name.rb @@ -3,71 +3,25 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - new(machine, name).change! - end + comm = machine.communicate - attr_reader :machine, :new_hostname + if !comm.test("hostname | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH +echo '#{name}' > /etc/hostname +hostname -F /etc/hostname +hostnamectl set-hostname --static '#{name}' +hostnamectl set-hostname --transient '#{name}' - def initialize(machine, new_hostname) - @machine = machine - @new_hostname = new_hostname - end +# Remove comments and blank lines from /etc/hosts +sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts - def change! - return unless should_change? - - update_etc_hostname - update_etc_hosts - refresh_hostname_service - end - - def should_change? - new_hostname != current_hostname - end - - def current_hostname - @current_hostname ||= get_current_hostname - end - - def get_current_hostname - hostname = "" - sudo "hostname -f" do |type, data| - hostname = data.chomp if type == :stdout && hostname.empty? +# 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 - - hostname - end - - def update_etc_hostname - sudo("echo '#{short_hostname}' > /etc/hostname") - end - - # /etc/hosts should resemble: - # 127.0.0.1 localhost - # 127.0.1.1 host.fqdn.com host.fqdn host - def update_etc_hosts - ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}' - search = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$" - replace = "\\1 #{fqdn} #{short_hostname} \\3" - expression = ['s', search, replace, 'g'].join('@') - - sudo("sed -ri '#{expression}' /etc/hosts") - end - - def refresh_hostname_service - sudo("hostname -F /etc/hostname") - end - - def fqdn - new_hostname - end - - def short_hostname - new_hostname.split('.').first - end - - def sudo(cmd, &block) - machine.communicate.sudo(cmd, &block) end end end diff --git a/plugins/guests/fedora/plugin.rb b/plugins/guests/fedora/plugin.rb index 118a2f13c..d838f0a4a 100644 --- a/plugins/guests/fedora/plugin.rb +++ b/plugins/guests/fedora/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Fedora guest support." guest("fedora", "redhat") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index 6b24440b0..fc194d6d9 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -3,9 +3,27 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'", {shell: "sh"}) - machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf", {shell: "sh"}) - machine.communicate.sudo("hostname #{name}", {shell: "sh"}) + options = { shell: "sh" } + comm = machine.communicate + + if !comm.test("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", options) + basename = name.split(".", 2)[0] + command = <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + hostname '#{name}' + sed -i '' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf + + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' /etc/hosts + sed -i'' -e '/^$/d' /etc/hosts + + # 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 + comm.sudo(command, options) end end end diff --git a/plugins/guests/freebsd/cap/configure_networks.rb b/plugins/guests/freebsd/cap/configure_networks.rb index 6f3e04ae4..8e5627b5e 100644 --- a/plugins/guests/freebsd/cap/configure_networks.rb +++ b/plugins/guests/freebsd/cap/configure_networks.rb @@ -9,40 +9,57 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) + options = { shell: "sh" } + comm = machine.communicate + + commands = [] + interfaces = [] + # Remove any previous network additions to the configuration file. - machine.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf", {shell: "sh"}) + commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf" - networks.each do |network| - # Determine the interface prefix... - command = "ifconfig -a | grep -o ^[0-9a-z]*" - result = "" - ifname = "" - machine.communicate.execute(command) do |type, data| - result << data if type == :stdout - if result.split(/\n/).any?{|i| i.match(/vtnet*/)} - ifname = "vtnet#{network[:interface]}" - else - ifname = "em#{network[:interface]}" - end - end + comm.sudo("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'", options) do |_, stdout| + interfaces = stdout.split("\n") + end - entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", - options: network, ifname: ifname) + networks.each.with_index do |network, i| + network[:device] = interfaces[network[:interface]] + + entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", + options: network, + ) + + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" Tempfile.open("vagrant-freebsd-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, "/tmp/vagrant-network-entry") + comm.upload(f.path, remote_path) end - machine.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'", {shell: "sh"}) - machine.communicate.sudo("rm -f /tmp/vagrant-network-entry", {shell: "sh"}) + commands << <<-EOH.gsub(/^ {14}/, '') + cat '#{remote_path}' >> /etc/rc.conf + rm -f '#{remote_path}' + EOH - # Restart interface so it loads configuration stored in /etc/rc.conf - machine.communicate.sudo("service netif restart #{ifname}", {shell: "sh"}) + # If the network is DHCP, then we have to start the dhclient, unless + # it is already running. See GH-5852 for more information + if network[:type].to_sym == :dhcp + file = "/var/run/dhclient.#{network[:device]}.pid" + commands << <<-EOH.gsub(/^ {16}/, '') + if ! test -f '#{file}' || ! kill -0 $(cat '#{file}'); then + dhclient '#{network[:device]}' + fi + EOH + end + + # For some reason, this returns status 1... every time + commands << "/etc/rc.d/netif restart '#{network[:device]}' || true" end + + comm.sudo(commands.join("\n"), options) end end end diff --git a/plugins/guests/freebsd/cap/halt.rb b/plugins/guests/freebsd/cap/halt.rb index 4408fb051..237c942bb 100644 --- a/plugins/guests/freebsd/cap/halt.rb +++ b/plugins/guests/freebsd/cap/halt.rb @@ -4,7 +4,7 @@ module VagrantPlugins class Halt def self.halt(machine) begin - machine.communicate.sudo("shutdown -p now", {shell: "sh"}) + machine.communicate.sudo("shutdown -p now", { shell: "sh" }) rescue IOError # Do nothing because SSH connection closed and it probably # means the VM just shut down really fast. diff --git a/plugins/guests/freebsd/cap/insert_public_key.rb b/plugins/guests/freebsd/cap/insert_public_key.rb index 5fc414d39..86afb0b66 100644 --- a/plugins/guests/freebsd/cap/insert_public_key.rb +++ b/plugins/guests/freebsd/cap/insert_public_key.rb @@ -1,19 +1,32 @@ -require "vagrant/util/shell_quote" +require "tempfile" module VagrantPlugins module GuestFreeBSD module Cap class InsertPublicKey def self.insert_public_key(machine, contents) - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - contents = contents.gsub("\n", "\\n") + comm = machine.communicate + contents = contents.chomp - machine.communicate.tap do |comm| - comm.execute("mkdir -p ~/.ssh", shell: "sh") - comm.execute("chmod 0700 ~/.ssh", shell: "sh") - comm.execute("printf '#{contents}' >> ~/.ssh/authorized_keys", shell: "sh") - comm.execute("chmod 0600 ~/.ssh/authorized_keys", shell: "sh") + remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" + Tempfile.open("vagrant-freebsd-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) end + + command = <<-EOH.gsub(/^ {12}/, '') + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + # Remove the temporary file + rm -f '#{remote_path}' + EOH + comm.execute(command, { shell: "sh" }) end end end diff --git a/plugins/guests/freebsd/cap/mount_nfs_folder.rb b/plugins/guests/freebsd/cap/mount_nfs_folder.rb index 58d8f785c..1541eff33 100644 --- a/plugins/guests/freebsd/cap/mount_nfs_folder.rb +++ b/plugins/guests/freebsd/cap/mount_nfs_folder.rb @@ -3,17 +3,20 @@ module VagrantPlugins module Cap class MountNFSFolder def self.mount_nfs_folder(machine, ip, folders) - folders.each do |name, opts| + comm = machine.communicate + + commands = [] + + folders.each do |_, opts| if opts[:nfs_version] - nfs_version_mount_option="-o nfsv#{opts[:nfs_version]}" + mount_opts = "-o nfsv#{opts[:nfs_version]}" end - machine.communicate.sudo("mkdir -p #{opts[:guestpath]}", {shell: "sh"}) - - machine.communicate.sudo( - "mount -t nfs #{nfs_version_mount_option} " + - "'#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {shell: "sh"}) + commands << "mkdir -p '#{opts[:guestpath]}'" + commands << "mount -t nfs #{mount_opts} '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'" end + + comm.sudo(commands.join("\n"), { shell: "sh" }) end end end diff --git a/plugins/guests/freebsd/cap/rsync.rb b/plugins/guests/freebsd/cap/rsync.rb index fde6ed620..5bcffc347 100644 --- a/plugins/guests/freebsd/cap/rsync.rb +++ b/plugins/guests/freebsd/cap/rsync.rb @@ -3,14 +3,7 @@ module VagrantPlugins module Cap class RSync def self.rsync_install(machine) - version = nil - machine.communicate.execute("uname -r") do |type, result| - version = result.split('.')[0].to_i if type == :stdout - end - - pkg_cmd = "pkg install -y" - - machine.communicate.sudo("#{pkg_cmd} rsync") + machine.communicate.sudo("pkg install -y rsync") end def self.rsync_installed(machine) diff --git a/plugins/guests/freebsd/plugin.rb b/plugins/guests/freebsd/plugin.rb index a4ece8de2..14b5e9ef9 100644 --- a/plugins/guests/freebsd/plugin.rb +++ b/plugins/guests/freebsd/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "FreeBSD guest support." guest("freebsd") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index 289aa16af..1d4a26b56 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -31,7 +31,7 @@ module VagrantPlugins # Configure the interface comm.sudo("ln -fs /etc/init.d/net.lo /etc/init.d/net.eth#{network[:interface]}") - comm.sudo("/etc/init.d/net.eth#{network[:interface]} stop 2> /dev/null") + comm.sudo("/etc/init.d/net.eth#{network[:interface]} stop") comm.sudo("cat /tmp/vagrant-network-entry >> /etc/conf.d/net") comm.sudo("rm -f /tmp/vagrant-network-entry") comm.sudo("/etc/init.d/net.eth#{network[:interface]} start") diff --git a/plugins/guests/linux/cap/choose_addressable_ip_addr.rb b/plugins/guests/linux/cap/choose_addressable_ip_addr.rb index 7584bd77a..2ae94185d 100644 --- a/plugins/guests/linux/cap/choose_addressable_ip_addr.rb +++ b/plugins/guests/linux/cap/choose_addressable_ip_addr.rb @@ -3,12 +3,12 @@ module VagrantPlugins module Cap module ChooseAddressableIPAddr def self.choose_addressable_ip_addr(machine, possible) - machine.communicate.tap do |comm| - possible.each do |ip| - command = "ping -c1 -w1 -W1 #{ip}" - if comm.test(command) - return ip - end + comm = machine.communicate + + possible.each do |ip| + command = "ping -c1 -w1 -W1 #{ip}" + if comm.test(command) + return ip end end diff --git a/plugins/guests/linux/cap/insert_public_key.rb b/plugins/guests/linux/cap/insert_public_key.rb index f238cd9a1..b7335bdc5 100644 --- a/plugins/guests/linux/cap/insert_public_key.rb +++ b/plugins/guests/linux/cap/insert_public_key.rb @@ -1,19 +1,29 @@ -require "vagrant/util/shell_quote" - module VagrantPlugins module GuestLinux module Cap class InsertPublicKey def self.insert_public_key(machine, contents) + comm = machine.communicate contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - machine.communicate.tap do |comm| - comm.execute("mkdir -p ~/.ssh") - comm.execute("chmod 0700 ~/.ssh") - comm.execute("printf '#{contents}\\n' >> ~/.ssh/authorized_keys") - comm.execute("chmod 0600 ~/.ssh/authorized_keys") + remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" + Tempfile.open("vagrant-linux-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) end + + comm.execute <<-EOH.gsub(/^ {12}/, '') + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + # Remove the temporary file + rm -f '#{remote_path}' + EOH end end end diff --git a/plugins/guests/linux/cap/mount_nfs.rb b/plugins/guests/linux/cap/mount_nfs.rb index 26dfb625d..83731f563 100644 --- a/plugins/guests/linux/cap/mount_nfs.rb +++ b/plugins/guests/linux/cap/mount_nfs.rb @@ -7,13 +7,17 @@ module VagrantPlugins extend Vagrant::Util::Retryable def self.mount_nfs_folder(machine, ip, folders) + comm = machine.communicate + + commands = [] + folders.each do |name, opts| # Expand the guest path so we can handle things like "~/vagrant" expanded_guest_path = machine.guest.capability( :shell_expand_guest_path, opts[:guestpath]) # Do the actual creating and mounting - machine.communicate.sudo("mkdir -p #{expanded_guest_path}") + commands << "mkdir -p '#{expanded_guest_path}'" # Mount hostpath = opts[:hostpath].dup @@ -26,18 +30,18 @@ module VagrantPlugins mount_opts = opts[:mount_options].dup end - mount_command = "mount -o '#{mount_opts.join(",")}' #{ip}:'#{hostpath}' #{expanded_guest_path}" - retryable(on: Vagrant::Errors::LinuxNFSMountFailed, tries: 8, sleep: 3) do - machine.communicate.sudo(mount_command, - error_class: Vagrant::Errors::LinuxNFSMountFailed) - end + commands << "mount -o #{mount_opts.join(",")} '#{ip}:#{hostpath}' '#{expanded_guest_path}'" - # Emit an upstart event if we can - machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then - /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' -fi -SCRIPT + # Emit a mount event + commands << <<-EOH.gsub(/^ {14}/, '') + if command -v /sbin/init && /sbin/init --version | grep upstart; then + /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' + fi + EOH + end + + retryable(on: Vagrant::Errors::LinuxNFSMountFailed, tries: 8, sleep: 3) do + comm.sudo(commands.join("\n"), error_class: Vagrant::Errors::LinuxNFSMountFailed) end end end diff --git a/plugins/guests/linux/cap/mount_smb_shared_folder.rb b/plugins/guests/linux/cap/mount_smb_shared_folder.rb index d85bdf6dc..867a2ddc4 100644 --- a/plugins/guests/linux/cap/mount_smb_shared_folder.rb +++ b/plugins/guests/linux/cap/mount_smb_shared_folder.rb @@ -94,7 +94,7 @@ SCRIPT # Emit an upstart event if we can machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then +if command -v /sbin/init && /sbin/init --version | grep upstart; then /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' fi SCRIPT diff --git a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb index 57fdc207f..1d67c86e3 100644 --- a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb +++ b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb @@ -81,7 +81,7 @@ module VagrantPlugins # Emit an upstart event if we can machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then +if command -v /sbin/init && /sbin/init --version | grep upstart; then /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' fi SCRIPT diff --git a/plugins/guests/linux/guest.rb b/plugins/guests/linux/guest.rb index 7e1c9bd87..8effae8ad 100644 --- a/plugins/guests/linux/guest.rb +++ b/plugins/guests/linux/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestLinux class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index cadd4aa95..b8456de5d 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Linux guest support." guest("linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/mint/guest.rb b/plugins/guests/mint/guest.rb index afaf94c15..d12e42055 100644 --- a/plugins/guests/mint/guest.rb +++ b/plugins/guests/mint/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestMint class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/mint/plugin.rb b/plugins/guests/mint/plugin.rb index bfab4b749..83bbca677 100644 --- a/plugins/guests/mint/plugin.rb +++ b/plugins/guests/mint/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Mint guest support." guest("mint", "ubuntu") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end end diff --git a/plugins/guests/omnios/cap/change_host_name.rb b/plugins/guests/omnios/cap/change_host_name.rb index 13f70f304..4d238d350 100644 --- a/plugins/guests/omnios/cap/change_host_name.rb +++ b/plugins/guests/omnios/cap/change_host_name.rb @@ -3,12 +3,24 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - su_cmd = machine.config.solaris.suexec_cmd + comm = machine.communicate - # Only do this if the hostname is not already set - if !machine.communicate.test("#{su_cmd} hostname | grep '#{name}'") - machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"") - machine.communicate.execute("#{su_cmd} hostname #{name}") + if !comm.test("hostname | grep -w '#{name}'", sudo: false) + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + # Set hostname + echo '#{name}' > /etc/nodename + 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 || { + echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts + mv /tmp/tmp-hosts /etc/hosts + } + EOH end end end diff --git a/plugins/guests/omnios/cap/mount_nfs_folder.rb b/plugins/guests/omnios/cap/mount_nfs_folder.rb index 250f77992..0dc2c10ad 100644 --- a/plugins/guests/omnios/cap/mount_nfs_folder.rb +++ b/plugins/guests/omnios/cap/mount_nfs_folder.rb @@ -3,11 +3,17 @@ module VagrantPlugins module Cap class MountNFSFolder def self.mount_nfs_folder(machine, ip, folders) - su_cmd = machine.config.solaris.suexec_cmd - folders.each do |name, opts| - machine.communicate.execute("#{su_cmd} mkdir -p #{opts[:guestpath]}") - machine.communicate.execute("#{su_cmd} /sbin/mount '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") + comm = machine.communicate + commands = [] + + folders.each do |_, opts| + commands << <<-EOH.gsub(/^ {14}/, '') + mkdir -p '#{opts[:guestpath]}' + /sbin/mount '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}' + EOH end + + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/omnios/cap/rsync.rb b/plugins/guests/omnios/cap/rsync.rb new file mode 100644 index 000000000..64cda9177 --- /dev/null +++ b/plugins/guests/omnios/cap/rsync.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module GuestOmniOS + module Cap + class RSync + def self.rsync_install(machine) + machine.communicate.sudo("pkg install rsync") + end + end + end + end +end diff --git a/plugins/guests/omnios/guest.rb b/plugins/guests/omnios/guest.rb index 7474b020b..7a176cb31 100644 --- a/plugins/guests/omnios/guest.rb +++ b/plugins/guests/omnios/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestOmniOS class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/omnios/plugin.rb b/plugins/guests/omnios/plugin.rb index 9193f0db8..8437276db 100644 --- a/plugins/guests/omnios/plugin.rb +++ b/plugins/guests/omnios/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "OmniOS guest support." guest("omnios", "solaris") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end @@ -20,6 +20,11 @@ module VagrantPlugins require_relative "cap/mount_nfs_folder" Cap::MountNFSFolder end + + guest_capability("omnios", "rsync_install") do + require_relative "cap/rsync" + Cap::RSync + end end end end diff --git a/plugins/guests/photon/cap/change_host_name.rb b/plugins/guests/photon/cap/change_host_name.rb index 5249dadba..bb103d24c 100644 --- a/plugins/guests/photon/cap/change_host_name.rb +++ b/plugins/guests/photon/cap/change_host_name.rb @@ -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 diff --git a/plugins/guests/photon/cap/configure_networks.rb b/plugins/guests/photon/cap/configure_networks.rb index 50f4737b8..c628fe1f5 100644 --- a/plugins/guests/photon/cap/configure_networks.rb +++ b/plugins/guests/photon/cap/configure_networks.rb @@ -1,6 +1,3 @@ -require 'tempfile' -require 'vagrant/util/template_renderer' - module VagrantPlugins module GuestPhoton module Cap @@ -8,33 +5,24 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - machine.communicate.tap do |comm| - # Read network interface names - interfaces = [] - comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result| - interfaces = result.split("\n") - end + comm = machine.communicate - # Configure interfaces - networks.each do |network| - comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}") - end + commands = [] + interfaces = [] - primary_machine_config = machine.env.active_machines.first - primary_machine = machine.env.machine(*primary_machine_config, true) - - get_ip = lambda do |machine| - ip = nil - machine.config.vm.networks.each do |type, opts| - if type == :private_network && opts[:ip] - ip = opts[:ip] - break - end - end - - ip - end + comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result| + interfaces = result.split("\n") end + + networks.each do |network| + device = interfaces[network[:interface]] + command = "ifconfig #{device}" + command << " #{network[:ip]}" if network[:ip] + command << " netmast #{network[:netmask]}" if network[:netmask] + commands << command + end + + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/photon/guest.rb b/plugins/guests/photon/guest.rb index c33a9f8a2..13474d6ee 100644 --- a/plugins/guests/photon/guest.rb +++ b/plugins/guests/photon/guest.rb @@ -1,6 +1,6 @@ module VagrantPlugins module GuestPhoton - class Guest < Vagrant.plugin('2', :guest) + class Guest < Vagrant.plugin("2", :guest) def detect?(machine) machine.communicate.test("cat /etc/photon-release | grep 'VMware Photon Linux'") end diff --git a/plugins/guests/photon/plugin.rb b/plugins/guests/photon/plugin.rb index a56a0f44f..47542dc86 100644 --- a/plugins/guests/photon/plugin.rb +++ b/plugins/guests/photon/plugin.rb @@ -1,28 +1,28 @@ -require 'vagrant' +require "vagrant" module VagrantPlugins module GuestPhoton - class Plugin < Vagrant.plugin('2') - name 'VMware Photon guest' - description 'VMware Photon guest support.' + class Plugin < Vagrant.plugin("2") + name "VMware Photon guest" + description "VMware Photon guest support." - guest('photon', 'linux') do - require File.expand_path("../guest", __FILE__) + guest("photon", "linux") do + require_relative "guest" Guest end - guest_capability('photon', 'change_host_name') do - require_relative 'cap/change_host_name' + guest_capability("photon", "change_host_name") do + require_relative "cap/change_host_name" Cap::ChangeHostName end - guest_capability('photon', 'configure_networks') do - require_relative 'cap/configure_networks' + guest_capability("photon", "configure_networks") do + require_relative "cap/configure_networks" Cap::ConfigureNetworks end - guest_capability('photon', 'docker_daemon_running') do - require_relative 'cap/docker' + guest_capability("photon", "docker_daemon_running") do + require_relative "cap/docker" Cap::Docker end end diff --git a/plugins/guests/pld/cap/change_host_name.rb b/plugins/guests/pld/cap/change_host_name.rb index e7925b03b..09dc5eaee 100644 --- a/plugins/guests/pld/cap/change_host_name.rb +++ b/plugins/guests/pld/cap/change_host_name.rb @@ -3,15 +3,27 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.tap do |comm| - # Only do this if the hostname is not already set - if !comm.test("sudo hostname | grep --line-regexp '#{name}'") - comm.sudo("sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network") - comm.sudo("hostname #{name}") - comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") - comm.sudo("sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-*") - comm.sudo("service network restart") - end + comm = machine.communicate + + if !comm.test("hostname | grep -w '#{name}'", sudo: false) + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + hostname '#{name}' + sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network + + sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-* + + # 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 + } + + # Restart networking + service network restart + EOH end end end diff --git a/plugins/guests/pld/guest.rb b/plugins/guests/pld/guest.rb index 1d65ce044..a7f163d3b 100644 --- a/plugins/guests/pld/guest.rb +++ b/plugins/guests/pld/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestPld class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/pld/plugin.rb b/plugins/guests/pld/plugin.rb index 697599c81..45e105c17 100644 --- a/plugins/guests/pld/plugin.rb +++ b/plugins/guests/pld/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "PLD Linux guest support." guest("pld", "redhat") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb index 2520904ec..64691b8f5 100644 --- a/plugins/guests/redhat/cap/change_host_name.rb +++ b/plugins/guests/redhat/cap/change_host_name.rb @@ -3,104 +3,38 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - new(machine, name).change! - end + comm = machine.communicate - attr_reader :machine, :new_hostname + if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + basename = name.split('.', 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + # Update sysconfig + sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network - def initialize(machine, new_hostname) - @machine = machine - @new_hostname = new_hostname - end + # Update DNS + sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-* - def change! - return unless should_change? + # Set the hostname - use hostnamectl if available + echo '#{name}' > /etc/hostname + if command -v hostnamectl; then + hostnamectl set-hostname '#{name}' + else + hostname '#{name}' + fi - case machine.guest.capability("flavor") - when :rhel_7 - update_hostname_rhel7 - update_etc_hosts - else - update_sysconfig - update_hostname - update_etc_hosts - update_dhcp_hostnames - restart_networking + # 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 + } + + # Restart network + service network restart + EOH end end - - def should_change? - new_hostname != current_hostname - end - - def current_hostname - @current_hostname ||= get_current_hostname - end - - def get_current_hostname - hostname = '' - block = lambda do |type, data| - if type == :stdout - hostname += data.chomp - end - end - - execute 'hostname -f', error_check: false, &block - execute 'hostname',&block if hostname.empty? - /localhost(\..*)?/.match(hostname) ? '' : hostname - end - - def update_sysconfig - sudo "sed -i 's/\\(HOSTNAME=\\).*/\\1#{fqdn}/' /etc/sysconfig/network" - end - - def update_hostname - sudo "hostname #{fqdn}" - end - - def update_hostname_rhel7 - sudo "hostnamectl set-hostname #{fqdn}" - end - - # /etc/hosts should resemble: - # 127.0.0.1 host.fqdn.com host localhost ... - def update_etc_hosts - s = '[[:space:]]' - current_fqdn = Regexp.escape(current_hostname) - current_short = Regexp.escape(current_hostname.split('.').first.to_s) - currents = "\\(#{current_fqdn}#{s}\\+\\|#{current_short}#{s}\\+\\)*" unless current_hostname.empty? - local_ip = '127[.]0[.]0[.]1' - search = "^\\(#{local_ip}#{s}\\+\\)#{currents}" - replace = "\\1#{fqdn} " - replace = "#{replace}#{short_hostname} " unless fqdn == short_hostname - expression = ['s', search, replace,''].join('@') - - sudo "sed -i '#{expression}' /etc/hosts" - end - - def update_dhcp_hostnames - sudo "sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{short_hostname}\"/' /etc/sysconfig/network-scripts/ifcfg-*" - end - - def restart_networking - sudo 'service network restart' - end - - def fqdn - new_hostname - end - - def short_hostname - new_hostname.split('.').first - end - - def execute(cmd, opts=nil, &block) - machine.communicate.execute(cmd, opts, &block) - end - - def sudo(cmd, opts=nil, &block) - machine.communicate.sudo(cmd, opts, &block) - end end end end diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index 8aebff291..e7e81031a 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -1,4 +1,3 @@ -require "set" require "tempfile" require_relative "../../../../lib/vagrant/util/retryable" @@ -12,7 +11,7 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - case machine.guest.capability("flavor") + case machine.guest.capability(:flavor) when :rhel_7 configure_networks_rhel7(machine, networks) else @@ -21,66 +20,60 @@ module VagrantPlugins end def self.configure_networks_rhel7(machine, networks) - # This is kind of jank but the configure networks is the same - # as Fedora at this point. - require File.expand_path("../../../fedora/cap/configure_networks", __FILE__) - ::VagrantPlugins::GuestFedora::Cap::ConfigureNetworks. - configure_networks(machine, networks) + # This is kind of jank but the configure networks is the same as + # Fedora at this point. + require_relative "../../fedora/cap/configure_networks" + ::VagrantPlugins::GuestFedora::Cap::ConfigureNetworks + .configure_networks(machine, networks) end def self.configure_networks_default(machine, networks) - network_scripts_dir = machine.guest.capability("network_scripts_dir") + comm = machine.communicate - # Accumulate the configurations to add to the interfaces file as - # well as what interfaces we're actually configuring since we use that - # later. - interfaces = Set.new - networks.each do |network| - interfaces.add(network[:interface]) + network_scripts_dir = machine.guest.capability(:network_scripts_dir) - # Down the interface before munging the config file. This might fail - # if the interface is not actually set up yet so ignore errors. - machine.communicate.sudo( - "/sbin/ifdown eth#{network[:interface]} 2> /dev/null", error_check: false) + interfaces = [] + commands = [] - # Remove any previous vagrant configuration in this network interface's - # configuration files. - machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-eth#{network[:interface]}") + comm.sudo("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'") do |_, stdout| + interfaces = stdout.split("\n") + end - # Render and upload the network entry file to a deterministic - # temporary location. + networks.each.with_index do |network, i| + network[:device] = interfaces[network[:interface]] + + # Render a new configuration entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}", - options: network) + options: network, + ) - Tempfile.open("vagrant-red-hat-configure-networks") do |f| + # Upload the new configuration + remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}" + Tempfile.open("vagrant-redhat-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, "/tmp/vagrant-network-entry_#{network[:interface]}") - end - end - - # Bring down all the interfaces we're reconfiguring. By bringing down - # each specifically, we avoid reconfiguring eth0 (the NAT interface) so - # SSH never dies. - interfaces.each do |interface| - retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do - # The interface should already be down so this probably - # won't do anything, so we run it with error_check false. - machine.communicate.sudo( - "/sbin/ifdown eth#{interface} 2> /dev/null", error_check: false) - - # Add the new interface and bring it up - machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}") - machine.communicate.sudo("ARPCHECK=no /sbin/ifup eth#{interface} 2> /dev/null") + machine.communicate.upload(f.path, remote_path) end - machine.communicate.sudo("rm -f /tmp/vagrant-network-entry_#{interface}") + # Add the new interface and bring it back up + final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}" + commands << <<-EOH.gsub(/^ {14}/, '') + # Down the interface before munging the config file. This might + # fail if the interface is not actually set up yet so ignore + # errors. + /sbin/ifdown '#{network[:device]}' || true + + # Move new config into place + mv '#{remote_path}' '#{final_path}' + + # Bring the interface up + ARPCHECK=no /sbin/ifup '#{network[:device]}' + EOH end + + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/redhat/cap/flavor.rb b/plugins/guests/redhat/cap/flavor.rb index 2ae87201a..8bdda2629 100644 --- a/plugins/guests/redhat/cap/flavor.rb +++ b/plugins/guests/redhat/cap/flavor.rb @@ -5,10 +5,9 @@ module VagrantPlugins def self.flavor(machine) # Read the version file output = "" - machine.communicate.sudo("cat /etc/redhat-release") do |type, data| - output += data if type == :stdout + machine.communicate.sudo("cat /etc/redhat-release") do |_, data| + output = data end - output.chomp! # Detect various flavors we care about if output =~ /(CentOS|Red Hat Enterprise|Scientific) Linux( .+)? release 7/i diff --git a/plugins/guests/redhat/cap/nfs_client.rb b/plugins/guests/redhat/cap/nfs_client.rb index 78e2d5bca..b1eb4c6e4 100644 --- a/plugins/guests/redhat/cap/nfs_client.rb +++ b/plugins/guests/redhat/cap/nfs_client.rb @@ -3,32 +3,20 @@ module VagrantPlugins module Cap class NFSClient def self.nfs_client_install(machine) - if VagrantPlugins::GuestRedHat::Plugin.dnf?(machine) - machine.communicate.sudo("dnf -y install nfs-utils nfs-utils-lib") - else - machine.communicate.sudo("yum -y install nfs-utils nfs-utils-lib") - end - restart_nfs(machine) - end + machine.communicate.sudo <<-EOH.gsub(/^ {12}/, '') + if command -v dnf; then + dnf -y install nfs-utils nfs-utils-lib portmap + else + yum -y install nfs-utils nfs-utils-lib portmap + fi - def self.nfs_client_installed(machine) - installed = machine.communicate.test("test -x /sbin/mount.nfs") - restart_nfs(machine) if installed - installed - end - - protected - - def self.systemd?(machine) - machine.communicate.test("test $(ps -o comm= 1) == 'systemd'") - end - - def self.restart_nfs(machine) - if systemd?(machine) - machine.communicate.sudo("/bin/systemctl restart rpcbind nfs") - else - machine.communicate.sudo("/etc/init.d/rpcbind restart; /etc/init.d/nfs restart") - end + if test $(ps -o comm= 1) == 'systemd'; then + /bin/systemctl restart rpcbind nfs + else + /etc/init.d/rpcbind restart + /etc/init.d/nfs restart + fi + EOH end end end diff --git a/plugins/guests/redhat/cap/rsync.rb b/plugins/guests/redhat/cap/rsync.rb index 8eace63f0..83804191a 100644 --- a/plugins/guests/redhat/cap/rsync.rb +++ b/plugins/guests/redhat/cap/rsync.rb @@ -3,13 +3,13 @@ module VagrantPlugins module Cap class RSync def self.rsync_install(machine) - machine.communicate.tap do |comm| - if VagrantPlugins::GuestRedHat::Plugin.dnf?(machine) - comm.sudo("dnf -y install rsync") + machine.communicate.sudo <<-EOH.gsub(/^ {12}/, '') + if command -v dnf; then + dnf -y install rsync else - comm.sudo("yum -y install rsync") - end - end + yum -y install rsync + fi + EOH end end end diff --git a/plugins/guests/redhat/guest.rb b/plugins/guests/redhat/guest.rb index 197286200..272ad65ba 100644 --- a/plugins/guests/redhat/guest.rb +++ b/plugins/guests/redhat/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestRedHat class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/redhat/plugin.rb b/plugins/guests/redhat/plugin.rb index 0a97febee..c7f7d1b97 100644 --- a/plugins/guests/redhat/plugin.rb +++ b/plugins/guests/redhat/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Red Hat Enterprise Linux guest support." guest("redhat", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end @@ -36,19 +36,10 @@ module VagrantPlugins Cap::NFSClient end - guest_capability("redhat", "nfs_client_installed") do - require_relative "cap/nfs_client" - Cap::NFSClient - end - guest_capability("redhat", "rsync_install") do require_relative "cap/rsync" Cap::RSync end - - def self.dnf?(machine) - machine.communicate.test("/usr/bin/which -s dnf") - end end end end diff --git a/plugins/guests/slackware/cap/change_host_name.rb b/plugins/guests/slackware/cap/change_host_name.rb index 07976725f..d16bfabd0 100644 --- a/plugins/guests/slackware/cap/change_host_name.rb +++ b/plugins/guests/slackware/cap/change_host_name.rb @@ -3,14 +3,25 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.tap do |comm| - # Only do this if the hostname is not already set - if !comm.test("sudo hostname | grep '#{name}'") - comm.sudo("chmod o+w /etc/hostname") - comm.sudo("echo #{name} > /etc/hostname") - comm.sudo("chmod o-w /etc/hostname") - comm.sudo("hostname -F /etc/hostname") - 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 + chmod o+w /etc/hostname + echo '#{name}' > /etc/hostname + chmod o-w /etc/hostname + hostname -F /etc/hostname + + # 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 diff --git a/plugins/guests/slackware/cap/configure_networks.rb b/plugins/guests/slackware/cap/configure_networks.rb index b9b272cde..1d3011a10 100644 --- a/plugins/guests/slackware/cap/configure_networks.rb +++ b/plugins/guests/slackware/cap/configure_networks.rb @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- require "tempfile" require_relative "../../../../lib/vagrant/util/template_renderer" @@ -10,27 +9,42 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - interfaces = Array.new - machine.communicate.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result| + comm = machine.communicate + + commands = [] + interfaces = [] + + comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result| interfaces = result.split("\n") end - networks.each do |network| + # Remove any previous configuration + commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.d/rc.inet1.conf" + + networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] - entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}", options: network) + entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}", + i: i+1, + options: network, + ) + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now}-#{i}" Tempfile.open("vagrant-slackware-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, "/tmp/vagrant_network") + comm.upload(f.path, remote_path) end - machine.communicate.sudo("mv /tmp/vagrant_network /etc/rc.d/rc.inet1.conf") - machine.communicate.sudo("/etc/rc.d/rc.inet1") + commands << "cat '#{remote_path}' >> /etc/rc.d/rc.inet1.conf" end + + # Restart networking + commands << "/etc/rc.d/rc.inet1" + + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/slackware/guest.rb b/plugins/guests/slackware/guest.rb index 1442f26fc..5385ae12f 100644 --- a/plugins/guests/slackware/guest.rb +++ b/plugins/guests/slackware/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestSlackware class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/slackware/plugin.rb b/plugins/guests/slackware/plugin.rb index c81db6aa4..f118a1a85 100644 --- a/plugins/guests/slackware/plugin.rb +++ b/plugins/guests/slackware/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Slackware guest support." guest("slackware", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/smartos/config.rb b/plugins/guests/smartos/config.rb index 201141ed5..025061108 100644 --- a/plugins/guests/smartos/config.rb +++ b/plugins/guests/smartos/config.rb @@ -1,26 +1,14 @@ module VagrantPlugins module GuestSmartos class Config < Vagrant.plugin("2", :config) - attr_accessor :halt_timeout - attr_accessor :halt_check_interval - # This sets the command to use to execute items as a superuser. sudo is default + # This sets the command to use to execute items as a superuser. + # @default sudo attr_accessor :suexec_cmd attr_accessor :device def initialize - @halt_timeout = UNSET_VALUE - @halt_check_interval = UNSET_VALUE @suexec_cmd = 'pfexec' - @device = "e1000g" - end - - def finalize! - if @halt_timeout != UNSET_VALUE - puts "smartos.halt_timeout is deprecated and will be removed in Vagrant 1.7" - end - if @halt_check_interval != UNSET_VALUE - puts "smartos.halt_check_interval is deprecated and will be removed in Vagrant 1.7" - end + @device = "e1000g" end end end diff --git a/plugins/guests/suse/cap/change_host_name.rb b/plugins/guests/suse/cap/change_host_name.rb index 616b92495..a6f291a9c 100644 --- a/plugins/guests/suse/cap/change_host_name.rb +++ b/plugins/guests/suse/cap/change_host_name.rb @@ -3,14 +3,22 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - machine.communicate.tap do |comm| - # Only do this if the hostname is not already set - if !comm.test("sudo hostname | grep '#{name}'") - comm.sudo("echo #{name} > /etc/HOSTNAME") - comm.sudo("hostname #{name}") + comm = machine.communicate - comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") - end + if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + 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 diff --git a/plugins/guests/suse/cap/configure_networks.rb b/plugins/guests/suse/cap/configure_networks.rb index 54fbdda87..cd1b5d697 100644 --- a/plugins/guests/suse/cap/configure_networks.rb +++ b/plugins/guests/suse/cap/configure_networks.rb @@ -1,7 +1,5 @@ -require "set" require "tempfile" -require_relative "../../../../lib/vagrant/util/retryable" require_relative "../../../../lib/vagrant/util/template_renderer" module VagrantPlugins @@ -12,48 +10,43 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - network_scripts_dir = machine.guest.capability("network_scripts_dir") + comm = machine.communicate - # Accumulate the configurations to add to the interfaces file as - # well as what interfaces we're actually configuring since we use that - # later. - interfaces = Set.new - networks.each do |network| - interfaces.add(network[:interface]) + network_scripts_dir = machine.guest.capability(:network_scripts_dir) - # Remove any previous vagrant configuration in this network interface's - # configuration files. - machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-eth#{network[:interface]}") + commands = [] + interfaces = [] + + comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| + interfaces = stdout.split("\n") + end + + networks.each.with_index do |network, i| + network[:device] = interfaces[network[:interface]] - # Render and upload the network entry file to a deterministic - # temporary location. entry = TemplateRenderer.render("guests/suse/network_#{network[:type]}", - options: network) + options: network, + ) + + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" Tempfile.open("vagrant-suse-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, "/tmp/vagrant-network-entry_#{network[:interface]}") - end - end - - # Bring down all the interfaces we're reconfiguring. By bringing down - # each specifically, we avoid reconfiguring eth0 (the NAT interface) so - # SSH never dies. - interfaces.each do |interface| - retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do - machine.communicate.sudo("/sbin/ifdown eth#{interface} 2> /dev/null", error_check: false) - machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}") - machine.communicate.sudo("/sbin/ifup eth#{interface} 2> /dev/null") + comm.upload(f.path, remote_path) end - machine.communicate.sudo("rm -f /tmp/vagrant-network-entry_#{interface}") + local_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}" + commands << <<-EOH.gsub(/^ {14}/, '') + /sbin/ifdown '#{network[:device]}' || true + mv '#{remote_path}' '#{local_path}' + /sbin/ifup '#{network[:device]}' + EOH end + + comm.sudo(commands.join("\n")) end end end diff --git a/plugins/guests/suse/cap/nfs_client.rb b/plugins/guests/suse/cap/nfs_client.rb index 36a92d201..8a390ef93 100644 --- a/plugins/guests/suse/cap/nfs_client.rb +++ b/plugins/guests/suse/cap/nfs_client.rb @@ -3,12 +3,11 @@ module VagrantPlugins module Cap class NFSClient def self.nfs_client_install(machine) - machine.communicate.tap do |comm| - comm.sudo("zypper -n install nfs-client") - - comm.sudo("/sbin/service rpcbind restart") - comm.sudo("/sbin/service nfs restart") - end + machine.communicate.sudo <<-EOH.gsub(/^ {12}/, '') + zypper -n install nfs-client + /sbin/service rpcbind restart + /sbin/service nfs restart + EOH end end end diff --git a/plugins/guests/suse/cap/rsync.rb b/plugins/guests/suse/cap/rsync.rb index aed989613..2b7bb2646 100644 --- a/plugins/guests/suse/cap/rsync.rb +++ b/plugins/guests/suse/cap/rsync.rb @@ -7,9 +7,7 @@ module VagrantPlugins end def self.rsync_install(machine) - machine.communicate.tap do |comm| - comm.sudo("zypper -n install rsync") - end + machine.communicate.sudo("zypper -n install rsync") end end end diff --git a/plugins/guests/suse/guest.rb b/plugins/guests/suse/guest.rb index 643403f19..259f4b617 100644 --- a/plugins/guests/suse/guest.rb +++ b/plugins/guests/suse/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestSUSE class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/suse/plugin.rb b/plugins/guests/suse/plugin.rb index 3850ae45c..58dcb6e90 100644 --- a/plugins/guests/suse/plugin.rb +++ b/plugins/guests/suse/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "SUSE guest support." guest("suse", "linux") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end diff --git a/plugins/guests/tinycore/cap/mount_nfs.rb b/plugins/guests/tinycore/cap/mount_nfs.rb index ccfc1666b..e1cb4a3ce 100644 --- a/plugins/guests/tinycore/cap/mount_nfs.rb +++ b/plugins/guests/tinycore/cap/mount_nfs.rb @@ -34,7 +34,7 @@ module VagrantPlugins # Emit an upstart event if we can machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then +if command -v /sbin/init && /sbin/init --version | grep upstart; then /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' fi SCRIPT diff --git a/plugins/guests/trisquel/guest.rb b/plugins/guests/trisquel/guest.rb index 3db9c8b38..d070ca73a 100644 --- a/plugins/guests/trisquel/guest.rb +++ b/plugins/guests/trisquel/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestTrisquel class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/trisquel/plugin.rb b/plugins/guests/trisquel/plugin.rb index 3cb00a89d..4c1431746 100644 --- a/plugins/guests/trisquel/plugin.rb +++ b/plugins/guests/trisquel/plugin.rb @@ -7,7 +7,7 @@ module VagrantPlugins description "Trisquel guest support." guest("trisquel", "ubuntu") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end end diff --git a/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb index df68a010a..bbcada3e7 100644 --- a/plugins/guests/ubuntu/cap/change_host_name.rb +++ b/plugins/guests/ubuntu/cap/change_host_name.rb @@ -1,50 +1,51 @@ module VagrantPlugins module GuestUbuntu module Cap - class ChangeHostName < VagrantPlugins::GuestDebian::Cap::ChangeHostName + class ChangeHostName def self.change_host_name(machine, name) - super - end + comm = machine.communicate - def update_etc_hostname - return super unless systemd? - sudo("hostnamectl set-hostname '#{short_hostname}'") - end + if !comm.test("hostname -f | grep -w '#{name}'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + echo '#{name}' > /etc/hostname + hostname -F /etc/hostname - def refresh_hostname_service - if hardy? - # hostname.sh returns 1, so use `true` to get a 0 exitcode - sudo("/etc/init.d/hostname.sh start; true") - elsif systemd? - # Service runs via hostnamectl - else - sudo("service hostname start") + if command -v hostnamectl; then + hostnamectl set-hostname '#{name}' + fi + + # 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 + } + + # Update mailname + echo '#{name}' > /etc/mailname + + # Restart networking and force new DHCP + if [ test -f /etc/init.d/hostname ]; then + /etc/init.d/hostname start || true + fi + + if [ test -f /etc/init.d/hostname.sh ]; then + /etc/init.d/hostname.sh start || true + fi + + if [ test -f /etc/init.d/networking ]; then + /etc/init.d/networking force-reload + fi + + if [ test -f /etc/init.d/network-manager ]; then + /etc/init.d/network-manager force-reload + fi + EOH end end - - def hardy? - os_version("hardy") - end - - def renew_dhcp - sudo("ifdown -a; ifup -a; ifup -a --allow=hotplug") - end - - private - - def init_package - machine.communicate.execute('cat /proc/1/comm') do |type, data| - return data.chomp if type == :stdout - end - end - - def os_version(name) - machine.communicate.test("[ `lsb_release -c -s` = #{name} ]") - end - - def systemd? - init_package == 'systemd' - end end end end diff --git a/plugins/guests/ubuntu/guest.rb b/plugins/guests/ubuntu/guest.rb index 9295ede22..76244cb8c 100644 --- a/plugins/guests/ubuntu/guest.rb +++ b/plugins/guests/ubuntu/guest.rb @@ -1,5 +1,3 @@ -require "vagrant" - module VagrantPlugins module GuestUbuntu class Guest < Vagrant.plugin("2", :guest) diff --git a/plugins/guests/ubuntu/plugin.rb b/plugins/guests/ubuntu/plugin.rb index ef56d063c..7a30be6d1 100644 --- a/plugins/guests/ubuntu/plugin.rb +++ b/plugins/guests/ubuntu/plugin.rb @@ -7,13 +7,11 @@ module VagrantPlugins description "Ubuntu guest support." guest("ubuntu", "debian") do - require File.expand_path("../guest", __FILE__) + require_relative "guest" Guest end guest_capability("ubuntu", "change_host_name") do - # ubuntu is just just a specialization of the debian code for this capability - require_relative "../debian/cap/change_host_name" require_relative "cap/change_host_name" Cap::ChangeHostName end diff --git a/templates/guests/debian/network_dhcp.erb b/templates/guests/debian/network_dhcp.erb index 4dc4f8672..6c305270a 100644 --- a/templates/guests/debian/network_dhcp.erb +++ b/templates/guests/debian/network_dhcp.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet dhcp +auto <%= options[:device] %> +iface <%= options[:device] %> inet dhcp <% if !options[:use_dhcp_assigned_default_route] %> post-up route del default dev $IFACE || true <% else %> diff --git a/templates/guests/debian/network_static.erb b/templates/guests/debian/network_static.erb index 91f0cd62e..bf9b9c6b3 100644 --- a/templates/guests/debian/network_static.erb +++ b/templates/guests/debian/network_static.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet static +auto <%= options[:device] %> +iface <%= options[:device] %> inet static address <%= options[:ip] %> netmask <%= options[:netmask] %> <% if options[:gateway] %> diff --git a/templates/guests/debian/network_static6.erb b/templates/guests/debian/network_static6.erb index 7b9e8a694..288da4396 100644 --- a/templates/guests/debian/network_static6.erb +++ b/templates/guests/debian/network_static6.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet6 static +auto <%= options[:device] %> +iface <%= options[:device] %> inet6 static address <%= options[:ip] %> netmask <%= options[:netmask] %> <% if options[:gateway] %> diff --git a/templates/guests/freebsd/network_dhcp.erb b/templates/guests/freebsd/network_dhcp.erb index f574f2679..38e49c483 100644 --- a/templates/guests/freebsd/network_dhcp.erb +++ b/templates/guests/freebsd/network_dhcp.erb @@ -1,3 +1,4 @@ #VAGRANT-BEGIN -ifconfig_<%= ifname %>="DHCP" +ifconfig_<%= options[:device] %>="DHCP" +synchronous_dhclient="YES" #VAGRANT-END diff --git a/templates/guests/freebsd/network_static.erb b/templates/guests/freebsd/network_static.erb index e73c7c124..633f410c3 100644 --- a/templates/guests/freebsd/network_static.erb +++ b/templates/guests/freebsd/network_static.erb @@ -1,5 +1,5 @@ #VAGRANT-BEGIN -ifconfig_<%= ifname %>="inet <%= options[:ip] %> netmask <%= options[:netmask] %>" +ifconfig_<%= options[:device] %>="inet <%= options[:ip] %> netmask <%= options[:netmask] %>" <% if options[:gateway] %> default_router="<%= options[:gateway] %>" <% end %> diff --git a/templates/guests/slackware/network_dhcp.erb b/templates/guests/slackware/network_dhcp.erb index 53f48f8c0..045496192 100644 --- a/templates/guests/slackware/network_dhcp.erb +++ b/templates/guests/slackware/network_dhcp.erb @@ -1,23 +1,11 @@ -IPADDR[0]="" -NETMASK[0]="" -USE_DHCP[0]="yes" -DHCP_HOSTNAME[0]="" +#VAGRANT-BEGIN +# Config for eth<%= i %> +USE_DHCP[<%= i %>]="yes" +DHCP_HOSTNAME[<%= i %>]="" -IPADDR[1]="" -NETMASK[1]="" -USE_DHCP[1]="yes" -DHCP_HOSTNAME[1]="" - -IPADDR[2]="" -NETMASK[2]="" -USE_DHCP[2]="" -DHCP_HOSTNAME[2]="" - -IPADDR[3]="" -NETMASK[3]="" -USE_DHCP[3]="" -DHCP_HOSTNAME[3]="" - -GATEWAY="" +<% if options[:gateway] -%> +GATEWAY="<%= options[:gateway] %>" +<% end -%> DEBUG_ETH_UP="no" +#VAGRANT-END diff --git a/templates/guests/slackware/network_static.erb b/templates/guests/slackware/network_static.erb index 0745e8b1a..70ac38378 100644 --- a/templates/guests/slackware/network_static.erb +++ b/templates/guests/slackware/network_static.erb @@ -1,25 +1,13 @@ -IPADDR[0]="" -NETMASK[0]="" -USE_DHCP[0]="yes" -DHCP_HOSTNAME[0]="" +#VAGRANT-BEGIN +# Config for eth<%= i %> +IPADDR[<%= i %>]="<%= options[:ip] %>" +NETMASK[<%= i %>]="<%= options[:ip] %>" +USE_DHCP[<%= i %>]="" +DHCP_HOSTNAME[<%= i %>]="" -IPADDR[1]="<%= options[:ip] %>" -NETMASK[1]="" -USE_DHCP[1]="" -DHCP_HOSTNAME[1]="" - -IPADDR[2]="" -NETMASK[2]="" -USE_DHCP[2]="" -DHCP_HOSTNAME[2]="" - -IPADDR[3]="" -NETMASK[3]="" -USE_DHCP[3]="" -DHCP_HOSTNAME[3]="" - -<% if options[:gateway] %> - GATEWAY="<%= options[:gateway] %>" -<% end %> +<% if options[:gateway] -%> +GATEWAY="<%= options[:gateway] %>" +<% end -%> DEBUG_ETH_UP="no" +#VAGRANT-END diff --git a/templates/guests/suse/network_dhcp.erb b/templates/guests/suse/network_dhcp.erb index 3504b265e..0ec6dcac4 100644 --- a/templates/guests/suse/network_dhcp.erb +++ b/templates/guests/suse/network_dhcp.erb @@ -2,5 +2,5 @@ # The contents below are automatically generated by Vagrant. Do not modify. BOOTPROTO='dhcp' STARTMODE='auto' -DEVICE='eth<%= options[:interface] %>' +DEVICE='<%= options[:device] %>' #VAGRANT-END diff --git a/templates/guests/suse/network_static.erb b/templates/guests/suse/network_static.erb index c9270d7cd..35bf34d64 100644 --- a/templates/guests/suse/network_static.erb +++ b/templates/guests/suse/network_static.erb @@ -3,10 +3,10 @@ BOOTPROTO='static' IPADDR='<%= options[:ip] %>' NETMASK='<%= options[:netmask] %>' -DEVICE='eth<%= options[:interface] %>' -<% if options[:gateway] %> +DEVICE='<%= options[:device] %>' +<% if options[:gateway] -%> GATEWAY='<%= options[:gateway] %>' -<% end %> +<% end -%> PEERDNS='no' STARTMODE='auto' USERCONTROL='no' diff --git a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb index c8cf9c1ce..e296caa0a 100644 --- a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb @@ -9,29 +9,30 @@ describe "VagrantPlugins::GuestArch::Cap::ChangeHostName" do 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 describe ".change_host_name" do - let(:hostname) { "example.com" } + let(:hostname) { "banana-rama.example.com" } it "sets the hostname" do - communicator.stub_command("sudo hostname | grep '#{hostname}'", exit_code: 1) - communicator.expect_command("hostnamectl set-hostname #{hostname}") + comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1) + described_class.change_host_name(machine, hostname) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/) end it "does not change the hostname if already set" do - communicator.stub_command("sudo hostname | grep '#{hostname}'", exit_code: 0) + comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0) described_class.change_host_name(machine, hostname) - expect(communicator.received_commands.size).to eq(1) + expect(comm.received_commands.size).to eq(1) end end end diff --git a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb index 2f6de689e..24c89a2ed 100644 --- a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb @@ -9,16 +9,16 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do 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) - communicator.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", + 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 - communicator.verify_expectations! + comm.verify_expectations! end describe ".configure_networks" do @@ -40,9 +40,16 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - communicator.expect_command("ip link set eth1 down && netctl restart eth1 && netctl enable eth1") - communicator.expect_command("ip link set eth2 down && netctl restart eth2 && netctl enable eth2") described_class.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth1'/) + expect(comm.received_commands[1]).to match(/ip link set 'eth1' down/) + expect(comm.received_commands[1]).to match(/netctl restart 'eth1'/) + expect(comm.received_commands[1]).to match(/netctl enable 'eth1'/) + + expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth2'/) + expect(comm.received_commands[1]).to match(/ip link set 'eth2' down/) + expect(comm.received_commands[1]).to match(/netctl restart 'eth2'/) + expect(comm.received_commands[1]).to match(/netctl enable 'eth2'/) end end end diff --git a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb new file mode 100644 index 000000000..72d0dc75b --- /dev/null +++ b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb @@ -0,0 +1,38 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestAtomic::Plugin + .components + .guest_capabilities[:atomic] + .get(:change_host_name) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:hostname) { "banana-rama.example.com" } + + it "sets the hostname" do + comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1) + + described_class.change_host_name(machine, hostname) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/) + end + + it "does not change the hostname if already set" do + comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0) + described_class.change_host_name(machine, hostname) + expect(comm.received_commands.size).to eq(1) + end + end +end diff --git a/test/unit/plugins/guests/atomic/cap/docker_test.rb b/test/unit/plugins/guests/atomic/cap/docker_test.rb new file mode 100644 index 000000000..ab3b6489d --- /dev/null +++ b/test/unit/plugins/guests/atomic/cap/docker_test.rb @@ -0,0 +1,28 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestAtomic::Plugin + .components + .guest_capabilities[:atomic] + .get(:docker_daemon_running) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".docker_daemon_running" do + it "checks /run/docker/sock" do + described_class.docker_daemon_running(machine) + expect(comm.received_commands[0]).to eq("test -S /run/docker.sock") + end + end +end diff --git a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb index d893384ab..e29d52aa5 100644 --- a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb @@ -9,29 +9,29 @@ describe "VagrantPlugins::GuestCoreOS::Cap::ChangeHostName" do 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 describe ".change_host_name" do - let(:hostname) { "example.com" } + let(:hostname) { "banana-rama.example.com" } it "sets the hostname" do - communicator.stub_command("sudo hostname --fqdn | grep '#{hostname}'", exit_code: 1) - communicator.expect_command("hostname example") + comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 1) + comm.expect_command("hostname 'banana-rama'") described_class.change_host_name(machine, hostname) end it "does not change the hostname if already set" do - communicator.stub_command("sudo hostname --fqdn | grep '#{hostname}'", exit_code: 0) + comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 0) described_class.change_host_name(machine, hostname) - expect(communicator.received_commands.size).to eq(1) + expect(comm.received_commands.size).to eq(1) end end end diff --git a/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb b/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb new file mode 100644 index 000000000..28409ea6f --- /dev/null +++ b/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb @@ -0,0 +1,59 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestCoreOS::Cap::ConfigureNetworks" do + let(:described_class) do + VagrantPlugins::GuestCoreOS::Plugin + .components + .guest_capabilities[:coreos] + .get(:configure_networks) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + let(:env) do + double("env", machine: machine, active_machines: [machine]) + end + + before do + allow(machine).to receive(:communicate).and_return(comm) + allow(machine).to receive(:env).and_return(env) + + allow(described_class).to receive(:get_ip).and_return("1.2.3.4") + + comm.stub_command("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:", + stdout: "eth1\neth2") + end + + after do + comm.verify_expectations! + end + + describe ".configure_networks" do + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + it "creates and starts the networks" do + described_class.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/systemctl stop etcd/) + expect(comm.received_commands[1]).to match(/ifconfig eth1 netmask/) + expect(comm.received_commands[1]).to match(/ifconfig eth2 33.33.33.10 netmask 255.255.0.0/) + expect(comm.received_commands[1]).to match(/systemctl restart local-enable.service/) + expect(comm.received_commands[1]).to match(/systemctl start etcd/) + end + end +end diff --git a/test/unit/plugins/guests/coreos/cap/docker_test.rb b/test/unit/plugins/guests/coreos/cap/docker_test.rb new file mode 100644 index 000000000..25f5f6856 --- /dev/null +++ b/test/unit/plugins/guests/coreos/cap/docker_test.rb @@ -0,0 +1,28 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestCoreOS::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestCoreOS::Plugin + .components + .guest_capabilities[:coreos] + .get(:docker_daemon_running) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".docker_daemon_running" do + it "checks /run/docker/sock" do + described_class.docker_daemon_running(machine) + expect(comm.received_commands[0]).to eq("test -S /run/docker.sock") + end + end +end diff --git a/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb new file mode 100644 index 000000000..8852817fa --- /dev/null +++ b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb @@ -0,0 +1,40 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestDarwin::Plugin + .components + .guest_capabilities[:darwin] + .get(:change_host_name) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:hostname) { "banana-rama.example.com" } + + it "sets the hostname" do + comm.stub_command("hostname -f | grep -w '#{hostname}' || hostname -s | grep -w '#{hostname}'", exit_code: 1) + described_class.change_host_name(machine, hostname) + expect(comm.received_commands[1]).to match(/scutil --set ComputerName 'banana-rama.example.com'/) + expect(comm.received_commands[1]).to match(/scutil --set HostName 'banana-rama.example.com'/) + expect(comm.received_commands[1]).to match(/scutil --set LocalHostName 'banana-rama'/) + expect(comm.received_commands[1]).to match(/hostname 'banana-rama.example.com'/) + end + + it "does not change the hostname if already set" do + comm.stub_command("hostname -f | grep -w '#{hostname}' || hostname -s | grep -w '#{hostname}'", exit_code: 0) + described_class.change_host_name(machine, hostname) + expect(comm.received_commands.size).to eq(1) + end + end +end diff --git a/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb b/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb new file mode 100644 index 000000000..4468db8e9 --- /dev/null +++ b/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb @@ -0,0 +1,36 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestDarwin::Plugin + .components + .guest_capabilities[:darwin] + .get(:choose_addressable_ip_addr) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".choose_addressable_ip_addr" do + let(:possible) { ["1.2.3.4", "5.6.7.8"] } + + it "retrieves the value" do + comm.stub_command("ping -c1 -t1 5.6.7.8", exit_code: 0) + result = described_class.choose_addressable_ip_addr(machine, possible) + expect(result).to eq("5.6.7.8") + end + + it "returns nil if no ips are found" do + result = described_class.choose_addressable_ip_addr(machine, []) + expect(result).to be(nil) + end + end +end 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 51d717445..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,25 +1,28 @@ 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") } - 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) + comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", + stdout: "eth1\neth2") end after do - communicator.verify_expectations! + comm.verify_expectations! end describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + let(:network_0) do { interface: 0, @@ -38,13 +41,14 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - communicator.expect_command("/sbin/ifdown eth0 2> /dev/null || true") - communicator.expect_command("/sbin/ip addr flush dev eth0 2> /dev/null") - communicator.expect_command("/sbin/ifdown eth1 2> /dev/null || true") - communicator.expect_command("/sbin/ip addr flush dev eth1 2> /dev/null") - communicator.expect_command("/sbin/ifup eth0") - communicator.expect_command("/sbin/ifup eth1") - described_class.configure_networks(machine, [network_0, network_1]) + cap.configure_networks(machine, [network_0, network_1]) + + 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/debian/cap/nfs_client_test.rb b/test/unit/plugins/guests/debian/cap/nfs_client_test.rb new file mode 100644 index 000000000..5bc854d41 --- /dev/null +++ b/test/unit/plugins/guests/debian/cap/nfs_client_test.rb @@ -0,0 +1,30 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestDebian::Cap::NFSClient" do + let(:described_class) do + VagrantPlugins::GuestDebian::Plugin + .components + .guest_capabilities[:debian] + .get(:nfs_client_install) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".nfs_client_install" do + it "installs nfs client utilities" do + described_class.nfs_client_install(machine) + + expect(comm.received_commands[0]).to match(/apt-get -yqq update/) + expect(comm.received_commands[0]).to match(/apt-get -yqq install nfs-common portmap/) + end + end +end diff --git a/test/unit/plugins/guests/debian/cap/rsync_test.rb b/test/unit/plugins/guests/debian/cap/rsync_test.rb new file mode 100644 index 000000000..753a9ab16 --- /dev/null +++ b/test/unit/plugins/guests/debian/cap/rsync_test.rb @@ -0,0 +1,38 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestDebian::Cap:RSync" do + let(:described_class) do + VagrantPlugins::GuestDebian::Plugin + .components + .guest_capabilities[:debian] + .get(:rsync_install) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".rsync_install" do + it "installs rsync when not installed" do + comm.stub_command("command -v rsync", exit_code: 1) + described_class.rsync_install(machine) + + expect(comm.received_commands[1]).to match(/apt-get -yqq update/) + expect(comm.received_commands[1]).to match(/apt-get -yqq install rsync/) + end + + it "does not install rsync when installed" do + comm.stub_command("command -v rsync", exit_code: 0) + described_class.rsync_install(machine) + + expect(comm.received_commands.join("")).to_not match(/update/) + end + end +end diff --git a/test/unit/plugins/guests/debian/cap/smb_test.rb b/test/unit/plugins/guests/debian/cap/smb_test.rb new file mode 100644 index 000000000..7e4159eff --- /dev/null +++ b/test/unit/plugins/guests/debian/cap/smb_test.rb @@ -0,0 +1,38 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestDebian::Cap::SMB" do + let(:described_class) do + VagrantPlugins::GuestDebian::Plugin + .components + .guest_capabilities[:debian] + .get(:smb_install) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".smb_install" do + it "installs smb when /sbin/mount.cifs does not exist" do + comm.stub_command("test -f /sbin/mount.cifs", exit_code: 1) + described_class.smb_install(machine) + + expect(comm.received_commands[1]).to match(/apt-get -yqq update/) + expect(comm.received_commands[1]).to match(/apt-get -yqq install cifs-utils/) + end + + it "does not install smb when /sbin/mount.cifs exists" do + comm.stub_command("test -f /sbin/mount.cifs", exit_code: 0) + described_class.smb_install(machine) + + expect(comm.received_commands.join("")).to_not match(/update/) + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb new file mode 100644 index 000000000..fc5c061e9 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb @@ -0,0 +1,40 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:change_host_name) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:name) { "banana-rama.example.com" } + + it "sets the hostname and /etc/hosts" do + comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 1) + described_class.change_host_name(machine, name) + + expect(comm.received_commands[1]).to match(/hostname '#{name}'/) + expect(comm.received_commands[1]).to match(/grep -w '#{name}' \/etc\/hosts/) + expect(comm.received_commands[1]).to match(/echo -e '127.0.0.1\\t#{name}\\tbanana-rama'/) + end + + it "does nothing if the hostname is already set" do + comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 0) + described_class.change_host_name(machine, name) + expect(comm.received_commands.size).to eq(1) + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/configure_networks_test.rb b/test/unit/plugins/guests/freebsd/cap/configure_networks_test.rb new file mode 100644 index 000000000..20b4cab61 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/configure_networks_test.rb @@ -0,0 +1,51 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::ConfigureNetworks" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:configure_networks) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + comm.stub_command("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'", + stdout: "em1\nem2") + end + + after do + comm.verify_expectations! + end + + describe ".configure_networks" do + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + it "creates and starts the networks" do + described_class.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/dhclient 'em1'/) + expect(comm.received_commands[1]).to match(/\/etc\/rc.d\/netif restart 'em1'/) + + expect(comm.received_commands[1]).to_not match(/dhclient 'em2'/) + expect(comm.received_commands[1]).to match(/\/etc\/rc.d\/netif restart 'em2'/) + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/halt_test.rb b/test/unit/plugins/guests/freebsd/cap/halt_test.rb new file mode 100644 index 000000000..e44de83d6 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/halt_test.rb @@ -0,0 +1,35 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::Halt" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:halt) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".halt" do + it "runs the shutdown command" do + comm.expect_command("shutdown -p now") + described_class.halt(machine) + end + + it "does not raise an IOError" do + comm.stub_command("shutdown -p now", raise: IOError) + expect { + described_class.halt(machine) + }.to_not raise_error + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb b/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb new file mode 100644 index 000000000..8f4c45da8 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb @@ -0,0 +1,31 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::InsertPublicKey" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:insert_public_key) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".insert_public_key" do + it "inserts the public key" do + described_class.insert_public_key(machine, "ssh-rsa ...") + expect(comm.received_commands[0]).to match(/mkdir -p ~\/.ssh/) + expect(comm.received_commands[0]).to match(/chmod 0700 ~\/.ssh/) + expect(comm.received_commands[0]).to match(/cat '\/tmp\/vagrant-(.+)' >> ~\/.ssh\/authorized_keys/) + expect(comm.received_commands[0]).to match(/chmod 0600 ~\/.ssh\/authorized_keys/) + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/mount_nfs_folder_test.rb b/test/unit/plugins/guests/freebsd/cap/mount_nfs_folder_test.rb new file mode 100644 index 000000000..b86ad26df --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/mount_nfs_folder_test.rb @@ -0,0 +1,53 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::MountNFSFolder" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:mount_nfs_folder) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".mount_nfs_folder" do + let(:ip) { "1.2.3.4" } + + it "mounts the folder" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + described_class.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mkdir -p '\/guest'/) + expect(comm.received_commands[0]).to match(/'1.2.3.4:\/host' '\/guest'/) + end + + it "mounts with options" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + nfs_version: 2, + } + } + described_class.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mount -t nfs -o nfsv2/) + end + end +end diff --git a/test/unit/plugins/guests/freebsd/cap/rsync_test.rb b/test/unit/plugins/guests/freebsd/cap/rsync_test.rb new file mode 100644 index 000000000..d0ad41829 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/rsync_test.rb @@ -0,0 +1,46 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::RSync" do + let(:caps) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".rsync_install" do + let(:cap) { caps.get(:rsync_install) } + + it "installs rsync" do + comm.expect_command("pkg install -y rsync") + cap.rsync_install(machine) + end + end + + describe ".rsync_installed" do + let(:cap) { caps.get(:rsync_installed) } + + it "checks if rsync is installed" do + comm.expect_command("which rsync") + cap.rsync_installed(machine) + end + end + + describe ".rsync_command" do + let(:cap) { caps.get(:rsync_command) } + + it "defaults to 'sudo rsync'" do + expect(cap.rsync_command(machine)).to eq("sudo rsync") + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/choose_addressable_ip_addr_test.rb b/test/unit/plugins/guests/linux/cap/choose_addressable_ip_addr_test.rb new file mode 100644 index 000000000..9bf26c0f6 --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/choose_addressable_ip_addr_test.rb @@ -0,0 +1,38 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::ChooseAddressableIPAddr" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".choose_addressable_ip_addr" do + let(:cap) { caps.get(:choose_addressable_ip_addr) } + + it "returns the first matching IP address" do + possible = ["1.2.3.4", "5.6.7.8"] + possible.each do |ip| + comm.stub_command("ping -c1 -w1 -W1 #{ip}", exit_code: 0) + end + result = cap.choose_addressable_ip_addr(machine, possible) + expect(result).to eq("1.2.3.4") + end + + it "returns nil when there are no matches" do + result = cap.choose_addressable_ip_addr(machine, []) + expect(result).to be(nil) + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/halt_test.rb b/test/unit/plugins/guests/linux/cap/halt_test.rb new file mode 100644 index 000000000..7b90e7bed --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/halt_test.rb @@ -0,0 +1,36 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::Halt" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".halt" do + let(:cap) { caps.get(:halt) } + + it "runs the shutdown command" do + comm.expect_command("shutdown -h now") + cap.halt(machine) + end + + it "does not raise an IOError" do + comm.stub_command("shutdown -h now", raise: IOError) + expect { + cap.halt(machine) + }.to_not raise_error + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/insert_public_key_test.rb b/test/unit/plugins/guests/linux/cap/insert_public_key_test.rb new file mode 100644 index 000000000..704f2161d --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/insert_public_key_test.rb @@ -0,0 +1,32 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::InsertPublicKey" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".insert_public_key" do + let(:cap) { caps.get(:insert_public_key) } + + it "inserts the public key" do + cap.insert_public_key(machine, "ssh-rsa ...") + expect(comm.received_commands[0]).to match(/mkdir -p ~\/.ssh/) + expect(comm.received_commands[0]).to match(/chmod 0700 ~\/.ssh/) + expect(comm.received_commands[0]).to match(/cat '\/tmp\/vagrant-(.+)' >> ~\/.ssh\/authorized_keys/) + expect(comm.received_commands[0]).to match(/chmod 0600 ~\/.ssh\/authorized_keys/) + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb new file mode 100644 index 000000000..6a53c33ed --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb @@ -0,0 +1,78 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".mount_nfs_folder" do + let(:cap) { caps.get(:mount_nfs_folder) } + + let(:ip) { "1.2.3.4" } + + let(:hostpath) { "/host" } + let(:guestpath) { "/guest" } + + before do + allow(machine).to receive(:guest).and_return( + double("capability", capability: guestpath) + ) + end + + it "mounts the folder" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mkdir -p '#{guestpath}'/) + expect(comm.received_commands[0]).to match(/'1.2.3.4:#{hostpath}' '#{guestpath}'/) + end + + it "mounts with options" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + nfs_version: 2, + nfs_udp: true, + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mount -o vers=2,udp/) + end + + it "emits an event" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to include( + "/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{guestpath}'") + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/mount_shared_folder_test.rb b/test/unit/plugins/guests/linux/cap/mount_shared_folder_test.rb index 714e633d8..42f666c30 100644 --- a/test/unit/plugins/guests/linux/cap/mount_shared_folder_test.rb +++ b/test/unit/plugins/guests/linux/cap/mount_shared_folder_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::GuestLinux::Cap::MountSharedFolder" do let(:machine) { double("machine") } diff --git a/test/unit/plugins/guests/linux/cap/nfs_client_test.rb b/test/unit/plugins/guests/linux/cap/nfs_client_test.rb new file mode 100644 index 000000000..a0bed0033 --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/nfs_client_test.rb @@ -0,0 +1,29 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::NFSClient" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".nfs_client_installed" do + let(:cap) { caps.get(:nfs_client_installed) } + + it "installs nfs client utilities" do + comm.expect_command("test -x /sbin/mount.nfs") + cap.nfs_client_installed(machine) + end + end +end diff --git a/test/unit/plugins/guests/linux/cap/port_test.rb b/test/unit/plugins/guests/linux/cap/port_test.rb new file mode 100644 index 000000000..36792e8e2 --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/port_test.rb @@ -0,0 +1,30 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::Port" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".port_open_check" do + let(:cap) { caps.get(:port_open_check) } + + it "checks if the port is open" do + port = 8080 + comm.expect_command("nc -z -w2 127.0.0.1 #{port}") + cap.port_open_check(machine, port) + 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 new file mode 100644 index 000000000..1ad9fb4c9 --- /dev/null +++ b/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb @@ -0,0 +1,41 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do + let(:caps) do + VagrantPlugins::GuestOmniOS::Plugin + .components + .guest_capabilities[:omnios] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } + + let(:name) { "banana-rama.example.com" } + + it "sets the hostname if unset" do + comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1) + cap.change_host_name(machine, name) + + expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/nodename/) + expect(comm.received_commands[1]).to match(/hostname '#{name}'/) + end + + it "does not set the hostname if set" do + comm.stub_command("hostname | 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/omnios/cap/mount_nfs_folder_test.rb b/test/unit/plugins/guests/omnios/cap/mount_nfs_folder_test.rb new file mode 100644 index 000000000..ec5b2cf9f --- /dev/null +++ b/test/unit/plugins/guests/omnios/cap/mount_nfs_folder_test.rb @@ -0,0 +1,47 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do + let(:caps) do + VagrantPlugins::GuestOmniOS::Plugin + .components + .guest_capabilities[:omnios] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".mount_nfs_folder" do + let(:cap) { caps.get(:mount_nfs_folder) } + + let(:ip) { "1.2.3.4" } + + let(:hostpath) { "/host" } + let(:guestpath) { "/guest" } + + it "mounts the folder" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mkdir -p '#{guestpath}'/) + expect(comm.received_commands[0]).to match(/'1.2.3.4:#{hostpath}' '#{guestpath}'/) + end + + it "mounts with options" do + pending "not yet implemented" + end + end +end diff --git a/test/unit/plugins/guests/omnios/cap/rsync_test.rb b/test/unit/plugins/guests/omnios/cap/rsync_test.rb new file mode 100644 index 000000000..86ff39e30 --- /dev/null +++ b/test/unit/plugins/guests/omnios/cap/rsync_test.rb @@ -0,0 +1,29 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do + let(:caps) do + VagrantPlugins::GuestOmniOS::Plugin + .components + .guest_capabilities[:omnios] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".rsync_install" do + let(:cap) { caps.get(:rsync_install) } + + it "installs rsync" do + cap.rsync_install(machine) + expect(comm.received_commands[0]).to match(/pkg install rsync/) + end + end +end diff --git a/test/unit/plugins/guests/photon/cap/change_host_name_test.rb b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb index 5e9e9e9e1..39634aec2 100644 --- a/test/unit/plugins/guests/photon/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb @@ -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 diff --git a/test/unit/plugins/guests/photon/cap/configure_networks_test.rb b/test/unit/plugins/guests/photon/cap/configure_networks_test.rb index dc6e9aaf1..97f66a727 100644 --- a/test/unit/plugins/guests/photon/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/photon/cap/configure_networks_test.rb @@ -1,40 +1,51 @@ -# encoding: UTF-8 # Copyright (c) 2015 VMware, Inc. All Rights Reserved. -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" -describe "VagrantPlugins::GuestPhoton::Cap::ConfigureNetworks" do - let(:described_class) do - VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:configure_networks) +describe "VagrantPlugins::GuestPhoton::Cap:ConfigureNetworks" do + 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) + comm.stub_command("ifconfig | grep 'eth' | cut -f1 -d' '", + stdout: "eth1\neth2") end after do - communicator.verify_expectations! + comm.verify_expectations! end - it 'should configure networks' do - networks = [ - { :type => :static, :ip => '192.168.10.10', :netmask => '255.255.255.0', :interface => 1, :name => 'eth0' }, - { :type => :dhcp, :interface => 2, :name => 'eth1' }, - { :type => :static, :ip => '10.168.10.10', :netmask => '255.255.0.0', :interface => 3, :name => 'docker0' } - ] - communicator.should_receive(:sudo).with("ifconfig | grep 'eth' | cut -f1 -d' '") - communicator.should_receive(:sudo).with('ifconfig 192.168.10.10 netmask 255.255.255.0') - communicator.should_receive(:sudo).with('ifconfig netmask ') - communicator.should_receive(:sudo).with('ifconfig 10.168.10.10 netmask 255.255.0.0') + describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } - allow_message_expectations_on_nil - machine.should_receive(:env).at_least(5).times - machine.env.should_receive(:active_machines).at_least(:twice) - machine.env.active_machines.should_receive(:first) - machine.env.should_receive(:machine) + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end - described_class.configure_networks(machine, networks) + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + it "creates and starts the networks" do + cap.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/ifconfig eth1/) + expect(comm.received_commands[1]).to match(/ifconfig eth2 33.33.33.10 netmast 255.255.0.0/) + end end end diff --git a/test/unit/plugins/guests/photon/cap/docker_test.rb b/test/unit/plugins/guests/photon/cap/docker_test.rb index 6a1c726ff..28c733fa4 100644 --- a/test/unit/plugins/guests/photon/cap/docker_test.rb +++ b/test/unit/plugins/guests/photon/cap/docker_test.rb @@ -1,26 +1,31 @@ -# encoding: UTF-8 # Copyright (c) 2015 VMware, Inc. All Rights Reserved. -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" -describe "VagrantPlugins::GuestPhoton::Cap::Docker" do - let(:described_class) do - VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:docker_daemon_running) +describe "VagrantPlugins::GuestPhoton::Cap:Docker" do + let(:caps) do + VagrantPlugins::GuestPhoton::Plugin + .components + .guest_capabilities[:photon] 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) + allow(machine).to receive(:communicate).and_return(comm) end after do - communicator.verify_expectations! + comm.verify_expectations! end - it 'should check docker' do - expect(communicator).to receive(:test).with('test -S /run/docker.sock') - described_class.docker_daemon_running(machine) + describe ".docker_daemon_running" do + let(:cap) { caps.get(:docker_daemon_running) } + + it "installs rsync" do + comm.expect_command("test -S /run/docker.sock") + cap.docker_daemon_running(machine) + end end end diff --git a/test/unit/plugins/guests/pld/cap/change_host_name_test.rb b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb new file mode 100644 index 000000000..75af68d6f --- /dev/null +++ b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb @@ -0,0 +1,39 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestPld::Cap::ChangeHostName" do + let(:caps) do + VagrantPlugins::GuestPld::Plugin + .components + .guest_capabilities[:pld] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } + + let(:name) { "banana-rama.example.com" } + + it "sets the hostname" do + comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1) + + cap.change_host_name(machine, name) + expect(comm.received_commands[1]).to match(/hostname '#{name}'/) + end + + it "does not change the hostname if already set" do + comm.stub_command("hostname | 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/pld/cap/flavor_test.rb b/test/unit/plugins/guests/pld/cap/flavor_test.rb new file mode 100644 index 000000000..e20d3a784 --- /dev/null +++ b/test/unit/plugins/guests/pld/cap/flavor_test.rb @@ -0,0 +1,21 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestPld::Cap::Flavor" do + let(:caps) do + VagrantPlugins::GuestPld::Plugin + .components + .guest_capabilities[:pld] + end + + let(:machine) { double("machine") } + + describe ".flavor" do + let(:cap) { caps.get(:flavor) } + + let(:name) { "banana-rama.example.com" } + + it "is pld" do + expect(cap.flavor(machine)).to be(:pld) + end + end +end diff --git a/test/unit/plugins/guests/pld/cap/network_scripts_dir_test.rb b/test/unit/plugins/guests/pld/cap/network_scripts_dir_test.rb new file mode 100644 index 000000000..3fbb7bd8f --- /dev/null +++ b/test/unit/plugins/guests/pld/cap/network_scripts_dir_test.rb @@ -0,0 +1,21 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestPld::Cap::NetworkScriptsDir" do + let(:caps) do + VagrantPlugins::GuestPld::Plugin + .components + .guest_capabilities[:pld] + end + + let(:machine) { double("machine") } + + describe ".network_scripts_dir" do + let(:cap) { caps.get(:network_scripts_dir) } + + let(:name) { "banana-rama.example.com" } + + it "is /etc/sysconfig/interfaces" do + expect(cap.network_scripts_dir(machine)).to eq("/etc/sysconfig/interfaces") + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb index 558bb5983..84da01b20 100644 --- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb @@ -1,70 +1,42 @@ -require File.expand_path("../../../../../base", __FILE__) -require File.expand_path("../../../support/shared/redhat_like_host_name_examples", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do - let(:described_class) do - VagrantPlugins::GuestRedHat::Plugin.components.guest_capabilities[:redhat].get(:change_host_name) + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] end + let(:machine) { double("machine") } - let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - let(:guest) { double("guest") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do - allow(guest).to receive(:capability).and_return(nil) - allow(machine).to receive(:communicate).and_return(communicator) - allow(machine).to receive(:guest).and_return(guest) - communicator.stub_command('hostname -f', stdout: old_hostname) - communicator.expect_command('hostname -f') + allow(machine).to receive(:communicate).and_return(comm) end after do - communicator.verify_expectations! + comm.verify_expectations! end - context 'when oldhostname is qualified' do - let(:old_hostname) { 'oldhostname.olddomain.tld' } - let(:similar_hostname) {'oldhostname'} + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } - it_behaves_like 'a full redhat-like host name change' + let(:name) { "banana-rama.example.com" } - include_examples 'inserting hostname in /etc/hosts' - include_examples 'swapping simple hostname in /etc/hosts' - include_examples 'swapping qualified hostname in /etc/hosts' - end + it "sets the hostname" do + comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) - context 'when oldhostname is simple' do - let(:old_hostname) { 'oldhostname' } - let(:similar_hostname) {'oldhostname.olddomain.tld'} - - it_behaves_like 'a full redhat-like host name change' - - include_examples 'inserting hostname in /etc/hosts' - include_examples 'swapping simple hostname in /etc/hosts' - - context 'and is only able to be determined by hostname (without -f)' do - before do - communicator.stub_command('hostname -f',nil) - communicator.stub_command('hostname', stdout: old_hostname) - communicator.expect_command('hostname') - end - - it_behaves_like 'a full redhat-like host name change' - - include_examples 'inserting hostname in /etc/hosts' - include_examples 'swapping simple hostname in /etc/hosts' + cap.change_host_name(machine, name) + expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/) + expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{name}'/) + expect(comm.received_commands[1]).to match(/service network restart/) end - end - context 'when the short version of hostname is localhost' do - let(:old_hostname) { 'localhost.olddomain.tld' } - - it_behaves_like 'a partial redhat-like host name change' - - include_examples 'inserting hostname in /etc/hosts' - - it "does more even when the provided hostname is not different" do - described_class.change_host_name(machine, old_hostname) - expect(communicator.received_commands.to_set).not_to eq(communicator.expected_commands.keys.to_set) + 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 diff --git a/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb new file mode 100644 index 000000000..d89f8928a --- /dev/null +++ b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb @@ -0,0 +1,80 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + comm.stub_command("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'", + stdout: "eth1\neth2") + end + + after do + comm.verify_expectations! + end + + describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + let(:network_scripts_dir) { "/" } + + let(:capability) { double("capability") } + + before do + allow(machine).to receive(:guest).and_return(capability) + allow(capability).to receive(:capability) + .with(:network_scripts_dir) + .and_return(network_scripts_dir) + end + + it "uses fedora for rhel7 configuration" do + require_relative "../../../../../../plugins/guests/fedora/cap/configure_networks" + + allow(capability).to receive(:capability) + .with(:flavor) + .and_return(:rhel_7) + allow(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks) + .to receive(:configure_networks) + + expect(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks) + .to receive(:configure_networks).once + cap.configure_networks(machine, [network_1, network_2]) + end + + it "creates and starts the networks" do + allow(capability).to receive(:capability) + .with(:flavor) + .and_return(:rhel) + + cap.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/) + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/flavor_test.rb b/test/unit/plugins/guests/redhat/cap/flavor_test.rb new file mode 100644 index 000000000..636b079cb --- /dev/null +++ b/test/unit/plugins/guests/redhat/cap/flavor_test.rb @@ -0,0 +1,39 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestRedHat::Cap::Flavor" do + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".flavor" do + let(:cap) { caps.get(:flavor) } + + { + "CentOS Linux 2.4 release 7" => :rhel_7, + "Red Hat Enterprise Linux release 7" => :rhel_7, + "Scientific Linux release 7" => :rhel_7, + + "CentOS" => :rhel, + "RHEL 6" => :rhel, + "banana" => :rhel, + }.each do |str, expected| + it "returns #{expected} for #{str}" do + comm.stub_command("cat /etc/redhat-release", stdout: str) + expect(cap.flavor(machine)).to be(expected) + end + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/network_scripts_dir_test.rb b/test/unit/plugins/guests/redhat/cap/network_scripts_dir_test.rb new file mode 100644 index 000000000..189fb659c --- /dev/null +++ b/test/unit/plugins/guests/redhat/cap/network_scripts_dir_test.rb @@ -0,0 +1,21 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestRedHat::Cap::NetworkScriptsDir" do + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] + end + + let(:machine) { double("machine") } + + describe ".network_scripts_dir" do + let(:cap) { caps.get(:network_scripts_dir) } + + let(:name) { "banana-rama.example.com" } + + it "is /etc/sysconfig/network-scripts" do + expect(cap.network_scripts_dir(machine)).to eq("/etc/sysconfig/network-scripts") + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/nfs_client_test.rb b/test/unit/plugins/guests/redhat/cap/nfs_client_test.rb new file mode 100644 index 000000000..a86136e19 --- /dev/null +++ b/test/unit/plugins/guests/redhat/cap/nfs_client_test.rb @@ -0,0 +1,30 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestRedHat::Cap:NFSClient" do + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".nfs_client_install" do + let(:cap) { caps.get(:nfs_client_install) } + + it "installs rsync" do + cap.nfs_client_install(machine) + expect(comm.received_commands[0]).to match(/install nfs-utils nfs-utils-lib portmap/) + expect(comm.received_commands[0]).to match(/\/bin\/systemctl restart rpcbind nfs/) + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/rsync_test.rb b/test/unit/plugins/guests/redhat/cap/rsync_test.rb new file mode 100644 index 000000000..654af9344 --- /dev/null +++ b/test/unit/plugins/guests/redhat/cap/rsync_test.rb @@ -0,0 +1,29 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestRedHat::Cap:RSync" do + let(:caps) do + VagrantPlugins::GuestRedHat::Plugin + .components + .guest_capabilities[:redhat] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".rsync_install" do + let(:cap) { caps.get(:rsync_install) } + + it "installs rsync" do + cap.rsync_install(machine) + expect(comm.received_commands[0]).to match(/install rsync/) + end + end +end diff --git a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb new file mode 100644 index 000000000..2c128fc61 --- /dev/null +++ b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb @@ -0,0 +1,40 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSlackware::Cap::ChangeHostName" do + let(:caps) do + VagrantPlugins::GuestSlackware::Plugin + .components + .guest_capabilities[:slackware] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } + + 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 -F \/etc\/hostname/) + 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 diff --git a/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb b/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb new file mode 100644 index 000000000..10cc8493b --- /dev/null +++ b/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb @@ -0,0 +1,48 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do + let(:caps) do + VagrantPlugins::GuestSlackware::Plugin + .components + .guest_capabilities[:slackware] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + 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 + comm.verify_expectations! + end + + describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + it "creates and starts the networks" do + cap.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/\/etc\/rc.d\/rc.inet1/) + 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 1d9a84692..3c91edf92 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,4 +1,5 @@ -require File.expand_path("../../../../../base", __FILE__) +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) } @@ -30,4 +31,3 @@ describe "VagrantPlugins::VagrantPlugins::Cap::ChangeHostName" do 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 index ca47a988f..7d6312a24 100644 --- a/test/unit/plugins/guests/smartos/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/smartos/cap/configure_networks_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::VagrantPlugins::Cap::ConfigureNetworks" do let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:configure_networks) } diff --git a/test/unit/plugins/guests/smartos/cap/halt_test.rb b/test/unit/plugins/guests/smartos/cap/halt_test.rb index 782fc6b09..664b77ec8 100644 --- a/test/unit/plugins/guests/smartos/cap/halt_test.rb +++ b/test/unit/plugins/guests/smartos/cap/halt_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::VagrantPlugins::Cap::Halt" do let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:halt) } 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 b4038f76a..d41dcd808 100644 --- a/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb +++ b/test/unit/plugins/guests/smartos/cap/mount_nfs_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::VagrantPlugins::Cap::MountNFS" do let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:mount_nfs_folder) } diff --git a/test/unit/plugins/guests/smartos/cap/rsync_test.rb b/test/unit/plugins/guests/smartos/cap/rsync_test.rb index ed2b20e78..1c3deb716 100644 --- a/test/unit/plugins/guests/smartos/cap/rsync_test.rb +++ b/test/unit/plugins/guests/smartos/cap/rsync_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::VagrantPlugins::Cap::Rsync" do let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:rsync_installed) } 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/suse/cap/change_host_name_test.rb b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb new file mode 100644 index 000000000..9001614d1 --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb @@ -0,0 +1,40 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } + + 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 diff --git a/test/unit/plugins/guests/suse/cap/configure_networks_test.rb b/test/unit/plugins/guests/suse/cap/configure_networks_test.rb new file mode 100644 index 000000000..b31f4e3e7 --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/configure_networks_test.rb @@ -0,0 +1,60 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + 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 + comm.verify_expectations! + end + + describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + let(:guest) { double("guest") } + + before do + allow(machine).to receive(:guest).and_return(guest) + allow(guest).to receive(:capability) + .with(:network_scripts_dir) + .and_return("/scripts") + end + + it "creates and starts the networks" do + cap.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/) + expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/) + end + end +end diff --git a/test/unit/plugins/guests/suse/cap/halt_test.rb b/test/unit/plugins/guests/suse/cap/halt_test.rb new file mode 100644 index 000000000..acce6536d --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/halt_test.rb @@ -0,0 +1,36 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::Halt" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".halt" do + let(:cap) { caps.get(:halt) } + + it "runs the shutdown command" do + comm.expect_command("/sbin/shutdown -h now") + cap.halt(machine) + end + + it "does not raise an IOError" do + comm.stub_command("shutdown -h now", raise: IOError) + expect { + cap.halt(machine) + }.to_not raise_error + end + end +end diff --git a/test/unit/plugins/guests/suse/cap/network_scripts_dir_test.rb b/test/unit/plugins/guests/suse/cap/network_scripts_dir_test.rb new file mode 100644 index 000000000..cea7b497a --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/network_scripts_dir_test.rb @@ -0,0 +1,19 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::NetworkScriptsDir" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + + describe ".network_scripts_dir" do + let(:cap) { caps.get(:network_scripts_dir) } + + it "runs /etc/sysconfig/network" do + expect(cap.network_scripts_dir(machine)).to eq("/etc/sysconfig/network") + end + end +end diff --git a/test/unit/plugins/guests/suse/cap/nfs_client_test.rb b/test/unit/plugins/guests/suse/cap/nfs_client_test.rb new file mode 100644 index 000000000..b079b3029 --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/nfs_client_test.rb @@ -0,0 +1,31 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::NFSClient" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".nfs_client_install" do + let(:cap) { caps.get(:nfs_client_install) } + + it "installs nfs client utilities" do + cap.nfs_client_install(machine) + expect(comm.received_commands[0]).to match(/zypper -n install nfs-client/) + expect(comm.received_commands[0]).to match(/service rpcbind restart/) + expect(comm.received_commands[0]).to match(/service nfs restart/) + end + end +end diff --git a/test/unit/plugins/guests/suse/cap/rsync_test.rb b/test/unit/plugins/guests/suse/cap/rsync_test.rb new file mode 100644 index 000000000..a6c19e593 --- /dev/null +++ b/test/unit/plugins/guests/suse/cap/rsync_test.rb @@ -0,0 +1,38 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSUSE::Cap::RSync" do + let(:caps) do + VagrantPlugins::GuestSUSE::Plugin + .components + .guest_capabilities[:suse] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".rsync_install" do + let(:cap) { caps.get(:rsync_install) } + + it "installs rsync" do + comm.expect_command("zypper -n install rsync") + cap.rsync_install(machine) + end + end + + describe ".rsync_installed" do + let(:cap) { caps.get(:rsync_installed) } + + it "checks if rsync is installed" do + comm.expect_command("test -f /usr/bin/rsync") + cap.rsync_installed(machine) + end + end +end diff --git a/test/unit/plugins/guests/tinycore/cap/change_host_name_test.rb b/test/unit/plugins/guests/tinycore/cap/change_host_name_test.rb index ba258261f..ce43cf84c 100644 --- a/test/unit/plugins/guests/tinycore/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/tinycore/cap/change_host_name_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" describe "VagrantPlugins::GuestTinyCore::Cap::ChangeHostName" do let(:described_class) do 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 diff --git a/test/unit/plugins/guests/windows/cap/change_host_name_test.rb b/test/unit/plugins/guests/windows/cap/change_host_name_test.rb index 1f8b45170..33bf261ba 100644 --- a/test/unit/plugins/guests/windows/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/windows/cap/change_host_name_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" require Vagrant.source_root.join("plugins/guests/windows/cap/change_host_name") diff --git a/test/unit/plugins/guests/windows/cap/halt_test.rb b/test/unit/plugins/guests/windows/cap/halt_test.rb index da4f51f38..913cba188 100644 --- a/test/unit/plugins/guests/windows/cap/halt_test.rb +++ b/test/unit/plugins/guests/windows/cap/halt_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" require Vagrant.source_root.join("plugins/guests/windows/cap/halt") diff --git a/test/unit/plugins/guests/windows/cap/mount_shared_folder_test.rb b/test/unit/plugins/guests/windows/cap/mount_shared_folder_test.rb index bd443142b..851c71da7 100644 --- a/test/unit/plugins/guests/windows/cap/mount_shared_folder_test.rb +++ b/test/unit/plugins/guests/windows/cap/mount_shared_folder_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" require Vagrant.source_root.join("plugins/guests/windows/cap/mount_shared_folder") diff --git a/test/unit/plugins/guests/windows/cap/reboot_test.rb b/test/unit/plugins/guests/windows/cap/reboot_test.rb index 8297b9a26..afa50d381 100644 --- a/test/unit/plugins/guests/windows/cap/reboot_test.rb +++ b/test/unit/plugins/guests/windows/cap/reboot_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" require Vagrant.source_root.join("plugins/guests/windows/cap/reboot") diff --git a/test/unit/plugins/guests/windows/cap/rsync_test.rb b/test/unit/plugins/guests/windows/cap/rsync_test.rb index e28a5b3fb..d32568c40 100644 --- a/test/unit/plugins/guests/windows/cap/rsync_test.rb +++ b/test/unit/plugins/guests/windows/cap/rsync_test.rb @@ -1,4 +1,4 @@ -require File.expand_path("../../../../../base", __FILE__) +require_relative "../../../../base" require Vagrant.source_root.join("plugins/guests/windows/cap/rsync") diff --git a/test/unit/templates/guests/arch/network_dhcp_test.rb b/test/unit/templates/guests/arch/network_dhcp_test.rb index 10a3f2c7d..b845eb666 100644 --- a/test/unit/templates/guests/arch/network_dhcp_test.rb +++ b/test/unit/templates/guests/arch/network_dhcp_test.rb @@ -7,11 +7,11 @@ describe "templates/guests/arch/network_dhcp" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") Description='A basic dhcp ethernet connection' - Interface=en0 + Interface=eth1 Connection=ethernet IP=dhcp EOH diff --git a/test/unit/templates/guests/arch/network_static_test.rb b/test/unit/templates/guests/arch/network_static_test.rb index 375ec67c5..fb3251d3b 100644 --- a/test/unit/templates/guests/arch/network_static_test.rb +++ b/test/unit/templates/guests/arch/network_static_test.rb @@ -7,13 +7,13 @@ describe "templates/guests/arch/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", ip: "1.1.1.1", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") Connection=ethernet Description='A basic static ethernet connection' - Interface=en0 + Interface=eth1 IP=static Address=('1.1.1.1/24') EOH @@ -21,14 +21,14 @@ describe "templates/guests/arch/network_static" do it "includes the gateway" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", ip: "1.1.1.1", gateway: "1.2.3.4", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") Connection=ethernet Description='A basic static ethernet connection' - Interface=en0 + Interface=eth1 IP=static Address=('1.1.1.1/24') Gateway='1.2.3.4' diff --git a/test/unit/templates/guests/debian/network_dhcp_test.rb b/test/unit/templates/guests/debian/network_dhcp_test.rb index a5a0eba8d..e239d2eea 100644 --- a/test/unit/templates/guests/debian/network_dhcp_test.rb +++ b/test/unit/templates/guests/debian/network_dhcp_test.rb @@ -7,13 +7,13 @@ describe "templates/guests/debian/network_dhcp" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet dhcp + auto eth1 + iface eth1 inet dhcp post-up route del default dev $IFACE || true #VAGRANT-END EOH @@ -22,14 +22,14 @@ describe "templates/guests/debian/network_dhcp" do context "when use_dhcp_assigned_default_route is set" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", use_dhcp_assigned_default_route: true, }) expect(result).to eq <<-EOH.gsub(/^ {8}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet dhcp + auto eth1 + iface eth1 inet dhcp # We need to disable eth0, see GH-2648 post-up route del default dev eth0 || true post-up dhclient $IFACE diff --git a/test/unit/templates/guests/debian/network_static_test.rb b/test/unit/templates/guests/debian/network_static_test.rb index 936a6a806..08f9a67af 100644 --- a/test/unit/templates/guests/debian/network_static_test.rb +++ b/test/unit/templates/guests/debian/network_static_test.rb @@ -7,15 +7,15 @@ describe "templates/guests/debian/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", ip: "1.1.1.1", netmask: "255.255.0.0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet static + auto eth1 + iface eth1 inet static address 1.1.1.1 netmask 255.255.0.0 #VAGRANT-END @@ -24,7 +24,7 @@ describe "templates/guests/debian/network_static" do it "includes the gateway" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + device: "eth1", ip: "1.1.1.1", netmask: "255.255.0.0", gateway: "1.2.3.4", @@ -32,8 +32,8 @@ describe "templates/guests/debian/network_static" do expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet static + auto eth1 + iface eth1 inet static address 1.1.1.1 netmask 255.255.0.0 gateway 1.2.3.4 diff --git a/test/unit/templates/guests/freebsd/network_dhcp_test.rb b/test/unit/templates/guests/freebsd/network_dhcp_test.rb index 322de3746..a3ad5b61c 100644 --- a/test/unit/templates/guests/freebsd/network_dhcp_test.rb +++ b/test/unit/templates/guests/freebsd/network_dhcp_test.rb @@ -6,10 +6,13 @@ describe "templates/guests/freebsd/network_dhcp" do let(:template) { "guests/freebsd/network_dhcp" } it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, ifname: "vtneten0") + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN - ifconfig_vtneten0="DHCP" + ifconfig_eth1="DHCP" + synchronous_dhclient="YES" #VAGRANT-END EOH end diff --git a/test/unit/templates/guests/freebsd/network_static_test.rb b/test/unit/templates/guests/freebsd/network_static_test.rb index ca9eb0153..d398a8647 100644 --- a/test/unit/templates/guests/freebsd/network_static_test.rb +++ b/test/unit/templates/guests/freebsd/network_static_test.rb @@ -6,32 +6,28 @@ describe "templates/guests/freebsd/network_static" do let(:template) { "guests/freebsd/network_static" } it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, - ifname: "vtneten0", - options: { - ip: "1.1.1.1", - netmask: "255.255.0.0", - }, - ) + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + ip: "1.1.1.1", + netmask: "255.255.0.0", + }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN - ifconfig_vtneten0="inet 1.1.1.1 netmask 255.255.0.0" + ifconfig_eth1="inet 1.1.1.1 netmask 255.255.0.0" #VAGRANT-END EOH end it "includes the gateway" do - result = Vagrant::Util::TemplateRenderer.render(template, - ifname: "vtneten0", - options: { - ip: "1.1.1.1", - netmask: "255.255.0.0", - gateway: "1.2.3.4", - }, - ) + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + ip: "1.1.1.1", + netmask: "255.255.0.0", + gateway: "1.2.3.4", + }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN - ifconfig_vtneten0="inet 1.1.1.1 netmask 255.255.0.0" + ifconfig_eth1="inet 1.1.1.1 netmask 255.255.0.0" default_router="1.2.3.4" #VAGRANT-END EOH diff --git a/test/unit/templates/guests/suse/network_dhcp_test.rb b/test/unit/templates/guests/suse/network_dhcp_test.rb index 1ecb4dc1f..bef88433b 100644 --- a/test/unit/templates/guests/suse/network_dhcp_test.rb +++ b/test/unit/templates/guests/suse/network_dhcp_test.rb @@ -7,14 +7,14 @@ describe "templates/guests/suse/network_dhcp" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", + device: "eth0" }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. BOOTPROTO='dhcp' STARTMODE='auto' - DEVICE='ethen0' + DEVICE='eth0' #VAGRANT-END EOH end diff --git a/test/unit/templates/guests/suse/network_static_test.rb b/test/unit/templates/guests/suse/network_static_test.rb index 09117268d..b336e6097 100644 --- a/test/unit/templates/guests/suse/network_static_test.rb +++ b/test/unit/templates/guests/suse/network_static_test.rb @@ -7,7 +7,7 @@ describe "templates/guests/suse/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", + device: "eth1", ip: "1.1.1.1", netmask: "255.255.0.0", }) @@ -17,7 +17,7 @@ describe "templates/guests/suse/network_static" do BOOTPROTO='static' IPADDR='1.1.1.1' NETMASK='255.255.0.0' - DEVICE='ethen0' + DEVICE='eth1' PEERDNS='no' STARTMODE='auto' USERCONTROL='no' @@ -27,7 +27,7 @@ describe "templates/guests/suse/network_static" do it "includes the gateway" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", + device: "eth1", ip: "1.1.1.1", gateway: "1.2.3.4", netmask: "255.255.0.0", @@ -38,7 +38,7 @@ describe "templates/guests/suse/network_static" do BOOTPROTO='static' IPADDR='1.1.1.1' NETMASK='255.255.0.0' - DEVICE='ethen0' + DEVICE='eth1' GATEWAY='1.2.3.4' PEERDNS='no' STARTMODE='auto'