From 784a5b2e32e24fd84cf94a1fc7d5bdf3f14926bd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 15 Feb 2014 17:06:26 -0800 Subject: [PATCH] providers/hyperv: all sorts of tests to verify Windows/admins --- lib/vagrant/util/platform.rb | 21 +++++++++++++++++++ plugins/providers/hyperv/errors.rb | 8 +++++++ plugins/providers/hyperv/provider.rb | 9 ++++++++ templates/locales/providers_hyperv.yml | 9 ++++++++ .../plugins/providers/hyperv/provider_test.rb | 18 ++++++++++++++++ 5 files changed, 65 insertions(+) diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index bb9181648..bdee0f7f4 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -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. # diff --git a/plugins/providers/hyperv/errors.rb b/plugins/providers/hyperv/errors.rb index d820198ab..fa48b67d6 100644 --- a/plugins/providers/hyperv/errors.rb +++ b/plugins/providers/hyperv/errors.rb @@ -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 diff --git a/plugins/providers/hyperv/provider.rb b/plugins/providers/hyperv/provider.rb index 882b002a4..d7d985f80 100644 --- a/plugins/providers/hyperv/provider.rb +++ b/plugins/providers/hyperv/provider.rb @@ -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 diff --git a/templates/locales/providers_hyperv.yml b/templates/locales/providers_hyperv.yml index c3a47969c..26322a4c9 100644 --- a/templates/locales/providers_hyperv.yml +++ b/templates/locales/providers_hyperv.yml @@ -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. diff --git a/test/unit/plugins/providers/hyperv/provider_test.rb b/test/unit/plugins/providers/hyperv/provider_test.rb index 258087028..c69301a22 100644 --- a/test/unit/plugins/providers/hyperv/provider_test.rb +++ b/test/unit/plugins/providers/hyperv/provider_test.rb @@ -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)