test/vagrant/environment_test.rb in vagrantup-0.5.4 vs test/vagrant/environment_test.rb in vagrantup-0.6.0

- old
+ new

@@ -1,443 +1,433 @@ require "test_helper" +require "pathname" class EnvironmentTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Environment + end + context "class method check virtualbox version" do setup do VirtualBox.stubs(:version).returns("3.1.4") end should "not error and exit if everything is good" do VirtualBox.expects(:version).returns("3.2.4") - Vagrant::Environment.expects(:error_and_exit).never - Vagrant::Environment.check_virtualbox! + assert_nothing_raised { @klass.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.expects(:version).returns(nil) - Vagrant::Environment.check_virtualbox! + assert_raises(Vagrant::Errors::VirtualBoxNotDetected) { @klass.check_virtualbox! } end should "error and exit if VirtualBox is lower than version 3.2" do version = "3.1.12r1041" - Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once VirtualBox.expects(:version).returns(version) - Vagrant::Environment.check_virtualbox! + assert_raises(Vagrant::Errors::VirtualBoxInvalidVersion) { @klass.check_virtualbox! } end should "error and exit for OSE VirtualBox" do version = "3.2.6_OSE" - Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_ose, :version => version.to_s).once VirtualBox.expects(:version).returns(version) - Vagrant::Environment.check_virtualbox! + assert_raises(Vagrant::Errors::VirtualBoxInvalidOSE) { @klass.check_virtualbox! } end end - context "class method load!" do - setup do - @cwd = mock('cwd') - - @env = mock('env') - @env.stubs(:load!).returns(@env) - end - - should "create the environment with given cwd, load it, and return it" do - Vagrant::Environment.expects(:new).with(:cwd => @cwd).once.returns(@env) - @env.expects(:load!).returns(@env) - assert_equal @env, Vagrant::Environment.load!(@cwd) - end - - should "work without a given cwd" do - Vagrant::Environment.expects(:new).with(:cwd => nil).returns(@env) - - assert_nothing_raised { - env = Vagrant::Environment.load! - assert_equal env, @env - } - end - end - context "initialization" do should "set the cwd if given" do cwd = "foobarbaz" - env = Vagrant::Environment.new(:cwd => cwd) - assert_equal cwd, env.cwd + env = @klass.new(:cwd => cwd) + assert_equal Pathname.new(cwd), env.cwd end should "default to pwd if cwd is nil" do - env = Vagrant::Environment.new - assert_equal Dir.pwd, env.cwd + env = @klass.new + assert_equal Pathname.new(Dir.pwd), env.cwd end end context "paths" do setup do - @env = mock_environment + @env = vagrant_env end - context "cwd" do - should "default to Dir.pwd" do - assert_equal Dir.pwd, @env.cwd - end - - should "return cwd if set" do - @env.cwd = "foo" - assert_equal "foo", @env.cwd - end - end - context "dotfile path" do - setup do - @env.stubs(:root_path).returns("foo") - end - should "build up the dotfile out of the root path and the dotfile name" do - assert_equal File.join(@env.root_path, @env.config.vagrant.dotfile_name), @env.dotfile_path + assert_equal @env.root_path.join(@env.config.vagrant.dotfile_name), @env.dotfile_path end end context "home path" do - should "return nil if config is not yet loaded" do - @env.stubs(:config).returns(nil) - assert_nil @env.home_path - end - should "return the home path if it loaded" do - assert_equal @env.config.vagrant.home, @env.home_path + expected = Pathname.new(File.expand_path(@env.config.vagrant.home, @env.root_path)) + assert_equal expected, @env.home_path end end context "temp path" do should "return the home path joined with 'tmp'" do - home_path = "foo" - @env.stubs(:home_path).returns(home_path) - assert_equal File.join("foo", "tmp"), @env.tmp_path + assert_equal @env.home_path.join("tmp"), @env.tmp_path end end context "boxes path" do should "return the home path joined with 'tmp'" do - home_path = "foo" - @env.stubs(:home_path).returns(home_path) - assert_equal File.join("foo", "boxes"), @env.boxes_path + assert_equal @env.home_path.join("boxes"), @env.boxes_path end end end - context "primary VM helper" do + context "resource" do setup do - @env = mock_environment - @env.stubs(:multivm?).returns(true) + @env = vagrant_env end - should "return the first VM if not multivm" do - result = mock("result") + should "return 'vagrant' as a default" do + assert_equal 'vagrant', @env.resource + end - @env.stubs(:multivm?).returns(false) - @env.stubs(:vms).returns({:default => result}) + should "return the VM name if it is specified" do + @env.stubs(:vm).returns(mock("vm", :name => "foo")) + assert_equal "foo", @env.resource + end + end - assert_equal result, @env.primary_vm + context "primary VM helper" do + should "return the first VM if not multivm" do + env = vagrant_env + assert_equal env.vms[@klass::DEFAULT_VM], env.primary_vm end should "call and return the primary VM from the parent if has one" do - result = mock("result") - parent = mock("parent") - parent.expects(:primary_vm).returns(result) + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define(:web, :primary => true) do; end + config.vm.define :db do; end + vf - @env.stubs(:parent).returns(parent) - assert_equal result, @env.primary_vm + assert_equal :web, env.primary_vm.name end should "return nil if no VM is marked as primary" do - @env.config.vm.define(:foo) - @env.config.vm.define(:bar) - @env.config.vm.define(:baz) + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + config.vm.define :utility + vf - assert @env.primary_vm.nil? + assert env.primary_vm.nil? end + end - should "return the primary VM" do - @env.config.vm.define(:foo) - @env.config.vm.define(:bar, :primary => true) - @env.config.vm.define(:baz) + context "multivm? helper" do + should "return true if VM length greater than 1" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf - result = mock("result") - vms = { - :foo => :foo, - :bar => result, - :baz => :baz - } - @env.stubs(:vms).returns(vms) + assert env.multivm? + end - assert_equal result, @env.primary_vm + should "return false if VM length is 1" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + vf + + assert !env.multivm? end end - context "multivm? helper" do - setup do - @env = mock_environment + context "local data" do + should "lazy load the data store only once" do + result = { :foo => :bar } + Vagrant::DataStore.expects(:new).returns(result).once + env = vagrant_env + assert_equal result, env.local_data + assert_equal result, env.local_data + assert_equal result, env.local_data end - context "with a parent" do - setup do - @parent = mock('parent') - @env.stubs(:parent).returns(@parent) - end + should "return the parent's local data if a parent exists" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf - should "return the value of multivm? from the parent" do - result = mock("result") - @parent.stubs(:multivm?).returns(result) - assert_equal result, @env.multivm? - end + env.local_data[:foo] = :bar + + Vagrant::DataStore.expects(:new).never + assert_equal :bar, env.vms[:web].env.local_data[:foo] end + end - context "without a parent" do - setup do - @env.stubs(:parent).returns(nil) - end + context "accessing host" do + should "load the host once" do + env = @klass.new(:cwd => vagrantfile) + result = mock("result") + Vagrant::Hosts::Base.expects(:load).with(env, env.config.vagrant.host).once.returns(result) + assert_equal result, env.host + assert_equal result, env.host + assert_equal result, env.host + end + end - should "return true if VM length greater than 1" do - @env.stubs(:vms).returns([1,2,3]) - assert @env.multivm? - end + context "accessing actions" do + should "initialize the Action object with the given environment" do + env = @klass.new(:cwd => vagrant_app) + result = mock("result") + Vagrant::Action.expects(:new).with(env).returns(result).once + assert_equal result, env.actions + assert_equal result, env.actions + assert_equal result, env.actions + end + end - should "return false if VM length is 1" do - @env.stubs(:vms).returns([1]) - assert !@env.multivm? - end + context "global data" do + should "lazy load the data store only once" do + env = vagrant_env + store = env.global_data + + assert env.global_data.equal?(store) + assert env.global_data.equal?(store) + assert env.global_data.equal?(store) end + + should "return the parent's global data if a parent exists" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf + + result = env.global_data + assert env.vms[:web].env.global_data.equal?(result) + end end - context "loading" do - setup do - @env = mock_environment + context "loading logger" do + should "lazy load the logger only once" do + result = Vagrant::Util::ResourceLogger.new("vagrant", vagrant_env) + Vagrant::Util::ResourceLogger.expects(:new).returns(result).once + env = vagrant_env + assert_equal result, env.logger + assert_equal result, env.logger + assert_equal result, env.logger end - context "overall load method" do - should "load! should call proper sequence and return itself" do - call_seq = sequence("call_sequence") - @env.expects(:load_logger!).once.in_sequence(call_seq) - @env.expects(:load_root_path!).once.in_sequence(call_seq) - @env.expects(:load_config!).once.in_sequence(call_seq) - @env.expects(:load_home_directory!).once.in_sequence(call_seq) - @env.expects(:load_host!).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) - @env.expects(:load_active_list!).once.in_sequence(call_seq) - @env.expects(:load_commands!).once.in_sequence(call_seq) - @env.expects(:load_actions!).once.in_sequence(call_seq) - assert_equal @env, @env.load! - end + should "return the parent's logger if a parent exists" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf + + result = env.logger + + Vagrant::Util::ResourceLogger.expects(:new).never + assert env.vms[:web].env.logger.equal?(result) end + end - context "loading the root path" do - setup do - @env.cwd = "/foo" - end + context "loading the root path" do + should "should walk the parent directories looking for rootfile" do + paths = [Pathname.new("/foo/bar/baz"), + Pathname.new("/foo/bar"), + Pathname.new("/foo"), + Pathname.new("/") + ] - should "default the path to the cwd instance var if nil" do - @path = mock("path") - @path.stubs(:root?).returns(true) - File.expects(:expand_path).with(@env.cwd).returns(@env.cwd) - Pathname.expects(:new).with(@env.cwd).returns(@path) - @env.load_root_path!(nil) + search_seq = sequence("search_seq") + paths.each do |path| + File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(false).in_sequence(search_seq) end - should "not default the path to pwd if its not nil" do - @path = mock("path") - @path.stubs(:to_s).returns("/") - File.expects(:expand_path).with(@path).returns("/") - Pathname.expects(:new).with("/").returns(@path) - @path.stubs(:root?).returns(true) - @env.load_root_path!(@path) - end + assert !@klass.new(:cwd => paths.first).root_path + end - should "should walk the parent directories looking for rootfile" do - paths = [ - Pathname.new("/foo/bar/baz"), - Pathname.new("/foo/bar"), - Pathname.new("/foo") - ] + should "should set the path for the rootfile" do + path = Pathname.new(File.expand_path("/foo")) + File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(true) - search_seq = sequence("search_seq") - paths.each do |path| - # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution - File.expects(:exist?).with("#{File.expand_path(path)}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq) - end + assert_equal path, @klass.new(:cwd => path).root_path + end - assert !@env.load_root_path!(paths.first) - end + should "only load the root path once" do + env = @klass.new + File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once - should "return false if not found" do - path = Pathname.new("/") - assert !@env.load_root_path!(path) - end + assert_equal env.cwd, env.root_path + assert_equal env.cwd, env.root_path + assert_equal env.cwd, env.root_path + end - should "return false if not found on windows-style root" do - # TODO: Is there _any_ way to test this on unix machines? The - # expand path doesn't work [properly for the test] on unix machines. - if RUBY_PLATFORM.downcase.include?("mswin") - # Note the escaped back slash - path = Pathname.new("C:\\") - assert !@env.load_root_path!(path) - end - end + should "only load the root path once even if nil" do + File.stubs(:exist?).returns(false) - should "should set the path for the rootfile" do - # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution - path = File.expand_path("/foo") - File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true) + env = @klass.new + assert env.root_path.nil? + assert env.root_path.nil? + assert env.root_path.nil? + end + end - assert @env.load_root_path!(Pathname.new(path)) - assert_equal path, @env.root_path - end + context "accessing the configuration" do + should "load the environment if its not already loaded" do + env = @klass.new(:cwd => vagrantfile) + env.expects(:load!).once + env.config end - context "loading config" do - setup do - @root_path = "/foo" - @home_path = "/bar" - @env.stubs(:root_path).returns(@root_path) - @env.stubs(:home_path).returns(@home_path) - @env.stubs(:load_logger!) + should "not load the environment if its already loaded" do + env = vagrant_env + env.expects(:load!).never + env.config + end + end - @parent_env = mock_environment + context "accessing the box collection" do + should "create a box collection representing the environment" do + env = vagrant_env + assert env.boxes.is_a?(Vagrant::BoxCollection) + assert_equal env, env.boxes.env + end - File.stubs(:exist?).returns(false) - end + should "not load the environment if its already loaded" do + env = vagrant_env + env.expects(:load!).never + env.boxes + end - should "reset the configuration object" do - Vagrant::Config.expects(:reset!).with(@env).once - @env.load_config! - end + should "return the parent's box collection if it has one" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf - should "load from the project root" do - File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once - @env.load_config! - end + assert env.vms[:web].env.boxes.equal?(env.boxes) + end + end - should "load from the root path" do - File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).once - @env.load_config! + context "accessing the current box" do + should "return the box that is specified in the config" do + vagrant_box("foo") + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.box = "foo" + vf + + assert env.box + assert_equal "foo", env.box.name + end + end + + context "accessing the VMs hash" do + should "load the environment if its not already loaded" do + env = @klass.new(:cwd => vagrantfile) + assert !env.loaded? + env.vms + assert env.loaded? + end + + should "not load the environment if its already loaded" do + env = vagrant_env + env.expects(:load!).never + env.vms + end + + should "return the parent's VMs hash if it has one" do + env = vagrant_env(vagrantfile(<<-vf)) + config.vm.define :web + config.vm.define :db + vf + + assert env.vms[:web].env.vms.equal?(env.vms) + end + end + + context "loading" do + context "overall load method" do + should "load! should call proper sequence and return itself" do + env = @klass.new(:cwd => vagrantfile) + call_seq = sequence("call_sequence") + @klass.expects(:check_virtualbox!).once.in_sequence(call_seq) + env.expects(:load_config!).once.in_sequence(call_seq) + env.actions.expects(:run).with(:environment_load).once.in_sequence(call_seq) + assert_equal env, env.load! end + end - should "not load from the root path if nil" do - @env.stubs(:root_path).returns(nil) - File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).never - @env.load_config! + context "loading config" do + setup do + clean_paths + @env = @klass.new(:cwd => vagrantfile) end - should "load from the home directory" do - File.expects(:exist?).with(File.join(@env.home_path, Vagrant::Environment::ROOTFILE_NAME)).once - @env.load_config! + def create_box_vagrantfile + vagrantfile(vagrant_box("box"), 'config.package.name = "box.box"') end - should "not load from the home directory if the config is nil" do - @env.stubs(:home_path).returns(nil) - File.expects(:exist?).twice.returns(false) - @env.load_config! + def create_home_vagrantfile + vagrantfile(home_path, 'config.package.name = "home.box"') end - should "not load from the box directory if it is nil" do - @env.expects(:box).once.returns(nil) - File.expects(:exist?).twice.returns(false) - @env.load_config! + def create_root_vagrantfile + vagrantfile(@env.root_path, 'config.package.name = "root.box"') end - should "load from the box directory if it is not nil" do - dir = "foo" - box = mock("box") - box.stubs(:directory).returns(dir) - @env.expects(:box).twice.returns(box) - File.expects(:exist?).with(File.join(dir, Vagrant::Environment::ROOTFILE_NAME)).once - @env.load_config! + should "load from the project root" do + assert_equal "package.box", @env.config.package.name end - should "load a sub-VM configuration if specified" do - vm_name = :foo - sub_box = :YO - @parent_env.config.vm.box = :NO - @parent_env.config.vm.define(vm_name) do |config| - config.vm.box = sub_box - end + should "load from box if specified" do + create_box_vagrantfile + vagrantfile(@env.root_path, "config.vm.box = 'box'") - # Sanity - assert_equal :NO, @parent_env.config.vm.box - - @env.stubs(:vm_name).returns(vm_name) - @env.stubs(:parent).returns(@parent_env) - - @env.load_config! - - assert_equal sub_box, @env.config.vm.box + assert_equal "box.box", @env.config.package.name end - should "load the files only if exist? returns true" do - File.expects(:exist?).once.returns(true) - @env.expects(:load).once - @env.load_config! + should "load from home path if exists" do + create_home_vagrantfile + assert_equal "home.box", @env.config.package.name end - should "not load the files if exist? returns false" do - @env.expects(:load).never - @env.load_config! + should "load from root path" do + create_home_vagrantfile + create_root_vagrantfile + assert_equal "root.box", @env.config.package.name end - should "execute after loading and set result to environment config" do - result = mock("result") - File.expects(:exist?).once.returns(true) - @env.expects(:load).once - Vagrant::Config.expects(:execute!).once.returns(result) - @env.load_config! - assert_equal result, @env.config + should "load from a sub-vm configuration if environment represents a VM" do + create_home_vagrantfile + vagrantfile(@env.root_path, <<-vf) + config.package.name = "root.box" + config.vm.define :web do |web| + web.package.name = "web.box" + end + vf + + assert_equal "root.box", @env.config.package.name + assert_equal "web.box", @env.vms[:web].env.config.package.name end should "reload the logger after executing" do - load_seq = sequence("load_seq") - Vagrant::Config.expects(:execute!).once.returns(nil).in_sequence(load_seq) - @env.expects(:load_logger!).once.in_sequence(load_seq) @env.load_config! + assert @env.instance_variable_get(:@logger).nil? end end - context "loading logger" do - setup do - @env = mock_environment - @env.stubs(:vm_name).returns(nil) - end - - should "use 'vagrant' by default" do - assert @env.vm_name.nil? # sanity - @env.load_logger! - assert_equal "vagrant", @env.logger.resource - end - - should "use the vm name if available" do - name = "foo" - @env.stubs(:vm_name).returns(name) - @env.load_logger! - assert_equal name, @env.logger.resource - end - end - context "loading home directory" do setup do - @env = mock_environment - @home_dir = File.expand_path(@env.config.vagrant.home) + @env = vagrant_env File.stubs(:directory?).returns(true) FileUtils.stubs(:mkdir_p) end should "create each directory if it doesn't exist" do create_seq = sequence("create_seq") File.stubs(:directory?).returns(false) - Vagrant::Environment::HOME_SUBDIRS.each do |subdir| - FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq) + @klass::HOME_SUBDIRS.each do |subdir| + FileUtils.expects(:mkdir_p).with(@env.home_path.join(subdir)).in_sequence(create_seq) end @env.load_home_directory! end @@ -446,325 +436,35 @@ FileUtils.expects(:mkdir_p).never @env.load_home_directory! end end - context "loading host" do - setup do - @env = mock_environment - end - - should "load the host by calling the load method on Host::Base" do - result = mock("result") - Vagrant::Hosts::Base.expects(:load).with(@env, @env.config.vagrant.host).once.returns(result) - @env.load_host! - assert_equal result, @env.host - end - end - - context "loading box" do - setup do - @box = mock("box") - @box.stubs(:env=) - - @env = mock_environment - @env.stubs(:root_path).returns("foo") - end - - should "do nothing if the root path is nil" do - Vagrant::Box.expects(:find).never - @env.stubs(:root_path).returns(nil) - @env.load_box! - end - - should "not load the box if its not set" do - @env = mock_environment do |config| - config.vm.box = nil - end - - Vagrant::Box.expects(:find).never - @env.load_box! - end - - should "set the box to what is found by the Box class" do - Vagrant::Box.expects(:find).with(@env, @env.config.vm.box).once.returns(@box) - @env.load_box! - assert @box.equal?(@env.box) - end - end - context "loading the UUID out from the persisted dotfile" do setup do - @env = mock_environment - @env.stubs(:root_path).returns("foo") - - File.stubs(:file?).returns(true) + @env = vagrant_env end - should "blank the VMs" do - load_seq = sequence("load_seq") - @env.stubs(:root_path).returns("foo") - @env.expects(:load_blank_vms!).in_sequence(load_seq) - File.expects(:open).in_sequence(load_seq) - @env.load_vm! - end - - should "load the UUID if the JSON parsing fails" do - vm = mock("vm") - - filemock = mock("filemock") - filemock.expects(:read).returns("foo") - Vagrant::VM.expects(:find).with("foo", @env, Vagrant::Environment::DEFAULT_VM).returns(vm) - File.expects(:open).with(@env.dotfile_path).once.yields(filemock) - File.expects(:file?).with(@env.dotfile_path).once.returns(true) - @env.load_vm! - - assert_equal vm, @env.vms.values.first - end - should "load all the VMs from the dotfile" do - vms = { :foo => "bar", :bar => "baz" } - results = {} + @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" } - filemock = mock("filemock") - filemock.expects(:read).returns(vms.to_json) - File.expects(:open).with(@env.dotfile_path).once.yields(filemock) - File.expects(:file?).with(@env.dotfile_path).once.returns(true) - - vms.each do |key, value| + results = {} + @env.local_data[:active].each do |key, value| vm = mock("vm#{key}") Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm) - results[key] = vm + results[key.to_sym] = vm end - @env.load_vm! + returned = @env.load_vms! results.each do |key, value| - assert_equal value, @env.vms[key] + assert_equal value, returned[key] end end - should "do nothing if the vm_name is set" do - @env.stubs(:vm_name).returns(:foo) - File.expects(:open).never - @env.load_vm! - end - - should "do nothing if the dotfile is nil" do - @env.stubs(:dotfile_path).returns(nil) - File.expects(:open).never - - assert_nothing_raised { - @env.load_vm! - } - end - - should "do nothing if dotfile is not a file" do - File.expects(:file?).returns(false) - File.expects(:open).never - @env.load_vm! - end - - should "uuid should be nil if dotfile didn't exist" do - File.expects(:open).raises(Errno::ENOENT) - @env.load_vm! + should "uuid should be nil if local data contains nothing" do + assert @env.local_data.empty? # sanity + @env.load_vms! assert_nil @env.vm end - end - - context "loading blank VMs" do - setup do - @env = mock_environment - end - - should "blank the VMs" do - @env = mock_environment do |config| - config.vm.define :foo do |config| - end - - config.vm.define :bar do |config| - end - end - - @env.load_blank_vms! - - assert_equal 2, @env.vms.length - assert(@env.vms.all? { |name, vm| !vm.created? }) - - sorted_vms = @env.vms.keys.sort { |a,b| a.to_s <=> b.to_s } - assert_equal [:bar, :foo], sorted_vms - end - - should "load the default VM blank if no multi-VMs are specified" do - assert @env.config.vm.defined_vms.empty? # sanity - - @env.load_blank_vms! - - assert_equal 1, @env.vms.length - assert !@env.vms.values.first.created? - end - end - - context "loading the active list" do - setup do - @env = mock_environment - end - - should "initialize the ActiveList object with the given environment" do - active_list = mock("active_list") - Vagrant::ActiveList.expects(:new).with(@env).returns(active_list) - @env.load_active_list! - assert_equal active_list, @env.active_list - end - end - - context "loading the commands" do - setup do - @env = mock_environment - end - - should "initialize the Commands object with the given environment" do - commands = mock("commands") - Vagrant::Command.expects(:new).with(@env).returns(commands) - @env.load_commands! - assert_equal commands, @env.commands - end - end - - context "loading actions" do - setup do - @env = mock_environment - end - - should "initialize the Action object with the given environment" do - result = mock("result") - Vagrant::Action.expects(:new).with(@env).returns(result) - @env.load_actions! - assert_equal result, @env.actions - end - end - end - - context "requiring properties" do - setup do - @env = mock_environment - 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 - - context "requiring a persisted VM" do - setup do - @env.stubs(:vm).returns("foo") - @env.stubs(:require_root_path) - end - - should "require a root path" do - @env.expects(:require_root_path).once - @env.expects(:error_and_exit).never - @env.require_persisted_vm - end - - should "error and exit if the VM is not set" do - @env.expects(:vm).returns(nil) - @env.expects(:error_and_exit).once - @env.require_persisted_vm - end - end - end - - context "managing VM" do - setup do - @env = mock_environment - - @dotfile_path = "foo" - @env.stubs(:dotfile_path).returns(@dotfile_path) - end - - def mock_vm - @vm = mock("vm") - @vm.stubs(:uuid).returns("foo") - @env.stubs(:vm).returns(@vm) - end - end - - context "updating the dotfile" do - setup do - @env = mock_environment - @env.stubs(:parent).returns(nil) - @env.stubs(:dotfile_path).returns("foo") - File.stubs(:open) - File.stubs(:exist?).returns(true) - end - - def create_vm(created) - vm = mock("vm") - vm.stubs(:created?).returns(created) - vm.stubs(:uuid).returns("foo") - vm - end - - should "call parent if exists" do - parent = mock("parent") - @env.stubs(:parent).returns(parent) - parent.expects(:update_dotfile).once - - @env.update_dotfile - end - - should "remove the dotfile if the data is empty" do - vms = { - :foo => create_vm(false) - } - - @env.stubs(:vms).returns(vms) - File.expects(:delete).with(@env.dotfile_path).once - @env.update_dotfile - end - - should "not remove the dotfile if it doesn't exist" do - vms = { - :foo => create_vm(false) - } - - @env.stubs(:vms).returns(vms) - File.expects(:exist?).with(@env.dotfile_path).returns(false) - File.expects(:delete).never - assert_nothing_raised { @env.update_dotfile } - end - - should "write the proper data to dotfile" do - vms = { - :foo => create_vm(false), - :bar => create_vm(true), - :baz => create_vm(true) - } - - f = mock("f") - @env.stubs(:vms).returns(vms) - File.expects(:open).with(@env.dotfile_path, 'w+').yields(f) - f.expects(:write).with() do |json| - assert_nothing_raised { - data = JSON.parse(json) - assert_equal 2, data.length - assert_equal vms[:bar].uuid, data["bar"] - assert_equal vms[:baz].uuid, data["baz"] - } - - true - end - - @env.update_dotfile end end end