Skip to content

Commit 8f78fe8

Browse files
committed
Merge remote-tracking branch 'ahayworth/resource_validation' into kpaulisse-ruby-version-test-update
2 parents 3b32835 + 60dedfe commit 8f78fe8

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

lib/octocatalog-diff/catalog.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,32 @@ def resources_missing_from_catalog(resources_to_check)
304304
unless res =~ /\A([\w:]+)\[(.+)\]\z/
305305
raise ArgumentError, "Resource #{res} is not in the expected format"
306306
end
307-
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil?
307+
308+
type = Regexp.last_match(1)
309+
title = normalized_title(Regexp.last_match(2), type)
310+
resource(type: type, title: title).nil?
308311
end
309312
end
310313

314+
# Private method: Given a title string, normalize it according to the rules
315+
# used by puppet 4.10.x for file resource title normalization:
316+
# https://github.com/puppetlabs/puppet/blob/4.10.x/lib/puppet/type/file.rb#L42
317+
def normalized_title(title_string, type)
318+
return title_string if type != 'File'
319+
320+
matches = title_string.match(%r{^(?<normalized_path>/|.+:/|.*[^/])/*\Z}m)
321+
matches[:normalized_path] || title_string
322+
end
323+
311324
# Private method: Build the resource hash to be used used for O(1) lookups by type and title.
312325
# This method is called the first time the resource hash is accessed.
313326
def build_resource_hash
314327
@resource_hash = {}
315328
resources.each do |resource|
316329
@resource_hash[resource['type']] ||= {}
317-
@resource_hash[resource['type']][resource['title']] = resource
330+
331+
title = normalized_title(resource['title'], resource['type'])
332+
@resource_hash[resource['type']][title] = resource
318333

319334
if resource.key?('parameters') && resource['parameters'].key?('alias')
320335
@resource_hash[resource['type']][resource['parameters']['alias']] = resource
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
classes:
3+
- test::file_tests
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class test::file_tests {
2+
file { '/foo':
3+
ensure => directory,
4+
}
5+
6+
file { '/bar':
7+
ensure => directory,
8+
require => File['/foo/'],
9+
}
10+
}

spec/octocatalog-diff/integration/reference_validation_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ def self.catalog_contains_resource(result, type, title)
9393
end
9494
end
9595

96+
context 'with valid files that have trailing slashes' do
97+
before(:all) do
98+
@result = OctocatalogDiff::Spec.reference_validation_catalog('working-file', %w(require))
99+
end
100+
101+
it 'should succeed' do
102+
expect(@result.exitcode).to eq(0)
103+
end
104+
105+
it 'should not raise any exceptions' do
106+
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
107+
end
108+
109+
it 'should contain representative resources' do
110+
pending 'Catalog failed' unless @result.exitcode.zero?
111+
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'File', '/foo')).to eq(true)
112+
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'File', '/bar')).to eq(true)
113+
end
114+
end
115+
96116
context 'with broken subscribe' do
97117
before(:all) do
98118
@result = OctocatalogDiff::Spec.reference_validation_catalog('broken-subscribe', %w(subscribe))

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@
621621
'alias' => 'the exec',
622622
'command' => '/bin/true'
623623
}
624+
},
625+
{
626+
'type' => 'File',
627+
'title' => '/foo/'
628+
},
629+
{
630+
'type' => 'File',
631+
'title' => '/bar'
624632
}
625633
]
626634
described_object = described_class.allocate
@@ -636,5 +644,14 @@
636644
it 'should contain the entry for the aliased resource' do
637645
expect(@resource_hash['Exec']['the exec']).to be_a_kind_of(Hash)
638646
end
647+
648+
it 'should normalize trailing slashes on file resources' do
649+
expect(@resource_hash['File']['/foo']).to be_a_kind_of(Hash)
650+
expect(@resource_hash['File']['/foo/']).to eq(nil)
651+
end
652+
653+
it 'should not otherwise touch file resources that do not need to be normalized' do
654+
expect(@resource_hash['File']['/bar']).to be_a_kind_of(Hash)
655+
end
639656
end
640657
end

0 commit comments

Comments
 (0)