From 42893524204218cf8caea0007882595891063db8 Mon Sep 17 00:00:00 2001 From: Andriy Kopachevskyy Date: Thu, 26 Sep 2019 15:31:18 +0300 Subject: [PATCH 1/2] Implement integration testing for Terraform Validator Added Cloud Build config to build folder and needed resources provisioning to tests folder. It's not very clean option because project already had test folder for go testing. Fixes #30 --- build/int.cloudbuild.yaml | 50 +++++++++++++++++++++++++++++++ test/.gitignore | 2 ++ test/setup/.gitignore | 2 ++ test/setup/iam.tf | 39 ++++++++++++++++++++++++ test/setup/main.tf | 35 ++++++++++++++++++++++ test/setup/make_source.sh | 27 +++++++++++++++++ test/setup/outputs.tf | 28 +++++++++++++++++ test/setup/variables.tf | 31 +++++++++++++++++++ test/setup/versions.tf | 27 +++++++++++++++++ test/tf_generated/0.12/.gitignore | 1 + 10 files changed, 242 insertions(+) create mode 100644 build/int.cloudbuild.yaml create mode 100644 test/.gitignore create mode 100644 test/setup/.gitignore create mode 100644 test/setup/iam.tf create mode 100644 test/setup/main.tf create mode 100755 test/setup/make_source.sh create mode 100644 test/setup/outputs.tf create mode 100644 test/setup/variables.tf create mode 100644 test/setup/versions.tf diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml new file mode 100644 index 000000000..18895838e --- /dev/null +++ b/build/int.cloudbuild.yaml @@ -0,0 +1,50 @@ +# 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: test + name: 'gcr.io/cloud-builders/go' + entrypoint: /usr/bin/make + args: [test] +- id: build + name: 'gcr.io/cloud-builders/go' + entrypoint: /usr/bin/make + args: [build] +- 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: print_source + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'cat test/source.sh'] +- id: test-integration + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source test/source.sh && make test-integration'] +- 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 && cleanup_environment'] + env: + - 'TF_VAR_org_id=$_ORG_ID' + - 'TF_VAR_folder_id=$_FOLDER_ID' + - 'TF_VAR_billing_account=$_BILLING_ACCOUNT' +tags: +- 'ci' +- 'integration' +substitutions: + _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0' diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 000000000..b93dc2fea --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +source.sh +credentials.json diff --git a/test/setup/.gitignore b/test/setup/.gitignore new file mode 100644 index 000000000..0e515f83d --- /dev/null +++ b/test/setup/.gitignore @@ -0,0 +1,2 @@ +terraform.tfvars +source.sh diff --git a/test/setup/iam.tf b/test/setup/iam.tf new file mode 100644 index 000000000..526a57267 --- /dev/null +++ b/test/setup/iam.tf @@ -0,0 +1,39 @@ +/** + * 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. + */ + +locals { + int_required_roles = [ + "roles/editor" + ] +} + +resource "google_service_account" "int_test" { + project = module.project.project_id + account_id = "ci-account" + display_name = "ci-account" +} + +resource "google_project_iam_member" "int_test" { + count = length(local.int_required_roles) + + project = module.project.project_id + role = local.int_required_roles[count.index] + member = "serviceAccount:${google_service_account.int_test.email}" +} + +resource "google_service_account_key" "int_test" { + service_account_id = google_service_account.int_test.id +} diff --git a/test/setup/main.tf b/test/setup/main.tf new file mode 100644 index 000000000..f2034cbfc --- /dev/null +++ b/test/setup/main.tf @@ -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 + * + * 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. + */ + +locals { + project_name = "ci-terraform-validator" +} + + +module "project" { + source = "terraform-google-modules/project-factory/google" + version = "~> 3.0" + + name = local.project_name + random_project_id = true + org_id = var.org_id + folder_id = var.folder_id + billing_account = var.billing_account + + activate_apis = [ + "cloudresourcemanager.googleapis.com" + ] +} diff --git a/test/setup/make_source.sh b/test/setup/make_source.sh new file mode 100755 index 000000000..b3bc3ce5f --- /dev/null +++ b/test/setup/make_source.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# 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. +# 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. + +project_id=$(terraform output project_id) + +sa_json=$(terraform output sa_key | base64 --decode) +cred_file="/workspace/test/credentials.json" +echo $sa_json > $cred_file +{ + echo "#!/usr/bin/env bash" + echo "export TEST_PROJECT='$project_id'" + echo "export TEST_CREDENTIALS='$cred_file'" +} > ../source.sh + diff --git a/test/setup/outputs.tf b/test/setup/outputs.tf new file mode 100644 index 000000000..a758b75ee --- /dev/null +++ b/test/setup/outputs.tf @@ -0,0 +1,28 @@ +/** + * 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. + */ + +output "project_id" { + value = module.project.project_id +} + +output "sa_key" { + value = google_service_account_key.int_test.private_key + sensitive = true +} + +output "region" { + value = var.region +} diff --git a/test/setup/variables.tf b/test/setup/variables.tf new file mode 100644 index 000000000..0d285c253 --- /dev/null +++ b/test/setup/variables.tf @@ -0,0 +1,31 @@ +/** + * 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. + */ +variable "org_id" { + description = "The numeric organization id" +} + +variable "folder_id" { + description = "The folder to deploy in" +} + +variable "billing_account" { + description = "The billing account id associated with the project, e.g. XXXXXX-YYYYYY-ZZZZZZ" +} + +variable "region" { + description = "Compute region to create test resources" + default = "us-east4" +} \ No newline at end of file diff --git a/test/setup/versions.tf b/test/setup/versions.tf new file mode 100644 index 000000000..efbd8ea51 --- /dev/null +++ b/test/setup/versions.tf @@ -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 + * + * 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. + */ + +terraform { + required_version = ">= 0.12" +} + +provider "google" { + version = "~> 2.13.0" +} + +provider "google-beta" { + version = "~> 2.13.0" +} diff --git a/test/tf_generated/0.12/.gitignore b/test/tf_generated/0.12/.gitignore index b3edb30f1..247f6ef9c 100644 --- a/test/tf_generated/0.12/.gitignore +++ b/test/tf_generated/0.12/.gitignore @@ -1,2 +1,3 @@ *.tfplan *.tf +*.tfplan.json From d6248aa3552d84bf089ddc1593725f6987903937 Mon Sep 17 00:00:00 2001 From: Andriy Kopachevskyy Date: Sat, 12 Oct 2019 16:59:45 +0300 Subject: [PATCH 2/2] Review Fixes for Implement integration testing for Terraform Validator Added Cloud Build config to build folder and needed resources provisioning to tests folder. It's not very clean option because project already had test folder for go testing. Related PR GoogleCloudPlatform/cloud-foundation-toolkit#376 --- build/int.cloudbuild.yaml | 20 +++----------------- test/setup/.gitignore | 2 -- test/setup/iam.tf | 39 --------------------------------------- test/setup/main.tf | 35 ----------------------------------- test/setup/make_source.sh | 27 --------------------------- test/setup/outputs.tf | 28 ---------------------------- test/setup/variables.tf | 31 ------------------------------- test/setup/versions.tf | 27 --------------------------- test/utils.go | 16 ++++++++-------- 9 files changed, 11 insertions(+), 214 deletions(-) delete mode 100644 test/setup/.gitignore delete mode 100644 test/setup/iam.tf delete mode 100644 test/setup/main.tf delete mode 100755 test/setup/make_source.sh delete mode 100644 test/setup/outputs.tf delete mode 100644 test/setup/variables.tf delete mode 100644 test/setup/versions.tf diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index 18895838e..9c8582b89 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -22,29 +22,15 @@ steps: name: 'gcr.io/cloud-builders/go' entrypoint: /usr/bin/make args: [build] -- 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: print_source - name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' - args: ['/bin/bash', '-c', 'cat test/source.sh'] - id: test-integration name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' - args: ['/bin/bash', '-c', 'source test/source.sh && make test-integration'] -- 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 && cleanup_environment'] + args: ['/bin/bash', '-c', 'make test-integration'] env: - - 'TF_VAR_org_id=$_ORG_ID' - - 'TF_VAR_folder_id=$_FOLDER_ID' - - 'TF_VAR_billing_account=$_BILLING_ACCOUNT' + - 'TEST_PROJECT=$_TEST_PROJECT' tags: - 'ci' - 'integration' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0' + diff --git a/test/setup/.gitignore b/test/setup/.gitignore deleted file mode 100644 index 0e515f83d..000000000 --- a/test/setup/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -terraform.tfvars -source.sh diff --git a/test/setup/iam.tf b/test/setup/iam.tf deleted file mode 100644 index 526a57267..000000000 --- a/test/setup/iam.tf +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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. - */ - -locals { - int_required_roles = [ - "roles/editor" - ] -} - -resource "google_service_account" "int_test" { - project = module.project.project_id - account_id = "ci-account" - display_name = "ci-account" -} - -resource "google_project_iam_member" "int_test" { - count = length(local.int_required_roles) - - project = module.project.project_id - role = local.int_required_roles[count.index] - member = "serviceAccount:${google_service_account.int_test.email}" -} - -resource "google_service_account_key" "int_test" { - service_account_id = google_service_account.int_test.id -} diff --git a/test/setup/main.tf b/test/setup/main.tf deleted file mode 100644 index f2034cbfc..000000000 --- a/test/setup/main.tf +++ /dev/null @@ -1,35 +0,0 @@ -/** - * 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. - */ - -locals { - project_name = "ci-terraform-validator" -} - - -module "project" { - source = "terraform-google-modules/project-factory/google" - version = "~> 3.0" - - name = local.project_name - random_project_id = true - org_id = var.org_id - folder_id = var.folder_id - billing_account = var.billing_account - - activate_apis = [ - "cloudresourcemanager.googleapis.com" - ] -} diff --git a/test/setup/make_source.sh b/test/setup/make_source.sh deleted file mode 100755 index b3bc3ce5f..000000000 --- a/test/setup/make_source.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env bash - -# 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. -# 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. - -project_id=$(terraform output project_id) - -sa_json=$(terraform output sa_key | base64 --decode) -cred_file="/workspace/test/credentials.json" -echo $sa_json > $cred_file -{ - echo "#!/usr/bin/env bash" - echo "export TEST_PROJECT='$project_id'" - echo "export TEST_CREDENTIALS='$cred_file'" -} > ../source.sh - diff --git a/test/setup/outputs.tf b/test/setup/outputs.tf deleted file mode 100644 index a758b75ee..000000000 --- a/test/setup/outputs.tf +++ /dev/null @@ -1,28 +0,0 @@ -/** - * 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. - */ - -output "project_id" { - value = module.project.project_id -} - -output "sa_key" { - value = google_service_account_key.int_test.private_key - sensitive = true -} - -output "region" { - value = var.region -} diff --git a/test/setup/variables.tf b/test/setup/variables.tf deleted file mode 100644 index 0d285c253..000000000 --- a/test/setup/variables.tf +++ /dev/null @@ -1,31 +0,0 @@ -/** - * 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. - */ -variable "org_id" { - description = "The numeric organization id" -} - -variable "folder_id" { - description = "The folder to deploy in" -} - -variable "billing_account" { - description = "The billing account id associated with the project, e.g. XXXXXX-YYYYYY-ZZZZZZ" -} - -variable "region" { - description = "Compute region to create test resources" - default = "us-east4" -} \ No newline at end of file diff --git a/test/setup/versions.tf b/test/setup/versions.tf deleted file mode 100644 index efbd8ea51..000000000 --- a/test/setup/versions.tf +++ /dev/null @@ -1,27 +0,0 @@ -/** - * 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. - */ - -terraform { - required_version = ">= 0.12" -} - -provider "google" { - version = "~> 2.13.0" -} - -provider "google-beta" { - version = "~> 2.13.0" -} diff --git a/test/utils.go b/test/utils.go index bd527415c..f61061a2a 100644 --- a/test/utils.go +++ b/test/utils.go @@ -108,15 +108,15 @@ func configure(t *testing.T) config { } cfg.credentials, ok = os.LookupEnv("TEST_CREDENTIALS") - if !ok { - t.Fatal("missing required env var TEST_CREDENTIALS") - } - // Make credentials path relative to repo root rather than - // test/ dir if it is a relative path. - if !filepath.IsAbs(cfg.credentials) { - cfg.credentials = filepath.Join("..", cfg.credentials) + if ok { + // Make credentials path relative to repo root rather than + // test/ dir if it is a relative path. + if !filepath.IsAbs(cfg.credentials) { + cfg.credentials = filepath.Join("..", cfg.credentials) + } + } else { + t.Log("missing env var TEST_CREDENTIALS, will try to use Application Default Credentials") } - return cfg }