providers/hyperv: all sorts of tests to verify Windows/admins
This commit is contained in:
parent
ab4390eb67
commit
784a5b2e32
|
@ -29,6 +29,27 @@ module Vagrant
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Checks if the user running Vagrant on Windows has administrative
|
||||||
|
# privileges.
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
def windows_admin?
|
||||||
|
# We lazily-load this because it is only available on Windows
|
||||||
|
require 'win32/registry'
|
||||||
|
|
||||||
|
# Verify that we have administrative privileges. The odd method of
|
||||||
|
# detecting this is based on this StackOverflow question:
|
||||||
|
#
|
||||||
|
# http://stackoverflow.com/questions/560366/
|
||||||
|
# detect-if-running-with-administrator-privileges-under-windows-xp
|
||||||
|
begin
|
||||||
|
Win32::Registry::HKEY_USERS.open("S-1-5-19") {}
|
||||||
|
return true
|
||||||
|
rescue Win32::Registry::Error
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# This takes any path and converts it to a full-length Windows
|
# This takes any path and converts it to a full-length Windows
|
||||||
# path on Windows machines in Cygwin.
|
# path on Windows machines in Cygwin.
|
||||||
#
|
#
|
||||||
|
|
|
@ -6,6 +6,10 @@ module VagrantPlugins
|
||||||
error_namespace("vagrant_hyperv.errors")
|
error_namespace("vagrant_hyperv.errors")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class AdminRequired < HyperVError
|
||||||
|
error_key(:admin_required)
|
||||||
|
end
|
||||||
|
|
||||||
class PowerShellError < HyperVError
|
class PowerShellError < HyperVError
|
||||||
error_key(:powershell_error)
|
error_key(:powershell_error)
|
||||||
end
|
end
|
||||||
|
@ -13,6 +17,10 @@ module VagrantPlugins
|
||||||
class PowerShellRequired < HyperVError
|
class PowerShellRequired < HyperVError
|
||||||
error_key(:powershell_required)
|
error_key(:powershell_required)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class WindowsRequired < HyperVError
|
||||||
|
error_key(:windows_required)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,7 @@ require "log4r"
|
||||||
require_relative "driver"
|
require_relative "driver"
|
||||||
require_relative "plugin"
|
require_relative "plugin"
|
||||||
|
|
||||||
|
require "vagrant/util/platform"
|
||||||
require "vagrant/util/powershell"
|
require "vagrant/util/powershell"
|
||||||
|
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
|
@ -14,6 +15,14 @@ module VagrantPlugins
|
||||||
@driver = Driver.new
|
@driver = Driver.new
|
||||||
@machine = machine
|
@machine = machine
|
||||||
|
|
||||||
|
if !Vagrant::Util::Platform.windows?
|
||||||
|
raise Errors::WindowsRequired
|
||||||
|
end
|
||||||
|
|
||||||
|
if !Vagrant::Util::Platform.windows_admin?
|
||||||
|
raise Errors::AdminRequired
|
||||||
|
end
|
||||||
|
|
||||||
if !Vagrant::Util::PowerShell.available?
|
if !Vagrant::Util::PowerShell.available?
|
||||||
raise Errors::PowerShellRequired
|
raise Errors::PowerShellRequired
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
en:
|
en:
|
||||||
vagrant_hyperv:
|
vagrant_hyperv:
|
||||||
errors:
|
errors:
|
||||||
|
admin_required: |-
|
||||||
|
The Hyper-V provider requires that Vagrant be run with
|
||||||
|
administrative privileges. This is a limitation of Hyper-V itself.
|
||||||
|
Hyper-V requires administrative privileges for management
|
||||||
|
commands. Please restart your console with administrative
|
||||||
|
privileges and try again.
|
||||||
powershell_error: |-
|
powershell_error: |-
|
||||||
An error occurred while executing a PowerShell script. This error
|
An error occurred while executing a PowerShell script. This error
|
||||||
is shown below. Please read the error message and see if this is
|
is shown below. Please read the error message and see if this is
|
||||||
|
@ -14,3 +20,6 @@ en:
|
||||||
powershell_required: |-
|
powershell_required: |-
|
||||||
The Vagrant Hyper-V provider requires PowerShell to be available.
|
The Vagrant Hyper-V provider requires PowerShell to be available.
|
||||||
Please make sure "powershell.exe" is available on your PATH.
|
Please make sure "powershell.exe" is available on your PATH.
|
||||||
|
windows_required: |-
|
||||||
|
The Hyper-V provider only works on Windows. Please try to
|
||||||
|
use another provider.
|
||||||
|
|
|
@ -4,16 +4,34 @@ require Vagrant.source_root.join("plugins/providers/hyperv/provider")
|
||||||
|
|
||||||
describe VagrantPlugins::HyperV::Provider do
|
describe VagrantPlugins::HyperV::Provider do
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
|
let(:platform) { double("platform") }
|
||||||
let(:powershell) { double("powershell") }
|
let(:powershell) { double("powershell") }
|
||||||
|
|
||||||
subject { described_class.new(machine) }
|
subject { described_class.new(machine) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
stub_const("Vagrant::Util::Platform", platform)
|
||||||
stub_const("Vagrant::Util::PowerShell", powershell)
|
stub_const("Vagrant::Util::PowerShell", powershell)
|
||||||
|
platform.stub(windows?: true)
|
||||||
|
platform.stub(windows_admin?: true)
|
||||||
powershell.stub(available?: true)
|
powershell.stub(available?: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#initialize" do
|
describe "#initialize" do
|
||||||
|
it "raises an exception if not windows" do
|
||||||
|
platform.stub(windows?: false)
|
||||||
|
|
||||||
|
expect { subject }.
|
||||||
|
to raise_error(VagrantPlugins::HyperV::Errors::WindowsRequired)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an exception if not an admin" do
|
||||||
|
platform.stub(windows_admin?: false)
|
||||||
|
|
||||||
|
expect { subject }.
|
||||||
|
to raise_error(VagrantPlugins::HyperV::Errors::AdminRequired)
|
||||||
|
end
|
||||||
|
|
||||||
it "raises an exception if powershell is not available" do
|
it "raises an exception if powershell is not available" do
|
||||||
powershell.stub(available?: false)
|
powershell.stub(available?: false)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue