From 6eec4684898f5aaaf21e6cbcc0bae50ead528135 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Thu, 17 Sep 2015 16:45:32 -0700 Subject: [PATCH] (PUP-4890) Add code_id accessor to the catalog This commit adds a code_id accessor to the catalog, updates the schema, api docs, and test. The code_id is a required attribute during serialization, but its value can be null (default) or a string. --- api/docs/http_catalog.md | 1 + api/schemas/catalog.json | 5 ++++- lib/puppet/resource/catalog.rb | 8 ++++++++ spec/unit/resource/catalog_spec.rb | 29 +++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/api/docs/http_catalog.md b/api/docs/http_catalog.md index e06adc29edc..4996f7635f2 100644 --- a/api/docs/http_catalog.md +++ b/api/docs/http_catalog.md @@ -58,6 +58,7 @@ escaping is still used/supported. ], "name": "elmo.mydomain.com", "version": 1377473054, + "code_id": null, "environment": "production", "resources": [ { diff --git a/api/schemas/catalog.json b/api/schemas/catalog.json index 57758571b56..caef553433e 100644 --- a/api/schemas/catalog.json +++ b/api/schemas/catalog.json @@ -18,6 +18,9 @@ "version": { "type": "integer" }, + "code_id": { + "type": ["string", "null"] + }, "environment": { "type": "string" }, @@ -90,6 +93,6 @@ } } }, - "required": ["tags", "name", "version", "environment", "resources", "edges", "classes"], + "required": ["tags", "name", "version", "code_id", "environment", "resources", "edges", "classes"], "additionalProperties": false } diff --git a/lib/puppet/resource/catalog.rb b/lib/puppet/resource/catalog.rb index a73a40d83f3..8200ca914fc 100644 --- a/lib/puppet/resource/catalog.rb +++ b/lib/puppet/resource/catalog.rb @@ -29,6 +29,9 @@ class DuplicateResourceError < Puppet::Error # is up to date. attr_accessor :version + # The id of the code input to the compiler. + attr_accessor :code_id + # How long this catalog took to retrieve. Used for reporting stats. attr_accessor :retrieval_duration @@ -351,6 +354,10 @@ def self.from_data_hash(data) result.version = version end + if code_id = data['code_id'] + result.code_id = code_id + end + if environment = data['environment'] result.environment = environment result.environment_instance = Puppet::Node::Environment.remote(environment.to_sym) @@ -391,6 +398,7 @@ def to_data_hash 'tags' => tags, 'name' => name, 'version' => version, + 'code_id' => code_id, 'environment' => environment.to_s, 'resources' => @resources.collect { |v| @resource_table[v].to_data_hash }, 'edges' => edges. collect { |e| e.to_data_hash }, diff --git a/spec/unit/resource/catalog_spec.rb b/spec/unit/resource/catalog_spec.rb index 19a5fb8866d..faf763b9092 100755 --- a/spec/unit/resource/catalog_spec.rb +++ b/spec/unit/resource/catalog_spec.rb @@ -86,6 +86,11 @@ expect(@catalog.server_version).to eq(5) end + it "defaults code_id to nil" do + catalog = Puppet::Resource::Catalog.new("host") + expect(catalog.code_id).to be_nil + end + describe "when compiling" do it "should accept tags" do config = Puppet::Resource::Catalog.new("mynode") @@ -782,17 +787,23 @@ class {'multi_param_class': @catalog = Puppet::Resource::Catalog.new("myhost") end - def pson_output_should - @catalog.class.expects(:from_data_hash).with { |hash| yield hash }.returns(:something) + { :name => 'myhost', + :version => 42, + :code_id => 'b59e5df0578ef411f773ee6c33d8073c50e7b8fe' + }.each do |param, value| + it "emits a #{param} equal to #{value.inspect}" do + @catalog.send(param.to_s + "=", value) + pson = PSON.parse(@catalog.to_pson) + + expect(pson[param.to_s]).to eq(@catalog.send(param)) + end end - [:name, :version, :classes].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) + it "emits an array of classes" do + @catalog.add_class('foo') + pson = PSON.parse(@catalog.to_pson) - pson_output_should { |hash| expect(hash[param.to_s]).to eq(@catalog.send(param)) } - Puppet::Resource::Catalog.from_data_hash PSON.parse @catalog.to_pson - end + expect(pson['classes']).to eq(['foo']) end it "should convert its resources to a PSON-encoded array and store it as the 'resources' data" do @@ -831,6 +842,7 @@ def pson_output_should it "should create it with the provided name" do @data['version'] = 50 + @data['code_id'] = 'b59e5df0578ef411f773ee6c33d8073c50e7b8fe' @data['tags'] = %w{one two} @data['classes'] = %w{one two} @data['edges'] = [Puppet::Relationship.new("File[/foo]", "File[/bar]", @@ -844,6 +856,7 @@ def pson_output_should expect(catalog.name).to eq('myhost') expect(catalog.version).to eq(@data['version']) + expect(catalog.code_id).to eq(@data['code_id']) expect(catalog).to be_tagged("one") expect(catalog).to be_tagged("two")