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

Normalize file resource titles #176

Merged
merged 2 commits into from
Mar 5, 2018
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
19 changes: 17 additions & 2 deletions lib/octocatalog-diff/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,32 @@ def resources_missing_from_catalog(resources_to_check)
unless res =~ /\A([\w:]+)\[(.+)\]\z/
raise ArgumentError, "Resource #{res} is not in the expected format"
end
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil?

type = Regexp.last_match(1)
title = normalized_title(Regexp.last_match(2), type)
resource(type: type, title: title).nil?
end
end

# Private method: Given a title string, normalize it according to the rules
# used by puppet 4.10.x for file resource title normalization:
# https://github.com/puppetlabs/puppet/blob/4.10.x/lib/puppet/type/file.rb#L42
def normalized_title(title_string, type)
return title_string if type != 'File'

matches = title_string.match(%r{^(?<normalized_path>/|.+:/|.*[^/])/*\Z}m)
matches[:normalized_path] || title_string
end

# Private method: Build the resource hash to be used used for O(1) lookups by type and title.
# This method is called the first time the resource hash is accessed.
def build_resource_hash
@resource_hash = {}
resources.each do |resource|
@resource_hash[resource['type']] ||= {}
@resource_hash[resource['type']][resource['title']] = resource

title = normalized_title(resource['title'], resource['type'])
@resource_hash[resource['type']][title] = resource

if resource.key?('parameters') && resource['parameters'].key?('alias')
@resource_hash[resource['type']][resource['parameters']['alias']] = resource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
classes:
- test::file_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class test::file_tests {
file { '/foo':
ensure => directory,
}

file { '/bar':
ensure => directory,
require => File['/foo/'],
}
}
20 changes: 20 additions & 0 deletions spec/octocatalog-diff/integration/reference_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ def self.catalog_contains_resource(result, type, title)
end
end

context 'with valid files that have trailing slashes' do
before(:all) do
@result = OctocatalogDiff::Spec.reference_validation_catalog('working-file', %w(require))
end

it 'should succeed' do
expect(@result.exitcode).to eq(0)
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.zero?
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'File', '/foo')).to eq(true)
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'File', '/bar')).to eq(true)
end
end

context 'with broken subscribe' do
before(:all) do
@result = OctocatalogDiff::Spec.reference_validation_catalog('broken-subscribe', %w(subscribe))
Expand Down
17 changes: 17 additions & 0 deletions spec/octocatalog-diff/tests/catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,14 @@
'alias' => 'the exec',
'command' => '/bin/true'
}
},
{
'type' => 'File',
'title' => '/foo/'
},
{
'type' => 'File',
'title' => '/bar'
}
]
described_object = described_class.allocate
Expand All @@ -636,5 +644,14 @@
it 'should contain the entry for the aliased resource' do
expect(@resource_hash['Exec']['the exec']).to be_a_kind_of(Hash)
end

it 'should normalize trailing slashes on file resources' do
expect(@resource_hash['File']['/foo']).to be_a_kind_of(Hash)
expect(@resource_hash['File']['/foo/']).to eq(nil)
end

it 'should not otherwise touch file resources that do not need to be normalized' do
expect(@resource_hash['File']['/bar']).to be_a_kind_of(Hash)
end
end
end