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