2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "resources/aws/aws_cloudwatch_alarm"
|
2017-11-22 19:04:13 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws"
|
2019-05-21 00:19:38 +00:00
|
|
|
|
2017-11-22 19:04:13 +00:00
|
|
|
# MCWAB = MockCloudwatchAlarmBackend
|
|
|
|
# Abbreviation not used outside this file
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Constructor Tests
|
|
|
|
#=============================================================================#
|
|
|
|
class AwsCWAConstructor < Minitest::Test
|
|
|
|
def setup
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsCloudwatchAlarm::BackendFactory.select(AwsMCWAB::Empty)
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_some_args_required
|
|
|
|
assert_raises(ArgumentError) { AwsCloudwatchAlarm.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_accepts_known_resource_params_combos
|
|
|
|
[
|
2019-06-11 22:24:35 +00:00
|
|
|
{ metric_name: "some-val", metric_namespace: "some-val" },
|
2017-11-22 19:04:13 +00:00
|
|
|
].each do |combo|
|
|
|
|
AwsCloudwatchAlarm.new(combo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_rejects_bad_resource_params_combos
|
|
|
|
[
|
2019-06-11 22:24:35 +00:00
|
|
|
{ metric_name: "some-val" },
|
|
|
|
{ metric_namespace: "some-val" },
|
2017-11-22 19:04:13 +00:00
|
|
|
].each do |combo|
|
|
|
|
assert_raises(ArgumentError) { AwsCloudwatchAlarm.new(combo) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_reject_unknown_resource_params
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_raises(ArgumentError) { AwsCloudwatchAlarm.new(beep: "boop") }
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Search / Recall
|
|
|
|
#=============================================================================#
|
|
|
|
|
|
|
|
class AwsCWARecall < Minitest::Test
|
|
|
|
def setup
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsCloudwatchAlarm::BackendFactory.select(AwsMCWAB::Basic)
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_recall_no_match_is_no_exception
|
2019-06-11 22:24:35 +00:00
|
|
|
alarm = AwsCloudwatchAlarm.new(metric_name: "nope", metric_namespace: "nope")
|
2017-11-22 19:04:13 +00:00
|
|
|
refute alarm.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recall_match_single_result_works
|
|
|
|
alarm = AwsCloudwatchAlarm.new(
|
2019-06-11 22:24:35 +00:00
|
|
|
metric_name: "metric-01",
|
|
|
|
metric_namespace: "metric-namespace-01"
|
2017-11-22 19:04:13 +00:00
|
|
|
)
|
|
|
|
assert alarm.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recall_multiple_result_raises
|
|
|
|
assert_raises(RuntimeError) do
|
|
|
|
AwsCloudwatchAlarm.new(
|
2019-06-11 22:24:35 +00:00
|
|
|
metric_name: "metric-02",
|
|
|
|
metric_namespace: "metric-namespace-01"
|
2017-11-22 19:04:13 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Properties
|
|
|
|
#=============================================================================#
|
|
|
|
|
|
|
|
class AwsCWAProperties < Minitest::Test
|
|
|
|
def setup
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsCloudwatchAlarm::BackendFactory.select(AwsMCWAB::Basic)
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#---------------------------------------
|
|
|
|
# alarm_actions
|
|
|
|
#---------------------------------------
|
|
|
|
def test_prop_actions_empty
|
|
|
|
alarm = AwsCloudwatchAlarm.new(
|
2019-06-11 22:24:35 +00:00
|
|
|
metric_name: "metric-02",
|
|
|
|
metric_namespace: "metric-namespace-02"
|
2017-11-22 19:04:13 +00:00
|
|
|
)
|
|
|
|
assert_kind_of Array, alarm.alarm_actions
|
|
|
|
assert_empty alarm.alarm_actions
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prop_actions_hit
|
|
|
|
alarm = AwsCloudwatchAlarm.new(
|
2019-06-11 22:24:35 +00:00
|
|
|
metric_name: "metric-01",
|
|
|
|
metric_namespace: "metric-namespace-01"
|
2017-11-22 19:04:13 +00:00
|
|
|
)
|
|
|
|
assert_kind_of Array, alarm.alarm_actions
|
|
|
|
refute_empty alarm.alarm_actions
|
|
|
|
assert_kind_of String, alarm.alarm_actions.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Test Fixtures
|
|
|
|
#=============================================================================#
|
|
|
|
|
|
|
|
module AwsMCWAB
|
2018-02-08 04:26:37 +00:00
|
|
|
class Empty < AwsBackendBase
|
2017-11-22 19:04:13 +00:00
|
|
|
def describe_alarms_for_metric(_criteria)
|
|
|
|
OpenStruct.new({
|
|
|
|
metric_alarms: [],
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class Basic < AwsBackendBase
|
2017-11-22 19:04:13 +00:00
|
|
|
def describe_alarms_for_metric(criteria)
|
|
|
|
OpenStruct.new({
|
|
|
|
metric_alarms: [ # rubocop:disable Metrics/BlockLength
|
|
|
|
# Each one here is an alarm that is subscribed to the given metric
|
|
|
|
# Each has an enormous number of properties, most omitted here
|
|
|
|
# http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudWatch/Client.html#describe_alarms_for_metric-instance_method
|
|
|
|
OpenStruct.new({
|
2019-06-11 22:24:35 +00:00
|
|
|
alarm_name: "alarm-01",
|
|
|
|
metric_name: "metric-01",
|
|
|
|
namespace: "metric-namespace-01",
|
|
|
|
statistic: "SampleCount",
|
2017-11-22 19:04:13 +00:00
|
|
|
alarm_actions: [
|
2019-07-09 00:20:30 +00:00
|
|
|
"arn::::", # TODO: get SNS ARN format
|
2017-11-22 19:04:13 +00:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
OpenStruct.new({
|
|
|
|
# Alarm 02 and 03 both watch metric-01, metric-namespace-01
|
2019-06-11 22:24:35 +00:00
|
|
|
alarm_name: "alarm-02",
|
|
|
|
metric_name: "metric-02",
|
|
|
|
namespace: "metric-namespace-01",
|
|
|
|
statistic: "SampleCount",
|
2017-11-22 19:04:13 +00:00
|
|
|
alarm_actions: [],
|
|
|
|
}),
|
|
|
|
OpenStruct.new({
|
|
|
|
# Alarm 02 and 03 both watch metric-02, metric-namespace-01
|
2019-06-11 22:24:35 +00:00
|
|
|
alarm_name: "alarm-03",
|
|
|
|
metric_name: "metric-02",
|
|
|
|
namespace: "metric-namespace-01",
|
|
|
|
statistic: "SampleCount",
|
2017-11-22 19:04:13 +00:00
|
|
|
alarm_actions: [],
|
|
|
|
}),
|
|
|
|
OpenStruct.new({
|
2019-06-11 22:24:35 +00:00
|
|
|
alarm_name: "alarm-04",
|
|
|
|
metric_name: "metric-02",
|
|
|
|
namespace: "metric-namespace-02",
|
|
|
|
statistic: "SampleCount",
|
2017-11-22 19:04:13 +00:00
|
|
|
alarm_actions: [],
|
|
|
|
}),
|
|
|
|
].select do |alarm|
|
|
|
|
criteria.keys.all? do |criterion|
|
2019-06-11 22:24:35 +00:00
|
|
|
criterion = "namespace" if criterion == "metric_namespace"
|
2017-11-22 19:04:13 +00:00
|
|
|
alarm[criterion] == criteria[criterion]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|