Add unit/spec test for catalog validation

This commit is contained in:
Kevin Paulisse 2016-12-23 12:39:23 -05:00
Родитель d96a797e11
Коммит b5979a67a2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 66DA91D838188671
2 изменённых файлов: 75 добавлений и 3 удалений

Просмотреть файл

@ -158,7 +158,7 @@ module OctocatalogDiff
# This is a bug condition
# :nocov:
raise "BUG: catalog has no data::resources or ::resources array. Please report this. #{@catalog.inspect}"
# :nocov
# :nocov:
end
# This retrieves the number of retries necessary to compile the catalog. If the underlying catalog
@ -224,14 +224,14 @@ module OctocatalogDiff
# Private method: Determine if a catalog contains resource or resources, which may
# have been passed in as an array or a string. Return the references to resources
# that are missing from the catalog. (An empty array would indicate all references
# are present.
# are present.)
# @param resources_to_check [String / Array] Resources to check
# @return [Array] References that are missing from catalog
def remove_existing_resources(resources_to_check)
rtc_array = resources_to_check.is_a?(Array) ? resources_to_check : [resources_to_check]
rtc_array.map do |res|
unless res =~ /\A([\w:]+)\[(.+)\]\z/
raise CatalogError, "Resource #{res} is not in the expected format"
raise ArgumentError, "Resource #{res} is not in the expected format"
end
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil? ? res : nil
end.compact

Просмотреть файл

@ -408,3 +408,75 @@ describe OctocatalogDiff::Catalog do
end
end
end
describe OctocatalogDiff::Catalog do
describe '#remove_existing_resources' do
let(:catalog) do
opts = {
compare_file_text: false,
node: 'my.rspec.node',
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'))
}
OctocatalogDiff::Catalog.new(opts)
end
it 'should raise error if resource is not in expected format' do
test_arg = ['Foo-Bar']
expect { catalog.send(:remove_existing_resources, test_arg) }.to raise_error(ArgumentError, /Resource Foo-Bar /)
end
it 'should return full array when no matches' do
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(nil)
test_arg = ['Foo[bar]', 'Baz[biff]']
result = catalog.send(:remove_existing_resources, test_arg)
expect(result).to eq(['Foo[bar]', 'Baz[biff]'])
end
it 'should remove matching entries' do
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
test_arg = ['Foo[bar]', 'Baz[biff]']
result = catalog.send(:remove_existing_resources, test_arg)
expect(result).to eq(['Foo[bar]'])
end
it 'should return empty array with all matches' do
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(true)
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
test_arg = ['Foo[bar]', 'Baz[biff]']
result = catalog.send(:remove_existing_resources, test_arg)
expect(result).to eq([])
end
end
describe '#validate_references' do
it 'should return nil if no reference validation is requested' do
opts = {
compare_file_text: false,
node: 'my.rspec.node',
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
}
catalog = OctocatalogDiff::Catalog.new(opts)
result = catalog.validate_references
expect(result).to be_nil
end
it 'should raise error if reference validation is requested' do
opts = {
compare_file_text: false,
validate_references: %w(before notify require subscribe),
node: 'my.rspec.node',
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
}
catalog = OctocatalogDiff::Catalog.new(opts)
error_str = [
'Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]]',
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]',
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target 2]]',
'exec[subscribe caller 3] -> subscribe[Exec[subscribe target]]'
].join('; ')
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Catalog::ReferenceValidationError, error_str)
end
end
end