Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate references consider aliased resources too #43

Merged
merged 6 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/octocatalog-diff/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ def build_resource_hash
resources.each do |resource|
@resource_hash[resource['type']] ||= {}
@resource_hash[resource['type']][resource['title']] = resource

if resource.key?('parameters') && resource['parameters'].key?('alias')
@resource_hash[resource['type']][resource['parameters']['alias']] = resource
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
classes:
- test::alias_callers
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
classes:
- test::alias_callers
- test::alias_targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class test::alias_callers {
exec { 'before alias caller':
command => '/bin/true',
before => Exec['before alias target'],
}

exec { 'notify alias caller':
command => '/bin/true',
before => Exec['notify alias target'],
}

exec { 'require alias caller':
command => '/bin/true',
before => Exec['require alias target'],
}

exec { 'subscribe alias caller':
command => '/bin/true',
before => Exec['subscribe alias target'],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class test::alias_targets {
exec { 'the before alias target':
command => '/bin/true',
alias => 'before alias target',
}

exec { 'the notify alias target':
command => '/bin/true',
alias => 'notify alias target',
}

exec { 'the require alias target':
command => '/bin/true',
alias => 'require alias target',
}

exec { 'the subscribe alias target':
command => '/bin/true',
alias => 'subscribe alias target',
}
}
45 changes: 45 additions & 0 deletions spec/octocatalog-diff/integration/reference_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,51 @@ def self.catalog_contains_resource(result, type, title)
end
end

describe 'validation of alias references' do
context 'with valid catalog' do
before(:all) do
@result = OctocatalogDiff::Spec.reference_validation_catalog('working-alias', %w(before require subscribe notify))
end

it 'should succeed' do
expect(@result.exitcode).to eq(2)
end

it 'should not raise any exceptions' do
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
end

it 'should contain representative resources' do
pending 'Catalog failed' unless @result.exitcode == 2
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias caller')).to eq(true)
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias target')).to eq(true)
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'the before alias target')).to eq(true)
end
end

context 'with broken references' do
before(:all) do
@result = OctocatalogDiff::Spec.reference_validation_catalog('broken-alias', %w(before require subscribe notify))
end

it 'should not succeed' do
expect(@result.exitcode).to eq(-1), OctocatalogDiff::Integration.format_exception(@result)
end

it 'should raise ReferenceValidationError' do
expect(@result.exception).to be_a_kind_of(OctocatalogDiff::Catalog::ReferenceValidationError)
end

it 'should have formatted error messages' do
msg = @result.exception.message
expect(msg).to match(/exec\[before alias caller\] -> before\[Exec\[before alias target\]\]/)
expect(msg).to match(/exec\[notify alias caller\] -> before\[Exec\[notify alias target\]\]/)
expect(msg).to match(/exec\[require alias caller\] -> before\[Exec\[require alias target\]\]/)
expect(msg).to match(/exec\[subscribe alias caller\] -> before\[Exec\[subscribe alias target\]\]/)
end
end
end

describe 'validation of references in catalog-diff' do
context 'with valid catalog' do
before(:all) do
Expand Down
30 changes: 30 additions & 0 deletions spec/octocatalog-diff/tests/catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,34 @@
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Catalog::ReferenceValidationError, error_str)
end
end

describe '#build_resource_hash' do
before(:each) do
resource_array = [
{
'type' => 'Exec',
'title' => 'title of the exec',
'file' => '/etc/puppetlabs/code/site/manifests/init.pp',
'line' => 6,
'exported' => false,
'parameters' => {
'alias' => 'the exec',
'command' => '/bin/true'
}
}
]
described_object = described_class.allocate
expect(described_object).to receive(:resources).and_return(resource_array)
described_object.send(:build_resource_hash)
@resource_hash = described_object.instance_variable_get(:'@resource_hash')
end

it 'should contain the entry for the titled resource' do
expect(@resource_hash['Exec']['title of the exec']).to be_a_kind_of(Hash)
end

it 'should contain the entry for the aliased resource' do
expect(@resource_hash['Exec']['the exec']).to be_a_kind_of(Hash)
end
end
end