test interfaces in compute_vm test

This commit is contained in:
Ludovico Magnocavallo
2020-07-10 16:56:28 +02:00
parent 6da821f490
commit 87597a2a5f
3 changed files with 99 additions and 11 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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"]