From 38925e8517403294b0958296aa5f20da6ba5cc55 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Thu, 23 Sep 2021 12:51:01 -0400 Subject: [PATCH] fix: adding validation for snapshot name for hbase import pipeline (#3203) (#3226) * fix: adding validation for snapshot name * add messages to failures in test * Check empty project id and snapshot directory (cherry picked from commit fa9991a2703c0faf4a1ba5737f5844619a497c17) --- .../HBaseSnapshotInputConfigBuilder.java | 13 +-- .../HBaseSnapshotInputConfigBuilderTest.java | 84 +++++++++++++++++++ 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/bigtable-dataflow-parent/bigtable-beam-import/src/main/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilder.java b/bigtable-dataflow-parent/bigtable-beam-import/src/main/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilder.java index 63ebddb20a..428f987f14 100644 --- a/bigtable-dataflow-parent/bigtable-beam-import/src/main/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilder.java +++ b/bigtable-dataflow-parent/bigtable-beam-import/src/main/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilder.java @@ -95,13 +95,16 @@ public String getRestoreDir() { } public Configuration build() throws Exception { - Preconditions.checkNotNull(projectId, "Required value projectId must be set"); - Preconditions.checkNotNull( - hbaseSnapshotSourceDir, "Required value hbaseSnapshotSourceDir must be set"); - Preconditions.checkNotNull(snapshotName, "Required value snapshotName must be set"); + Preconditions.checkState( + projectId != null && !projectId.isEmpty(), "Required value projectId must be set"); + Preconditions.checkState( + hbaseSnapshotSourceDir != null && !hbaseSnapshotSourceDir.isEmpty(), + "Required value hbaseSnapshotSourceDir must be set"); + Preconditions.checkState( + snapshotName != null && !snapshotName.isEmpty(), "Required value snapshotName must be set"); Preconditions.checkState( hbaseSnapshotSourceDir.startsWith(GcsPath.SCHEME), - "snapshot folder must be hosted in a GCS bucket "); + "Snapshot folder must be hosted in a GCS bucket"); Configuration conf = createHBaseConfiguration(); diff --git a/bigtable-dataflow-parent/bigtable-beam-import/src/test/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilderTest.java b/bigtable-dataflow-parent/bigtable-beam-import/src/test/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilderTest.java index 579a57c238..fb5346f72a 100644 --- a/bigtable-dataflow-parent/bigtable-beam-import/src/test/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilderTest.java +++ b/bigtable-dataflow-parent/bigtable-beam-import/src/test/java/com/google/cloud/bigtable/beam/hbasesnapshots/HBaseSnapshotInputConfigBuilderTest.java @@ -16,6 +16,7 @@ package com.google.cloud.bigtable.beam.hbasesnapshots; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.mapreduce.TableSnapshotInputFormat; @@ -45,4 +46,87 @@ public void testBuildingHBaseSnapshotInputConfigBuilder() { conf.getClass( "mapreduce.job.inputformat.class", TableSnapshotInputFormat.class, InputFormat.class)); } + + @Test + public void testInvalidProjectHBaseSnapshotInputConfig() { + try { + new HBaseSnapshotInputConfigBuilder() + .setSnapshotName(TEST_SNAPSHOT_NAME) + .setHbaseSnapshotSourceDir(TEST_SNAPSHOT_DIR) + .build(); + fail("Expected unset project to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value projectId must be set"); + } + + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId("") + .setSnapshotName(TEST_SNAPSHOT_NAME) + .setHbaseSnapshotSourceDir(TEST_SNAPSHOT_DIR) + .build(); + fail("Expected empty project to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value projectId must be set"); + } + } + + @Test + public void testInvalidSnapshotHBaseSnapshotInputConfig() { + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId(TEST_PROJECT) + .setHbaseSnapshotSourceDir(TEST_SNAPSHOT_DIR) + .build(); + fail("Expected unset snapshot name to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value snapshotName must be set"); + } + + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId(TEST_PROJECT) + .setSnapshotName("") + .setHbaseSnapshotSourceDir(TEST_SNAPSHOT_DIR) + .build(); + fail("Expected empty snapshot name to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value snapshotName must be set"); + } + } + + @Test + public void testInvalidSourceDirHBaseSnapshotInputConfig() { + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId(TEST_PROJECT) + .setSnapshotName(TEST_SNAPSHOT_NAME) + .build(); + fail("Expected unset snapshot directory to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value hbaseSnapshotSourceDir must be set"); + } + + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId(TEST_PROJECT) + .setSnapshotName(TEST_SNAPSHOT_NAME) + .setHbaseSnapshotSourceDir("") + .build(); + fail("Expected empty snapshot directory to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Required value hbaseSnapshotSourceDir must be set"); + } + + try { + new HBaseSnapshotInputConfigBuilder() + .setProjectId(TEST_PROJECT) + .setSnapshotName(TEST_SNAPSHOT_NAME) + .setHbaseSnapshotSourceDir("test-bucket/hbase-export") + .build(); + fail("Expected snapshot directory without gs prefix to fail"); + } catch (Exception e) { + assertEquals(e.getMessage(), "Snapshot folder must be hosted in a GCS bucket"); + } + } }