Skip to content

Latest commit

 

History

History
137 lines (97 loc) · 5.9 KB

TESTING.md

File metadata and controls

137 lines (97 loc) · 5.9 KB

gcloud-java tools for testing

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

  • [Datastore] (#testing-code-that-uses-datastore)
  • [Storage] (#testing-code-that-uses-storage)
  • [Resource Manager] (#testing-code-that-uses-resource-manager)
  • [BigQuery] (#testing-code-that-uses-bigquery)

Testing code that uses Datastore

On your machine

You can test against a temporary local datastore by following these steps:

  1. Start the local datastore emulator using LocalGcdHelper. This can be done in two ways:
  • Run LocalGcdHelper.java's main method with arguments START and (optionally) --port=<port number>. This will create a temporary folder on your computer and bind localhost:<port number> for communication with the local datastore. The port number is an optional argument. If no port number is specified, port 8080 will be used.
  • Call LocalGcdHelper.start(<project ID>, <port number>) before running your tests. Save the LocalGcdHelper object returned so that you can stop the emulator later.
  1. In your program, create and use a datastore whose host is set host to localhost:<port number>. For example,
DatastoreOptions options = DatastoreOptions.builder()
    .projectId(PROJECT_ID)
    .host("http://localhost:8080")
    .build();
Datastore localDatastore = options.service();
  1. Run your tests.

  2. Stop the local datastore emulator.

  • If you ran LocalGcdHelper.java's main function to start the emulator, run LocalGcdHelper.java's main method with arguments STOP and (optionally) --port=<port number>. If the port is not supplied, the program will attempt to close the last port started.
  • If you ran LocalGcdHelper.start() to start the emulator, call the stop() method on the LocalGcdHelper object returned by LocalGcdHelper.start().

On a remote machine

You can test against a remote datastore emulator as well. To do this, set the DatastoreOptions project endpoint to the hostname of the remote machine, like the example below.

DatastoreOptions options = DatastoreOptions.builder()
    .projectId(PROJECT_ID)
    .host("http://<hostname of machine>:<port>")
    .build();
Datastore localDatastore = options.service();

Note that the remote datastore must be running before your tests are run.

Testing code that uses Storage

Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. RemoteGcsHelper contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:

  1. Create a test Google Cloud project.

  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.

  3. Create a RemoteGcsHelper object using your project ID and JSON key. Here is an example that uses the RemoteGcsHelper to create a bucket.

RemoteGcsHelper gcsHelper =
    RemoteGcsHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Storage storage = gcsHelper.options().service();
String bucket = RemoteGcsHelper.generateBucketName();
storage.create(BucketInfo.of(bucket));
  1. Run your tests.

  2. Clean up the test project by using forceDelete to clear any buckets used. Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.

RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);

Testing code that uses Resource Manager

On your machine

You can test against a temporary local Resource Manager by following these steps:

  1. Before running your testing code, start the Resource Manager emulator LocalResourceManagerHelper. This can be done as follows:
import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper;

LocalResourceManagerHelper helper = LocalResourceManagerHelper.create();
helper.start();

This will spawn a server thread that listens to localhost at an ephemeral port for Resource Manager requests.

  1. In your program, create and use a Resource Manager service object whose host is set to localhost at the appropriate port. For example:
ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
  1. Run your tests.

  2. Stop the Resource Manager emulator.

helper.stop();

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

Testing code that uses BigQuery

Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test project. RemoteBigQueryHelper contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:

  1. Create a test Google Cloud project.

  2. Download a JSON service account credentials file from the Google Developer's Console.

  3. Create a RemoteBigQueryHelper object using your project ID and JSON key. Here is an example that uses the RemoteBigQueryHelper to create a dataset.

RemoteBigQueryHelper bigqueryHelper =
    RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
BigQuery bigquery = bigqueryHelper.options().service();
String dataset = RemoteBigQueryHelper.generateDatasetName();
bigquery.create(DatasetInfo.builder(dataset).build());
  1. Run your tests.

  2. Clean up the test project by using forceDelete to clear any datasets used. Here is an example that clears the dataset created in Step 3.

RemoteBigQueryHelper.forceDelete(bigquery, dataset);