inspec/test/unit/resources/aws_sns_topic_test.rb
Clinton Wolfe ab2170f717 Add aws_sns_topic resource (#120)
* Docs first draft, integration tests, and constructor unit tests for SNS topic

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Skeleton of SNS topic

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Constructor arg validation works

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Passing unit tests for recall

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Subscription Count property, works

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Subscription, not subscriber

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Integration tests pass; also wildard ARNs are not allowed

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Rubocop changes

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Doc updates per kagarmoe

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-11-17 01:44:43 +07:00

126 lines
No EOL
4.4 KiB
Ruby

require 'ostruct'
require 'helper'
require 'aws_sns_topic'
# MSNB = MockSnsBackend
# Abbreviation not used outside this file
#=============================================================================#
# Constructor Tests
#=============================================================================#
class AwsSnsTopicConstructorTest < Minitest::Test
def setup
AwsSnsTopic::Backend.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
AwsSnsTopic::Backend.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::Backend.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
AwsSnsTopic::Backend.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_zero
AwsSnsTopic::Backend.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 < AwsSnsTopic::Backend
def get_topic_attributes(criteria)
raise Aws::SNS::Errors::NotFound.new("No SNS topic for #{criteria[:topic_arn]}", 'Nope')
end
end
class NoSubscriptions < AwsSnsTopic::Backend
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 < AwsSnsTopic::Backend
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