Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
Refactor test app code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Jan 6, 2016
1 parent e5a4917 commit 581866c
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 413 deletions.
40 changes: 24 additions & 16 deletions test-apps/README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
gcloud-java Test Applications
=============================

This folder contains examples of end-to-end test apps for gcloud-java.
This folder contains examples of test apps for gcloud-java.

In order to run these tests, you will need to:

* Create a [Google Developers Console](https://console.developers.google.com/) project with the BigQuery, Datastore, and Storage JSON API enabled.
* [Enable billing](https://support.google.com/cloud/answer/6158867?hl=en). [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up.
* Create a [Google Developers Console](https://console.developers.google.com/) project with the BigQuery, Datastore, and Storage JSON APIs enabled. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up.
* [Enable billing](https://support.google.com/cloud/answer/6158867?hl=en).
* Set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.

To run the App Engine test app:
1. Change "your-app-id" to your project ID in `src/main/webapp/WEB-INF/appengine-web.xml` and in `src/main/java/com/google/gcloud/tests/gcloud-appengine-test/MainServlet.java`.
2. Create JSON service account credentials and save it as follows: `src/main/webapp/WEB-INF/lib/my-service-account-credentials.json`.

1. Change "your-project-id" to your project ID in `src/main/webapp/WEB-INF/appengine-web.xml` and in `src/main/java/com/google/gcloud/tests/gcloud-appengine-test/MainServlet.java`.
2. Create JSON service account credentials and save the file as follows: `src/main/webapp/WEB-INF/lib/my-service-account-credentials.json`.
3. Run `mvn appengine:devserver` to test locally and `mvn appengine:update` to test the app in production.
4. Navigate to the URL where the app is deployed. Add the service you wish to test to the URL ("bigquery", "datastore", "resourcemanager", or "storage"). You can also set optional request parameters for the project ID and whether to use the JSON credentials file you included in `WEB-INF/lib`. Here's an example URL using datastore with project ID "my-project-id" that uses the JSON credentials file:

```
http://localhost:8080/datastore?project-id=my-project-id&credentials-file=true
```

To run the Compute Engine test app:
To run the command line test app on Compute Engine:

1. Change "your-app-id" to your project ID in `src/main/java/com/google/gcloud/tests/gcloud-java-compute-test/GcloudJavaComputeEngineTest.java`.
1. Change "your-project-id" to your project ID in `src/main/java/com/google/gcloud/tests/gcloud-java-command-line-test/GcloudJavaCommandLineTest.java`.
2. Create JSON service account credentials and save the file in application's base directory as `my-service-account-credentials.json`.
3. Create a Compute Engine instance with the User Info, Big Query, Datastore, and Storage APIs (read write) enabled.
4. Copy your the app to your instance using the Google Cloud SDK command:

```
gcloud compute copy-files [the app's base directory] [username@instance-name]:~
gcloud compute copy-files test-apps/command-line-test-app [username@instance-name]:~
```
5. Ensure that Maven and the Java 7 JDK are installed. Also check that the environment variable `JAVA_HOME` points to the Java 7 JDK.
6. Run the app using the command
6. Ensure that you have User Credentials available on your Compute Engine instance. These credentials are necessary to use Resource Manager. If you get authentication errors stemming from inadequate authentication scope in step 7, you can copy your gcloud SDK credentials (usually located in the `~/.config/gcloud` directory) to your Compute Engine instance and set the GOOGLE_APPLICATION_CREDENTIALS environment variable as follows: `export GOOGLE_APPLICATION_CREDENTIALS=<insert-credential-location>`.
7. Run the app using Maven's exec plugin. Specify the service and optional parameters for explicitly setting a project ID and using your JSON credentials file as command line parameters. Here is an example of running the storage test, including both the optional parameters.

```
mvn clean compile exec:java -Dexec.mainClass="com.google.gcloud.tests.desktop.GcloudJavaComputeEngineTest"
mvn clean compile exec:java \
-Dexec.mainClass="com.google.gcloud.tests.commandline.GcloudJavaCommandLineTest" \
-Dexec.args="storage project-id=my-project-id credentials-file=true"
```

To run the desktop test app:
To run the command line test app on your desktop:

1. Change "your-app-id" to your project ID in `src/main/java/com/google/gcloud/tests/gcloud-java-desktop-test/GcloudJavaDesktopTest.java`.
1. Change "your-app-id" to your project ID in `src/main/java/com/google/gcloud/tests/gcloud-java-command-line-test/GcloudJavaCommandLineTest.java`.
2. Create JSON service account credentials and save the file in application's base directory as `my-service-account-credentials.json`.
3. Run the app using the command
```
mvn clean compile exec:java -Dexec.mainClass="com.google.gcloud.tests.desktop.GcloudJavaDesktopTest"
```
3. Run the app using the same command line structure as for Compute Engine (see step 7 under Compute Engine).

License
-------
Expand Down
14 changes: 10 additions & 4 deletions test-apps/app-engine-test-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>compile</phase>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../shared/src/main/java/com/google/gcloud/tests</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gcloud.tests.appengine;

import static java.nio.charset.StandardCharsets.UTF_8;
import static com.google.gcloud.tests.ServiceTests.SUPPORTED_SERVICES;
import static com.google.gcloud.tests.ServiceTests.runAction;

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.bigquery.BaseTableInfo;
import com.google.gcloud.bigquery.BigQuery;
import com.google.gcloud.bigquery.BigQueryOptions;
import com.google.gcloud.bigquery.DatasetInfo;
import com.google.gcloud.bigquery.Field;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.TableId;
import com.google.gcloud.bigquery.TableInfo;
import com.google.gcloud.bigquery.testing.RemoteBigQueryHelper;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import com.google.gcloud.storage.testing.RemoteGcsHelper;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
Expand All @@ -39,83 +33,38 @@

public class MainServlet extends HttpServlet {

private PrintWriter responseWriter;

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
responseWriter = resp.getWriter();
runBigQueryActions(BigQueryOptions.defaultInstance().service());
ServletContext context = getServletContext();
InputStream in =
context.getResourceAsStream("/WEB-INF/lib/my-service-account-credentials.json");
runDatastoreActions(
DatastoreOptions.builder()
.authCredentials(AuthCredentials.createForJson(in))
.projectId("your-project-id")
.build()
.service());
runStorageActions(StorageOptions.defaultInstance().service());
}

private void runBigQueryActions(BigQuery bigquery) {
responseWriter.println("Testing BigQuery.");
String datasetName = RemoteBigQueryHelper.generateDatasetName();
bigquery.create(DatasetInfo.builder(datasetName).build());
TableId tableId = TableId.of(datasetName, "my_table");
BaseTableInfo info = bigquery.getTable(tableId);
if (info == null) {
responseWriter.println("Creating table " + tableId);
Field integerField = Field.of("fieldName", Field.Type.integer());
bigquery.create(TableInfo.of(tableId, Schema.of(integerField)));
}
RemoteBigQueryHelper.forceDelete(bigquery, datasetName);
responseWriter.println("Finished BigQuery test.");
}

private void runDatastoreActions(Datastore datastore) {
responseWriter.println("Testing Datastore.");
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
Key key = keyFactory.newKey("myid");
Entity entity = datastore.get(key);
if (entity == null) {
entity = Entity.builder(key)
.set("name", "John Doe")
.set("age", 30)
.set("access_time", DateTime.now())
.build();
datastore.put(entity);
PrintWriter responseWriter = resp.getWriter();
String projectId = null;
AuthCredentials credentials = null;
String[] pathInfo = req.getPathInfo().split("/");
if (pathInfo.length != 2) {
printHelpMessage(responseWriter);
} else {
responseWriter.println("Updating access_time for " + entity.getString("name"));
entity = Entity.builder(entity)
.set("access_time", DateTime.now())
.build();
datastore.update(entity);
Map params = req.getParameterMap();
projectId =
params.get("project-id") != null ? ((String[]) params.get("project-id"))[0] : null;
Boolean useCredentialsFile =
params.get("credentials-file") != null
? Boolean.parseBoolean(((String[]) params.get("credentials-file"))[0]) : null;
if (useCredentialsFile != null) {
ServletContext context = getServletContext();
InputStream in =
context.getResourceAsStream("/WEB-INF/lib/my-service-account-credentials.json");
credentials = AuthCredentials.createForJson(in);
}
runAction(pathInfo[1], responseWriter, projectId, credentials);
}
responseWriter.println("Finished Datastore test.");
}

private void runStorageActions(Storage storage) {
responseWriter.println("Testing Storage.");
String bucketName = RemoteGcsHelper.generateBucketName();
storage.create(BucketInfo.of(bucketName));
BlobId blobId = BlobId.of(bucketName, "my_blob");
Blob blob = Blob.load(storage, blobId);
if (blob == null) {
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
responseWriter.println("Writing a file to Storage.");
} else {
responseWriter.println("Updating content for " + blobId.name());
byte[] prevContent = blob.content();
responseWriter.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
try {
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
channel.close();
} catch (IOException e) {
responseWriter.println(e.toString());
}
}
responseWriter.println("Finished Storage test.");
private static void printHelpMessage(PrintWriter pw) {
pw.println(
"Specify the service you wish to test in the URL (i.e. http://localhost:8080/datastore). "
+ "The following services are supported: " + SUPPORTED_SERVICES.toString() + ". "
+ "To explicitly set a project ID, specify the request parameter project-id=my-project-id. "
+ "To use a service account credentials file, specify the request parameter "
+ "'credentials-file=true'. An example of setting both parameters: "
+ "http://localhost:8080/datastore?project-id=my-project-id&credential-file=true");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<groupId>com.google.gcloud.tests</groupId>
<artifactId>gcloud-java-compute-test</artifactId>
<artifactId>gcloud-java-command-line-test</artifactId>
<version>1</version>
<prerequisites>
<maven>3.1.0</maven>
Expand Down Expand Up @@ -54,6 +54,25 @@
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../shared/src/main/java/com/google/gcloud/tests</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gcloud.tests.commandline;

import static com.google.gcloud.tests.ServiceTests.SUPPORTED_SERVICES;
import static com.google.gcloud.tests.ServiceTests.runAction;

import com.google.gcloud.AuthCredentials;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class GcloudJavaCommandLineTest {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out, true);
String projectId = null;
AuthCredentials credentials = null;
if (args.length == 0) {
pw.println("Must specify a service to test as the first command line argument. Options are: "
+ SUPPORTED_SERVICES.toString());
} else {
for (String arg : Arrays.copyOfRange(args, 1, args.length)) {
String[] argInfo = arg.split("=");
switch (argInfo[0]) {
case "project-id":
projectId = argInfo.length > 1 ? argInfo[1] : null;
break;
case "credentials-file":
credentials = AuthCredentials.createForJson(
new FileInputStream("my-service-account-credentials.json"));
break;
default:
pw.println("Unrecognized optional argument " + arg + ". Acceptable optional "
+ "arguments are 'project-id=[your-project-id]' and "
+ "'credentials-file=[true|false]'");
}
}
runAction(args[0], pw, projectId, credentials);
}
}
}
Loading

0 comments on commit 581866c

Please sign in to comment.