Skip to content

Commit

Permalink
Update testing to use CloudBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
averbuks committed Sep 18, 2019
1 parent 71b7db3 commit be1021f
Show file tree
Hide file tree
Showing 25 changed files with 274 additions and 194 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export SERVICE_ACCOUNT_JSON=$(< credentials.json)

You will also need to set a few environment variables:
```
export TF_VAR_project_id="your_project_id"
export TF_VAR_org_id="your_org_id"
export TF_VAR_folder_id="your_folder_id"
export TF_VAR_billing_account="your_billing_account_id"
```

With these settings in place, you can prepare a test project using Docker:
Expand Down
163 changes: 50 additions & 113 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# 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.
Expand All @@ -18,131 +18,68 @@
# Make will use bash instead of sh
SHELL := /usr/bin/env bash

# Docker build config variables
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
DOCKER_ORG := gcr.io/cloud-foundation-cicd
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 2.3.0
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.1.0
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
REGISTRY_URL := gcr.io/cloud-foundation-cicd

# All is the first target in the file so it will get picked up when you just run 'make' on its own
.PHONY: all
all: check generate_docs

# Run all available linters
.PHONY: check
check: check_shell check_python check_golang check_terraform check_base_files test_check_headers check_headers check_trailing_whitespace

# The .PHONY directive tells make that this isn't a real target and so
# the presence of a file named 'check_shell' won't cause this target to stop
# working
.PHONY: check_shell
check_shell:
@source test/make.sh && check_shell

.PHONY: check_python
check_python:
@source test/make.sh && check_python

.PHONY: check_golang
check_golang:
@source test/make.sh && golang

.PHONY: check_terraform
check_terraform:
@source test/make.sh && check_terraform

.PHONY: check_docker
check_docker:
@source test/make.sh && docker

.PHONY: check_base_files
check_base_files:
@source test/make.sh && basefiles

.PHONY: check_trailing_whitespace
check_trailing_whitespace:
@source test/make.sh && check_trailing_whitespace

.PHONY: test_check_headers
test_check_headers:
@echo "Testing the validity of the header check"
@python test/test_verify_boilerplate.py

.PHONY: check_headers
check_headers:
@source test/make.sh && check_headers

# Integration tests
.PHONY: test_integration
test_integration:
test/ci_integration.sh

.PHONY: generate_docs
generate_docs:
@source test/make.sh && generate_docs

# Versioning
.PHONY: version
version:
@source helpers/version-repo.sh

# Run docker
# Enter docker container for local development
.PHONY: docker_run
docker_run:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && exec /bin/bash"
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash

.PHONY: docker_create
docker_create:
# Execute prepare tests within the docker container
.PHONY: docker_test_prepare
docker_test_prepare:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen create"

.PHONY: docker_converge
docker_converge:
-e TF_VAR_org_id \
-e TF_VAR_folder_id \
-e TF_VAR_billing_account \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/execute_with_credentials.sh prepare_environment

# Clean up test environment within the docker container
.PHONY: docker_test_cleanup
docker_test_cleanup:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen converge"

.PHONY: docker_verify
docker_verify:
-e TF_VAR_org_id \
-e TF_VAR_folder_id \
-e TF_VAR_billing_account \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/execute_with_credentials.sh cleanup_environment

# Execute integration tests within the docker container
.PHONY: docker_test_integration
docker_test_integration:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen verify"
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/test_integration.sh

.PHONY: docker_destroy
docker_destroy:
# Execute lint tests within the docker container
.PHONY: docker_test_lint
docker_test_lint:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen destroy"
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/test_lint.sh

.PHONY: test_integration_docker
test_integration_docker:
# Generate documentation
.PHONY: docker_generate_docs
docker_generate_docs:
docker run --rm -it \
-e PROJECT_ID \
-e SERVICE_ACCOUNT_JSON \
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
-v $(CURDIR):/cft/workdir \
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && make test_integration"
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs'

# Alias for backwards compatibility
.PHONY: generate_docs
generate_docs: docker_generate_docs
42 changes: 42 additions & 0 deletions build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# 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.

timeout: 3600s
steps:
- id: prepare
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && prepare_environment']
env:
- 'TF_VAR_org_id=$_ORG_ID'
- 'TF_VAR_folder_id=$_FOLDER_ID'
- 'TF_VAR_billing_account=$_BILLING_ACCOUNT'
- id: create
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create']
- id: converge
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge']
- id: verify
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify']
- id: destroy
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy']
tags:
- 'ci'
- 'integration'
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0'
2 changes: 1 addition & 1 deletion build/lint.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

steps:
- name: 'gcr.io/cloud-foundation-cicd/cft/developer-tools:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
- name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
id: 'lint'
args: ['/usr/local/bin/test_lint.sh']
tags:
Expand Down
1 change: 0 additions & 1 deletion examples/forwarding-zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ module "dns-forwarding-zone" {
private_visibility_config_networks = [var.network_self_link]
target_name_server_addresses = ["8.8.8.8", "8.8.4.4"]
}

1 change: 0 additions & 1 deletion examples/forwarding-zone/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ output "name_servers" {
description = "Zone name servers."
value = module.dns-forwarding-zone.name_servers
}

1 change: 0 additions & 1 deletion examples/forwarding-zone/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ provider "google" {
provider "google-beta" {
version = ">= 2.14"
}

1 change: 0 additions & 1 deletion examples/forwarding-zone/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ variable "domain" {
description = "Zone domain."
default = "foo.local."
}

1 change: 0 additions & 1 deletion examples/peering-zone/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ output "name_servers" {
description = "Zone name servers."
value = module.dns-peering-zone.name_servers
}

1 change: 0 additions & 1 deletion examples/peering-zone/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ provider "google" {
provider "google-beta" {
version = ">= 2.14"
}

1 change: 0 additions & 1 deletion examples/peering-zone/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ variable "domain" {
description = "Zone domain."
default = "foo.local."
}

2 changes: 1 addition & 1 deletion kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# 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.
Expand Down
3 changes: 1 addition & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019 Google LLC
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -113,4 +113,3 @@ resource "google_dns_record_set" "cloud-static-records" {
google_dns_managed_zone.public,
]
}

1 change: 0 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,3 @@ output "name_servers" {
),
)
}

64 changes: 0 additions & 64 deletions test/ci_integration.sh

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/private_zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/


module "example-private-zone" {
source = "../../../examples/private-zone"
project_id = var.project_id
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/public_zone/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* 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.
Expand All @@ -14,7 +14,6 @@
* limitations under the License.
*/


module "example-public-zone" {
source = "../../../examples/public-zone"
project_id = var.project_id
Expand Down
Loading

0 comments on commit be1021f

Please sign in to comment.