-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to test with the services #218
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3751460
Document how to test
25bde60
Testing doc fixes and adding/updating examples
59722fa
Minor wording and link changes to testing readme.
26fb875
Fixing merge conflict in README.md
396db25
fix typo in README.md
b52ded3
make testing directions more standardized between datastore and storage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## gcloud-java tools for testing | ||
|
||
gcloud-java provides tools to make testing your application easier. | ||
|
||
### Testing interactions with Datastore | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
#### 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. | ||
|
||
2. In your program, create and use a datastore whose host is set host to `localhost:<port number>`. For example, | ||
```java | ||
DatastoreOptions options = DatastoreOptions.builder() | ||
.projectId(PROJECT_ID) | ||
.host("localhost:8080") | ||
.build(); | ||
Datastore localDatastore = DatastoreFactory.instance().get(options); | ||
``` | ||
3. Run your tests. | ||
|
||
4. 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. | ||
|
||
```java | ||
DatastoreOptions options = DatastoreOptions.builder() | ||
.projectId(PROJECT_ID) | ||
.host("http://<hostname of machine>") | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
.build(); | ||
Datastore localDatastore = DatastoreFactory.instance().get(options); | ||
``` | ||
|
||
Note that the remote datastore must be running before your tests are run. Also note that the `host` argument must start with "http://" or "https://" if you are testing with a remote machine. | ||
|
||
|
||
### Testing interactions with Storage | ||
|
||
There currently 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. Create and 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]. | ||
|
||
3. Set environment variables `GCLOUD_TESTS_PROJECT_ID` and `GCLOUD_TESTS_KEY` according to your test project's ID and the location of the newly-downloaded JSON key file. On linux and mac, for example, | ||
``` | ||
export GCLOUD_TESTS_PROJECT_ID=<project id> | ||
export GCLOUD_TESTS_KEY=/path/to/JSON/key.json | ||
``` | ||
|
||
4. Create and and use a `RemoteGcsHelper` object. | ||
Here is an example that uses the `RemoteGcsHelper` to create a bucket and clear the bucket at the end of the test. | ||
```java | ||
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
Storage storage = StorageFactory.instance().get(gcsHelper.options()); | ||
String bucket = RemoteGcsHelper.generateBucketName(); | ||
storage.create(BucketInfo.of(bucket)); | ||
// Do tests using Storage | ||
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS); | ||
``` | ||
|
||
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.