Skip to content

Commit

Permalink
Simplify tests, re-enable CI
Browse files Browse the repository at this point in the history
* add instance group support to compute-vm, start tests refactoring

* modules/compute-vm: group fixes, tests

* modules/compute-vm: minimal test beautification

* simplify top-level pytest fixture

* modules/dns: tests and minor tweaks

* fix missing boilerplate in tests

* re-add requirements file to tests folder

* re-enable tests in ci build configuration
  • Loading branch information
ludoo authored Feb 19, 2020
1 parent 9019651 commit c7fb026
Show file tree
Hide file tree
Showing 53 changed files with 650 additions and 1,346 deletions.
28 changes: 9 additions & 19 deletions .ci/cloudbuild.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,18 @@ steps:
unzip terraform_${_TERRAFORM_VERSION}_linux_amd64.zip -d /builder/home/.local/bin &&
rm terraform_${_TERRAFORM_VERSION}_linux_amd64.zip &&
chmod 755 /builder/home/.local/bin/terraform
# TODO(ludoo): reenable once development tests are ready
# TODO(ludoo): add a step that detects change files and sets tests to run
# - name: python:3-alpine
# id: test-foundations
# entrypoint: pytest
# args:
# - -v
# - tests/foundations
# env:
# - PATH=/usr/local/bin:/usr/bin:/bin:/builder/home/.local/bin
# - name: python:3-alpine
# id: test-infrastructure
# entrypoint: pytest
# args:
# - -v
# - tests/infrastructure
# env:
# - PATH=/usr/local/bin:/usr/bin:/bin:/builder/home/.local/bin
# - PYTHONDONTWRITEBYTECODE=true
- name: python:3-alpine
id: test-modules
entrypoint: pytest
args:
- -v
- tests/modules
env:
- PATH=/usr/local/bin:/usr/bin:/bin:/builder/home/.local/bin

substitutions:
_TERRAFORM_VERSION: 0.12.19
_TERRAFORM_VERSION: 0.12.20

tags:
- "ci"
Expand Down
4 changes: 4 additions & 0 deletions modules/compute-vm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ module "debian-test" {
| *attached_disk_defaults* | Defaults for attached disks options. | <code title="object&#40;&#123;&#10;auto_delete &#61; bool&#10;mode &#61; string&#10;type &#61; string&#10;source &#61; string&#10;&#125;&#41;">object({...})</code> | | <code title="&#123;&#10;auto_delete &#61; true&#10;source &#61; null&#10;mode &#61; &#34;READ_WRITE&#34;&#10;type &#61; &#34;pd-ssd&#34;&#10;&#125;">...</code> |
| *attached_disks* | Additional disks, if options is null defaults will be used in its place. | <code title="list&#40;object&#40;&#123;&#10;name &#61; string&#10;image &#61; string&#10;size &#61; string&#10;options &#61; object&#40;&#123;&#10;auto_delete &#61; bool&#10;mode &#61; string&#10;source &#61; string&#10;type &#61; string&#10;&#125;&#41;&#10;&#125;&#41;&#41;">list(object({...}))</code> | | <code title="">[]</code> |
| *boot_disk* | Boot disk properties. | <code title="object&#40;&#123;&#10;image &#61; string&#10;size &#61; number&#10;type &#61; string&#10;&#125;&#41;">object({...})</code> | | <code title="&#123;&#10;image &#61; &#34;projects&#47;debian-cloud&#47;global&#47;images&#47;family&#47;debian-10&#34;&#10;type &#61; &#34;pd-ssd&#34;&#10;size &#61; 10&#10;&#125;">...</code> |
| *group* | Instance group (for instance use). | <code title="object&#40;&#123;&#10;name &#61; string&#10;named_ports &#61; map&#40;number&#41;&#10;&#125;&#41;">object({...})</code> | | <code title="">null</code> |
| *group_manager* | Instance group manager (for template use). | <code title="object&#40;&#123;&#10;auto_healing_policies &#61; object&#40;&#123;&#10;health_check &#61; string&#10;initial_delay_sec &#61; number&#10;&#125;&#41;&#10;named_ports &#61; map&#40;number&#41;&#10;options &#61; object&#40;&#123;&#10;target_pools &#61; list&#40;string&#41;&#10;wait_for_instances &#61; bool&#10;&#125;&#41;&#10;regional &#61; bool&#10;target_size &#61; number&#10;update_policy &#61; object&#40;&#123;&#10;type &#61; string &#35; OPPORTUNISTIC &#124; PROACTIVE&#10;minimal_action &#61; string &#35; REPLACE &#124; RESTART&#10;min_ready_sec &#61; number&#10;max_surge_type &#61; string &#35; fixed &#124; percent&#10;max_surge &#61; number&#10;max_unavailable_type &#61; string&#10;max_unavailable &#61; number&#10;&#125;&#41;&#10;versions &#61; list&#40;object&#40;&#123;&#10;name &#61; string&#10;instance_template &#61; string&#10;target_type &#61; string &#35; fixed &#124; percent&#10;target_size &#61; number&#10;&#125;&#41;&#41;&#10;&#125;&#41;">object({...})</code> | | <code title="">null</code> |
| *hostname* | Instance FQDN name. | <code title="">string</code> | | <code title="">null</code> |
| *instance_count* | Number of instances to create (only for non-template usage). | <code title="">number</code> | | <code title="">1</code> |
| *instance_type* | Instance type. | <code title="">string</code> | | <code title="">f1-micro</code> |
Expand All @@ -93,6 +95,8 @@ module "debian-test" {
| name | description | sensitive |
|---|---|:---:|
| external_ips | Instance main interface external IP addresses. | |
| group | Instance group resource. | |
| group_manager | Instance group resource. | |
| instances | Instance resources. | |
| internal_ips | Instance main interface internal IP addresses. | |
| names | Instance names. | |
Expand Down
215 changes: 215 additions & 0 deletions modules/compute-vm/instance_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/**
* 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
*
* 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.
*/

resource "google_compute_instance_group" "unmanaged" {
count = (
var.group != null && ! var.use_instance_template ? 1 : 0
)
project = var.project_id
network = (
length(var.network_interfaces) > 0
? var.network_interfaces.0.network
: ""
)
zone = var.zone
name = var.name
description = "Terraform-managed."
instances = [
for name, instance in google_compute_instance.default : instance.self_link
]
dynamic named_port {
for_each = var.group.named_ports != null ? var.group.named_ports : {}
iterator = config
content {
name = config.key
port = config.value
}
}
}

resource "google_compute_instance_group_manager" "managed" {
count = (
var.group_manager != null && var.use_instance_template
? var.group_manager.regional ? 0 : 1
: 0
)
project = var.project_id
zone = var.zone
name = var.name
base_instance_name = var.name
description = "Terraform-managed."
target_size = var.group_manager.target_size
target_pools = (
var.group_manager.options == null
? null
: var.group_manager.options.target_pools
)
wait_for_instances = (
var.group_manager.options == null
? null
: var.group_manager.options.wait_for_instances
)
dynamic auto_healing_policies {
for_each = (
var.group_manager.auto_healing_policies == null
? []
: [var.group_manager.auto_healing_policies]
)
iterator = config
content {
health_check = config.value.health_check
initial_delay_sec = config.value.initial_delay_sec
}
}
dynamic update_policy {
for_each = (
var.group_manager.update_policy == null
? []
: [var.group_manager.update_policy]
)
iterator = config
content {
type = config.value.type
minimal_action = config.value.minimal_action
min_ready_sec = config.value.min_ready_sec
max_surge_fixed = (
config.value.max_surge_type == "fixed" ? config.value.max_surge : null
)
max_surge_percent = (
config.value.max_surge_type == "percent" ? config.value.max_surge : null
)
max_unavailable_fixed = (
config.value.max_unavailable_type == "fixed" ? config.value.max_unavailable : null
)
max_unavailable_percent = (
config.value.max_unavailable_type == "percent" ? config.value.max_unavailable : null
)
}
}
dynamic named_port {
for_each = var.group_manager.named_ports != null ? var.group_manager.named_ports : {}
iterator = config
content {
name = config.key
port = config.value
}
}
version {
name = "${var.name}-default"
instance_template = google_compute_instance_template.default.0.self_link
}
dynamic version {
for_each = (
var.group_manager.versions == null ? [] : [var.group_manager.versions]
)
iterator = config
content {
name = config.value.name
instance_template = config.value.instance_template
target_size {
fixed = config.value.target_type == "fixed" ? config.value.target_size : null
percent = config.value.target_type == "percent" ? config.value.target_size : null
}
}
}
}

resource "google_compute_region_instance_group_manager" "managed" {
count = (
var.group_manager != null && var.use_instance_template
? var.group_manager.regional ? 1 : 0
: 0
)
project = var.project_id
region = var.region
name = var.name
base_instance_name = var.name
description = "Terraform-managed."
target_size = var.group_manager.target_size
target_pools = (
var.group_manager.options == null
? null
: var.group_manager.options.target_pools
)
wait_for_instances = (
var.group_manager.options == null
? null
: var.group_manager.options.wait_for_instances
)
dynamic auto_healing_policies {
for_each = (
var.group_manager.auto_healing_policies == null
? []
: [var.group_manager.auto_healing_policies]
)
iterator = config
content {
health_check = config.value.health_check
initial_delay_sec = config.value.initial_delay_sec
}
}
dynamic update_policy {
for_each = (
var.group_manager.update_policy == null
? []
: [var.group_manager.update_policy]
)
iterator = config
content {
type = config.value.type
minimal_action = config.value.minimal_action
min_ready_sec = config.value.min_ready_sec
max_surge_fixed = (
config.value.max_surge_type == "fixed" ? config.value.max_surge : null
)
max_surge_percent = (
config.value.max_surge_type == "percent" ? config.value.max_surge : null
)
max_unavailable_fixed = (
config.value.max_unavailable_type == "fixed" ? config.value.max_unavailable : null
)
max_unavailable_percent = (
config.value.max_unavailable_type == "percent" ? config.value.max_unavailable : null
)
}
}
dynamic named_port {
for_each = var.group.named_ports
iterator = config
content {
name = config.key
port = config.value
}
}
version {
name = "${var.name}-default"
instance_template = google_compute_instance_template.default.0.self_link
}
dynamic version {
for_each = (
var.group_manager.versions == null ? [] : [var.group_manager.versions]
)
iterator = config
content {
name = config.value.name
instance_template = config.value.instance_template
target_size {
fixed = config.value.target_type == "fixed" ? config.value.target_size : null
percent = config.value.target_type == "percent" ? config.value.target_size : null
}
}
}
}
22 changes: 22 additions & 0 deletions modules/compute-vm/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ output "external_ips" {
)
}

output "group" {
description = "Instance group resource."
value = (
length(google_compute_instance_group.unmanaged) > 0
? google_compute_instance_group.unmanaged.0
: null
)
}

output "group_manager" {
description = "Instance group resource."
value = (
length(google_compute_instance_group_manager.managed) > 0
? google_compute_instance_group_manager.managed.0
: (
length(google_compute_region_instance_group_manager.managed) > 0
? google_compute_region_instance_group_manager.managed.0
: null
)
)
}

output "instances" {
description = "Instance resources."
value = [for name, instance in google_compute_instance.default : instance]
Expand Down
41 changes: 41 additions & 0 deletions modules/compute-vm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,47 @@ variable "boot_disk" {
}
}

variable "group" {
description = "Instance group (for instance use)."
type = object({
named_ports = map(number)
})
default = null
}

variable "group_manager" {
description = "Instance group manager (for template use)."
type = object({
auto_healing_policies = object({
health_check = string
initial_delay_sec = number
})
named_ports = map(number)
options = object({
target_pools = list(string)
wait_for_instances = bool
})
regional = bool
target_size = number
update_policy = object({
type = string # OPPORTUNISTIC | PROACTIVE
minimal_action = string # REPLACE | RESTART
min_ready_sec = number
max_surge_type = string # fixed | percent
max_surge = number
max_unavailable_type = string
max_unavailable = number
})
versions = list(object({
name = string
instance_template = string
target_type = string # fixed | percent
target_size = number
}))
})
default = null
}

variable "hostname" {
description = "Instance FQDN name."
type = string
Expand Down
20 changes: 16 additions & 4 deletions modules/dns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

locals {
is_static_zone = var.type == "public" || var.type == "private"
recordsets = {
recordsets = var.recordsets == null ? {} : {
for record in var.recordsets :
join("/", [record.name, record.type]) => record
}
Expand All @@ -35,7 +35,11 @@ resource "google_dns_managed_zone" "non-public" {
visibility = "private"

dynamic forwarding_config {
for_each = var.type == "forwarding" ? { config = var.forwarders } : {}
for_each = (
var.type == "forwarding" && var.forwarders != null
? { config = var.forwarders }
: {}
)
iterator = config
content {
dynamic "target_name_servers" {
Expand All @@ -49,7 +53,11 @@ resource "google_dns_managed_zone" "non-public" {
}

dynamic peering_config {
for_each = var.type == "peering" ? { config = var.peer_network } : {}
for_each = (
var.type == "peering" && var.peer_network != null
? { config = var.peer_network }
: {}
)
iterator = config
content {
target_network {
Expand Down Expand Up @@ -104,7 +112,11 @@ resource "google_dns_managed_zone" "public" {
}

resource "google_dns_record_set" "cloud-static-records" {
for_each = local.recordsets
for_each = (
var.type == "public" || var.type == "private"
? local.recordsets
: {}
)
project = var.project_id
managed_zone = var.name
name = each.value.name != "" ? "${each.value.name}.${var.domain}" : var.domain
Expand Down
Loading

0 comments on commit c7fb026

Please sign in to comment.