Skip to content

Commit 15049e3

Browse files
authored
Merge pull request #43 from github/kpaulisse-validate-references-alias
Validate references consider aliased resources too
2 parents d2fa843 + 9185848 commit 15049e3

7 files changed

Lines changed: 128 additions & 0 deletions

File tree

lib/octocatalog-diff/catalog.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ def build_resource_hash
272272
resources.each do |resource|
273273
@resource_hash[resource['type']] ||= {}
274274
@resource_hash[resource['type']][resource['title']] = resource
275+
276+
if resource.key?('parameters') && resource['parameters'].key?('alias')
277+
@resource_hash[resource['type']][resource['parameters']['alias']] = resource
278+
end
275279
end
276280
end
277281
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
classes:
3+
- test::alias_callers
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
classes:
3+
- test::alias_callers
4+
- test::alias_targets
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class test::alias_callers {
2+
exec { 'before alias caller':
3+
command => '/bin/true',
4+
before => Exec['before alias target'],
5+
}
6+
7+
exec { 'notify alias caller':
8+
command => '/bin/true',
9+
before => Exec['notify alias target'],
10+
}
11+
12+
exec { 'require alias caller':
13+
command => '/bin/true',
14+
before => Exec['require alias target'],
15+
}
16+
17+
exec { 'subscribe alias caller':
18+
command => '/bin/true',
19+
before => Exec['subscribe alias target'],
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class test::alias_targets {
2+
exec { 'the before alias target':
3+
command => '/bin/true',
4+
alias => 'before alias target',
5+
}
6+
7+
exec { 'the notify alias target':
8+
command => '/bin/true',
9+
alias => 'notify alias target',
10+
}
11+
12+
exec { 'the require alias target':
13+
command => '/bin/true',
14+
alias => 'require alias target',
15+
}
16+
17+
exec { 'the subscribe alias target':
18+
command => '/bin/true',
19+
alias => 'subscribe alias target',
20+
}
21+
}

spec/octocatalog-diff/integration/reference_validation_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,51 @@ def self.catalog_contains_resource(result, type, title)
188188
end
189189
end
190190

191+
describe 'validation of alias references' do
192+
context 'with valid catalog' do
193+
before(:all) do
194+
@result = OctocatalogDiff::Spec.reference_validation_catalog('working-alias', %w(before require subscribe notify))
195+
end
196+
197+
it 'should succeed' do
198+
expect(@result.exitcode).to eq(2)
199+
end
200+
201+
it 'should not raise any exceptions' do
202+
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
203+
end
204+
205+
it 'should contain representative resources' do
206+
pending 'Catalog failed' unless @result.exitcode == 2
207+
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias caller')).to eq(true)
208+
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias target')).to eq(true)
209+
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'the before alias target')).to eq(true)
210+
end
211+
end
212+
213+
context 'with broken references' do
214+
before(:all) do
215+
@result = OctocatalogDiff::Spec.reference_validation_catalog('broken-alias', %w(before require subscribe notify))
216+
end
217+
218+
it 'should not succeed' do
219+
expect(@result.exitcode).to eq(-1), OctocatalogDiff::Integration.format_exception(@result)
220+
end
221+
222+
it 'should raise ReferenceValidationError' do
223+
expect(@result.exception).to be_a_kind_of(OctocatalogDiff::Catalog::ReferenceValidationError)
224+
end
225+
226+
it 'should have formatted error messages' do
227+
msg = @result.exception.message
228+
expect(msg).to match(/exec\[before alias caller\] -> before\[Exec\[before alias target\]\]/)
229+
expect(msg).to match(/exec\[notify alias caller\] -> before\[Exec\[notify alias target\]\]/)
230+
expect(msg).to match(/exec\[require alias caller\] -> before\[Exec\[require alias target\]\]/)
231+
expect(msg).to match(/exec\[subscribe alias caller\] -> before\[Exec\[subscribe alias target\]\]/)
232+
end
233+
end
234+
end
235+
191236
describe 'validation of references in catalog-diff' do
192237
context 'with valid catalog' do
193238
before(:all) do

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,34 @@
479479
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Catalog::ReferenceValidationError, error_str)
480480
end
481481
end
482+
483+
describe '#build_resource_hash' do
484+
before(:each) do
485+
resource_array = [
486+
{
487+
'type' => 'Exec',
488+
'title' => 'title of the exec',
489+
'file' => '/etc/puppetlabs/code/site/manifests/init.pp',
490+
'line' => 6,
491+
'exported' => false,
492+
'parameters' => {
493+
'alias' => 'the exec',
494+
'command' => '/bin/true'
495+
}
496+
}
497+
]
498+
described_object = described_class.allocate
499+
expect(described_object).to receive(:resources).and_return(resource_array)
500+
described_object.send(:build_resource_hash)
501+
@resource_hash = described_object.instance_variable_get(:'@resource_hash')
502+
end
503+
504+
it 'should contain the entry for the titled resource' do
505+
expect(@resource_hash['Exec']['title of the exec']).to be_a_kind_of(Hash)
506+
end
507+
508+
it 'should contain the entry for the aliased resource' do
509+
expect(@resource_hash['Exec']['the exec']).to be_a_kind_of(Hash)
510+
end
511+
end
482512
end

0 commit comments

Comments
 (0)