Add tests for th ehub-and-spoke-vpns infra example

This commit is contained in:
Aleksandr Averbukh 2019-11-04 17:39:42 +01:00
parent 5350173a88
commit b5998918b5
7 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,13 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

View File

@ -0,0 +1,27 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"Plan fixture."
import os
import pytest
_TFDIR = os.path.sep.join(os.path.abspath(__file__).split(os.path.sep)[-3:-1])
@pytest.fixture(scope='package')
def plan(plan):
return plan(_TFDIR)

View File

@ -0,0 +1,3 @@
hub_project_id = "automation-examples"
spoke_1_project_id = "automation-examples"
spoke_2_project_id = "automation-examples"

View File

@ -0,0 +1,40 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"Test cloud routers resources creation in root module."
def test_hub_custom_routers(plan):
"Custom routers should match input variables."
hub_custom_router_tpl = ('google_compute_router.hub-to-spoke-%s-custom[0]')
count = range(1, 3)
for i in count:
name = hub_custom_router_tpl % i
custom_router = plan.resource_changes[name]
assert custom_router['change']['after']['bgp'][0]['advertise_mode'] == 'CUSTOM'
assert custom_router['change']['after']['bgp'][0]['advertised_groups'] == ['ALL_SUBNETS']
assert custom_router['change']['after']['bgp'][0]['asn'] == plan.variables['hub_bgp_asn']
assert [range['range'] for range in custom_router['change']['after']['bgp'][0]['advertised_ip_ranges']] == [subnet['subnet_ip'] for subnet in plan.variables['spoke_%s_subnets' % (3 - i)]]
def test_spoke_routers(plan):
"Spoke routers should match input variables."
spoke_router_tpl = ('google_compute_router.spoke-%s')
spoke_bgp_asn_tpl = ('spoke_%s_bgp_asn')
count = range(1, 3)
for i in count:
spoke_router = plan.resource_changes[spoke_router_tpl % i]
spoke_bgp_asn = plan.variables[spoke_bgp_asn_tpl % i]
assert spoke_router['change']['after']['bgp'][0]['advertise_mode'] == 'DEFAULT'
assert spoke_router['change']['after']['bgp'][0]['advertised_groups'] == None
assert spoke_router['change']['after']['bgp'][0]['asn'] == spoke_bgp_asn

View File

@ -0,0 +1,40 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"Test firewall resources creation in root module."
import pytest
@pytest.fixture(scope='module')
def firewall_modules(plan):
names = ['module.firewall-%s' %
name for name in ('hub', 'spoke-1', 'spoke-2')]
return dict((name, plan.modules[name]) for name in names)
def test_firewall_rules(plan, firewall_modules):
"Test that the hub and spoke VPCs have allow-admin firewall rules"
names = ['%s_subnets' %
name for name in ('hub', 'spoke_1', 'spoke_2')]
source_ranges = []
for name in names:
subnets = plan.variables[name]
for subnet in subnets:
source_ranges.append(subnet['subnet_ip'])
for mod in firewall_modules.values():
allow_admins_resource = mod.resources['google_compute_firewall.allow-admins[0]']
allow_ssh = mod.resources['google_compute_firewall.allow-tag-ssh[0]']
assert allow_admins_resource['values']['source_ranges'] == source_ranges
assert allow_ssh['values']['source_ranges'] == ['0.0.0.0/0']

View File

@ -0,0 +1,35 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"Test root module outputs."
def test_hub_outputs(plan):
"Hub VPC ranges and regions should match input variables."
hub_output = plan.outputs['hub']
for hub_subnet in plan.variables['hub_subnets']:
assert hub_output['subnets_ips'][hub_subnet['subnet_name']] == hub_subnet['subnet_ip']
assert hub_output['subnets_regions'][hub_subnet['subnet_name']] == hub_subnet['subnet_region']
def test_spokes_outputs(plan):
"Spokes VPC ranges and regions should match input variables."
spoke_output_tpl = ('spoke-%s')
spole_subnets_tpl = ('spoke_%s_subnets')
count = range(1, 3)
for i in count:
spoke_output = plan.outputs[spoke_output_tpl % i]
for spoke_subnet in plan.variables[spole_subnets_tpl % i]:
assert spoke_output['subnets_ips'][spoke_subnet['subnet_name']] == spoke_subnet['subnet_ip']
assert spoke_output['subnets_regions'][spoke_subnet['subnet_name']] == spoke_subnet['subnet_region']

View File

@ -0,0 +1,51 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"Test firewall resources creation in root module."
import pytest
@pytest.fixture(scope='module')
def spoke_to_hub_vpn(plan):
count = range(1,3)
names = ['module.vpn-spoke-%s-to-hub' % i for i in count]
return dict((name, plan.modules[name]) for name in names)
@pytest.fixture(scope='module')
def hub_to_spoke_vpn(plan):
count = range(1,3)
var_names = ['spoke_%s_bgp_asn' % i for i in count]
names = ['module.vpn-hub-to-spoke-%s' % i for i in count]
return var_names, [plan.modules[name] for name in names]
def test_spokes_peer_asn(plan, spoke_to_hub_vpn):
"Test that the spoke-to-hub VPNs mach input variables"
hub_bgp_asn = plan.variables['hub_bgp_asn']
for mod in spoke_to_hub_vpn.values():
spoke_bgp_peer = mod.resources['google_compute_router_peer.bgp_peer[0]']
assert spoke_bgp_peer['values']['peer_asn'] == hub_bgp_asn
def test_hub_peer_asns(plan, hub_to_spoke_vpn):
"Test that the hub-to-spoke VPNs mach input variables"
count = range(1,3)
var_names, mods = hub_to_spoke_vpn
for var_name, mod in zip(var_names, mods):
hub_bgp_peer = mod.resources['google_compute_router_peer.bgp_peer[0]']
spoke_bgp_asn = plan.variables[var_name]
assert hub_bgp_peer['values']['peer_asn'] == spoke_bgp_asn