Revamped provisioning JSON generation which includes the entire configuration tree. Plus better testing all around for JSON generation + configuration.
This commit is contained in:
parent
da2150da89
commit
66bc5a19e2
|
@ -19,7 +19,20 @@ module Vagrant
|
||||||
def setup_json
|
def setup_json
|
||||||
logger.info "Generating JSON and uploading..."
|
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"))
|
SSH.upload!(StringIO.new(json), File.join(Vagrant.config.chef.provisioning_path, "dna.json"))
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,6 +40,17 @@ module Vagrant
|
||||||
def [](key)
|
def [](key)
|
||||||
send(key)
|
send(key)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class SSHConfig < Base
|
class SSHConfig < Base
|
||||||
|
@ -96,6 +107,14 @@ module Vagrant
|
||||||
def initialize
|
def initialize
|
||||||
@enabled = false
|
@enabled = false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class VagrantConfig < Base
|
class VagrantConfig < Base
|
||||||
|
|
|
@ -35,21 +35,35 @@ class ProvisionActionTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "generating and uploading json" do
|
context "generating and uploading json" do
|
||||||
should "convert the JSON config to JSON" do
|
def assert_json
|
||||||
Hash.any_instance.expects(:to_json).once.returns("foo")
|
|
||||||
@action.setup_json
|
|
||||||
end
|
|
||||||
|
|
||||||
should "add the project directory to the JSON" do
|
|
||||||
Vagrant::SSH.expects(:upload!).with do |json, path|
|
Vagrant::SSH.expects(:upload!).with do |json, path|
|
||||||
data = JSON.parse(json.read)
|
data = JSON.parse(json.read)
|
||||||
assert_equal Vagrant.config.vm.project_directory, data["project_directory"]
|
yield data
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
@action.setup_json
|
@action.setup_json
|
||||||
end
|
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
|
should "upload a StringIO to dna.json" do
|
||||||
StringIO.expects(:new).with(anything).returns("bar")
|
StringIO.expects(:new).with(anything).returns("bar")
|
||||||
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "dna.json").once.returns("baz")
|
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "dna.json").once.returns("baz")
|
||||||
|
|
|
@ -74,4 +74,50 @@ class ConfigTest < Test::Unit::TestCase
|
||||||
assert Vagrant::Config.config.loaded?
|
assert Vagrant::Config.config.loaded?
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue