Skip to content

Commit b7f8480

Browse files
committed
Add missing links to TESTING.md, fix ordering, add Compute section
1 parent 68e61d1 commit b7f8480

File tree

1 file changed

+103
-74
lines changed

1 file changed

+103
-74
lines changed

TESTING.md

+103-74
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,68 @@
22

33
This library provides tools to help write tests for code that uses the following gcloud-java services:
44

5+
- [BigQuery] (#testing-code-that-uses-bigquery)
6+
- [Compute] (#testing-code-that-uses-compute)
57
- [Datastore] (#testing-code-that-uses-datastore)
6-
- [Storage] (#testing-code-that-uses-storage)
8+
- [DNS] (#testing-code-that-uses-dns)
9+
- [PubSub] (#testing-code-that-uses-pubsub)
710
- [Resource Manager] (#testing-code-that-uses-resource-manager)
8-
- [BigQuery] (#testing-code-that-uses-bigquery)
11+
- [Storage] (#testing-code-that-uses-storage)
12+
13+
### Testing code that uses BigQuery
14+
15+
Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test
16+
project. `RemoteBigQueryHelper` contains convenience methods to make setting up and cleaning up the
17+
test project easier. To use this class, follow the steps below:
18+
19+
1. Create a test Google Cloud project.
20+
21+
2. Download a [JSON service account credentials file][create-service-account] from the Google
22+
Developer's Console.
23+
24+
3. Create a `RemoteBigQueryHelper` object using your project ID and JSON key.
25+
Here is an example that uses the `RemoteBigQueryHelper` to create a dataset.
26+
```java
27+
RemoteBigQueryHelper bigqueryHelper =
28+
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
29+
BigQuery bigquery = bigqueryHelper.options().service();
30+
String dataset = RemoteBigQueryHelper.generateDatasetName();
31+
bigquery.create(DatasetInfo.builder(dataset).build());
32+
```
33+
34+
4. Run your tests.
35+
36+
5. Clean up the test project by using `forceDelete` to clear any datasets used.
37+
Here is an example that clears the dataset created in Step 3.
38+
```java
39+
RemoteBigQueryHelper.forceDelete(bigquery, dataset);
40+
```
41+
42+
### Testing code that uses Compute
43+
44+
Currently, there isn't an emulator for Google Compute, so an alternative is to create a test
45+
project. `RemoteComputeHelper` contains convenience methods to make setting up the test project
46+
easier. To use this class, follow the steps below:
47+
48+
1. Create a test Google Cloud project.
49+
50+
2. Download a [JSON service account credentials file][create-service-account] from the Google
51+
Developer's Console.
52+
53+
3. Create a `RemoteComputeHelper` object using your project ID and JSON key. Here is an example that
54+
uses the `RemoteComputeHelper` to create an address.
55+
```java
56+
RemoteComputeHelper computeHelper =
57+
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
58+
Compute compute = computeHelper.options().service();
59+
// Pick a name for the resource with low probability of clashing
60+
String addressName = RemoteComputeHelper.baseResourceName() + "address";
61+
AddressId addressId = RegionAddressId.of(REGION, addressName);
62+
AddressInfo addressInfo = AddressInfo.of(addressId);
63+
Operation operation = compute.create(addressInfo);
64+
```
65+
66+
4. Run your tests.
967

1068
### Testing code that uses Datastore
1169

@@ -88,30 +146,45 @@ You can test against an in-memory local DNS by following these steps:
88146

89147
This method will block until the server thread has been terminated.
90148

91-
### Testing code that uses Storage
149+
### Testing code that uses Pub/Sub
92150

93-
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
151+
#### On your machine
94152

95-
1. Create a test Google Cloud project.
153+
You can test against a temporary local Pub/Sub by following these steps:
96154

97-
2. Download a JSON service account credentials file from the Google Developer's Console. See more about this on the [Google Cloud Platform Storage Authentication page][cloud-platform-storage-authentication].
155+
1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create`
156+
and `start` methods. This will bind a port for communication with the local Pub/Sub emulator.
157+
```java
158+
LocalPubSubHelper helper = LocalPubSubHelper.create();
98159

99-
3. Create a `RemoteStorageHelper` object using your project ID and JSON key.
100-
Here is an example that uses the `RemoteStorageHelper` to create a bucket.
160+
helper.start(); // Starts the local Pub/Sub emulator in a separate process
161+
```
162+
163+
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
164+
example:
101165
```java
102-
RemoteStorageHelper helper =
103-
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
104-
Storage storage = helper.options().service();
105-
String bucket = RemoteStorageHelper.generateBucketName();
106-
storage.create(BucketInfo.of(bucket));
166+
PubSub localPubsub = helper.options().service();
107167
```
108168

109-
4. Run your tests.
169+
3. Run your tests.
110170

111-
5. Clean up the test project by using `forceDelete` to clear any buckets used.
112-
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
171+
4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so:
113172
```java
114-
RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
173+
helper.stop();
174+
```
175+
176+
#### On a remote machine
177+
178+
You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project
179+
endpoint to the hostname of the remote machine, like the example below.
180+
181+
```java
182+
PubSubOptions options = PubSubOptions.builder()
183+
.projectId("my-project-id") // must match project ID specified on remote machine
184+
.host("<hostname of machine>:<port>")
185+
.authCredentials(AuthCredentials.noAuth())
186+
.build();
187+
PubSub localPubsub= options.service();
115188
```
116189

117190
### Testing code that uses Resource Manager
@@ -145,74 +218,30 @@ You can test against an in-memory local Resource Manager by following these step
145218

146219
This method will block until the server thread has been terminated.
147220

148-
### Testing code that uses BigQuery
221+
### Testing code that uses Storage
149222

150-
Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test
151-
project. `RemoteBigQueryHelper` contains convenience methods to make setting up and cleaning up the
152-
test project easier. To use this class, follow the steps below:
223+
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
153224

154225
1. Create a test Google Cloud project.
155226

156-
2. Download a [JSON service account credentials file][create-service-account] from the Google
157-
Developer's Console.
227+
2. Download a JSON service account credentials file from the Google Developer's Console. See more about this on the [Google Cloud Platform Storage Authentication page][cloud-platform-storage-authentication].
158228

159-
3. Create a `RemoteBigQueryHelper` object using your project ID and JSON key.
160-
Here is an example that uses the `RemoteBigQueryHelper` to create a dataset.
229+
3. Create a `RemoteStorageHelper` object using your project ID and JSON key.
230+
Here is an example that uses the `RemoteStorageHelper` to create a bucket.
161231
```java
162-
RemoteBigQueryHelper bigqueryHelper =
163-
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
164-
BigQuery bigquery = bigqueryHelper.options().service();
165-
String dataset = RemoteBigQueryHelper.generateDatasetName();
166-
bigquery.create(DatasetInfo.builder(dataset).build());
232+
RemoteStorageHelper helper =
233+
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
234+
Storage storage = helper.options().service();
235+
String bucket = RemoteStorageHelper.generateBucketName();
236+
storage.create(BucketInfo.of(bucket));
167237
```
168238

169239
4. Run your tests.
170240

171-
5. Clean up the test project by using `forceDelete` to clear any datasets used.
172-
Here is an example that clears the dataset created in Step 3.
173-
```java
174-
RemoteBigQueryHelper.forceDelete(bigquery, dataset);
175-
```
176-
177-
### Testing code that uses Pub/Sub
178-
179-
#### On your machine
180-
181-
You can test against a temporary local Pub/Sub by following these steps:
182-
183-
1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create`
184-
and `start` methods. This will bind a port for communication with the local Pub/Sub emulator.
185-
```java
186-
LocalPubSubHelper helper = LocalPubSubHelper.create();
187-
188-
helper.start(); // Starts the local Pub/Sub emulator in a separate process
189-
```
190-
191-
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
192-
example:
193-
```java
194-
PubSub localPubsub = helper.options().service();
195-
```
196-
197-
3. Run your tests.
198-
199-
4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so:
200-
```java
201-
helper.stop();
202-
```
203-
204-
#### On a remote machine
205-
206-
You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project
207-
endpoint to the hostname of the remote machine, like the example below.
208-
241+
5. Clean up the test project by using `forceDelete` to clear any buckets used.
242+
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
209243
```java
210-
PubSubOptions options = PubSubOptions.builder()
211-
.projectId("my-project-id") // must match project ID specified on remote machine
212-
.host("<hostname of machine>:<port>")
213-
.authCredentials(AuthCredentials.noAuth())
214-
.build();
215-
PubSub localPubsub= options.service();
244+
RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
216245
```
217246

218247
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts

0 commit comments

Comments
 (0)