From 87597a2a5f305505b695403514a9611b1641cb3c Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Fri, 10 Jul 2020 16:56:28 +0200 Subject: [PATCH] test interfaces in compute_vm test --- tests/modules/compute_vm/fixture/main.tf | 17 ++--- tests/modules/compute_vm/fixture/variables.tf | 18 +++++ .../compute_vm/test_plan_interfaces.py | 75 +++++++++++++++++++ 3 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 tests/modules/compute_vm/test_plan_interfaces.py diff --git a/tests/modules/compute_vm/fixture/main.tf b/tests/modules/compute_vm/fixture/main.tf index 41999d081..34345d9a4 100644 --- a/tests/modules/compute_vm/fixture/main.tf +++ b/tests/modules/compute_vm/fixture/main.tf @@ -15,17 +15,12 @@ */ module "test" { - source = "../../../../modules/compute-vm" - project_id = "my-project" - region = "europe-west1" - zone = "europe-west1-b" - name = "test" - network_interfaces = [{ - network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", - subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", - nat = false, - addresses = null - }] + source = "../../../../modules/compute-vm" + project_id = "my-project" + region = "europe-west1" + zone = "europe-west1-b" + name = "test" + network_interfaces = var.network_interfaces service_account_create = var.service_account_create instance_count = var.instance_count use_instance_template = var.use_instance_template diff --git a/tests/modules/compute_vm/fixture/variables.tf b/tests/modules/compute_vm/fixture/variables.tf index 118383fb5..18fba14ca 100644 --- a/tests/modules/compute_vm/fixture/variables.tf +++ b/tests/modules/compute_vm/fixture/variables.tf @@ -34,6 +34,24 @@ variable "instance_count" { default = 1 } +variable "network_interfaces" { + type = list(object({ + nat = bool + network = string + subnetwork = string + addresses = object({ + internal = list(string) + external = list(string) + }) + })) + default = [{ + network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", + subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", + nat = false, + addresses = null + }] +} + variable "use_instance_template" { type = bool default = false diff --git a/tests/modules/compute_vm/test_plan_interfaces.py b/tests/modules/compute_vm/test_plan_interfaces.py new file mode 100644 index 000000000..560be3cb1 --- /dev/null +++ b/tests/modules/compute_vm/test_plan_interfaces.py @@ -0,0 +1,75 @@ +# Copyright 2020 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 +# +# http://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. + + +import os +import pytest + + +FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture') + + +def test_no_addresses(plan_runner): + network_interfaces = '''[{ + network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", + subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", + nat = false, + addresses = {external=[], internal=[]} + }] + ''' + _, resources = plan_runner( + FIXTURES_DIR, instance_count=2, network_interfaces=network_interfaces) + assert len(resources) == 2 + + +def test_internal_addresses(plan_runner): + network_interfaces = '''[{ + network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", + subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", + nat = false, + addresses = {external=[], internal=["1.1.1.2", "1.1.1.3"]} + }] + ''' + _, resources = plan_runner( + FIXTURES_DIR, instance_count=2, network_interfaces=network_interfaces) + assert [r['values']['network_interface'][0]['network_ip'] + for r in resources] == ["1.1.1.2", "1.1.1.3"] + + +def test_internal_addresses_nat(plan_runner): + network_interfaces = '''[{ + network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", + subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", + nat = true, + addresses = {external=[], internal=["1.1.1.2", "1.1.1.3"]} + }] + ''' + _, resources = plan_runner( + FIXTURES_DIR, instance_count=2, network_interfaces=network_interfaces) + assert [r['values']['network_interface'][0]['network_ip'] + for r in resources] == ["1.1.1.2", "1.1.1.3"] + + +def test_all_addresses(plan_runner): + network_interfaces = '''[{ + network = "https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default", + subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/europe-west1/subnetworks/default-default", + nat = true, + addresses = {external=["2.2.2.2", "2.2.2.3"], internal=["1.1.1.2", "1.1.1.3"]} + }] + ''' + _, resources = plan_runner( + FIXTURES_DIR, instance_count=2, network_interfaces=network_interfaces) + assert [r['values']['network_interface'][0]['access_config'][0]['nat_ip'] + for r in resources] == ["2.2.2.2", "2.2.2.3"]