1# Introduction 2 3When you experience integration test failures, they may be due to concurrent tests accessing the 4same GCP project resources, required resources not existing on the GCP project, quota limits being 5exceeded due to overreliance on a single project, incorrect permissions, disabled APIs, and/or 6service account impersonation requirements. 7 8[Terraform](https://www.terraform.io/) is an "Infrastructure as Code" (IaC) tool that allows cloud 9and on-prem resources to be defined in configuration files to be versioned, reused, and shared. By 10using Terraform to provision our GCP projects prior to running integration tests we: 11 12* Ensure infrastructure provisioning consistency, 13* Prevent conflicts between concurrent integration test invocations using the same GCP project by 14 randomizing resource names, 15* Provide a managed lifecycle including the ability to destroy short-lived testing environments, 16* Version the infrastructure requirements with the test source. 17 18# Running `google-cloud-java` Integration Tests 19 20When you need to troubleshoot integration tests in the google-cloud-java repository, follow one of 21the supported workflows below to provision a GCP project with the required configuration. 22 23## Installation 24 251. Download and install the latest version of Terraform: https://www.terraform.io/downloads 262. *(Optional)* Update your default shell environment variables. (For Mac users, 27 modify `~/.bash_profile`. 28 For Linux users, modify `~/.bashrc`.) 29 * See 30 also: [About Cloud Billing accounts](https://cloud.google.com/billing/docs/how-to/manage-billing-account) 31 * See 32 also: [Creating and managing Folders](https://cloud.google.com/resource-manager/docs/creating-managing-folders) 33 34```shell 35export GOOGLE_CLOUD_FOLDER_ID="000000000000" 36export GOOGLE_CLOUD_BILLING_ACCOUNT="000000-000000-000000" 37export GOOGLE_CLOUD_PROJECT_PREFIX="my-project" 38``` 39 40## Supported Workflows 41 42During these workflows, you may be asked to authenticate one or two times with gcloud as it 43optionally creates a GCP project, and then sets up the 44[application-default credentials](https://cloud.google.com/docs/authentication/application-default-credentials) 45to be used by Terraform and the client libraries. 46 47A service account will be created, assigned `roles/owner`[^1], and used for all client library 48integration tests. Your user credentials must have permission in the designated GCP project to 49self-assign `roles/iam.serviceAccountTokenCreator` to impersonate the service account. 50 51[^1]: Basic roles like `roles/owner` are 52[not recommended in production environments](https://cloud.google.com/iam/docs/understanding-roles#basic) 53. 54 55### Workflow: Set up a GCP Project for **One or More** Client Libraries, then Test 56 571. Invoke `setup.sh` with the directory names of the selected client libraries: 58 ```shell 59 $ ./.cloud/setup.sh [library][,library ...] 60 ``` 61 62 Examples: 63 * `./.cloud/setup.sh java-accessapproval` 64 * `./.cloud/setup.sh java-asset,java-compute,java-container` 65 662. When ready to begin testing, invoke `verify.sh`: 67 ```shell 68 $ ./.cloud/verify.sh [library][,library ...] 69 ``` 70 71 Examples: 72 * `./.cloud/verify.sh java-accessapproval` 73 * `./.cloud/verify.sh java-asset,java-compute,java-container` 74 75Note: Invoking `verify.sh` without arguments will begin performing integration testing on all client 76libraries. In this workflow, the GCP project will only have been configured the project for a subset 77of client libraries, this command is unlikely to succeed unless the GCP project has been previously 78resourced and configured manually. If you wish to perform integration testing on all client 79libraries, use the below workflow to first provision the project for all client libraries. 80 81### Workflow: Set up a GCP Project for **All** Client Libraries, then Test 82 831. Invoke `setup.sh` without arguments: 84 ```shell 85 $ ./.cloud/setup.sh 86 ``` 87 Every client library with a `.cloud` subdirectory will be included in the generated Terraform 88 root module, and the GCP project will be prepared to test all client libraries. 892. When ready to begin testing, invoke `verify.sh` with or without arguments: 90 ```shell 91 $ ./.cloud/verify.sh 92 ``` 93 The above will perform integration tests on all client libraries. 94 ```shell 95 $ ./.cloud/verify.sh [library][,library ...] 96 ``` 97 The above will only perform integration testing on the specified client libraries. 98 99 Examples: 100 * `./.cloud/verify.sh` 101 * `./.cloud/verify.sh java-accessapproval` 102 * `./.cloud/verify.sh java-asset,java-compute,java-container` 103 104### Workflow: Set up and Test with One Command 105 106Use `test.sh` to perform a combination of both `setup.sh` and `verify.sh`. 107 108```shell 109$ ./.cloud/test.sh [library][,library ...] 110``` 111 112Examples: 113 114* `./.cloud/test.sh` 115* `./.cloud/test.sh java-accessapproval` 116* `./.cloud/test.sh java-asset,java-compute,java-container` 117 118### Workflow: Clean up GCP Project of Test Resources 119 120To clean-up (or “destroy”) all provisioned resources under Terraform’s management, 121invoke `cleanup.sh`: 122 123```shell 124$ ./.cloud/cleanup.sh 125``` 126 127### Workflow: Add / Remove Client Libraries in an Existing Test Configuration 128 129Terraform will automatically manage the resources for a GCP project when regenerating the root 130module to include different client libraries. No additional commands are necessary to support this 131use-case. 132 133Example: 134 135```shell 136$ ./.cloud/setup.sh java-bigqueryconnection 137# Terraform provisions the project for java-bigqueryconnection's requirements. 138 139$ ./.cloud/setup.sh java-accessapproval 140# Terraform simultaneously destroys java-bigqueryconnection's resources that 141# are no longer being used, and provisions the project for java-accessapproval's 142# requirements. 143# Warning: This 'implicit removal' is prone to errors. If an error occurs, an 144# explicit invocation of 'cleanup.sh' may be required. 145 146$ ./.cloud/setup.sh java-accessapproval,java-texttospeech 147# Terraform maintains the java-accessapproval resources while also provisioning 148# the project for java-texttospeech's requirements. 149``` 150