Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make powershell version detection timeout configurable #9653

Merged
merged 2 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/vagrant/util/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Util
class PowerShell
# NOTE: Version checks are only on Major
MINIMUM_REQUIRED_VERSION = 3
# Number of seconds to wait while attempting to get powershell version
DEFAULT_VERSION_DETECTION_TIMEOUT = 30
LOGGER = Log4r::Logger.new("vagrant::util::powershell")

# @return [String|nil] a powershell executable, depending on environment
Expand Down Expand Up @@ -101,8 +103,15 @@ def self.version
].flatten

version = nil
timeout = ENV["VAGRANT_POWERSHELL_VERSION_DETECTION_TIMEOUT"].to_i
if timeout < 1
timeout = DEFAULT_VERSION_DETECTION_TIMEOUT
end
begin
r = Subprocess.execute(*command, notify: [:stdout, :stderr], timeout: 10) {|io_name,data| version = data}
r = Subprocess.execute(*command,
notify: [:stdout, :stderr],
timeout: timeout,
) {|io_name,data| version = data}
rescue Vagrant::Util::Subprocess::TimeoutExceeded
LOGGER.debug("Timeout exceeded while attempting to determine version of Powershell.")
end
Expand Down Expand Up @@ -183,6 +192,13 @@ def self.powerup_command(path, args, opts)
Subprocess::Result.new(code, r_stdout, r_stderr)
end
end

# @private
# Reset the cached values for platform. This is not considered a public
# API and should only be used for testing.
def self.reset!
instance_variables.each(&method(:remove_instance_variable))
end
end
end
end
44 changes: 44 additions & 0 deletions test/unit/vagrant/util/powershell_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require File.expand_path("../../../base", __FILE__)

require 'vagrant/util/powershell'

describe Vagrant::Util::PowerShell do
include_context "unit"
describe ".version" do
before do
allow(described_class).to receive(:executable)
.and_return("powershell")
allow(Vagrant::Util::Subprocess).to receive(:execute)
end

after do
described_class.version
described_class.reset!
end

it "should execute powershell command" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with("powershell", any_args)
end

it "should use the default timeout" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(any_args, hash_including(
timeout: Vagrant::Util::PowerShell::DEFAULT_VERSION_DETECTION_TIMEOUT))
end

it "should use environment variable provided timeout" do
with_temp_env("VAGRANT_POWERSHELL_VERSION_DETECTION_TIMEOUT" => "1") do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(any_args, hash_including(
timeout: 1))
described_class.version
end
end

it "should use default timeout when environment variable value is invalid" do
with_temp_env("VAGRANT_POWERSHELL_VERSION_DETECTION_TIMEOUT" => "invalid value") do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(any_args, hash_including(
timeout: Vagrant::Util::PowerShell::DEFAULT_VERSION_DETECTION_TIMEOUT))
described_class.version
end
end
end
end
8 changes: 8 additions & 0 deletions website/source/docs/other/environmental-variables.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,11 @@ Use the Ruby Resolv library in place of the libc resolver.

Vagrant can optionally use the Ruby Resolv library in place of the libc resolver.
This can be disabled setting this environment variable.

## `VAGRANT_POWERSHELL_VERSION_DETECTION_TIMEOUT`

Vagrant will use a default timeout when checking for the installed version
of PowerShell. Occasionally the default can be too low and Vagrant will report
being unable to detect the installed version of PowerShell. This environment
variable can be used to extend the timeout used during PowerShell version
detection.