providers/hyperv: all sorts of tests to verify Windows/admins

This commit is contained in:
Mitchell Hashimoto 2014-02-15 17:06:26 -08:00
parent ab4390eb67
commit 784a5b2e32
5 changed files with 65 additions and 0 deletions

View File

@ -29,6 +29,27 @@ module Vagrant
false
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
# path on Windows machines in Cygwin.
#

View File

@ -6,6 +6,10 @@ module VagrantPlugins
error_namespace("vagrant_hyperv.errors")
end
class AdminRequired < HyperVError
error_key(:admin_required)
end
class PowerShellError < HyperVError
error_key(:powershell_error)
end
@ -13,6 +17,10 @@ module VagrantPlugins
class PowerShellRequired < HyperVError
error_key(:powershell_required)
end
class WindowsRequired < HyperVError
error_key(:windows_required)
end
end
end
end

View File

@ -3,6 +3,7 @@ require "log4r"
require_relative "driver"
require_relative "plugin"
require "vagrant/util/platform"
require "vagrant/util/powershell"
module VagrantPlugins
@ -14,6 +15,14 @@ module VagrantPlugins
@driver = Driver.new
@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?
raise Errors::PowerShellRequired
end

View File

@ -1,6 +1,12 @@
en:
vagrant_hyperv:
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: |-
An error occurred while executing a PowerShell script. This error
is shown below. Please read the error message and see if this is
@ -14,3 +20,6 @@ en:
powershell_required: |-
The Vagrant Hyper-V provider requires PowerShell to be available.
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.

View File

@ -4,16 +4,34 @@ require Vagrant.source_root.join("plugins/providers/hyperv/provider")
describe VagrantPlugins::HyperV::Provider do
let(:machine) { double("machine") }
let(:platform) { double("platform") }
let(:powershell) { double("powershell") }
subject { described_class.new(machine) }
before do
stub_const("Vagrant::Util::Platform", platform)
stub_const("Vagrant::Util::PowerShell", powershell)
platform.stub(windows?: true)
platform.stub(windows_admin?: true)
powershell.stub(available?: true)
end
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
powershell.stub(available?: false)