2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "resources/aws/aws_vpcs"
|
|
|
|
require "ipaddr"
|
2018-01-18 15:51:06 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws"
|
2019-05-21 00:19:38 +00:00
|
|
|
|
2018-01-18 15:51:06 +00:00
|
|
|
# MAVPB = MockAwsVpcsPluralBackend
|
|
|
|
# Abbreviation not used outside this file
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Constructor Tests
|
|
|
|
#=============================================================================#
|
|
|
|
class AwsVpcsConstructorTest < Minitest::Test
|
|
|
|
|
|
|
|
def setup
|
|
|
|
AwsVpcs::BackendFactory.select(MAVPB::Empty)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_params_ok
|
|
|
|
AwsVpcs.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rejects_unrecognized_params
|
|
|
|
assert_raises(ArgumentError) { AwsVpcs.new(shoe_size: 9) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
2018-04-12 19:48:55 +00:00
|
|
|
# Filter Criteria
|
2018-01-18 15:51:06 +00:00
|
|
|
#=============================================================================#
|
2018-04-12 19:48:55 +00:00
|
|
|
class AwsVpcsFilterCriteriaTest < Minitest::Test
|
2018-01-18 15:51:06 +00:00
|
|
|
|
|
|
|
def setup
|
2018-04-12 19:48:55 +00:00
|
|
|
AwsVpcs::BackendFactory.select(MAVPB::Basic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_with_no_criteria
|
|
|
|
assert AwsVpcs.new.exists?
|
2018-01-18 15:51:06 +00:00
|
|
|
end
|
|
|
|
|
2018-04-12 19:48:55 +00:00
|
|
|
def test_filter_with_vpc_id
|
2019-06-11 22:24:35 +00:00
|
|
|
hit = AwsVpcs.new.where(vpc_id: "vpc-aaaabbbb")
|
2018-04-12 19:48:55 +00:00
|
|
|
assert(hit.exists?)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
miss = AwsVpcs.new.where(vpc_id: "vpc-09876543")
|
2018-04-12 19:48:55 +00:00
|
|
|
refute(miss.exists?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_with_cidr_block
|
2019-06-11 22:24:35 +00:00
|
|
|
hit = AwsVpcs.new.where(cidr_block: "10.0.0.0/16")
|
2018-04-12 19:48:55 +00:00
|
|
|
assert(hit.exists?)
|
|
|
|
|
|
|
|
# This triggers a bug/misfeature in FilterTable - see https://github.com/chef/inspec/issues/2929
|
|
|
|
# hit = AwsVpcs.new.where { IPAddr.new('10.0.0.0/8').include? cidr_block }
|
2019-06-11 22:24:35 +00:00
|
|
|
hit = AwsVpcs.new.where { cidr_block.start_with? "10" }
|
2018-04-12 19:48:55 +00:00
|
|
|
assert(hit.exists?)
|
|
|
|
assert_equal(2, hit.entries.count)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
miss = AwsVpcs.new.where(cidr_block: "11.0.0.0/16")
|
2018-04-12 19:48:55 +00:00
|
|
|
refute(miss.exists?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_with_dhcp_options_id
|
2019-06-11 22:24:35 +00:00
|
|
|
hit = AwsVpcs.new.where(dhcp_options_id: "dopt-aaaabbbb")
|
2018-04-12 19:48:55 +00:00
|
|
|
assert(hit.exists?)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
miss = AwsVpcs.new.where(dhcp_options_id: "dopt-12345678")
|
2018-04-12 19:48:55 +00:00
|
|
|
refute(miss.exists?)
|
2018-01-18 15:51:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 19:48:55 +00:00
|
|
|
#=============================================================================#
|
|
|
|
# Properties
|
|
|
|
#=============================================================================#
|
|
|
|
class AwsVpcsProperties < Minitest::Test
|
2018-01-18 15:51:06 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
AwsVpcs::BackendFactory.select(MAVPB::Basic)
|
2018-04-12 19:48:55 +00:00
|
|
|
@vpcs = AwsVpcs.new
|
2018-01-18 15:51:06 +00:00
|
|
|
end
|
|
|
|
|
2018-04-12 19:48:55 +00:00
|
|
|
def test_property_vpc_ids
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_includes(@vpcs.vpc_ids, "vpc-aaaabbbb")
|
|
|
|
assert_includes(@vpcs.vpc_ids, "vpc-12344321")
|
2018-04-12 19:48:55 +00:00
|
|
|
refute_includes(@vpcs.vpc_ids, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_cidr_blocks
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_includes(@vpcs.cidr_blocks, "10.0.0.0/16")
|
|
|
|
assert_includes(@vpcs.cidr_blocks, "10.1.0.0/16")
|
|
|
|
refute_includes(@vpcs.cidr_blocks, nil)
|
2018-01-18 15:51:06 +00:00
|
|
|
end
|
|
|
|
|
2018-04-12 19:48:55 +00:00
|
|
|
def test_property_dhcp_options_ids
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_includes(@vpcs.dhcp_options_ids, "dopt-aaaabbbb")
|
2018-04-12 19:48:55 +00:00
|
|
|
# Should be de-duplicated
|
|
|
|
assert_equal(1, @vpcs.dhcp_options_ids.count)
|
|
|
|
end
|
|
|
|
end
|
2020-12-18 16:49:35 +00:00
|
|
|
|
2018-01-18 15:51:06 +00:00
|
|
|
#=============================================================================#
|
|
|
|
# Test Fixtures
|
|
|
|
#=============================================================================#
|
|
|
|
module MAVPB
|
2018-02-08 04:26:37 +00:00
|
|
|
class Empty < AwsBackendBase
|
2018-01-18 15:51:06 +00:00
|
|
|
def describe_vpcs(query = {})
|
|
|
|
OpenStruct.new({ vpcs: [] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class Basic < AwsBackendBase
|
2018-01-18 15:51:06 +00:00
|
|
|
def describe_vpcs(query = {})
|
|
|
|
fixtures = [
|
|
|
|
OpenStruct.new({
|
2019-06-11 22:24:35 +00:00
|
|
|
cidr_block: "10.0.0.0/16",
|
|
|
|
dhcp_options_id: "dopt-aaaabbbb",
|
|
|
|
state: "available",
|
|
|
|
vpc_id: "vpc-aaaabbbb",
|
|
|
|
instance_tenancy: "default",
|
|
|
|
is_default: true,
|
2018-01-18 15:51:06 +00:00
|
|
|
}),
|
|
|
|
OpenStruct.new({
|
2019-06-11 22:24:35 +00:00
|
|
|
cidr_block: "10.1.0.0/16",
|
|
|
|
dhcp_options_id: "dopt-aaaabbbb", # Same as vpc-aaaabbbb
|
|
|
|
state: "available",
|
|
|
|
vpc_id: "vpc-12344321",
|
|
|
|
instance_tenancy: "default",
|
|
|
|
is_default: false,
|
2018-01-18 15:51:06 +00:00
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
|
|
|
query[:filters] = [] if query[:filters].nil?
|
|
|
|
|
|
|
|
selected = fixtures.select do |vpc|
|
|
|
|
query[:filters].all? do |filter|
|
|
|
|
if filter[:name].eql? "isDefault"
|
|
|
|
filter[:name] = "is_default"
|
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
filter[:values].include?(vpc[filter[:name].tr("-", "_")].to_s)
|
2018-01-18 15:51:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
OpenStruct.new({ vpcs: selected })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|