Skip to content

Commit

Permalink
(PUP-4890) Add code_id accessor to the catalog
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshcooper committed Sep 17, 2015
1 parent 27a5949 commit 6eec468
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions api/docs/http_catalog.md
Expand Up @@ -58,6 +58,7 @@ escaping is still used/supported.
],
"name": "elmo.mydomain.com",
"version": 1377473054,
"code_id": null,
"environment": "production",
"resources": [
{
Expand Down
5 changes: 4 additions & 1 deletion api/schemas/catalog.json
Expand Up @@ -18,6 +18,9 @@
"version": {
"type": "integer"
},
"code_id": {
"type": ["string", "null"]
},
"environment": {
"type": "string"
},
Expand Down Expand Up @@ -90,6 +93,6 @@
}
}
},
"required": ["tags", "name", "version", "environment", "resources", "edges", "classes"],
"required": ["tags", "name", "version", "code_id", "environment", "resources", "edges", "classes"],
"additionalProperties": false
}
8 changes: 8 additions & 0 deletions lib/puppet/resource/catalog.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 },
Expand Down
29 changes: 21 additions & 8 deletions spec/unit/resource/catalog_spec.rb
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]",
Expand All @@ -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")

Expand Down

0 comments on commit 6eec468

Please sign in to comment.