Config can now be deserialized as well as serialized to JSON
This commit is contained in:
parent
d2d2404961
commit
43cdcb7808
|
@ -6,12 +6,24 @@ module Vagrant
|
|||
class Base
|
||||
attr_accessor :env
|
||||
|
||||
def [](key)
|
||||
send(key)
|
||||
# Loads configuration values from JSON back into the proper
|
||||
# configuration classes. By default, this is done by simply
|
||||
# iterating over all values in the JSON hash and assigning them
|
||||
# to instance variables on the class.
|
||||
def self.json_create(data)
|
||||
data.inject(new) do |result, data|
|
||||
key, value = data
|
||||
result.instance_variable_set("@#{key}".to_sym, value) if key != "json_class"
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
def to_json(*a)
|
||||
{ 'json_class' => self.class.name }.merge(instance_variables_hash).to_json(*a)
|
||||
opts = a.first if a.first.is_a?(Hash)
|
||||
opts ||= {}
|
||||
result = {}
|
||||
result.merge!('json_class' => self.class.name) if opts[:loadable]
|
||||
result.merge(instance_variables_hash).to_json(*a)
|
||||
end
|
||||
|
||||
def instance_variables_hash
|
||||
|
|
|
@ -58,7 +58,7 @@ module Vagrant
|
|||
opts[:forward_agent] = true if env.config.ssh.forward_agent
|
||||
|
||||
Net::SSH.start(env.config.ssh.host,
|
||||
env.config[:ssh][:username],
|
||||
env.config.ssh.username,
|
||||
opts.merge( :port => port,
|
||||
:keys => [env.config.ssh.private_key_path],
|
||||
:user_known_hosts_file => [],
|
||||
|
|
|
@ -5,11 +5,6 @@ class ConfigBaseTest < Test::Unit::TestCase
|
|||
@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" }
|
||||
|
||||
|
@ -26,11 +21,17 @@ class ConfigBaseTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "converting to JSON" do
|
||||
should "include magic `json_class` if loadable is set to true" do
|
||||
@iv_hash = { "foo" => "bar" }
|
||||
@base.expects(:instance_variables_hash).returns(@iv_hash)
|
||||
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
|
||||
assert_equal @json, @base.to_json(:loadable => true)
|
||||
end
|
||||
|
||||
should "convert instance variable hash to json" do
|
||||
@iv_hash = { "foo" => "bar" }
|
||||
@base.expects(:instance_variables_hash).returns(@iv_hash)
|
||||
@json = { "json_class" => @base.class.name }.merge(@iv_hash).to_json
|
||||
assert_equal @json, @base.to_json
|
||||
assert_equal @iv_hash.to_json, @base.to_json
|
||||
end
|
||||
|
||||
should "not include env in the JSON hash" do
|
||||
|
|
|
@ -7,7 +7,7 @@ class ConfigSSHTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "expand any path when requesting the value" do
|
||||
result = File.expand_path(@env.config.ssh[:private_key_path], @env.root_path)
|
||||
result = File.expand_path(@env.config.ssh.private_key_path, @env.root_path)
|
||||
assert_equal result, @env.config.ssh.private_key_path
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,8 +25,8 @@ class ChefProvisionerTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "not include the 'json' key in the config dump" do
|
||||
result = JSON.parse(@config.to_json)
|
||||
assert !result.has_key?("json")
|
||||
result = @config.to_json
|
||||
assert result !~ /"json":/
|
||||
end
|
||||
|
||||
should "provide accessors to the run list" do
|
||||
|
@ -141,7 +141,7 @@ class ChefProvisionerTest < Test::Unit::TestCase
|
|||
context "generating and uploading json" do
|
||||
def assert_json
|
||||
@vm.ssh.expects(:upload!).with do |json, path|
|
||||
data = JSON.parse(json.read)
|
||||
data = JSON.parse(json.read, :object_class => Hash)
|
||||
yield data
|
||||
true
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue