Merge pull request #11482 from soapy1/systemd-cap-halt
Use systemd to halt system if available
This commit is contained in:
commit
cf9a690bc9
@ -1,10 +1,18 @@
|
||||
require 'vagrant/util/guest_inspection'
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestLinux
|
||||
module Cap
|
||||
class Halt
|
||||
extend Vagrant::Util::GuestInspection::Linux
|
||||
|
||||
def self.halt(machine)
|
||||
begin
|
||||
machine.communicate.sudo("shutdown -h now")
|
||||
if systemd?(machine.communicate)
|
||||
machine.communicate.sudo("systemctl poweroff")
|
||||
else
|
||||
machine.communicate.sudo("shutdown -h now")
|
||||
end
|
||||
rescue IOError, Vagrant::Errors::SSHDisconnected
|
||||
# Do nothing, because it probably means the machine shut down
|
||||
# and SSH connection was lost.
|
||||
|
||||
@ -10,34 +10,57 @@ describe "VagrantPlugins::GuestLinux::Cap::Halt" do
|
||||
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)
|
||||
context "systemd not enabled" do
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
allow(comm).to receive(:test).and_return(false)
|
||||
end
|
||||
|
||||
it "does not raise an IOError" do
|
||||
comm.stub_command("shutdown -h now", raise: IOError)
|
||||
expect {
|
||||
cap.halt(machine)
|
||||
}.to_not raise_error
|
||||
after do
|
||||
comm.verify_expectations!
|
||||
end
|
||||
|
||||
it "does not raise a SSHDisconnected" do
|
||||
comm.stub_command("shutdown -h now", raise: Vagrant::Errors::SSHDisconnected)
|
||||
expect {
|
||||
describe ".halt" do
|
||||
let(:cap) { caps.get(:halt) }
|
||||
|
||||
it "runs the shutdown command" do
|
||||
comm.expect_command("shutdown -h now")
|
||||
cap.halt(machine)
|
||||
}.to_not raise_error
|
||||
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
|
||||
|
||||
it "does not raise a SSHDisconnected" do
|
||||
comm.stub_command("shutdown -h now", raise: Vagrant::Errors::SSHDisconnected)
|
||||
expect {
|
||||
cap.halt(machine)
|
||||
}.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context "systemd enabled" do
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
allow(comm).to receive(:test).and_return(true)
|
||||
end
|
||||
|
||||
after do
|
||||
comm.verify_expectations!
|
||||
end
|
||||
|
||||
describe ".halt" do
|
||||
let(:cap) { caps.get(:halt) }
|
||||
|
||||
it "runs the shutdown command" do
|
||||
comm.expect_command("systemctl poweroff")
|
||||
cap.halt(machine)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user