diff --git a/lib/vagrant/actions/vm/provision.rb b/lib/vagrant/actions/vm/provision.rb index 2c6ce3b56..b9f8c0995 100644 --- a/lib/vagrant/actions/vm/provision.rb +++ b/lib/vagrant/actions/vm/provision.rb @@ -19,7 +19,20 @@ module Vagrant def setup_json logger.info "Generating JSON and uploading..." - json = { :project_directory => Vagrant.config.vm.project_directory }.merge(Vagrant.config.chef.json).to_json + # Set up initial configuration + data = { + :config => Vagrant.config, + :directory => Vagrant.config.vm.project_directory, + } + + # And wrap it under the "vagrant" namespace + data = { :vagrant => data } + + # Merge with the "extra data" which isn't put under the + # vagrant namespace by default + data.merge!(Vagrant.config.chef.json) + + json = data.to_json SSH.upload!(StringIO.new(json), File.join(Vagrant.config.chef.provisioning_path, "dna.json")) end diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 435878e7f..97a8592f0 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -40,6 +40,17 @@ module Vagrant def [](key) send(key) end + + def to_json + instance_variables_hash.to_json + end + + def instance_variables_hash + instance_variables.inject({}) do |acc, iv| + acc[iv.to_s[1..-1].to_sym] = instance_variable_get(iv) + acc + end + end end class SSHConfig < Base @@ -96,6 +107,14 @@ module Vagrant def initialize @enabled = false end + + def to_json + # Overridden so that the 'json' key could be removed, since its just + # merged into the config anyways + data = instance_variables_hash + data.delete(:json) + data.to_json + end end class VagrantConfig < Base diff --git a/test/vagrant/actions/vm/provision_test.rb b/test/vagrant/actions/vm/provision_test.rb index ac8f5b363..9322d799f 100644 --- a/test/vagrant/actions/vm/provision_test.rb +++ b/test/vagrant/actions/vm/provision_test.rb @@ -35,21 +35,35 @@ class ProvisionActionTest < Test::Unit::TestCase end context "generating and uploading json" do - should "convert the JSON config to JSON" do - Hash.any_instance.expects(:to_json).once.returns("foo") - @action.setup_json - end - - should "add the project directory to the JSON" do + def assert_json Vagrant::SSH.expects(:upload!).with do |json, path| data = JSON.parse(json.read) - assert_equal Vagrant.config.vm.project_directory, data["project_directory"] + yield data true end @action.setup_json end + should "merge in the extra json specified in the config" do + Vagrant.config.chef.json = { :foo => "BAR" } + assert_json do |data| + assert_equal "BAR", data["foo"] + end + end + + should "add the directory as a special case to the JSON" do + assert_json do |data| + assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["directory"] + end + end + + should "add the config to the JSON" do + assert_json do |data| + assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["config"]["vm"]["project_directory"] + end + end + should "upload a StringIO to dna.json" do StringIO.expects(:new).with(anything).returns("bar") File.expects(:join).with(Vagrant.config.chef.provisioning_path, "dna.json").once.returns("baz") diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index fa159d350..f3c635ce8 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -74,4 +74,50 @@ class ConfigTest < Test::Unit::TestCase assert Vagrant::Config.config.loaded? end end + + context "base class" do + setup do + @base = Vagrant::Config::Base.new + end + + should "forward [] access to methods" do + @base.expects(:foo).once + @base[:foo] + end + + should "return a hash of instance variables" do + data = { :foo => "bar", :bar => "baz" } + + data.each do |iv, value| + @base.instance_variable_set("@#{iv}".to_sym, value) + end + + result = @base.instance_variables_hash + assert_equal data.length, result.length + + data.each do |iv, value| + assert_equal value, result[iv] + end + end + + should "convert instance variable hash to json" do + @json = mock("json") + @iv_hash = mock("iv_hash") + @iv_hash.expects(:to_json).once.returns(@json) + @base.expects(:instance_variables_hash).returns(@iv_hash) + assert_equal @json, @base.to_json + end + end + + context "chef config" do + setup do + @config = Vagrant::Config::ChefConfig.new + @config.json = "HEY" + end + + should "not include the 'json' key in the config dump" do + result = JSON.parse(@config.to_json) + assert !result.has_key?("json") + end + end end