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 1016467 test prepare support windows tests #1785

3 changes: 2 additions & 1 deletion FIPS/src/test/java/net/snowflake/client/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public interface TestRunInterface {
/**
* System.getenv wrapper. If System.getenv raises an SecurityException, it is ignored and returns
* null.
*
* @deprecated This method should be replaced by SnowflakeUtil.systemGetEnv.
* <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.
*/
@Deprecated
public static String systemGetEnv(String env) {
try {
return System.getenv(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void dumpLogBuffer(String identifier) {
cleanupSfDumps(true);

String logDumpPath =
logDumpPathPrefix + "/" + LOG_DUMP_FILE_NAME + identifier + LOG_DUMP_FILE_EXT;
logDumpPathPrefix + File.separator + LOG_DUMP_FILE_NAME + identifier + LOG_DUMP_FILE_EXT;

if (!disableCompression) {
logDumpPath += LOG_DUMP_COMP_EXT;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/snowflake/client/core/EventUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static net.snowflake.client.jdbc.SnowflakeUtil.systemGetProperty;

import java.io.File;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void triggerStateTransition(BasicEvent.QueryState newState, String
}

public static String getDumpPathPrefix() {
return DUMP_PATH_PREFIX + "/" + DUMP_SUBDIR;
return DUMP_PATH_PREFIX + File.separator + DUMP_SUBDIR;
}

public static String getDumpFileId() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Types;
Expand All @@ -35,6 +36,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import net.snowflake.client.core.Constants;
import net.snowflake.client.core.HttpClientSettingsKey;
import net.snowflake.client.core.OCSPMode;
import net.snowflake.client.core.ObjectMapperFactory;
Expand Down Expand Up @@ -706,6 +708,16 @@ public static void systemSetEnv(String key, String value) {
field.setAccessible(true);
Map<String, String> writableEnv = (Map<String, String>) field.get(env);
writableEnv.put(key, value);

if (Constants.getOS() == Constants.OS.WINDOWS) {
Class<?> pe = Class.forName("java.lang.ProcessEnvironment");
Method getenv = pe.getDeclaredMethod("getenv", String.class);
getenv.setAccessible(true);
Field props = pe.getDeclaredField("theCaseInsensitiveEnvironment");
props.setAccessible(true);
Map<String, String> writableEnvForGet = (Map<String, String>) props.get(null);
writableEnvForGet.put(key, value);
}
} catch (Exception e) {
System.out.println("Failed to set value");
logger.error(
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/net/snowflake/client/AbstractDriverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.common.base.Strings;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
Expand Down Expand Up @@ -385,7 +387,11 @@ public static String getFullPathFileInResource(String fileName) {
ClassLoader classLoader = AbstractDriverIT.class.getClassLoader();
URL url = classLoader.getResource(fileName);
if (url != null) {
return url.getFile();
try {
return Paths.get(url.toURI()).toAbsolutePath().toString();
} catch (URISyntaxException ex) {
throw new RuntimeException("Unable to get absolute path: " + fileName);
}
} else {
throw new RuntimeException("No file is found: " + fileName);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/net/snowflake/client/RunningNotOnLinuxMac.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.snowflake.client;

import net.snowflake.client.core.Constants;

public class RunningNotOnLinuxMac implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
return Constants.getOS() != Constants.OS.LINUX && Constants.getOS() != Constants.OS.MAC;
}

public static boolean isNotRunningOnLinuxMac() {
return Constants.getOS() != Constants.OS.LINUX && Constants.getOS() != Constants.OS.MAC;
}
}
9 changes: 5 additions & 4 deletions src/test/java/net/snowflake/client/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public interface TestRunInterface {
* 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.
*
* @deprecated This method should be replaced by SnowflakeUtil.systemGetEnv.
* <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.
*/
@Deprecated
public static String systemGetEnv(String env) {
try {
return System.getenv(env);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/snowflake/client/core/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testWriteEventDumpLine() throws IOException {
File dumpFile =
new File(
EventUtil.getDumpPathPrefix()
+ "/"
+ File.separator
+ "sf_event_"
+ EventUtil.getDumpFileId()
+ ".dmp.gz");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand All @@ -31,20 +32,20 @@ public void testProcessFileNames() throws Exception {
System.setProperty("user.home", folderName);

String[] locations = {
folderName + "/Tes*Fil*A",
folderName + "/TestFil?B",
"~/TestFileC",
folderName + File.separator + "Tes*Fil*A",
folderName + File.separator + "TestFil?B",
"~" + File.separator + "TestFileC",
"TestFileD",
folderName + "/TestFileE~"
folderName + File.separator + "TestFileE~"
};

Set<String> files = SnowflakeFileTransferAgent.expandFileNames(locations, null);

assertTrue(files.contains(folderName + "/TestFileA"));
assertTrue(files.contains(folderName + "/TestFileB"));
assertTrue(files.contains(folderName + "/TestFileC"));
assertTrue(files.contains(folderName + "/TestFileD"));
assertTrue(files.contains(folderName + "/TestFileE~"));
assertTrue(files.contains(folderName + File.separator + "TestFileA"));
assertTrue(files.contains(folderName + File.separator + "TestFileB"));
assertTrue(files.contains(folderName + File.separator + "TestFileC"));
assertTrue(files.contains(folderName + File.separator + "TestFileD"));
assertTrue(files.contains(folderName + File.separator + "TestFileE~"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ public void testPopulateStatusRowsWithSortOn() throws Exception {

// upload files orders_101.csv and orders_100.csv
String command =
"PUT file://" + getFullPathFileInResource("") + "/orders_10*.csv @testStage";
"PUT file://"
+ getFullPathFileInResource("")
+ File.separator
+ "orders_10*.csv @testStage";
SnowflakeFileTransferAgent sfAgent1 =
new SnowflakeFileTransferAgent(command, sfSession, new SFStatement(sfSession));
sfAgent1.execute(); // upload files
Expand Down Expand Up @@ -597,7 +600,10 @@ public void testFileTransferMappingFromSourceFile() throws SQLException {
SFSession sfSession = con.unwrap(SnowflakeConnectionV1.class).getSfSession();

String command =
"PUT file://" + getFullPathFileInResource("") + "/orders_10*.csv @testStage";
"PUT file://"
+ getFullPathFileInResource("")
+ File.separator
+ "orders_10*.csv @testStage";
SnowflakeFileTransferAgent sfAgent1 =
new SnowflakeFileTransferAgent(command, sfSession, new SFStatement(sfSession));
sfAgent1.execute();
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/net/snowflake/client/jdbc/RestRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -17,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import net.snowflake.client.RunningNotOnLinuxMac;
import net.snowflake.client.core.ExecTimeTelemetryData;
import net.snowflake.client.core.HttpUtil;
import net.snowflake.client.jdbc.telemetryOOB.TelemetryService;
Expand Down Expand Up @@ -519,8 +522,9 @@ public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwabl
}
}

@Test(expected = SnowflakeSQLException.class)
public void testLoginTimeout() throws IOException, SnowflakeSQLException {
@Test
public void testLoginTimeout() throws IOException {
assumeFalse(RunningNotOnLinuxMac.isNotRunningOnLinuxMac());
boolean telemetryEnabled = TelemetryService.getInstance().isEnabled();

CloseableHttpClient client = mock(CloseableHttpClient.class);
Expand All @@ -542,8 +546,11 @@ public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwabl

try {
TelemetryService.disable();
execute(client, "/session/v1/login-request", 1, 0, 0, true, false, 10);
fail("testMaxRetries");
assertThrows(
SnowflakeSQLException.class,
() -> {
execute(client, "/session/v1/login-request", 1, 0, 0, true, false, 10);
});
} finally {
if (telemetryEnabled) {
TelemetryService.enable();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/snowflake/client/jdbc/StatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void testExecuteBatch() throws Exception {
+ getFullPathFileInResource(TEST_DATA_FILE)
+ " @%test_batch auto_compress=false");
File tempFolder = tmpFolder.newFolder("test_downloads_folder");
statement.addBatch("get @%test_batch file://" + tempFolder);
statement.addBatch("get @%test_batch file://" + tempFolder.getCanonicalPath());

rowCounts = statement.executeBatch();
assertThat(rowCounts.length, is(2));
Expand Down
Loading