From 06947002f7444effb5b6fe38af6bd9a2b6100fba Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 18 Mar 2010 17:41:51 -0700 Subject: [PATCH] The require_* methods and check_virtualbox! integrated into new Environment --- lib/vagrant/environment.rb | 43 +++++++++++++++ test/vagrant/environment_test.rb | 94 ++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 5729ef727..fe71e1ba5 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -24,6 +24,22 @@ module Vagrant def load!(cwd=nil) Environment.new(cwd).load! end + + # Verifies that VirtualBox is installed and that the version of + # VirtualBox installed is high enough. Also verifies that the + # configuration path is properly set. + def check_virtualbox! + version = VirtualBox::Command.version + if version.nil? + error_and_exit(:virtualbox_not_detected) + elsif version.to_f < 3.1 + error_and_exit(:virtualbox_invalid_version, :version => version.to_s) + end + + if !VirtualBox::Global.vboxconfig? + error_and_exit(:virtualbox_xml_not_detected) + end + end end def initialize(cwd=nil) @@ -66,6 +82,7 @@ module Vagrant load_home_directory! load_box! load_config! + self.class.check_virtualbox! load_vm! self end @@ -148,5 +165,31 @@ module Vagrant rescue Errno::ENOENT @vm = nil end + + #--------------------------------------------------------------- + # Methods to check for properties and error + #--------------------------------------------------------------- + + def require_root_path + error_and_exit(:rootfile_not_found) if !root_path + end + + def require_box + require_root_path + + if !box + if !Vagrant.config.vm.box + error_and_exit(:box_not_specified) + else + error_and_exit(:box_specified_doesnt_exist, :box_name => Vagrant.config.vm.box) + end + end + end + + def require_persisted_vm + require_root_path + + error_and_exit(:environment_not_created) if !persisted_vm + end end end \ No newline at end of file diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index f25c7395b..023dc3350 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -5,6 +5,39 @@ class EnvironmentTest < Test::Unit::TestCase mock_config end + context "class method check virtualbox version" do + setup do + VirtualBox::Command.stubs(:version).returns("3.1.4") + VirtualBox::Global.stubs(:vboxconfig?).returns(true) + end + + should "not error and exit if everything is good" do + VirtualBox::Command.expects(:version).returns("3.1.4") + VirtualBox::Global.expects(:vboxconfig?).returns(true) + Vagrant::Env.expects(:error_and_exit).never + Vagrant::Environment.check_virtualbox! + end + + should "error and exit if VirtualBox is not installed or detected" do + Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_not_detected).once + VirtualBox::Command.expects(:version).returns(nil) + Vagrant::Environment.check_virtualbox! + end + + should "error and exit if VirtualBox is lower than version 3.1" do + version = "3.0.12r1041" + Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once + VirtualBox::Command.expects(:version).returns(version) + Vagrant::Environment.check_virtualbox! + end + + should "error and exit if the the vboxconfig is not set" do + VirtualBox::Global.expects(:vboxconfig?).returns(false) + Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_xml_not_detected).once + Vagrant::Environment.check_virtualbox! + end + end + context "class method load!" do setup do @cwd = mock('cwd') @@ -93,6 +126,7 @@ class EnvironmentTest < Test::Unit::TestCase @env.expects(:load_home_directory!).once.in_sequence(call_seq) @env.expects(:load_box!).once.in_sequence(call_seq) @env.expects(:load_config!).once.in_sequence(call_seq) + Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq) @env.expects(:load_vm!).once.in_sequence(call_seq) assert_equal @env, @env.load! end @@ -334,4 +368,64 @@ class EnvironmentTest < Test::Unit::TestCase end end end + + context "requiring properties" do + setup do + @env = mock_environment + end + + context "requiring boxes" do + setup do + reconfig_environment + end + + def reconfig_environment + @env = mock_environment do |config| + yield config if block_given? + end + + @env.stubs(:require_root_path) + @env.stubs(:error_and_exit) + end + + should "require root path" do + @env.expects(:require_root_path).once + @env.require_box + end + + should "error and exit if no box is specified" do + reconfig_environment do |config| + config.vm.box = nil + end + + @env.expects(:box).returns(nil) + @env.expects(:error_and_exit).once.with(:box_not_specified) + @env.require_box + end + + should "error and exit if box is specified but doesn't exist" do + reconfig_environment do |config| + config.vm.box = "foo" + end + + @env.expects(:box).returns(nil) + @env.expects(:error_and_exit).once.with(:box_specified_doesnt_exist, :box_name => "foo") + @env.require_box + end + end + + context "requiring root_path" do + should "error and exit if no root_path is set" do + @env.expects(:root_path).returns(nil) + @env.expects(:error_and_exit).with(:rootfile_not_found).once + @env.require_root_path + end + + should "not error and exit if root_path is set" do + @env.expects(:root_path).returns("foo") + @env.expects(:error_and_exit).never + @env.require_root_path + end + end + end end