diff options
| author | Markus Roberts <Markus@reality.com> | 2009-10-09 15:23:19 -0700 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-10-17 12:23:33 +1100 |
| commit | bca3b70437666a8b840af032cab20fc1ea4f18a2 (patch) | |
| tree | 0cde5cd7b39fc5a5e41b636d1f55a7ed71d6518b /spec | |
| parent | ce46be5773656f68eddc7edd6212e283b46f9320 (diff) | |
| download | puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.tar.gz puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.tar.xz puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.zip | |
Bundling of pure ruby json lib as "pson"
Bundeling and renaming the pure ruby json library to addresses a
number of cross version serliaization bugs (#2615, et al).
This patch adds a subset of the files from the json_pure gem to
lib/puppet/external/pson (renamed to avoid conflicts with rails) so
that we will always have a known-good erialization format available.
The pure ruby json gem as distibuted defers to the compiled version
if it is installed. This is problematic in some circumstances so the
files that have been brought over have been modified to always and
only use the bundled version.
It's a large patch, so here's a breakdown of the change categories:
The majority of the lines are only marginally interesting:
* The json lib itself (in lib/puppet/external/pson) make up the bulk
of the lines.
* Renaming of json to pson make up the second largest group.
Somewhat more interesting are the following, which can be located by
searching the diffs for the indicated strings:
* Adjusting tests to reflect the changes
* Changing the encoding/decoding behavior so that nested structures
(e.g. resources) don't serialize as escaped strings. This should
make it much easier to process the results with external tools, if
needed. Search for "to_pson" and "to_pson_data_hash"
* Cleaning up the envelope/metadata
* Now provides a document_type (as opposed to a ruby class name) by
using a symple registration scheme instead of constant lookup
(search for "document_type")
* Added an api_version (search for "api_version")
* Added a hash for document metadata (search for "metadata")
* Removing the yaml monkeypatch and instead disabling yaml serialization
on ruby 1.8.1 in favor of pson (search for "yaml")
* Cleaning up the json/rails feature interaction (they're now totally
independent) (search for "feature")
Diffstat (limited to 'spec')
| -rwxr-xr-x | spec/integration/application/puppet.rb | 8 | ||||
| -rwxr-xr-x | spec/integration/defaults.rb | 4 | ||||
| -rwxr-xr-x | spec/integration/indirector/catalog/queue.rb | 10 | ||||
| -rwxr-xr-x | spec/integration/network/formats.rb | 72 | ||||
| -rwxr-xr-x | spec/integration/resource/catalog.rb | 8 | ||||
| -rwxr-xr-x | spec/unit/application/puppet.rb | 30 | ||||
| -rw-r--r-- | spec/unit/application/puppetmasterd.rb | 12 | ||||
| -rwxr-xr-x | spec/unit/indirector/queue.rb | 30 | ||||
| -rwxr-xr-x | spec/unit/network/formats.rb | 78 | ||||
| -rwxr-xr-x | spec/unit/relationship.rb | 67 | ||||
| -rwxr-xr-x | spec/unit/resource.rb | 63 | ||||
| -rwxr-xr-x | spec/unit/resource/catalog.rb | 93 | ||||
| -rwxr-xr-x | spec/unit/util/json.rb | 18 |
13 files changed, 232 insertions, 261 deletions
diff --git a/spec/integration/application/puppet.rb b/spec/integration/application/puppet.rb index e3f8fb9fc..1342f3c5f 100755 --- a/spec/integration/application/puppet.rb +++ b/spec/integration/application/puppet.rb @@ -10,16 +10,16 @@ describe "Puppet" do include PuppetSpec::Files describe "when applying provided catalogs" do - confine "JSON library is missing; cannot test applying catalogs" => Puppet.features.json? - it "should be able to apply catalogs provided in a file in json" do - file_to_create = tmpfile("json_catalog") + confine "PSON library is missing; cannot test applying catalogs" => Puppet.features.pson? + it "should be able to apply catalogs provided in a file in pson" do + file_to_create = tmpfile("pson_catalog") catalog = Puppet::Resource::Catalog.new resource = Puppet::Resource.new(:file, file_to_create, :content => "my stuff") catalog.add_resource resource manifest = tmpfile("manifest") - File.open(manifest, "w") { |f| f.print catalog.to_json } + File.open(manifest, "w") { |f| f.print catalog.to_pson } puppet = Puppet::Application[:puppet] puppet.options[:catalog] = manifest diff --git a/spec/integration/defaults.rb b/spec/integration/defaults.rb index c38a1a14b..fb00f8646 100755 --- a/spec/integration/defaults.rb +++ b/spec/integration/defaults.rb @@ -89,8 +89,8 @@ describe "Puppet defaults" do end end - it "should default to json for the preferred serialization format" do - Puppet.settings.value(:preferred_serialization_format).should == "json" + it "should default to pson for the preferred serialization format" do + Puppet.settings.value(:preferred_serialization_format).should == "pson" end describe "when enabling storeconfigs" do diff --git a/spec/integration/indirector/catalog/queue.rb b/spec/integration/indirector/catalog/queue.rb index bf121811c..5a781c1a8 100755 --- a/spec/integration/indirector/catalog/queue.rb +++ b/spec/integration/indirector/catalog/queue.rb @@ -6,7 +6,7 @@ require 'puppet/resource/catalog' describe "Puppet::Resource::Catalog::Queue" do - confine "Missing json support; cannot test queue" => Puppet.features.json? + confine "Missing pson support; cannot test queue" => Puppet.features.pson? before do Puppet::Resource::Catalog.indirection.terminus(:queue) @@ -23,13 +23,13 @@ describe "Puppet::Resource::Catalog::Queue" do after { Puppet.settings.clear } - it "should render catalogs to json and send them via the queue client when catalogs are saved" do + it "should render catalogs to pson and send them via the queue client when catalogs are saved" do terminus = Puppet::Resource::Catalog.indirection.terminus(:queue) client = mock 'client' terminus.stubs(:client).returns client - client.expects(:send_message).with(:catalog, @catalog.to_json) + client.expects(:send_message).with(:catalog, @catalog.to_pson) request = Puppet::Indirector::Request.new(:catalog, :save, "foo", :instance => @catalog) @@ -40,9 +40,9 @@ describe "Puppet::Resource::Catalog::Queue" do client = mock 'client' Puppet::Resource::Catalog::Queue.stubs(:client).returns client - json = @catalog.to_json + pson = @catalog.to_pson - client.expects(:subscribe).with(:catalog).yields(json) + client.expects(:subscribe).with(:catalog).yields(pson) Puppet.expects(:err).never diff --git a/spec/integration/network/formats.rb b/spec/integration/network/formats.rb index 7d0d47ee7..35e797706 100755 --- a/spec/integration/network/formats.rb +++ b/spec/integration/network/formats.rb @@ -4,13 +4,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/network/formats' -class JsonIntTest +class PsonIntTest attr_accessor :string def ==(other) other.class == self.class and string == other.string end - def self.from_json(data) + def self.from_pson(data) new(data[0]) end @@ -18,15 +18,15 @@ class JsonIntTest @string = string end - def to_json(*args) + def to_pson(*args) { - 'json_class' => self.class.name, + 'type' => self.class.name, 'data' => [@string] - }.to_json(*args) + }.to_pson(*args) end def self.canonical_order(s) - s.gsub(/\{"data":\[(.*?)\],"json_class":"JsonIntTest"\}/,'{"json_class":"JsonIntTest","data":[\1]}') + s.gsub(/\{"data":\[(.*?)\],"type":"PsonIntTest"\}/,'{"type":"PsonIntTest","data":[\1]}') end end @@ -45,65 +45,65 @@ describe Puppet::Network::FormatHandler.format(:s) do end end -describe Puppet::Network::FormatHandler.format(:json) do - describe "when json is absent" do - confine "'json' library is present" => (! Puppet.features.json?) +describe Puppet::Network::FormatHandler.format(:pson) do + describe "when pson is absent" do + confine "'pson' library is present" => (! Puppet.features.pson?) before do - @json = Puppet::Network::FormatHandler.format(:json) + @pson = Puppet::Network::FormatHandler.format(:pson) end it "should not be suitable" do - @json.should_not be_suitable + @pson.should_not be_suitable end end - describe "when json is available" do - confine "Missing 'json' library" => Puppet.features.json? + describe "when pson is available" do + confine "Missing 'pson' library" => Puppet.features.pson? before do - @json = Puppet::Network::FormatHandler.format(:json) + @pson = Puppet::Network::FormatHandler.format(:pson) end - it "should be able to render an instance to json" do - instance = JsonIntTest.new("foo") - JsonIntTest.canonical_order(@json.render(instance)).should == JsonIntTest.canonical_order('{"json_class":"JsonIntTest","data":["foo"]}' ) + it "should be able to render an instance to pson" do + instance = PsonIntTest.new("foo") + PsonIntTest.canonical_order(@pson.render(instance)).should == PsonIntTest.canonical_order('{"type":"PsonIntTest","data":["foo"]}' ) end - it "should be able to render arrays to json" do - @json.render([1,2]).should == '[1,2]' + it "should be able to render arrays to pson" do + @pson.render([1,2]).should == '[1,2]' end - it "should be able to render arrays containing hashes to json" do - @json.render([{"one"=>1},{"two"=>2}]).should == '[{"one":1},{"two":2}]' + it "should be able to render arrays containing hashes to pson" do + @pson.render([{"one"=>1},{"two"=>2}]).should == '[{"one":1},{"two":2}]' end - it "should be able to render multiple instances to json" do - Puppet.features.add(:json, :libs => ["json"]) + it "should be able to render multiple instances to pson" do + Puppet.features.add(:pson, :libs => ["pson"]) - one = JsonIntTest.new("one") - two = JsonIntTest.new("two") + one = PsonIntTest.new("one") + two = PsonIntTest.new("two") - JsonIntTest.canonical_order(@json.render([one,two])).should == JsonIntTest.canonical_order('[{"json_class":"JsonIntTest","data":["one"]},{"json_class":"JsonIntTest","data":["two"]}]') + PsonIntTest.canonical_order(@pson.render([one,two])).should == PsonIntTest.canonical_order('[{"type":"PsonIntTest","data":["one"]},{"type":"PsonIntTest","data":["two"]}]') end - it "should be able to intern json into an instance" do - @json.intern(JsonIntTest, '{"json_class":"JsonIntTest","data":["foo"]}').should == JsonIntTest.new("foo") + it "should be able to intern pson into an instance" do + @pson.intern(PsonIntTest, '{"type":"PsonIntTest","data":["foo"]}').should == PsonIntTest.new("foo") end - it "should be able to intern json with no class information into an instance" do - @json.intern(JsonIntTest, '["foo"]').should == JsonIntTest.new("foo") + it "should be able to intern pson with no class information into an instance" do + @pson.intern(PsonIntTest, '["foo"]').should == PsonIntTest.new("foo") end - it "should be able to intern multiple instances from json" do - @json.intern_multiple(JsonIntTest, '[{"json_class": "JsonIntTest", "data": ["one"]},{"json_class": "JsonIntTest", "data": ["two"]}]').should == [ - JsonIntTest.new("one"), JsonIntTest.new("two") + it "should be able to intern multiple instances from pson" do + @pson.intern_multiple(PsonIntTest, '[{"type": "PsonIntTest", "data": ["one"]},{"type": "PsonIntTest", "data": ["two"]}]').should == [ + PsonIntTest.new("one"), PsonIntTest.new("two") ] end - it "should be able to intern multiple instances from json with no class information" do - @json.intern_multiple(JsonIntTest, '[["one"],["two"]]').should == [ - JsonIntTest.new("one"), JsonIntTest.new("two") + it "should be able to intern multiple instances from pson with no class information" do + @pson.intern_multiple(PsonIntTest, '[["one"],["two"]]').should == [ + PsonIntTest.new("one"), PsonIntTest.new("two") ] end end diff --git a/spec/integration/resource/catalog.rb b/spec/integration/resource/catalog.rb index 9c7c37d8e..f4004451d 100755 --- a/spec/integration/resource/catalog.rb +++ b/spec/integration/resource/catalog.rb @@ -6,10 +6,10 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Resource::Catalog do - describe "when json is available" do - confine "JSON library is missing" => Puppet.features.json? - it "should support json" do - Puppet::Resource::Catalog.supported_formats.should be_include(:json) + describe "when pson is available" do + confine "PSON library is missing" => Puppet.features.pson? + it "should support pson" do + Puppet::Resource::Catalog.supported_formats.should be_include(:pson) end end diff --git a/spec/unit/application/puppet.rb b/spec/unit/application/puppet.rb index 3c1143060..f4d606535 100755 --- a/spec/unit/application/puppet.rb +++ b/spec/unit/application/puppet.rb @@ -332,17 +332,17 @@ describe "Puppet" do end describe "the 'apply' command" do - confine "JSON library is missing; cannot test applying catalogs" => Puppet.features.json? + confine "PSON library is missing; cannot test applying catalogs" => Puppet.features.pson? before do - #Puppet::Resource::Catalog.stubs(:json_create).returns Puppet::Resource::Catalog.new - JSON.stubs(:parse).returns Puppet::Resource::Catalog.new + #Puppet::Resource::Catalog.stubs(:pson_create).returns Puppet::Resource::Catalog.new + PSON.stubs(:parse).returns Puppet::Resource::Catalog.new end it "should read the catalog in from disk if a file name is provided" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" - File.expects(:read).with("/my/catalog.json").returns "something" + File.expects(:read).with("/my/catalog.pson").returns "something" @puppet.apply end @@ -355,41 +355,41 @@ describe "Puppet" do @puppet.apply end - it "should deserialize the catalog from json" do - @puppet.options[:catalog] = "/my/catalog.json" + it "should deserialize the catalog from pson" do + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new + PSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new @puppet.apply end it "should fail helpfully if deserializing fails" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).raises ArgumentError + PSON.expects(:parse).raises ArgumentError lambda { @puppet.apply }.should raise_error(Puppet::Error) end it "should convert plain data structures into a catalog if deserialization does not do so" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).with("something").returns({:foo => "bar"}) - Puppet::Resource::Catalog.expects(:json_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) + PSON.expects(:parse).with("something").returns({:foo => "bar"}) + Puppet::Resource::Catalog.expects(:pson_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) @puppet.apply end it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" catalog = Puppet::Resource::Catalog.new - JSON.expects(:parse).returns catalog + PSON.expects(:parse).returns catalog catalog.expects(:to_ral).returns "mycatalog" diff --git a/spec/unit/application/puppetmasterd.rb b/spec/unit/application/puppetmasterd.rb index a4875c713..980138782 100644 --- a/spec/unit/application/puppetmasterd.rb +++ b/spec/unit/application/puppetmasterd.rb @@ -294,11 +294,11 @@ describe "PuppetMaster" do Puppet.stubs(:err) @puppetmasterd.stubs(:exit) Puppet::Parser::Interpreter.stubs(:new).returns(@interpreter) - Puppet.features.stubs(:json?).returns true + Puppet.features.stubs(:pson?).returns true end - it "should fail if json isn't available" do - Puppet.features.expects(:json?).returns false + it "should fail if pson isn't available" do + Puppet.features.expects(:pson?).returns false lambda { @puppetmasterd.compile }.should raise_error end @@ -310,13 +310,13 @@ describe "PuppetMaster" do @puppetmasterd.compile end - it "should render the catalog to json and print the output" do + it "should render the catalog to pson and print the output" do @puppetmasterd.options[:node] = "foo" catalog = Puppet::Resource::Catalog.new - catalog.expects(:render).with(:json).returns "myjson" + catalog.expects(:render).with(:pson).returns "mypson" Puppet::Resource::Catalog.expects(:find).returns catalog - $stdout.expects(:puts).with("myjson") + $stdout.expects(:puts).with("mypson") @puppetmasterd.compile end diff --git a/spec/unit/indirector/queue.rb b/spec/unit/indirector/queue.rb index c81cefec9..0748b5817 100755 --- a/spec/unit/indirector/queue.rb +++ b/spec/unit/indirector/queue.rb @@ -9,25 +9,25 @@ end class FooExampleData attr_accessor :name - def self.json_create(json) - new(json['data'].to_sym) + def self.pson_create(pson) + new(pson['data'].to_sym) end def initialize(name = nil) @name = name if name end - def render(format = :json) - to_json + def render(format = :pson) + to_pson end - def to_json(*args) - {:json_class => self.class.to_s, :data => name}.to_json(*args) + def to_pson(*args) + {:type => self.class.to_s, :data => name}.to_pson(*args) end end describe Puppet::Indirector::Queue do - confine "JSON library is missing; cannot test queueing" => Puppet.features.json? + confine "PSON library is missing; cannot test queueing" => Puppet.features.pson? before :each do @model = mock 'model' @@ -51,8 +51,8 @@ describe Puppet::Indirector::Queue do @request = stub 'request', :key => :me, :instance => @subject end - it "should require JSON" do - Puppet.features.expects(:json?).returns false + it "should require PSON" do + Puppet.features.expects(:pson?).returns false lambda { @store_class.new }.should raise_error(ArgumentError) end @@ -63,16 +63,16 @@ describe Puppet::Indirector::Queue do end describe "when saving" do - it 'should render the instance using json' do - @subject.expects(:render).with(:json) + it 'should render the instance using pson' do + @subject.expects(:render).with(:pson) @store.client.stubs(:send_message) @store.save(@request) end it "should send the rendered message to the appropriate queue on the client" do - @subject.expects(:render).returns "myjson" + @subject.expects(:render).returns "mypson" - @store.client.expects(:send_message).with(:my_queue, "myjson") + @store.client.expects(:send_message).with(:my_queue, "mypson") @store.save(@request) end @@ -89,8 +89,8 @@ describe Puppet::Indirector::Queue do @store_class.stubs(:model).returns @model end - it "should use the model's Format support to intern the message from json" do - @model.expects(:convert_from).with(:json, "mymessage") + it "should use the model's Format support to intern the message from pson" do + @model.expects(:convert_from).with(:pson, "mymessage") @store_class.client.expects(:subscribe).yields("mymessage") @store_class.subscribe {|o| o } diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb index c766f55f0..de2e0afe3 100755 --- a/spec/unit/network/formats.rb +++ b/spec/unit/network/formats.rb @@ -4,13 +4,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/network/formats' -class JsonTest +class PsonTest attr_accessor :string def ==(other) string == other.string end - def self.from_json(data) + def self.from_pson(data) new(data) end @@ -18,11 +18,11 @@ class JsonTest @string = string end - def to_json(*args) + def to_pson(*args) { - 'json_class' => self.class.name, + 'type' => self.class.name, 'data' => @string - }.to_json(*args) + }.to_pson(*args) end end @@ -173,76 +173,76 @@ describe "Puppet Network Format" do end end - it "should include a json format" do - Puppet::Network::FormatHandler.format(:json).should_not be_nil + it "should include a pson format" do + Puppet::Network::FormatHandler.format(:pson).should_not be_nil end - describe "json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do - @json = Puppet::Network::FormatHandler.format(:json) + @pson = Puppet::Network::FormatHandler.format(:pson) end - it "should have its mime type set to text/json" do - Puppet::Network::FormatHandler.format(:json).mime.should == "text/json" + it "should have its mime type set to text/pson" do + Puppet::Network::FormatHandler.format(:pson).mime.should == "text/pson" end it "should require the :render_method" do - Puppet::Network::FormatHandler.format(:json).required_methods.should be_include(:render_method) + Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:render_method) end it "should require the :intern_method" do - Puppet::Network::FormatHandler.format(:json).required_methods.should be_include(:intern_method) + Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:intern_method) end it "should have a weight of 10" do - @json.weight.should == 10 + @pson.weight.should == 10 end describe "when supported" do - it "should render by calling 'to_json' on the instance" do - instance = JsonTest.new("foo") - instance.expects(:to_json).returns "foo" - @json.render(instance).should == "foo" + it "should render by calling 'to_pson' on the instance" do + instance = PsonTest.new("foo") + instance.expects(:to_pson).returns "foo" + @pson.render(instance).should == "foo" end - it "should render multiple instances by calling 'to_json' on the array" do + it "should render multiple instances by calling 'to_pson' on the array" do instances = [mock('instance')] - instances.expects(:to_json).returns "foo" + instances.expects(:to_pson).returns "foo" - @json.render_multiple(instances).should == "foo" + @pson.render_multiple(instances).should == "foo" end - it "should intern by calling 'JSON.parse' on the text and then using from_json to convert the data into an instance" do + it "should intern by calling 'PSON.parse' on the text and then using from_pson to convert the data into an instance" do text = "foo" - JSON.expects(:parse).with("foo").returns("json_class" => "JsonTest", "data" => "foo") - JsonTest.expects(:from_json).with("foo").returns "parsed_json" - @json.intern(JsonTest, text).should == "parsed_json" + PSON.expects(:parse).with("foo").returns("type" => "PsonTest", "data" => "foo") + PsonTest.expects(:from_pson).with("foo").returns "parsed_pson" + @pson.intern(PsonTest, text).should == "parsed_pson" end - it "should not render twice if 'JSON.parse' creates the appropriate instance" do + it "should not render twice if 'PSON.parse' creates the appropriate instance" do text = "foo" - instance = JsonTest.new("foo") - JSON.expects(:parse).with("foo").returns(instance) - JsonTest.expects(:from_json).never - @json.intern(JsonTest, text).should equal(instance) + instance = PsonTest.new("foo") + PSON.expects(:parse).with("foo").returns(instance) + PsonTest.expects(:from_pson).never + @pson.intern(PsonTest, text).should equal(instance) end - it "should intern by calling 'JSON.parse' on the text and then using from_json to convert the actual into an instance if the json has no class/data separation" do + it "should intern by calling 'PSON.parse' on the text and then using from_pson to convert the actual into an instance if the pson has no class/data separation" do text = "foo" - JSON.expects(:parse).with("foo").returns("foo") - JsonTest.expects(:from_json).with("foo").returns "parsed_json" - @json.intern(JsonTest, text).should == "parsed_json" + PSON.expects(:parse).with("foo").returns("foo") + PsonTest.expects(:from_pson).with("foo").returns "parsed_pson" + @pson.intern(PsonTest, text).should == "parsed_pson" end it "should intern multiples by parsing the text and using 'class.intern' on each resulting data structure" do text = "foo" - JSON.expects(:parse).with("foo").returns ["bar", "baz"] - JsonTest.expects(:from_json).with("bar").returns "BAR" - JsonTest.expects(:from_json).with("baz").returns "BAZ" - @json.intern_multiple(JsonTest, text).should == %w{BAR BAZ} + PSON.expects(:parse).with("foo").returns ["bar", "baz"] + PsonTest.expects(:from_pson).with("bar").returns "BAR" + PsonTest.expects(:from_pson).with("baz").returns "BAZ" + @pson.intern_multiple(PsonTest, text).should == %w{BAR BAZ} end end end diff --git a/spec/unit/relationship.rb b/spec/unit/relationship.rb index fd7e0aaf7..5a52cb587 100755 --- a/spec/unit/relationship.rb +++ b/spec/unit/relationship.rb @@ -155,68 +155,43 @@ describe Puppet::Relationship, " when matching edges with a non-standard event" end end -describe Puppet::Relationship, "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Relationship, "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @edge = Puppet::Relationship.new(:a, :b, :event => :random, :callback => :whatever) end - def json_output_should - @edge.class.expects(:json_create).with { |hash| yield hash } - end - - # LAK:NOTE For all of these tests, we convert back to the edge so we can - # trap the actual data structure then. - it "should set the 'json_class' to Puppet::Relationship" do - json_output_should { |hash| hash['json_class'] == "Puppet::Relationship" } - - JSON.parse @edge.to_json - end - it "should store the stringified source as the source in the data" do - json_output_should { |hash| hash['data']['source'] == "a" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["source"].should == "a" end it "should store the stringified target as the target in the data" do - json_output_should { |hash| hash['data']['target'] == "b" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)['target'].should == "b" end - it "should store the jsonified event as the event in the data" do - @edge.event = :random - json_output_should { |hash| hash['data']['event'] == "random" } - - JSON.parse @edge.to_json + it "should store the psonified event as the event in the data" do + PSON.parse(@edge.to_pson)["event"].should == "random" end it "should not store an event when none is set" do @edge.event = nil - json_output_should { |hash| hash['data']['event'].nil? } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["event"].should be_nil end - it "should store the jsonified callback as the callback in the data" do + it "should store the psonified callback as the callback in the data" do @edge.callback = "whatever" - json_output_should { |hash| hash['data']['callback'] == "whatever" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["callback"].should == "whatever" end it "should not store a callback when none is set in the edge" do @edge.callback = nil - json_output_should { |hash| hash['data']['callback'].nil? } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["callback"].should be_nil end end -describe Puppet::Relationship, "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Relationship, "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @event = "random" @@ -227,35 +202,35 @@ describe Puppet::Relationship, "when converting from json" do "event" => @event, "callback" => @callback } - @json = { - "json_class" => "Puppet::Relationship", + @pson = { + "type" => "Puppet::Relationship", "data" => @data } end - def json_result_should + def pson_result_should Puppet::Relationship.expects(:new).with { |*args| yield args } end - it "should be extended with the JSON utility module" do - Puppet::Relationship.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should be extended with the PSON utility module" do + Puppet::Relationship.metaclass.ancestors.should be_include(Puppet::Util::Pson) end # LAK:NOTE For all of these tests, we convert back to the edge so we can # trap the actual data structure then. it "should pass the source in as the first argument" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").source.should == "mysource" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget").source.should == "mysource" end it "should pass the target in as the second argument" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").target.should == "mytarget" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget").target.should == "mytarget" end it "should pass the event as an argument if it's provided" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "event" => "myevent", "callback" => "eh").event.should == "myevent" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget", "event" => "myevent", "callback" => "eh").event.should == "myevent" end it "should pass the callback as an argument if it's provided" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "callback" => "mycallback").callback.should == "mycallback" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget", "callback" => "mycallback").callback.should == "mycallback" end end diff --git a/spec/unit/resource.rb b/spec/unit/resource.rb index c233dd3f8..b26f4f923 100755 --- a/spec/unit/resource.rb +++ b/spec/unit/resource.rb @@ -354,63 +354,60 @@ describe Puppet::Resource do end end - describe "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_output_should - @resource.class.expects(:json_create).with { |hash| yield hash } + def pson_output_should + @resource.class.expects(:pson_create).with { |hash| yield hash } end - it "should include the json util module" do - Puppet::Resource.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should include the pson util module" do + Puppet::Resource.metaclass.ancestors.should be_include(Puppet::Util::Pson) end # LAK:NOTE For all of these tests, we convert back to the resource so we can # trap the actual data structure then. - it "should set its json_class to 'Puppet::Resource'" do - JSON.parse(Puppet::Resource.new("file", "yay").to_json).should be_instance_of(Puppet::Resource) - end it "should set its type to the provided type" do - JSON.parse(Puppet::Resource.new("File", "/foo").to_json).type.should == "File" + Puppet::Resource.from_pson(PSON.parse(Puppet::Resource.new("File", "/foo").to_pson)).type.should == "File" end it "should set its title to the provided title" do - JSON.parse(Puppet::Resource.new("File", "/foo").to_json).title.should == "/foo" + Puppet::Resource.from_pson(PSON.parse(Puppet::Resource.new("File", "/foo").to_pson)).title.should == "/foo" end it "should include all tags from the resource" do resource = Puppet::Resource.new("File", "/foo") resource.tag("yay") - JSON.parse(resource.to_json).tags.should == resource.tags + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).tags.should == resource.tags end it "should include the file if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.file = "/my/file" - JSON.parse(resource.to_json).file.should == "/my/file" + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).file.should == "/my/file" end it "should include the line if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.line = 50 - JSON.parse(resource.to_json).line.should == 50 + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).line.should == 50 end it "should include the 'exported' value if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.exported = true - JSON.parse(resource.to_json).exported.should be_true + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).exported.should be_true end it "should set 'exported' to false if no value is set" do resource = Puppet::Resource.new("File", "/foo") - JSON.parse(resource.to_json).exported.should be_false + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).exported.should be_false end it "should set all of its parameters as the 'parameters' entry" do @@ -418,16 +415,16 @@ describe Puppet::Resource do resource[:foo] = %w{bar eh} resource[:fee] = %w{baz} - result = JSON.parse(resource.to_json) + result = Puppet::Resource.from_pson(PSON.parse(resource.to_pson)) result["foo"].should == %w{bar eh} result["fee"].should == %w{baz} end end - describe "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_result_should + def pson_result_should Puppet::Resource.expects(:new).with { |hash| yield hash } end @@ -439,59 +436,59 @@ describe Puppet::Resource do end it "should set its type to the provided type" do - Puppet::Resource.from_json(@data).type.should == "File" + Puppet::Resource.from_pson(@data).type.should == "File" end it "should set its title to the provided title" do - Puppet::Resource.from_json(@data).title.should == "yay" + Puppet::Resource.from_pson(@data).title.should == "yay" end it "should tag the resource with any provided tags" do @data['tags'] = %w{foo bar} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource.tags.should be_include("foo") resource.tags.should be_include("bar") end it "should set its file to the provided file" do @data['file'] = "/foo/bar" - Puppet::Resource.from_json(@data).file.should == "/foo/bar" + Puppet::Resource.from_pson(@data).file.should == "/foo/bar" end it "should set its line to the provided line" do @data['line'] = 50 - Puppet::Resource.from_json(@data).line.should == 50 + Puppet::Resource.from_pson(@data).line.should == 50 end - it "should 'exported' to true if set in the json data" do + it "should 'exported' to true if set in the pson data" do @data['exported'] = true - Puppet::Resource.from_json(@data).exported.should be_true + Puppet::Resource.from_pson(@data).exported.should be_true end - it "should 'exported' to false if not set in the json data" do - Puppet::Resource.from_json(@data).exported.should be_false + it "should 'exported' to false if not set in the pson data" do + Puppet::Resource.from_pson(@data).exported.should be_false end it "should fail if no title is provided" do @data.delete('title') - lambda { Puppet::Resource.from_json(@data) }.should raise_error(ArgumentError) + lambda { Puppet::Resource.from_pson(@data) }.should raise_error(ArgumentError) end it "should fail if no type is provided" do @data.delete('type') - lambda { Puppet::Resource.from_json(@data) }.should raise_error(ArgumentError) + lambda { Puppet::Resource.from_pson(@data) }.should raise_error(ArgumentError) end it "should set each of the provided parameters" do @data['parameters'] = {'foo' => %w{one two}, 'fee' => %w{three four}} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource['foo'].should == %w{one two} resource['fee'].should == %w{three four} end it "should convert single-value array parameters to normal values" do @data['parameters'] = {'foo' => %w{one}} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource['foo'].should == %w{one} end end diff --git a/spec/unit/resource/catalog.rb b/spec/unit/resource/catalog.rb index 1fbe3a984..39f185a7c 100755 --- a/spec/unit/resource/catalog.rb +++ b/spec/unit/resource/catalog.rb @@ -887,71 +887,70 @@ describe Puppet::Resource::Catalog, "when compiling" do end end -describe Puppet::Resource::Catalog, "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Resource::Catalog, "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @catalog = Puppet::Resource::Catalog.new("myhost") end - def json_output_should - @catalog.class.expects(:json_create).with { |hash| yield hash } + def pson_output_should + @catalog.class.expects(:pson_create).with { |hash| yield hash }.returns(:something) end # LAK:NOTE For all of these tests, we convert back to the resource so we can # trap the actual data structure then. - it "should set its json_class to 'Puppet::Resource::Catalog'" do - json_output_should { |hash| hash['json_class'] == "Puppet::Resource::Catalog" } + it "should set its document_type to 'Catalog'" do + pson_output_should { |hash| hash['document_type'] == "Catalog" } - JSON.parse @catalog.to_json + PSON.parse @catalog.to_pson end it "should set its data as a hash" do - json_output_should { |hash| hash['data'].is_a?(Hash) } - JSON.parse @catalog.to_json + pson_output_should { |hash| hash['data'].is_a?(Hash) } + PSON.parse @catalog.to_pson end [:name, :version, :tags].each do |param| it "should set its #{param} to the #{param} of the resource" do @catalog.send(param.to_s + "=", "testing") unless @catalog.send(param) - json_output_should { |hash| hash['data'][param.to_s] == @catalog.send(param) } - JSON.parse @catalog.to_json + pson_output_should { |hash| hash['data'][param.to_s] == @catalog.send(param) } + PSON.parse @catalog.to_pson end end - it "should convert its resources to a JSON-encoded array and store it as the 'resources' data" do - one = stub 'one', :to_json => '"one_resource"', :ref => "Foo[one]" - two = stub 'two', :to_json => '"two_resource"', :ref => "Foo[two]" + it "should convert its resources to a PSON-encoded array and store it as the 'resources' data" do + one = stub 'one', :to_pson_data_hash => "one_resource", :ref => "Foo[one]" + two = stub 'two', :to_pson_data_hash => "two_resource", :ref => "Foo[two]" @catalog.add_resource(one) @catalog.add_resource(two) # TODO this should really guarantee sort order - json_output_should { |hash| JSON.parse(hash['data']['resources']).sort == ["one_resource", "two_resource"].sort } - JSON.parse @catalog.to_json + PSON.parse(@catalog.to_pson,:create_additions => false)['data']['resources'].sort.should == ["one_resource", "two_resource"].sort + end - it "should convert its edges to a JSON-encoded array and store it as the 'edges' data" do - one = stub 'one', :to_json => '"one_resource"', :ref => 'Foo[one]' - two = stub 'two', :to_json => '"two_resource"', :ref => 'Foo[two]' - three = stub 'three', :to_json => '"three_resource"', :ref => 'Foo[three]' + it "should convert its edges to a PSON-encoded array and store it as the 'edges' data" do + one = stub 'one', :to_pson_data_hash => "one_resource", :ref => 'Foo[one]' + two = stub 'two', :to_pson_data_hash => "two_resource", :ref => 'Foo[two]' + three = stub 'three', :to_pson_data_hash => "three_resource", :ref => 'Foo[three]' @catalog.add_edge(one, two) @catalog.add_edge(two, three) - @catalog.edge(one, two).expects(:to_json).returns '"one_two_json"' - @catalog.edge(two, three).expects(:to_json).returns '"two_three_json"' + @catalog.edge(one, two ).expects(:to_pson_data_hash).returns "one_two_pson" + @catalog.edge(two, three).expects(:to_pson_data_hash).returns "two_three_pson" - json_output_should { |hash| JSON.parse(hash['data']['edges']).sort == %w{one_two_json two_three_json}.sort } - JSON.parse @catalog.to_json + PSON.parse(@catalog.to_pson,:create_additions => false)['data']['edges'].sort.should == %w{one_two_pson two_three_pson}.sort end end -describe Puppet::Resource::Catalog, "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Resource::Catalog, "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_result_should + def pson_result_should Puppet::Resource::Catalog.expects(:new).with { |hash| yield hash } end @@ -959,54 +958,54 @@ describe Puppet::Resource::Catalog, "when converting from json" do @data = { 'name' => "myhost" } - @json = { - 'json_class' => 'Puppet::Resource::Catalog', - 'data' => @data + @pson = { + 'document_type' => 'Puppet::Resource::Catalog', + 'data' => @data, + 'metadata' => {} } @catalog = Puppet::Resource::Catalog.new("myhost") Puppet::Resource::Catalog.stubs(:new).returns @catalog end - it "should be extended with the JSON utility module" do - Puppet::Resource::Catalog.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should be extended with the PSON utility module" do + Puppet::Resource::Catalog.metaclass.ancestors.should be_include(Puppet::Util::Pson) end it "should create it with the provided name" do Puppet::Resource::Catalog.expects(:new).with('myhost').returns @catalog - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set the provided version on the catalog if one is set" do @data['version'] = 50 @catalog.expects(:version=).with(@data['version']) - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set any provided tags on the catalog" do @data['tags'] = %w{one two} @catalog.expects(:tag).with("one", "two") - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it 'should convert the resources list into resources and add each of them' do @data['resources'] = [Puppet::Resource.new(:file, "/foo"), Puppet::Resource.new(:file, "/bar")] @catalog.expects(:add_resource).times(2).with { |res| res.type == "File" } - - JSON.parse @json.to_json + PSON.parse @pson.to_pson end - it 'should convert resources even if they do not include "json_class" information' do + it 'should convert resources even if they do not include "type" information' do @data['resources'] = [Puppet::Resource.new(:file, "/foo")] - @data['resources'][0].expects(:to_json).returns "{\"title\":\"\\/foo\",\"tags\":[\"file\"],\"type\":\"File\"}" + @data['resources'][0].expects(:to_pson).returns '{"title":"/foo","tags":["file"],"type":"File"}' @catalog.expects(:add_resource).with { |res| res.type == "File" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it 'should convert the edges list into edges and add each of them' do @@ -1020,12 +1019,12 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.event == "one" } @catalog.expects(:add_edge).with { |edge| edge.event == "two" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end - it "should be able to convert relationships that do not include 'json_class' information" do + it "should be able to convert relationships that do not include 'type' information" do one = Puppet::Relationship.new("osource", "otarget", :event => "one", :callback => "refresh") - one.expects(:to_json).returns "{\"event\":\"one\",\"callback\":\"refresh\",\"source\":\"osource\",\"target\":\"otarget\"}" + one.expects(:to_pson).returns "{\"event\":\"one\",\"callback\":\"refresh\",\"source\":\"osource\",\"target\":\"otarget\"}" @data['edges'] = [one] @@ -1033,7 +1032,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.event == "one" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set the source and target for each edge to the actual resource" do @@ -1046,7 +1045,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.source == "source_resource" and edge.target == "target_resource" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should fail if the source resource cannot be found" do @@ -1057,7 +1056,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:resource).with("source").returns(nil) @catalog.stubs(:resource).with("target").returns("target_resource") - lambda { JSON.parse @json.to_json }.should raise_error(ArgumentError) + lambda { PSON.parse @pson.to_pson }.should raise_error(ArgumentError) end it "should fail if the target resource cannot be found" do @@ -1068,6 +1067,6 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.stubs(:resource).with("source").returns("source_resource") @catalog.expects(:resource).with("target").returns(nil) - lambda { JSON.parse @json.to_json }.should raise_error(ArgumentError) + lambda { PSON.parse @pson.to_pson }.should raise_error(ArgumentError) end end diff --git a/spec/unit/util/json.rb b/spec/unit/util/json.rb index 5ef5dc6ac..ce9648616 100755 --- a/spec/unit/util/json.rb +++ b/spec/unit/util/json.rb @@ -2,20 +2,20 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } -require 'puppet/util/json' +require 'puppet/util/pson' -class JsonUtil - include Puppet::Util::Json +class PsonUtil + include Puppet::Util::Pson end -describe Puppet::Util::Json do +describe Puppet::Util::Pson do it "should fail if no data is provided" do - lambda { JsonUtil.new.json_create("json_class" => "foo") }.should raise_error(ArgumentError) + lambda { PsonUtil.new.pson_create("type" => "foo") }.should raise_error(ArgumentError) end - it "should call 'from_json' with the provided data" do - json = JsonUtil.new - json.expects(:from_json).with("mydata") - json.json_create("json_class" => "foo", "data" => "mydata") + it "should call 'from_pson' with the provided data" do + pson = PsonUtil.new + pson.expects(:from_pson).with("mydata") + pson.pson_create("type" => "foo", "data" => "mydata") end end |
