Skip to content
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

Snow-1016470: increase code coverage #2011

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class SnowflakeGCSClient implements SnowflakeStorageClient {

private static final SFLogger logger = SFLoggerFactory.getLogger(SnowflakeGCSClient.class);

private static Throwable injectedException = null; // for testing purpose
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using mocks instead for this test?


private SnowflakeGCSClient() {}

/*
Expand Down Expand Up @@ -198,6 +200,11 @@ public StorageObjectSummaryCollection listObjects(String remoteStorageLocation,
logger.debug(
"Listing objects in the bucket {} with prefix {}", remoteStorageLocation, prefix);
Page<Blob> blobs = this.gcsClient.list(remoteStorageLocation, BlobListOption.prefix(prefix));
// Normal flow will never hit here. This is only for testing purposes
if (isInjectedExceptionEnabled()
&& SnowflakeGCSClient.injectedException instanceof StorageProviderException) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this instanceof is needed. If we inject an exception, then we shouldn't check it's type, just throw it. If you want to have a guarantee of it's type, just make the injected exception of this type instead of throwable

throw (StorageProviderException) SnowflakeGCSClient.injectedException;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception should be a StorageException to accurately mimic a failure. See the documentation for the list(...) method.

}
return new StorageObjectSummaryCollection(blobs);
} catch (Exception e) {
logger.debug("Failed to list objects", false);
Expand Down Expand Up @@ -1387,4 +1394,13 @@ public String getStreamingIngestClientName(StorageObjectMetadata meta) {
public String getStreamingIngestClientKey(StorageObjectMetadata meta) {
return meta.getUserMetadata().get(GCS_STREAMING_INGEST_CLIENT_KEY);
}

// This function should only be used for testing purpose
public static void setInjectedException(Throwable th) {
injectedException = th;
}

public static boolean isInjectedExceptionEnabled() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be public

return injectedException != null;
}
}
28 changes: 28 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/ConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,34 @@ public void testFailOverOrgAccount() throws SQLException {
}
}

@Test
public void testDiagnosticCheckFailsWithNoAllowlistFileProvided() throws SQLException {
Properties props = new Properties();
props.put("ENABLE_DIAGNOSTICS", true);
try (Connection con = getConnection(props)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assertThrows instead

fail();
} catch (SnowflakeSQLException e) {
assertTrue(
e.getMessage()
.contains("Diagnostics was enabled but an allowlist file was not provided"));
}
}

@Test
public void testDiagnosticCheckWithFakeAllowlistFileProvided() throws SQLException {
Properties props = new Properties();
props.put("ENABLE_DIAGNOSTICS", true);
props.put("DIAGNOSTICS_ALLOWLIST_FILE", "/some/path/allowlist.json");
try (Connection con = getConnection(props)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, assetThrows

fail();
} catch (Exception e) {
assertTrue(
e.getMessage()
.contains(
"A connection was not created because the driver is running in diagnostics mode."));
}
}

private class ConcurrentConnections implements Runnable {

ConcurrentConnections() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,32 @@ public void testUploadWithTildeInPath() throws SQLException, IOException {
FileUtils.deleteDirectory(subDir.toFile());
}
}

@DontRunOnGithubActions
@Test
public void testListObjectsException() throws Exception {
Properties paramProperties = new Properties();
paramProperties.put("GCS_USE_DOWNSCOPED_CREDENTIAL", true);
SnowflakeGCSClient.setInjectedException(
new StorageProviderException(new Exception("could not list objects")));

try (Connection con = getConnection("gcpaccount", paramProperties);
Statement statement = con.createStatement()) {
try {
statement.execute("create or replace stage testStage");
SFSession sfSession = con.unwrap(SnowflakeConnectionV1.class).getSfSession();
String command = "PUT file://" + getFullPathFileInResource(TEST_DATA_FILE) + " @testStage";
SnowflakeFileTransferAgent sfAgent =
new SnowflakeFileTransferAgent(command, sfSession, new SFStatement(sfSession));

sfAgent.execute();
} catch (SnowflakeSQLException err) {
assertEquals(200016, err.getErrorCode());
assertTrue(err.getMessage().contains("Failed to list objects"));
} finally {
statement.execute("DROP STAGE if exists testStage");
}
}
SnowflakeGCSClient.setInjectedException(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in a finally block. If the DROP STAGE fails for whatever reason, this would not be called.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1489,55 +1489,6 @@ public void testNoSpaceLeftOnDeviceException() throws SQLException {
}
}

@Test
@Disabled // TODO: ignored until SNOW-1616480 is resolved
public void testUploadWithGCSPresignedUrlWithoutConnection() throws Throwable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this test?

File destFolder = new File(tmpFolder, "dest");
destFolder.mkdirs();
String destFolderCanonicalPath = destFolder.getCanonicalPath();
// set parameter for presignedUrl upload instead of downscoped token
Properties paramProperties = new Properties();
paramProperties.put("GCS_USE_DOWNSCOPED_CREDENTIAL", false);
try (Connection connection = getConnection("gcpaccount", paramProperties);
Statement statement = connection.createStatement()) {
try {
// create a stage to put the file in
statement.execute("CREATE OR REPLACE STAGE " + testStageName);

SFSession sfSession = connection.unwrap(SnowflakeConnectionV1.class).getSfSession();

// Test put file with internal compression
String putCommand = "put file:///dummy/path/file1.gz @" + testStageName;
SnowflakeFileTransferAgent sfAgent =
new SnowflakeFileTransferAgent(putCommand, sfSession, new SFStatement(sfSession));
List<SnowflakeFileTransferMetadata> metadata = sfAgent.getFileTransferMetadatas();

String srcPath = getFullPathFileInResource(TEST_DATA_FILE);
for (SnowflakeFileTransferMetadata oneMetadata : metadata) {
InputStream inputStream = new FileInputStream(srcPath);

assertTrue(oneMetadata.isForOneFile());
SnowflakeFileTransferAgent.uploadWithoutConnection(
SnowflakeFileTransferConfig.Builder.newInstance()
.setSnowflakeFileTransferMetadata(oneMetadata)
.setUploadStream(inputStream)
.setRequireCompress(true)
.setNetworkTimeoutInMilli(0)
.setOcspMode(OCSPMode.FAIL_OPEN)
.build());
}

assertTrue(
statement.execute(
"GET @" + testStageName + " 'file://" + destFolderCanonicalPath + "/' parallel=8"),
"Failed to get files");
assertTrue(isFileContentEqual(srcPath, false, destFolderCanonicalPath + "/file1.gz", true));
} finally {
statement.execute("DROP STAGE if exists " + testStageName);
}
}
}

@Test
@DontRunOnGithubActions
public void testUploadWithGCSDownscopedCredentialWithoutConnection() throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void tearDown() throws InterruptedException {
}

@SuppressWarnings("divzero")
@Disabled
@Test
public void testCreateException() {
TelemetryService service = TelemetryService.getInstance();
Expand Down Expand Up @@ -102,7 +101,6 @@ public void testWrongServerURL() throws InterruptedException {
assertThat("WrongServerURL do not block.", service.getEventCount() > count);
}

@Disabled
@Test
public void testCreateLog() {
// this log will be delivered to snowflake
Expand Down Expand Up @@ -161,7 +159,6 @@ public void stressTestCreateLog() {
sw.stop();
}

@Disabled
@Test
public void testCreateLogInBlackList() {
// this log will be delivered to snowflake
Expand Down
Loading