Skip to content

Commit

Permalink
fix: adding validation for snapshot name for hbase import pipeline (g…
Browse files Browse the repository at this point in the history
…oogleapis#3203)

* fix: adding validation for snapshot name

* add messages to failures in test

* Check empty project id and snapshot directory
  • Loading branch information
billyjacobson authored Sep 8, 2021
1 parent 26b20e9 commit fa9991a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit fa9991a

Please sign in to comment.