inspec/test/unit/resources/aws_sns_topic_test.rb

129 lines
4.4 KiB
Ruby
Raw Normal View History

require "helper"
require "inspec/resource"
require "resources/aws/aws_sns_topic"
require "resource_support/aws"
# MSNB = MockSnsBackend
# Abbreviation not used outside this file
#=============================================================================#
# Constructor Tests
#=============================================================================#
class AwsSnsTopicConstructorTest < Minitest::Test
def setup
DRY up AWS resource implementation and test backend implementations (#121) * Standardize requires in unit tests Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Standardize requires in resources Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Move AWS connection hook into non-resource library area Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Add an AWS resource mixin, pushing constructor out to it Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push resource param name recognition into mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push exists predicate up to mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rename base.rb to be resource_mixin for clarity Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Separate the backend from its factory, and push it out into a class mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push BackendFactory up into the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * De-linting Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove aws_conn require from LMF and CloudWatch Alarm filters Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Use resource mixin for Cloudwatch Alarm Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rework LMF to use the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rubocop. Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove SDK load from connection.rb; that happens in aws.rb now Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Mixin should default to allowing empty resource params Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Update LMF to enforce params being required Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-12-04 18:32:13 +00:00
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
end
def test_constructor_some_args_required
assert_raises(ArgumentError) { AwsSnsTopic.new }
end
def test_constructor_accepts_scalar_arn
AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:some-topic")
end
def test_constructor_accepts_arn_as_hash
AwsSnsTopic.new(arn: "arn:aws:sns:us-east-1:123456789012:some-topic")
end
def test_constructor_rejects_unrecognized_resource_params
assert_raises(ArgumentError) { AwsSnsTopic.new(beep: "boop") }
end
def test_constructor_rejects_non_arn_formats
[
"not-even-like-an-arn",
"arn:::::", # Empty
"arn::::::", # Too many colons
"arn:aws::us-east-1:123456789012:some-topic", # Omits SNS service
"arn::sns:us-east-1:123456789012:some-topic", # Omits partition
"arn:aws:sns:*:123456789012:some-topic", # All-region - not permitted for lookup
"arn:aws:sns:us-east-1::some-topic", # Default account - not permitted for lookup
].each do |example|
assert_raises(ArgumentError) { AwsSnsTopic.new(arn: example) }
end
end
end
#=============================================================================#
# Search / Recall
#=============================================================================#
class AwsSnsTopicRecallTest < Minitest::Test
# No setup here - each test needs to explicitly declare
# what they want from the backend.
def test_recall_no_match_is_no_exception
DRY up AWS resource implementation and test backend implementations (#121) * Standardize requires in unit tests Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Standardize requires in resources Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Move AWS connection hook into non-resource library area Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Add an AWS resource mixin, pushing constructor out to it Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push resource param name recognition into mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push exists predicate up to mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rename base.rb to be resource_mixin for clarity Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Separate the backend from its factory, and push it out into a class mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push BackendFactory up into the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * De-linting Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove aws_conn require from LMF and CloudWatch Alarm filters Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Use resource mixin for Cloudwatch Alarm Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rework LMF to use the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rubocop. Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove SDK load from connection.rb; that happens in aws.rb now Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Mixin should default to allowing empty resource params Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Update LMF to enforce params being required Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-12-04 18:32:13 +00:00
AwsSnsTopic::BackendFactory.select(AwsMSNB::Miss)
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:nope")
refute topic.exists?
end
def test_recall_match_single_result_works
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
assert topic.exists?
end
end
#=============================================================================#
# Properties
#=============================================================================#
class AwsSnsTopicPropertiesTest < Minitest::Test
# No setup here - each test needs to explicitly declare
# what they want from the backend.
#---------------------------------------
# confirmed_subscription_count
#---------------------------------------
def test_prop_conf_sub_count_zero
DRY up AWS resource implementation and test backend implementations (#121) * Standardize requires in unit tests Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Standardize requires in resources Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Move AWS connection hook into non-resource library area Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Add an AWS resource mixin, pushing constructor out to it Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push resource param name recognition into mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push exists predicate up to mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rename base.rb to be resource_mixin for clarity Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Separate the backend from its factory, and push it out into a class mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push BackendFactory up into the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * De-linting Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove aws_conn require from LMF and CloudWatch Alarm filters Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Use resource mixin for Cloudwatch Alarm Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rework LMF to use the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rubocop. Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove SDK load from connection.rb; that happens in aws.rb now Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Mixin should default to allowing empty resource params Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Update LMF to enforce params being required Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-12-04 18:32:13 +00:00
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
assert_equal(0, topic.confirmed_subscription_count)
end
def test_prop_conf_sub_count_one
DRY up AWS resource implementation and test backend implementations (#121) * Standardize requires in unit tests Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Standardize requires in resources Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Move AWS connection hook into non-resource library area Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Add an AWS resource mixin, pushing constructor out to it Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push resource param name recognition into mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push exists predicate up to mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rename base.rb to be resource_mixin for clarity Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Separate the backend from its factory, and push it out into a class mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Push BackendFactory up into the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * De-linting Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove aws_conn require from LMF and CloudWatch Alarm filters Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Use resource mixin for Cloudwatch Alarm Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rework LMF to use the resource mixin Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Rubocop. Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Remove SDK load from connection.rb; that happens in aws.rb now Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Mixin should default to allowing empty resource params Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com> * Update LMF to enforce params being required Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-12-04 18:32:13 +00:00
AwsSnsTopic::BackendFactory.select(AwsMSNB::OneSubscription)
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
assert_equal(1, topic.confirmed_subscription_count)
end
end
#=============================================================================#
# Test Fixtures
#=============================================================================#
module AwsMSNB
class Miss < AwsBackendBase
def get_topic_attributes(criteria)
raise Aws::SNS::Errors::NotFound.new("No SNS topic for #{criteria[:topic_arn]}", "Nope")
end
end
class NoSubscriptions < AwsBackendBase
def get_topic_attributes(_criteria)
OpenStruct.new({
attributes: { # Note that this is a plain hash, odd for AWS SDK
# Many other attributes available, see
# http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Types/GetTopicAttributesResponse.html
"SubscriptionsConfirmed" => 0,
},
})
end
end
class OneSubscription < AwsBackendBase
def get_topic_attributes(_criteria)
OpenStruct.new({
attributes: { # Note that this is a plain hash, odd for AWS SDK
# Many other attributes available, see
# http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Types/GetTopicAttributesResponse.html
"SubscriptionsConfirmed" => 1,
},
})
end
end
end