Skip to content

Commit 0568eb4

Browse files
authored
Merge pull request #12 from terraform-google-modules/aaron-lane/standard-integration-script
Update integration.sh to use standardized logic
2 parents 1fc8adc + 79a6430 commit 0568eb4

File tree

6 files changed

+67
-46
lines changed

6 files changed

+67
-46
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ use this module.
2929
| function\_labels | A set of key/value label pairs to assign to the function. | map | `<map>` | no |
3030
| function\_runtime | The runtime in which the function will be executed. | string | `"nodejs6"` | no |
3131
| function\_source\_archive\_bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map | `<map>` | no |
32-
| function\_source\_archive\_bucket\_location | The Google Cloud Storage location in which to create the function source archive bucket. | string | `"US"` | no |
3332
| function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes |
3433
| function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no |
3534
| log\_export\_filter | The filter to apply when exporting logs to the Pub/Sub topic. | string | n/a | yes |
36-
| name | The name to apply to any nameable resources. | string | `"event-function"` | no |
35+
| name | The name to apply to any nameable resources. | string | n/a | yes |
3736
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
3837
| region | The region in which resources will be applied. | string | n/a | yes |
3938

examples/automatic_labelling/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The project against which this module will be invoked requires no additional API
3535

3636
| Name | Description | Type | Default | Required |
3737
|------|-------------|:----:|:-----:|:-----:|
38+
| name | The name to apply to any nameable resources. | string | n/a | yes |
3839
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
3940
| region | The region in which resources will be applied. | string | n/a | yes |
4041

examples/automatic_labelling/main.tf

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
provider "archive" {
18-
version = "~> 1.1"
19-
}
20-
21-
provider "google" {
22-
version = "~> 1.20"
23-
}
24-
25-
provider "random" {
26-
version = "~> 2.0"
27-
}
28-
29-
resource "random_pet" "main" {
30-
separator = "-"
31-
}
32-
3317
module "event_function" {
3418
source = "../../"
3519

@@ -53,7 +37,7 @@ module "event_function" {
5337
)
5438
}"
5539

56-
name = "${random_pet.main.id}"
40+
name = "${var.name}"
5741
project_id = "${var.project_id}"
5842
region = "${var.region}"
5943
}

examples/automatic_labelling/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
variable "name" {
18+
type = "string"
19+
description = "The name to apply to any nameable resources."
20+
}
21+
1722
variable "project_id" {
1823
type = "string"
1924
description = "The ID of the project to which resources will be applied."

test/fixtures/automatic_labelling/main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17+
provider "archive" {
18+
version = "~> 1.1"
19+
}
20+
21+
provider "google" {
22+
version = "~> 1.20"
23+
}
24+
25+
provider "random" {
26+
version = "~> 2.0"
27+
}
28+
1729
provider "null" {
18-
version = "~> 1.0"
30+
version = "~> 2.0"
31+
}
32+
33+
resource "random_pet" "main" {
34+
separator = "-"
1935
}
2036

2137
module "automatic_labelling" {
2238
source = "../../../examples/automatic_labelling"
2339

2440
project_id = "${var.project_id}"
41+
name = "automatic-labelling-${random_pet.main.id}"
2542
region = "${var.region}"
2643
}
2744

test/integration.sh

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,49 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
18-
set -x
17+
# Always clean up.
18+
DELETE_AT_EXIT="$(mktemp -d)"
19+
finish() {
20+
echo 'BEGIN: finish() trap handler' >&2
21+
kitchen destroy
22+
[[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}"
23+
echo 'END: finish() trap handler' >&2
24+
}
1925

20-
if [ -z "${PROJECT_ID}" ]; then
21-
echo "The PROJECT_ID ENV variable must be set to proceed. Aborting."
22-
exit 1
23-
fi
26+
# Map the input parameters provided by Concourse CI, or whatever mechanism is
27+
# running the tests to Terraform input variables. Also setup credentials for
28+
# use with kitchen-terraform, inspec, and gcloud.
29+
setup_environment() {
30+
local tmpfile
31+
tmpfile="$(mktemp)"
32+
echo "${SERVICE_ACCOUNT_JSON}" > "${tmpfile}"
2433

25-
if [ -z "${SERVICE_ACCOUNT_JSON}" ]; then
26-
echo "The SERVICE_ACCOUNT_JSON ENV variable must be set to proceed. Aborting."
27-
exit 1
28-
fi
34+
# gcloud variables
35+
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${tmpfile}"
36+
# Application default credentials (Terraform google provider and inspec-gcp)
37+
export GOOGLE_APPLICATION_CREDENTIALS="${tmpfile}"
38+
39+
# Terraform variables
40+
export TF_VAR_project_id="${PROJECT_ID}"
41+
export TF_VAR_region="${REGION:-us-east1}"
42+
export TF_VAR_zone="${ZONE:-us-east1-b}"
43+
}
2944

30-
export TF_VAR_project_id="${PROJECT_ID}"
31-
export TF_VAR_region="${REGION:-us-east1}"
32-
export TF_VAR_zone="${ZONE:-us-east1-b}"
45+
main() {
46+
set -eu
47+
# Setup trap handler to auto-cleanup
48+
export TMPDIR="${DELETE_AT_EXIT}"
49+
trap finish EXIT
3350

34-
DELETE_AT_EXIT="$(mktemp -d)"
35-
finish() {
36-
[[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}"
51+
# Setup environment variables
52+
setup_environment
53+
set -x
54+
55+
# Execute the test lifecycle
56+
kitchen verify
3757
}
38-
trap finish EXIT
39-
CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="$(TMPDIR="${DELETE_AT_EXIT}" mktemp)"
40-
set +x
41-
echo "${SERVICE_ACCOUNT_JSON}" > "${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE}"
42-
set -x
43-
GOOGLE_APPLICATION_CREDENTIALS="${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE}"
44-
declare -rx CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE GOOGLE_APPLICATION_CREDENTIALS
45-
set +e
46-
bundle install
47-
bundle exec kitchen test --destroy=always
58+
59+
# if script is being executed and not sourced.
60+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
61+
main "$@"
62+
fi

0 commit comments

Comments
 (0)