From a2f5d615a057b3c82a4417d19cf9d8801f933166 Mon Sep 17 00:00:00 2001 From: Arthur Maltson Date: Sat, 31 Oct 2020 11:29:12 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Make=20max=20reboot=20retry=20durat?= =?UTF-8?q?ion=20configurable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the maximum amount of time Vagrant would poll for whether a machine has successfully reboot was hard coded to 120 seconds. This change introduces the VAGRANT_MAX_REBOOT_RETRY_TIMEOUT environment variable to allow this attribute to be configurable. Add RSpec tests of the maximum retry logic. Since the maximum retries are configured as a constant, we'd need to reload the class and that's fairly ugly to do in RSpec. Fixes #11695 --- plugins/guests/linux/cap/reboot.rb | 2 +- plugins/guests/windows/cap/reboot.rb | 2 +- .../plugins/guests/linux/cap/reboot_test.rb | 17 +++++++++++++++++ .../plugins/guests/windows/cap/reboot_test.rb | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/plugins/guests/linux/cap/reboot.rb b/plugins/guests/linux/cap/reboot.rb index 0ac7d302d..9c1cb3132 100644 --- a/plugins/guests/linux/cap/reboot.rb +++ b/plugins/guests/linux/cap/reboot.rb @@ -6,7 +6,7 @@ module VagrantPlugins module Cap class Reboot extend Vagrant::Util::GuestInspection::Linux - MAX_REBOOT_RETRY_DURATION = 120 + MAX_REBOOT_RETRY_DURATION = ENV.fetch('VAGRANT_MAX_REBOOT_RETRY_TIMEOUT', 120).to_i def self.reboot(machine) @logger = Log4r::Logger.new("vagrant::linux::reboot") diff --git a/plugins/guests/windows/cap/reboot.rb b/plugins/guests/windows/cap/reboot.rb index 70f10a41a..b0e92c02d 100644 --- a/plugins/guests/windows/cap/reboot.rb +++ b/plugins/guests/windows/cap/reboot.rb @@ -4,7 +4,7 @@ module VagrantPlugins module GuestWindows module Cap class Reboot - MAX_REBOOT_RETRY_DURATION = 120 + MAX_REBOOT_RETRY_DURATION = ENV.fetch('VAGRANT_MAX_REBOOT_RETRY_TIMEOUT', 120).to_i def self.reboot(machine) @logger = Log4r::Logger.new("vagrant::windows::reboot") diff --git a/test/unit/plugins/guests/linux/cap/reboot_test.rb b/test/unit/plugins/guests/linux/cap/reboot_test.rb index bffc8daf1..d5ae18388 100644 --- a/test/unit/plugins/guests/linux/cap/reboot_test.rb +++ b/test/unit/plugins/guests/linux/cap/reboot_test.rb @@ -75,5 +75,22 @@ describe "VagrantPlugins::GuestLinux::Cap::Reboot" do end end end + + context "reboot configuration" do + before do + allow(communicator).to receive(:execute) + expect(communicator).to receive(:execute).with(/reboot/, nil).and_return(0) + allow(described_class).to receive(:sleep) + allow(described_class).to receive(:wait_for_reboot).and_raise(Vagrant::Errors::MachineGuestNotReady) + end + + describe ".reboot default" do + it "allows setting a custom max reboot retry duration" do + max_retries = 26 # initial call + 25 retries since multiple of 5 + expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times + expect { described_class.reboot(machine) }.to raise_error(Vagrant::Errors::MachineGuestNotReady) + end + end + end end end diff --git a/test/unit/plugins/guests/windows/cap/reboot_test.rb b/test/unit/plugins/guests/windows/cap/reboot_test.rb index b7562cce2..114098758 100644 --- a/test/unit/plugins/guests/windows/cap/reboot_test.rb +++ b/test/unit/plugins/guests/windows/cap/reboot_test.rb @@ -112,4 +112,22 @@ describe "VagrantPlugins::GuestWindows::Cap::Reboot" do end end end + + context "reboot configuration" do + before do + allow(communicator).to receive(:execute) + expect(communicator).to receive(:test).with(/# Function/, { error_check: false, shell: :powershell }).and_return(0) + expect(communicator).to receive(:execute).with(/shutdown/, { shell: :powershell }).and_return(0) + allow(described_class).to receive(:sleep) + allow(described_class).to receive(:wait_for_reboot).and_raise(StandardError) + end + + describe ".reboot default" do + it "allows setting a custom max reboot retry duration" do + max_retries = 26 # initial call + 25 retries since multiple of 5 + expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times + expect { described_class.reboot(machine) }.to raise_error(Exception) + end + end + end end