-
Notifications
You must be signed in to change notification settings - Fork 172
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
private SnowflakeGCSClient() {} | ||
|
||
/* | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
throw (StorageProviderException) SnowflakeGCSClient.injectedException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
return new StorageObjectSummaryCollection(blobs); | ||
} catch (Exception e) { | ||
logger.debug("Failed to list objects", false); | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be public |
||
return injectedException != null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in a finally block. If the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1489,55 +1489,6 @@ public void testNoSpaceLeftOnDeviceException() throws SQLException { | |
} | ||
} | ||
|
||
@Test | ||
@Disabled // TODO: ignored until SNOW-1616480 is resolved | ||
public void testUploadWithGCSPresignedUrlWithoutConnection() throws Throwable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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?