Create validation function; create test based on broken subscribe call

This commit is contained in:
Kevin Paulisse 2016-12-21 20:13:18 -05:00
Родитель c7a8dc6eb7
Коммит 76da47ae7d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 66DA91D838188671
6 изменённых файлов: 89 добавлений и 2 удалений

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

@ -234,6 +234,7 @@ module OctocatalogDiff
# @return [Boolean] true if catalog is valid, false otherwise
def catalog_validator(catalog = nil, _logger = @logger)
return false unless catalog.is_a?(OctocatalogDiff::Catalog)
catalog.validate_references
catalog.valid?
end
end

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

@ -174,8 +174,54 @@ module OctocatalogDiff
!@catalog.nil?
end
# Determine if all of the (before, notify, require, subscribe) targets are actually in the catalog.
# Raise a ReferenceValidationError for any found to be missing.
# Uses @options[:validate_references] to influence which references are checked.
def validate_references
# Skip out early if no reference validation has been requested.
unless @options[:validate_references].is_a?(Array) && @options[:validate_references].any?
return
end
# Iterate over all the resources and check each one that has one of the attributes being checked.
# Keep track of all references that are missing for ultimate inclusion in the error message.
missing = []
resources.each do |x|
@options[:validate_references].each do |r|
next unless x.key?('parameters')
next unless x['parameters'].key?(r)
next if catalog_contains_all_resources?(x['parameters'][r])
missing << { source: x, target_type: r, target_value: x['parameters'][r] }
end
end
return if missing.empty?
# At this point there is at least one broken/missing reference. Format an error message and
# raise.
message = missing.inspect
raise ReferenceValidationError, message
end
private
# Private method: Determine if a catalog contains resource or resources, which may
# have been passed in as an array or a string. (If multiple resources are passed, the
# logic is AND.)
# @param resources_to_check [String / Array] Resources to check
# @return [Boolean] true if all resources are in catalog; false otherwise
def catalog_contains_all_resources?(resources_to_check)
rtc_array = resources_to_check.is_a?(Array) ? resources_to_check : [resources_to_check]
rtc_array.each do |res|
unless res =~ /\A([\w:]+)\[(.+)\]\z/
raise CatalogError, "Resource #{res} is not in the expected format"
end
type = Regexp.last_match(1)
title = Regexp.last_match(2)
return false if resource(type: type, title: title).nil?
end
true
end
# Private method: Choose backend based on passed-in options
# @param options [Hash] Options passed into constructor
# @return [Object] OctocatalogDiff::Catalog::<whatever> object

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

@ -0,0 +1,9 @@
---
classes:
- test::before_callers
- test::before_targets
- test::notify_callers
- test::notify_targets
- test::require_callers
- test::require_targets
- test::subscribe_callers

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

@ -1,6 +1,22 @@
class test::subscribe_callers {
exec { 'subscribe caller':
exec { 'subscribe caller 1':
command => '/bin/true',
subscribe => Exec['subscribe target'],
}
exec { 'subscribe caller 2':
command => '/bin/true',
subscribe => [
Exec['subscribe target'],
Exec['subscribe target 2']
]
}
exec { 'subscribe caller 3':
command => '/bin/true',
subscribe => [
Exec['subscribe caller 1'],
Exec['subscribe target']
]
}
}

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

@ -2,4 +2,8 @@ class test::subscribe_targets {
exec { 'subscribe target':
command => '/bin/true',
}
exec { 'subscribe target 2':
command => '/bin/true',
}
}

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

@ -64,7 +64,18 @@ describe 'validation of references' do
end
end
context 'with broken require' do
context 'with broken subscribe' do
before(:all) do
@result = OctocatalogDiff::Spec.reference_validation_catalog('broken-subscribe', %w(subscribe))
end
it 'should not succeed' do
expect(@result.exitcode).to eq(-1), OctocatalogDiff::Integration.format_exception(@result)
end
it 'should raise ReferenceValidationError' do
expect(@result.exception).to be_a_kind_of(OctocatalogDiff::Catalog::ReferenceValidationError)
end
end
context 'with broken before' do