diff --git a/plugins/guests/linux/cap/halt.rb b/plugins/guests/linux/cap/halt.rb index 60dc5dde4..81be3980b 100644 --- a/plugins/guests/linux/cap/halt.rb +++ b/plugins/guests/linux/cap/halt.rb @@ -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. diff --git a/test/unit/plugins/guests/linux/cap/halt_test.rb b/test/unit/plugins/guests/linux/cap/halt_test.rb index 81f682aa1..caa27251f 100644 --- a/test/unit/plugins/guests/linux/cap/halt_test.rb +++ b/test/unit/plugins/guests/linux/cap/halt_test.rb @@ -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