From 7eec1277041697c48544c340bb31cc14381ea42e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 30 Jan 2013 10:37:40 -0800 Subject: [PATCH] Support refreshing the machine cache --- lib/vagrant/environment.rb | 9 ++++++- .../providers/virtualbox/action/check_box.rb | 2 +- test/unit/vagrant/environment_test.rb | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c3dcf37ad..cab6e6f7a 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -217,14 +217,21 @@ module Vagrant # Vagrantfile). # @param [Symbol] provider The provider that this machine should be # backed by. + # @param [Boolean] refresh If true, then if there is a cached version + # it is reloaded. # @return [Machine] - def machine(name, provider) + def machine(name, provider, refresh=false) @logger.info("Getting machine: #{name} (#{provider})") # Compose the cache key of the name and provider, and return from # the cache if we have that. cache_key = [name, provider] @machines ||= {} + if refresh + @logger.info("Refreshing machine (busting cache): #{name} (#{provider})") + @machines.delete(cache_key) + end + if @machines.has_key?(cache_key) @logger.info("Returning cached machine: #{name} (#{provider})") return @machines[cache_key] diff --git a/plugins/providers/virtualbox/action/check_box.rb b/plugins/providers/virtualbox/action/check_box.rb index e3e4346b2..b5ee90efb 100644 --- a/plugins/providers/virtualbox/action/check_box.rb +++ b/plugins/providers/virtualbox/action/check_box.rb @@ -25,7 +25,7 @@ module VagrantPlugins # Reload the environment and set the VM to be the new loaded VM. env[:machine] = env[:machine].env.machine( - env[:machine].name, env[:machine].provider_name) + env[:machine].name, env[:machine].provider_name, true) end @app.call(env) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 7361c9039..8346abe90 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -454,6 +454,30 @@ VF machine.config.ssh.port.should == 100 end + it "should reload the cache if refresh is set" do + # Create a provider + foo_provider = register_provider("foo") + + # Create the configuration + isolated_env = isolated_environment do |e| + e.vagrantfile(<<-VF) +Vagrant.configure("2") do |config| + config.vm.box = "base" +end +VF + + e.box2("base", :foo) + end + + env = isolated_env.create_vagrant_env + vm1 = env.machine(:default, :foo) + vm2 = env.machine(:default, :foo, true) + vm3 = env.machine(:default, :foo) + + vm1.should_not eql(vm2) + vm2.should eql(vm3) + end + it "should raise an error if the VM is not found" do expect { instance.machine("i-definitely-dont-exist", :virtualbox) }. to raise_error(Vagrant::Errors::MachineNotFound)