Skip to content

Commit b5979a6

Browse files
author
Kevin Paulisse
committed
Add unit/spec test for catalog validation
1 parent d96a797 commit b5979a6

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

lib/octocatalog-diff/catalog.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def resources
158158
# This is a bug condition
159159
# :nocov:
160160
raise "BUG: catalog has no data::resources or ::resources array. Please report this. #{@catalog.inspect}"
161-
# :nocov
161+
# :nocov:
162162
end
163163

164164
# This retrieves the number of retries necessary to compile the catalog. If the underlying catalog
@@ -224,14 +224,14 @@ def validate_references
224224
# Private method: Determine if a catalog contains resource or resources, which may
225225
# have been passed in as an array or a string. Return the references to resources
226226
# that are missing from the catalog. (An empty array would indicate all references
227-
# are present.
227+
# are present.)
228228
# @param resources_to_check [String / Array] Resources to check
229229
# @return [Array] References that are missing from catalog
230230
def remove_existing_resources(resources_to_check)
231231
rtc_array = resources_to_check.is_a?(Array) ? resources_to_check : [resources_to_check]
232232
rtc_array.map do |res|
233233
unless res =~ /\A([\w:]+)\[(.+)\]\z/
234-
raise CatalogError, "Resource #{res} is not in the expected format"
234+
raise ArgumentError, "Resource #{res} is not in the expected format"
235235
end
236236
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil? ? res : nil
237237
end.compact

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,75 @@
408408
end
409409
end
410410
end
411+
412+
describe OctocatalogDiff::Catalog do
413+
describe '#remove_existing_resources' do
414+
let(:catalog) do
415+
opts = {
416+
compare_file_text: false,
417+
node: 'my.rspec.node',
418+
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'))
419+
}
420+
OctocatalogDiff::Catalog.new(opts)
421+
end
422+
423+
it 'should raise error if resource is not in expected format' do
424+
test_arg = ['Foo-Bar']
425+
expect { catalog.send(:remove_existing_resources, test_arg) }.to raise_error(ArgumentError, /Resource Foo-Bar /)
426+
end
427+
428+
it 'should return full array when no matches' do
429+
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
430+
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(nil)
431+
test_arg = ['Foo[bar]', 'Baz[biff]']
432+
result = catalog.send(:remove_existing_resources, test_arg)
433+
expect(result).to eq(['Foo[bar]', 'Baz[biff]'])
434+
end
435+
436+
it 'should remove matching entries' do
437+
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
438+
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
439+
test_arg = ['Foo[bar]', 'Baz[biff]']
440+
result = catalog.send(:remove_existing_resources, test_arg)
441+
expect(result).to eq(['Foo[bar]'])
442+
end
443+
444+
it 'should return empty array with all matches' do
445+
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(true)
446+
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
447+
test_arg = ['Foo[bar]', 'Baz[biff]']
448+
result = catalog.send(:remove_existing_resources, test_arg)
449+
expect(result).to eq([])
450+
end
451+
end
452+
453+
describe '#validate_references' do
454+
it 'should return nil if no reference validation is requested' do
455+
opts = {
456+
compare_file_text: false,
457+
node: 'my.rspec.node',
458+
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
459+
}
460+
catalog = OctocatalogDiff::Catalog.new(opts)
461+
result = catalog.validate_references
462+
expect(result).to be_nil
463+
end
464+
465+
it 'should raise error if reference validation is requested' do
466+
opts = {
467+
compare_file_text: false,
468+
validate_references: %w(before notify require subscribe),
469+
node: 'my.rspec.node',
470+
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
471+
}
472+
catalog = OctocatalogDiff::Catalog.new(opts)
473+
error_str = [
474+
'Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]]',
475+
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]',
476+
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target 2]]',
477+
'exec[subscribe caller 3] -> subscribe[Exec[subscribe target]]'
478+
].join('; ')
479+
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Catalog::ReferenceValidationError, error_str)
480+
end
481+
end
482+
end

0 commit comments

Comments
 (0)