Skip to content

Commit

Permalink
Fix testUtils to not repeatedly do keypair generation (#855)
Browse files Browse the repository at this point in the history
TestUtils has four unexpected behaviors:

When there is no profile.json available, we do keypair generation many many times in every testcase instead of doing it once. Fixed this.
The no-profile-json code path ends up hitting snowflake.qa1.int.snowflakecomputing.com from unit tests (should be hitting a mock)
If a profile.json is being used for local testing, all unit tests start using it too because TestUtils ends up reading the same profile.json. This is highly unexpected.
Profile.json parsing behavior between prod and test codepaths is different. the former is okay with just the URL being specified, the latter forces specification of scheme, host, port, ssl.
Fixing (1) with this PR.
  • Loading branch information
sfc-gh-hmadan authored Oct 4, 2024
1 parent f74ea1b commit f8a684c
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/test/java/net/snowflake/ingest/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class TestUtils {

private static final ObjectMapper mapper = new ObjectMapper();

private static ObjectNode profile = null;
private static boolean isInitialized = false;

private static String user = "";

Expand Down Expand Up @@ -109,7 +109,7 @@ private static void init() throws NoSuchAlgorithmException, InvalidKeySpecExcept
Path path = Paths.get(testProfilePath);

if (Files.exists(path)) {
profile = (ObjectNode) mapper.readTree(new String(Files.readAllBytes(path)));
ObjectNode profile = (ObjectNode) mapper.readTree(new String(Files.readAllBytes(path)));

user = profile.get(USER).asText();
account = profile.get(ACCOUNT).asText();
Expand Down Expand Up @@ -141,6 +141,8 @@ private static void init() throws NoSuchAlgorithmException, InvalidKeySpecExcept
privateKey = keyPair.getPrivate();
privateKeyPem = java.util.Base64.getEncoder().encodeToString(privateKey.getEncoded());
}

isInitialized = true;
}

/** @return profile path that will be used for tests. */
Expand All @@ -153,80 +155,80 @@ private static String getTestProfilePath() {
}

public static String getUser() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return user;
}

public static String getAccount() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return account;
}

public static String getAccountURL() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}

return Utils.constructAccountUrl(scheme, host, port);
}

public static String getRole() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}

return role;
}

public static String getWarehouse() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return warehouse;
}

public static String getHost() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return host;
}

public static String getPrivateKey() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return privateKeyPem;
}

public static KeyPair getKeyPair() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return keyPair;
}

public static String getDatabase() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return database;
}

public static String getSchema() throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
return schema;
}

public static Properties getProperties(Constants.BdecVersion bdecVersion, boolean useDefaultRole)
throws Exception {
if (profile == null) {
if (!isInitialized) {
init();
}
Properties props = new Properties();
Expand Down Expand Up @@ -272,7 +274,10 @@ public static Connection getConnection(boolean isStreamingConnection) throws Exc
return streamingConn;
}

if (profile == null) init();
if (!isInitialized) {
init();
}

// check first to see if we have the Snowflake JDBC
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");

Expand Down Expand Up @@ -321,7 +326,10 @@ public static ResultSet executeQuery(String query) {
* @throws Exception
*/
public static SimpleIngestManager getManager(String pipe) throws Exception {
if (profile == null) init();
if (!isInitialized) {
init();
}

return new SimpleIngestManager(
account, user, database + "." + schema + "." + pipe, privateKey, scheme, host, port);
}
Expand All @@ -337,7 +345,10 @@ public static SimpleIngestManager getManager(String pipe) throws Exception {
*/
public static SimpleIngestManager getManager(String pipe, final String userAgentSuffix)
throws Exception {
if (profile == null) init();
if (!isInitialized) {
init();
}

return new SimpleIngestManager(
account,
user,
Expand All @@ -355,7 +366,10 @@ public static SimpleIngestManager getManager(String pipe, final String userAgent
*/
public static SimpleIngestManager getManagerUsingBuilderPattern(
String pipe, final String userAgentSuffix) throws Exception {
if (profile == null) init();
if (!isInitialized) {
init();
}

return new SimpleIngestManager.Builder()
.setAccount(account)
.setUser(user)
Expand Down

0 comments on commit f8a684c

Please sign in to comment.