Merge pull request #7484 from mitchellh/sethvargo/bsd_halt

guests/bsd: Centralize logic for halting guests
This commit is contained in:
Seth Vargo 2016-06-18 15:12:09 -04:00 committed by GitHub
commit 2cbc819298
10 changed files with 57 additions and 60 deletions

View File

@ -1,10 +1,10 @@
module VagrantPlugins
module GuestNetBSD
module GuestBSD
module Cap
class Halt
def self.halt(machine)
begin
machine.communicate.sudo("/sbin/shutdown -p -h now")
machine.communicate.sudo("/sbin/shutdown -p -h now", shell: "sh")
rescue IOError
# Do nothing, because it probably means the machine shut down
# and SSH connection was lost.

View File

@ -11,6 +11,11 @@ module VagrantPlugins
Guest
end
guest_capability(:bsd, :halt) do
require_relative "cap/halt"
Cap::Halt
end
guest_capability(:bsd, :insert_public_key) do
require_relative "cap/public_key"
Cap::PublicKey

View File

@ -4,7 +4,9 @@ module VagrantPlugins
class Halt
def self.halt(machine)
begin
machine.communicate.sudo("shutdown -h now")
# Darwin does not support the `-p` option like the rest of the
# BSD-based guests, so it needs its own cap.
machine.communicate.sudo("/sbin/shutdown -h now")
rescue IOError
# Do nothing because SSH connection closed and it probably
# means the VM just shut down really fast.

View File

@ -1,16 +0,0 @@
module VagrantPlugins
module GuestFreeBSD
module Cap
class Halt
def self.halt(machine)
begin
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.
end
end
end
end
end
end

View File

@ -21,11 +21,6 @@ module VagrantPlugins
Cap::ConfigureNetworks
end
guest_capability(:freebsd, :halt) do
require_relative "cap/halt"
Cap::Halt
end
guest_capability(:freebsd, :rsync_install) do
require_relative "cap/rsync"
Cap::RSync

View File

@ -21,11 +21,6 @@ module VagrantPlugins
Cap::ConfigureNetworks
end
guest_capability(:netbsd, :halt) do
require_relative "cap/halt"
Cap::Halt
end
guest_capability(:netbsd, :rsync_install) do
require_relative "cap/rsync"
Cap::RSync

View File

@ -1,16 +0,0 @@
module VagrantPlugins
module GuestOpenBSD
module Cap
class Halt
def self.halt(machine)
begin
machine.communicate.sudo("shutdown -p -h now")
rescue IOError
# Do nothing, because it probably means the machine shut down
# and SSH connection was lost.
end
end
end
end
end
end

View File

@ -21,11 +21,6 @@ module VagrantPlugins
Cap::ConfigureNetworks
end
guest_capability(:openbsd, :halt) do
require_relative "cap/halt"
Cap::Halt
end
guest_capability(:openbsd, :rsync_install) do
require_relative "cap/rsync"
Cap::RSync

View File

@ -1,11 +1,10 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestFreeBSD::Cap::Halt" do
let(:described_class) do
VagrantPlugins::GuestFreeBSD::Plugin
describe "VagrantPlugins::GuestBSD::Cap::Halt" do
let(:caps) do
VagrantPlugins::GuestBSD::Plugin
.components
.guest_capabilities[:freebsd]
.get(:halt)
.guest_capabilities[:bsd]
end
let(:machine) { double("machine") }
@ -20,15 +19,17 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::Halt" do
end
describe ".halt" do
let(:cap) { caps.get(:halt) }
it "runs the shutdown command" do
comm.expect_command("shutdown -p now")
described_class.halt(machine)
comm.expect_command("/sbin/shutdown -p -h now")
cap.halt(machine)
end
it "does not raise an IOError" do
comm.stub_command("shutdown -p now", raise: IOError)
it "ignores an IOError" do
comm.stub_command("/sbin/shutdown -p -h now", raise: IOError)
expect {
described_class.halt(machine)
cap.halt(machine)
}.to_not raise_error
end
end

View File

@ -0,0 +1,36 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestDarwin::Cap::Halt" do
let(:caps) do
VagrantPlugins::GuestDarwin::Plugin
.components
.guest_capabilities[:darwin]
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 "ignores an IOError" do
comm.stub_command("/sbin/shutdown -h now", raise: IOError)
expect {
cap.halt(machine)
}.to_not raise_error
end
end
end