Skip to content

Commit

Permalink
Remove TestUtil.systemGetEnv, replace all uses by SnowflakeUtil function
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmartinezramirez committed Jun 3, 2024
1 parent ee437de commit 48d5dfc
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 119 deletions.
32 changes: 17 additions & 15 deletions FIPS/src/test/java/net/snowflake/client/AbstractDriverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.snowflake.client.jdbc.SnowflakeUtil;
import org.junit.Rule;

/** Base test class with common constants, data structures and methods */
Expand Down Expand Up @@ -49,13 +51,13 @@ public static Map<String, String> getConnectionParameters(String accountName) {
String account;
String host;
if (accountName == null) {
account = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
host = TestUtil.systemGetEnv("SNOWFLAKE_TEST_HOST");
account = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
host = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_HOST");
} else {
account = accountName;
// By default, the test will run against reg deployment.
// If developer needs to run in IntelliJ, you can set this env as ".dev.local"
String deployment = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DEPLOYMENT");
String deployment = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_DEPLOYMENT");
if (Strings.isNullOrEmpty(deployment)) {
deployment = ".reg.local";
}
Expand All @@ -75,7 +77,7 @@ public static Map<String, String> getConnectionParameters(String accountName) {
!Strings.isNullOrEmpty(host));
params.put("host", host);

String protocol = TestUtil.systemGetEnv("SNOWFLAKE_TEST_PROTOCOL");
String protocol = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PROTOCOL");
String ssl;
if ("http".equals(protocol)) {
ssl = "off";
Expand All @@ -84,16 +86,16 @@ public static Map<String, String> getConnectionParameters(String accountName) {
}
params.put("ssl", ssl);

String user = TestUtil.systemGetEnv("SNOWFLAKE_TEST_USER");
String user = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_USER");
assertThat("set SNOWFLAKE_TEST_USER environment variable.", !Strings.isNullOrEmpty(user));
params.put("user", user);

String password = TestUtil.systemGetEnv("SNOWFLAKE_TEST_PASSWORD");
String password = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PASSWORD");
assertThat(
"set SNOWFLAKE_TEST_PASSWORD environment variable.", !Strings.isNullOrEmpty(password));
params.put("password", password);

String port = TestUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
String port = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
if (Strings.isNullOrEmpty(port)) {
if ("on".equals(ssl)) {
port = "443";
Expand All @@ -104,36 +106,36 @@ public static Map<String, String> getConnectionParameters(String accountName) {
assertThat("set SNOWFLAKE_TEST_PORT environment variable.", !Strings.isNullOrEmpty(port));
params.put("port", port);

String database = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE");
String database = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE");
assertThat(
"set SNOWFLAKE_TEST_DATABASE environment variable.", !Strings.isNullOrEmpty(database));
params.put("database", database);

String schema = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA");
String schema = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA");
assertThat("set SNOWFLAKE_TEST_SCHEMA environment variable.", !Strings.isNullOrEmpty(schema));
params.put("schema", schema);

String role = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ROLE");
String role = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ROLE");
assertThat("set SNOWFLAKE_TEST_ROLE environment variable.", !Strings.isNullOrEmpty(role));
params.put("role", role);

String warehouse = TestUtil.systemGetEnv("SNOWFLAKE_TEST_WAREHOUSE");
String warehouse = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_WAREHOUSE");
assertThat(
"set SNOWFLAKE_TEST_WAREHOUSE environment variable.", !Strings.isNullOrEmpty(warehouse));
params.put("warehouse", warehouse);

params.put("uri", String.format("jdbc:snowflake://%s:%s", host, port));

String adminUser = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_USER");
String adminUser = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_USER");
params.put("adminUser", adminUser);

String adminPassword = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_PASSWORD");
String adminPassword = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_PASSWORD");
params.put("adminPassword", adminPassword);

String ssoUser = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_USER");
String ssoUser = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_USER");
params.put("ssoUser", ssoUser);

String ssoPassword = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_PASSWORD");
String ssoPassword = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_PASSWORD");
params.put("ssoPassword", ssoPassword);

return params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*/
package net.snowflake.client;

import net.snowflake.client.jdbc.SnowflakeUtil;

/** Run tests on CI */
public class RunningOnGithubActions implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
return TestUtil.systemGetEnv("GITHUB_ACTIONS") != null;
return SnowflakeUtil.systemGetEnv("GITHUB_ACTIONS") != null;
}
}
27 changes: 0 additions & 27 deletions FIPS/src/test/java/net/snowflake/client/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,4 @@ public static void assertSFException(int errorCode, TestRunInterface testCode) {
public interface TestRunInterface {
void run() throws SFException;
}

/**
* System.getenv wrapper. If System.getenv raises an SecurityException, it is ignored and returns
* null.
*
* <p>This is replicated from SnowflakeUtil.systemGetEnv, because the old driver doesn't have that
* function for the tests to use it. Replace this function call with SnowflakeUtil.systemGetEnv
* when it is available.
*
* @param env the environment variable name.
* @return the environment variable value if set, otherwise null.
*/
public static String systemGetEnv(String env) {
try {
String value = System.getenv(env);
if(value == null) {
value = System.getenv().getOrDefault(env, null);
}
return value;
} catch (SecurityException ex) {
logger.debug(
"Failed to get environment variable {}. Security exception raised: {}",
env,
ex.getMessage());
}
return null;
}
}
3 changes: 2 additions & 1 deletion src/test/java/net/snowflake/client/AbstractDriverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import net.snowflake.client.jdbc.SnowflakeUtil;
import org.junit.Rule;

/** Base test class with common constants, data structures and methods */
Expand Down Expand Up @@ -54,7 +55,7 @@ private static String getConnPropKeyFromEnv(String connectionType, String propKe

private static String getConnPropValueFromEnv(String connectionType, String propKey) {
String envKey = String.format("SNOWFLAKE_%s_%s", connectionType, propKey);
return TestUtil.systemGetEnv(envKey);
return SnowflakeUtil.systemGetEnv(envKey);
}

public static Map<String, String> getConnectionParameters(String accountName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static net.snowflake.client.RunningOnGithubAction.isRunningOnGithubAction;

import net.snowflake.client.jdbc.SnowflakeUtil;

public class RunningNotOnTestaccount implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
return (!("testaccount".equals(TestUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT")))
return (!("testaccount".equals(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT")))
|| isRunningOnGithubAction());
}
}
6 changes: 4 additions & 2 deletions src/test/java/net/snowflake/client/RunningOnGithubAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
*/
package net.snowflake.client;

import net.snowflake.client.jdbc.SnowflakeUtil;

/** Run tests on CI */
public class RunningOnGithubAction implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
return TestUtil.systemGetEnv("GITHUB_ACTIONS") != null;
return SnowflakeUtil.systemGetEnv("GITHUB_ACTIONS") != null;
}

public static boolean isRunningOnGithubAction() {
return TestUtil.systemGetEnv("GITHUB_ACTIONS") != null;
return SnowflakeUtil.systemGetEnv("GITHUB_ACTIONS") != null;
}
}
4 changes: 3 additions & 1 deletion src/test/java/net/snowflake/client/RunningOnTestaccount.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.snowflake.client;

import net.snowflake.client.jdbc.SnowflakeUtil;

public class RunningOnTestaccount implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
return TestUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT").contains("testaccount");
return SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT").contains("testaccount");
}
}
4 changes: 3 additions & 1 deletion src/test/java/net/snowflake/client/SkipOnThinJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*/
package net.snowflake.client;

import net.snowflake.client.jdbc.SnowflakeUtil;

/** Skip tests on CI when thin jar is tested */
public class SkipOnThinJar implements ConditionalIgnoreRule.IgnoreCondition {
@Override
public boolean isSatisfied() {
return "-Dthin-jar".equals(TestUtil.systemGetEnv("ADDITIONAL_MAVEN_PROFILE"));
return "-Dthin-jar".equals(SnowflakeUtil.systemGetEnv("ADDITIONAL_MAVEN_PROFILE"));
}
}
27 changes: 0 additions & 27 deletions src/test/java/net/snowflake/client/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,6 @@ public interface TestRunInterface {
void run() throws SFException;
}

/**
* System.getenv wrapper. If System.getenv raises an SecurityException, it is ignored and returns
* null.
*
* <p>This is replicated from SnowflakeUtil.systemGetEnv, because the old driver doesn't have that
* function for the tests to use it. Replace this function call with SnowflakeUtil.systemGetEnv
* when it is available.
*
* @param env the environment variable name.
* @return the environment variable value if set, otherwise null.
*/
public static String systemGetEnv(String env) {
try {
String value = System.getenv(env);
if (value == null) {
value = System.getenv().getOrDefault(env, null);
}
return value;
} catch (SecurityException ex) {
logger.debug(
"Failed to get environment variable {}. Security exception raised: {}",
env,
ex.getMessage());
}
return null;
}

public static void assertValidQueryId(String queryId) {
assertNotNull(queryId);
assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.IOException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import net.snowflake.client.TestUtil;
import net.snowflake.client.category.TestCategoryCore;
import net.snowflake.client.jdbc.SnowflakeUtil;
import org.junit.Ignore;
Expand Down Expand Up @@ -41,7 +40,7 @@ public void testUnitOCSPWithCustomCacheDirectory() throws IOException {

File cacheFolder = tmpFolder.newFolder();
mockedSnowflakeUtil
.when(() -> TestUtil.systemGetEnv("SF_OCSP_RESPONSE_CACHE_DIR"))
.when(() -> SnowflakeUtil.systemGetEnv("SF_OCSP_RESPONSE_CACHE_DIR"))
.thenReturn(cacheFolder.getCanonicalPath());

TrustManagerFactory tested = mock(TrustManagerFactory.class);
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package net.snowflake.client.core;

import static net.snowflake.client.TestUtil.systemGetEnv;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -25,6 +24,7 @@
import net.snowflake.client.jdbc.BaseJDBCTest;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import net.snowflake.client.jdbc.SnowflakeUtil;
import net.snowflake.common.core.ClientAuthnDTO;
import net.snowflake.common.core.SqlState;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -84,14 +84,14 @@ public void testJwtAuthTimeoutRetry() throws SFException, SnowflakeSQLException
private SFLoginInput initMockLoginInput() {
// mock SFLoginInput
SFLoginInput loginInput = mock(SFLoginInput.class);
when(loginInput.getServerUrl()).thenReturn(systemGetEnv("SNOWFLAKE_TEST_HOST"));
when(loginInput.getServerUrl()).thenReturn(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_HOST"));
when(loginInput.getAuthenticator())
.thenReturn(ClientAuthnDTO.AuthenticatorType.SNOWFLAKE_JWT.name());
when(loginInput.getPrivateKeyFile())
.thenReturn(systemGetEnv("SNOWFLAKE_TEST_PRIVATE_KEY_FILE"));
.thenReturn(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PRIVATE_KEY_FILE"));
when(loginInput.getPrivateKeyFilePwd())
.thenReturn(systemGetEnv("SNOWFLAKE_TEST_PRIVATE_KEY_FILE_PWD"));
when(loginInput.getUserName()).thenReturn(systemGetEnv("SNOWFLAKE_TEST_USER"));
.thenReturn(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PRIVATE_KEY_FILE_PWD"));
when(loginInput.getUserName()).thenReturn(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_USER"));
when(loginInput.getAccountName()).thenReturn("testaccount");
when(loginInput.getAppId()).thenReturn("testid");
when(loginInput.getOCSPMode()).thenReturn(OCSPMode.FAIL_OPEN);
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/net/snowflake/client/jdbc/ConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import net.snowflake.client.ConditionalIgnoreRule.ConditionalIgnore;
import net.snowflake.client.RunningNotOnTestaccount;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.TestUtil;
import net.snowflake.client.category.TestCategoryConnection;
import net.snowflake.client.core.SFSession;
import net.snowflake.common.core.SqlState;
Expand Down Expand Up @@ -220,8 +219,8 @@ public void testConnectionGetAndSetDBAndSchema() throws SQLException {
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
try {
final String database = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE").toUpperCase();
final String schema = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA").toUpperCase();
final String database = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE").toUpperCase();
final String schema = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA").toUpperCase();

assertEquals(database, con.getCatalog());
assertEquals(schema, con.getSchema());
Expand Down
27 changes: 13 additions & 14 deletions src/test/java/net/snowflake/client/jdbc/ConnectionManual.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;
import net.snowflake.client.TestUtil;
import net.snowflake.client.core.SessionUtil;

/**
Expand Down Expand Up @@ -51,9 +50,9 @@ public static void main(String args[]) throws Throwable {
}

private Properties getProperties() {
String account = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
String ssoUser = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_USER");
String ssl = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SSL");
String account = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
String ssoUser = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SSO_USER");
String ssl = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SSL");

Properties properties = new Properties();
properties.put("user", ssoUser);
Expand All @@ -66,20 +65,20 @@ private Properties getProperties() {
}

private String getUrl() {
String account = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
String port = TestUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
String account = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ACCOUNT");
String port = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
return String.format("jdbc:snowflake://%s.reg.snowflakecomputing.com:%s", account, port);
}

private Connection getAdminConnection() throws Throwable {
String adminAccount = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_ACCOUNT");
String adminAccount = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_ACCOUNT");
if (adminAccount == null) {
adminAccount = "snowflake";
}
String adminUser = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_USER");
String adminPassword = TestUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_PASSWORD");
String port = TestUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
String ssl = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SSL");
String adminUser = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_USER");
String adminPassword = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_ADMIN_PASSWORD");
String port = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_PORT");
String ssl = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SSL");
if (ssl == null) {
ssl = "on";
}
Expand Down Expand Up @@ -125,9 +124,9 @@ private void resetTokenValidity() throws Throwable {
}

private void testSSO() throws Throwable {
String database = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE");
String schema1 = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA");
String schema2 = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA2");
String database = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE");
String schema1 = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA");
String schema2 = SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA2");

Properties properties = getProperties();
String url = getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ public void testDatabaseMetadata() throws SQLException {
assertEquals(255, metaData.getMaxUserNameLength());
assertEquals(0, getSizeOfResultSet(metaData.getTablePrivileges(null, null, null)));
// assertEquals("", metaData.getTimeDateFunctions());
assertEquals(TestUtil.systemGetEnv("SNOWFLAKE_TEST_USER"), metaData.getUserName());
assertEquals(SnowflakeUtil.systemGetEnv("SNOWFLAKE_TEST_USER"), metaData.getUserName());
assertFalse(metaData.insertsAreDetected(1));
assertTrue(metaData.isCatalogAtStart());
assertFalse(metaData.isReadOnly());
Expand Down
Loading

0 comments on commit 48d5dfc

Please sign in to comment.