From 1a491c8fa43a7c3724c8e28535ea31c231c4e238 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Mon, 3 Jun 2019 15:48:31 -0400 Subject: [PATCH] Writing quickstart + test for Bigtable (#1431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Setup mvn package and copy in existing samples and tests * Fix style issues and ran tests * Writing quickstart + test for Bigtable * Updating pom with current versions * Adding default values for the system propertiesĖ† * surefire instead of failsafe * setup table for testing using cbt tool, retriggering test * Removing unnecessary code from quickstart test that was causing failures * cleaning up quickstart * Changing test variables to use GOOGLE_CLOUD_PROJECT and the instance environment variable --- .kokoro/tests/run_tests.sh | 1 + bigtable/pom.xml | 11 --- .../com/m/examples/bigtable/Quickstart.java | 59 +++++++++++++ .../m/examples/bigtable/HelloWorldTest.java | 21 +++-- .../bigtable/InstanceAdminExampleTest.java | 14 ++-- .../m/examples/bigtable/QuickstartTest.java | 82 +++++++++++++++++++ .../bigtable/TableAdminExampleTest.java | 22 ++--- 7 files changed, 171 insertions(+), 39 deletions(-) create mode 100644 bigtable/src/main/java/com/m/examples/bigtable/Quickstart.java create mode 100644 bigtable/src/test/java/com/m/examples/bigtable/QuickstartTest.java diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 89e660b41e7..8ef7495ac24 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -56,6 +56,7 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then source "${KOKORO_GFILE_DIR}/aws-secrets.sh" source "${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh" source "${KOKORO_GFILE_DIR}/dlp_secrets.txt" + source "${KOKORO_GFILE_DIR}/bigtable_secrets.txt" # Activate service account gcloud auth activate-service-account \ --key-file="$GOOGLE_APPLICATION_CREDENTIALS" \ diff --git a/bigtable/pom.xml b/bigtable/pom.xml index b9570dffeb9..86934be3887 100644 --- a/bigtable/pom.xml +++ b/bigtable/pom.xml @@ -98,17 +98,6 @@ maven-project-info-reports-plugin 3.0.0 - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M3 - - - java-docs-samples-testing - instance - - - diff --git a/bigtable/src/main/java/com/m/examples/bigtable/Quickstart.java b/bigtable/src/main/java/com/m/examples/bigtable/Quickstart.java new file mode 100644 index 00000000000..4d520614281 --- /dev/null +++ b/bigtable/src/main/java/com/m/examples/bigtable/Quickstart.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 Google LLC + * + * 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.m.examples.bigtable; + +// START [bigtable_quickstart_veneer] + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import com.google.cloud.bigtable.data.v2.models.Row; +import com.google.cloud.bigtable.data.v2.models.RowCell; + +public class Quickstart { + + public static void quickstart(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "my-table-id"; + + BigtableDataSettings settings = + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) { + try { + System.out.println("\nReading a single row by row key"); + Row row = dataClient.readRow(tableId, "r1"); + System.out.println("Row: " + row.getKey().toStringUtf8()); + for (RowCell cell : row.getCells()) { + System.out.printf( + "Family: %s Qualifier: %s Value: %s%n", + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); + } + } catch (NotFoundException e) { + System.err.println("Failed to read from a non-existent table: " + e.getMessage()); + } + + } catch (Exception e) { + System.out.println("Error during functionName: \n" + e.toString()); + } + } +} +// END [bigtable_quickstart_veneer] diff --git a/bigtable/src/test/java/com/m/examples/bigtable/HelloWorldTest.java b/bigtable/src/test/java/com/m/examples/bigtable/HelloWorldTest.java index e19a1a17e66..522a99e0148 100644 --- a/bigtable/src/test/java/com/m/examples/bigtable/HelloWorldTest.java +++ b/bigtable/src/test/java/com/m/examples/bigtable/HelloWorldTest.java @@ -40,8 +40,7 @@ /** Integration tests for {@link HelloWorld} */ public class HelloWorldTest { - private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; - private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; + private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE"; private static final String TABLE_PREFIX = "table"; private static String tableId; private static BigtableDataClient dataClient; @@ -50,10 +49,17 @@ public class HelloWorldTest { private static String instanceId; private HelloWorld helloWorld; + private static String requireEnv(String varName) { + assertNotNull( + System.getenv(varName), + "System property '%s' is required to perform these tests.".format(varName)); + return System.getenv(varName); + } + @BeforeClass public static void beforeClass() throws IOException { - projectId = System.getProperty(PROJECT_PROPERTY_NAME); - instanceId = System.getProperty(INSTANCE_PROPERTY_NAME); + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); + instanceId = requireEnv(INSTANCE_PROPERTY_NAME); if (projectId == null || instanceId == null) { dataClient = null; adminClient = null; @@ -79,13 +85,6 @@ public static void afterClass() throws Exception { @Before public void setup() throws IOException { - if (adminClient == null || dataClient == null) { - throw new AssumptionViolatedException( - PROJECT_PROPERTY_NAME - + " or " - + INSTANCE_PROPERTY_NAME - + " property is not set, skipping integration tests."); - } tableId = generateTableId(); helloWorld = new HelloWorld(projectId, instanceId, tableId); adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf1")); diff --git a/bigtable/src/test/java/com/m/examples/bigtable/InstanceAdminExampleTest.java b/bigtable/src/test/java/com/m/examples/bigtable/InstanceAdminExampleTest.java index 8a92449ba74..b7bcee7d3aa 100644 --- a/bigtable/src/test/java/com/m/examples/bigtable/InstanceAdminExampleTest.java +++ b/bigtable/src/test/java/com/m/examples/bigtable/InstanceAdminExampleTest.java @@ -42,7 +42,6 @@ /** Integration tests for {@link InstanceAdminExample} */ public class InstanceAdminExampleTest { - private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; private static final String ID_PREFIX = "instanceadmin"; private static final String CLUSTER = "cluster"; private static String projectId; @@ -51,9 +50,16 @@ public class InstanceAdminExampleTest { private String instanceId; private InstanceAdminExample instanceAdmin; + private static String requireEnv(String varName) { + assertNotNull( + System.getenv(varName), + "System property '%s' is required to perform these tests.".format(varName)); + return System.getenv(varName); + } + @BeforeClass public static void beforeClass() throws IOException { - projectId = System.getProperty(PROJECT_PROPERTY_NAME); + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); if (projectId == null) { adminClient = null; return; @@ -71,10 +77,6 @@ public static void afterClass() { @Before public void setup() throws IOException { - if (adminClient == null) { - throw new AssumptionViolatedException( - PROJECT_PROPERTY_NAME + " property is not set, skipping integration tests."); - } instanceId = generateId(); clusterId = generateId(); instanceAdmin = new InstanceAdminExample(projectId, instanceId, clusterId); diff --git a/bigtable/src/test/java/com/m/examples/bigtable/QuickstartTest.java b/bigtable/src/test/java/com/m/examples/bigtable/QuickstartTest.java new file mode 100644 index 00000000000..929506859ce --- /dev/null +++ b/bigtable/src/test/java/com/m/examples/bigtable/QuickstartTest.java @@ -0,0 +1,82 @@ +/* + * Copyright 2019 Google LLC + * + * 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.m.examples.bigtable; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Integration tests for {@link Quickstart} + */ +public class QuickstartTest { + + private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE"; + private static final String TABLE_ID = "quickstart-table"; + private static String projectId; + private static String instanceId; + private ByteArrayOutputStream bout; + + private static String requireEnv(String varName) { + assertNotNull( + System.getenv(varName), + "System property '%s' is required to perform these tests.".format(varName)); + return System.getenv(varName); + } + + @BeforeClass + public static void beforeClass() { + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); + instanceId = requireEnv(INSTANCE_PROPERTY_NAME); + } + + @Before + public void setupStream() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @Test + public void testQuickstart() { + try { + Quickstart.quickstart(projectId, instanceId, TABLE_ID); + } catch (Exception e) { + System.out.println("Failed to run quickstart."); + System.out.println(e); + } + + String output = bout.toString(); + assertThat(output, CoreMatchers.containsString("Reading a single row by row key")); + assertThat(output, CoreMatchers.containsString("Row: r1")); + assertThat( + output, CoreMatchers.containsString("Family: cf1 Qualifier: c1 Value: quickstart")); + } +} diff --git a/bigtable/src/test/java/com/m/examples/bigtable/TableAdminExampleTest.java b/bigtable/src/test/java/com/m/examples/bigtable/TableAdminExampleTest.java index d2163bf691a..51ed37cf053 100644 --- a/bigtable/src/test/java/com/m/examples/bigtable/TableAdminExampleTest.java +++ b/bigtable/src/test/java/com/m/examples/bigtable/TableAdminExampleTest.java @@ -18,6 +18,7 @@ import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; @@ -45,8 +46,7 @@ /** Integration tests for {@link TableAdminExample} */ public class TableAdminExampleTest { - private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; - private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; + private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE"; private static final String TABLE_PREFIX = "table"; private static BigtableTableAdminClient adminClient; private static String instanceId; @@ -54,10 +54,17 @@ public class TableAdminExampleTest { private String tableId; private TableAdminExample tableAdmin; + private static String requireEnv(String varName) { + assertNotNull( + System.getenv(varName), + "System property '%s' is required to perform these tests.".format(varName)); + return System.getenv(varName); + } + @BeforeClass public static void beforeClass() throws IOException { - projectId = System.getProperty(PROJECT_PROPERTY_NAME); - instanceId = System.getProperty(INSTANCE_PROPERTY_NAME); + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); + instanceId = requireEnv(INSTANCE_PROPERTY_NAME); if (projectId == null || instanceId == null) { adminClient = null; return; @@ -78,13 +85,6 @@ public static void afterClass() { @Before public void setup() throws IOException { - if (adminClient == null) { - throw new AssumptionViolatedException( - INSTANCE_PROPERTY_NAME - + " or " - + PROJECT_PROPERTY_NAME - + " property is not set, skipping integration tests."); - } tableId = generateTableId(); tableAdmin = new TableAdminExample(projectId, instanceId, tableId); adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf"));