From b5998918b52b0b192fff7e7c11aa15887b5d12e0 Mon Sep 17 00:00:00 2001 From: Aleksandr Averbukh Date: Mon, 4 Nov 2019 17:39:42 +0100 Subject: [PATCH] Add tests for th ehub-and-spoke-vpns infra example --- .../hub-and-spoke-vpns/__init__.py | 13 +++++ .../hub-and-spoke-vpns/conftest.py | 27 ++++++++++ .../hub-and-spoke-vpns/terraform.tfvars | 3 ++ .../hub-and-spoke-vpns/test_cloud_routers.py | 40 +++++++++++++++ .../hub-and-spoke-vpns/test_firewall.py | 40 +++++++++++++++ .../hub-and-spoke-vpns/test_outputs.py | 35 +++++++++++++ .../hub-and-spoke-vpns/test_vpns.py | 51 +++++++++++++++++++ 7 files changed, 209 insertions(+) create mode 100644 tests/infrastructure/hub-and-spoke-vpns/__init__.py create mode 100644 tests/infrastructure/hub-and-spoke-vpns/conftest.py create mode 100644 tests/infrastructure/hub-and-spoke-vpns/terraform.tfvars create mode 100644 tests/infrastructure/hub-and-spoke-vpns/test_cloud_routers.py create mode 100644 tests/infrastructure/hub-and-spoke-vpns/test_firewall.py create mode 100644 tests/infrastructure/hub-and-spoke-vpns/test_outputs.py create mode 100644 tests/infrastructure/hub-and-spoke-vpns/test_vpns.py diff --git a/tests/infrastructure/hub-and-spoke-vpns/__init__.py b/tests/infrastructure/hub-and-spoke-vpns/__init__.py new file mode 100644 index 00000000..086a24e6 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/__init__.py @@ -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. diff --git a/tests/infrastructure/hub-and-spoke-vpns/conftest.py b/tests/infrastructure/hub-and-spoke-vpns/conftest.py new file mode 100644 index 00000000..cb648e15 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/conftest.py @@ -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) diff --git a/tests/infrastructure/hub-and-spoke-vpns/terraform.tfvars b/tests/infrastructure/hub-and-spoke-vpns/terraform.tfvars new file mode 100644 index 00000000..cfe767c3 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/terraform.tfvars @@ -0,0 +1,3 @@ +hub_project_id = "automation-examples" +spoke_1_project_id = "automation-examples" +spoke_2_project_id = "automation-examples" diff --git a/tests/infrastructure/hub-and-spoke-vpns/test_cloud_routers.py b/tests/infrastructure/hub-and-spoke-vpns/test_cloud_routers.py new file mode 100644 index 00000000..0560a7e3 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/test_cloud_routers.py @@ -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 diff --git a/tests/infrastructure/hub-and-spoke-vpns/test_firewall.py b/tests/infrastructure/hub-and-spoke-vpns/test_firewall.py new file mode 100644 index 00000000..2dce8ac9 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/test_firewall.py @@ -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'] diff --git a/tests/infrastructure/hub-and-spoke-vpns/test_outputs.py b/tests/infrastructure/hub-and-spoke-vpns/test_outputs.py new file mode 100644 index 00000000..83500de0 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/test_outputs.py @@ -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'] diff --git a/tests/infrastructure/hub-and-spoke-vpns/test_vpns.py b/tests/infrastructure/hub-and-spoke-vpns/test_vpns.py new file mode 100644 index 00000000..7b244586 --- /dev/null +++ b/tests/infrastructure/hub-and-spoke-vpns/test_vpns.py @@ -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