Skip to content

Commit daadedd

Browse files
committed
Eliminate ReferenceValidationError to mimic Puppet 5 behavior
1 parent 9181834 commit daadedd

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/octocatalog-diff/catalog.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ class Catalog
3333
def initialize(options = {})
3434
@options = options
3535

36+
# The compilation directory can be overridden, e.g. when testing
37+
@override_compilation_dir = options[:compilation_dir]
38+
3639
# Call appropriate backend for catalog generation
3740
@catalog_obj = backend(options)
3841

3942
# The catalog is not built yet, except if the backend has no build method
4043
@built = false
4144
build unless @catalog_obj.respond_to?(:build)
4245

43-
# The compilation directory can be overridden, e.g. when testing
44-
@override_compilation_dir = nil
46+
# Keep track of whether references have been validated yet
47+
@references_validated = false
4548
end
4649

4750
# Build catalog - this method needs to be called to build the catalog. It is separate due to
@@ -69,6 +72,10 @@ def build(logger = Logger.new(StringIO.new))
6972

7073
# Perform post-generation processing of the catalog
7174
return unless valid?
75+
76+
validate_references
77+
return unless valid?
78+
7279
unless @catalog_obj.respond_to?(:convert_file_resources) && @catalog_obj.convert_file_resources == false
7380
if @options.fetch(:compare_file_text, false)
7481
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(self, environment)
@@ -182,6 +189,10 @@ def valid?
182189
# Raise a OctocatalogDiff::Errors::ReferenceValidationError for any found to be missing.
183190
# Uses @options[:validate_references] to influence which references are checked.
184191
def validate_references
192+
# If we've already done the validation, don't do it again
193+
return if @references_validated
194+
@references_validated = true
195+
185196
# Skip out early if no reference validation has been requested.
186197
unless @options[:validate_references].is_a?(Array) && @options[:validate_references].any?
187198
return
@@ -208,7 +219,7 @@ def validate_references
208219
# At this point there is at least one broken/missing reference. Format an error message and raise.
209220
errors = format_missing_references(missing)
210221
plural = errors =~ /;/ ? 's' : ''
211-
raise OctocatalogDiff::Errors::ReferenceValidationError, "Catalog has broken reference#{plural}: #{errors}"
222+
self.error_message = "Catalog has broken reference#{plural}: #{errors}"
212223
end
213224

214225
private

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,31 +468,33 @@
468468
compare_file_text: false,
469469
validate_references: %w(before notify require subscribe),
470470
node: 'my.rspec.node',
471-
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
471+
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json')),
472+
compilation_dir: '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20161223-46780-x10xaf/environments/production'
472473
}
474+
allow_any_instance_of(OctocatalogDiff::Catalog).to receive(:puppet_version).and_return('5.0.0')
473475
catalog = OctocatalogDiff::Catalog.new(opts)
474-
allow(catalog).to receive(:puppet_version).and_return('5.0.0')
475-
expect { catalog.validate_references }.not_to raise_error
476+
expect(catalog.valid?).to eq(true)
476477
end
477478

478-
it 'should raise error if reference validation is requested' do
479+
it 'should indicate the invalid catalog if reference validation is requested' do
479480
opts = {
480481
compare_file_text: false,
481482
validate_references: %w(before notify require subscribe),
482483
node: 'my.rspec.node',
483-
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
484+
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json')),
485+
compilation_dir: '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20161223-46780-x10xaf/environments/production'
484486
}
487+
allow_any_instance_of(OctocatalogDiff::Catalog).to receive(:puppet_version).and_return('4.10.0')
485488
catalog = OctocatalogDiff::Catalog.new(opts)
486-
allow(catalog).to receive(:puppet_version).and_return('4.10.0')
487-
catalog.compilation_dir = '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20161223-46780-x10xaf/environments/production'
488489
error_str = [
489490
'Catalog has broken references: exec[subscribe caller 1](modules/test/manifests/subscribe_callers.pp:2)' \
490491
' -> subscribe[Exec[subscribe target]]',
491492
'exec[subscribe caller 2](modules/test/manifests/subscribe_callers.pp:7) -> subscribe[Exec[subscribe target]]',
492493
'exec[subscribe caller 2](modules/test/manifests/subscribe_callers.pp:7) -> subscribe[Exec[subscribe target 2]]',
493494
'exec[subscribe caller 3](modules/test/manifests/subscribe_callers.pp:15) -> subscribe[Exec[subscribe target]]'
494495
].join('; ')
495-
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Errors::ReferenceValidationError, error_str)
496+
expect(catalog.valid?).to eq(false)
497+
expect(catalog.error_message).to eq(error_str)
496498
end
497499
end
498500

0 commit comments

Comments
 (0)