Skip to content

Commit 85e437d

Browse files
SNOW-2122782 Read test config from env variables (#1181)
1 parent a034791 commit 85e437d

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ public void TestOAuthFlow()
25122512
var driverRootPath = Path.Combine("..", "..", "..", "..");
25132513
var configFilePath = Path.Combine(driverRootPath, "..", ".parameters_oauth_authorization_code_okta.json"); // Adjust to a proper config for your manual testing
25142514
var authenticator = OAuthAuthorizationCodeAuthenticator.AuthName; // Set either OAuthAuthorizationCodeAuthenticator.AuthName or OAuthClientCredentialsAuthenticator.AuthName
2515-
var testConfig = TestEnvironment.ReadTestConfig(configFilePath);
2515+
var testConfig = TestEnvironment.ReadTestConfigFile(configFilePath);
25162516
RemoveOAuthCache(testConfig);
25172517
try
25182518
{

Snowflake.Data.Tests/SFBaseTest.cs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Runtime.InteropServices;
99
using NUnit.Framework;
1010
using Snowflake.Data.Client;
11+
using Snowflake.Data.Core.Tools;
1112
using Snowflake.Data.Log;
1213
using Snowflake.Data.Tests.Util;
1314

@@ -47,9 +48,9 @@ public static void TearDownContext()
4748
*/
4849
[TestFixture]
4950
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
50-
[SetCulture("en-US")]
51+
[SetCulture("en-US")]
5152
#if !SEQUENTIAL_TEST_RUN
52-
[Parallelizable(ParallelScope.All)]
53+
[Parallelizable(ParallelScope.All)]
5354
#endif
5455
public class SFBaseTestAsync
5556
{
@@ -193,11 +194,67 @@ public void Setup()
193194
var cloud = Environment.GetEnvironmentVariable("snowflake_cloud_env");
194195
Assert.IsTrue(cloud == null || cloud == "AWS" || cloud == "AZURE" || cloud == "GCP", "{0} is not supported. Specify AWS, AZURE or GCP as cloud environment", cloud);
195196

196-
TestConfig = ReadTestConfig("parameters.json");
197+
TestConfig = ReadTestConfig();
197198
ModifySchema(TestConfig.schema, SchemaAction.CREATE);
198199
}
199200

200-
internal static TestConfig ReadTestConfig(string fileName)
201+
private static TestConfig ReadTestConfig()
202+
{
203+
var fileName = "parameters.json";
204+
var testConfig = File.Exists(fileName) ? ReadTestConfigFile(fileName) : ReadTestConfigEnvVariables();
205+
testConfig.schema = testConfig.schema + "_" + Guid.NewGuid().ToString().Replace("-", "_");
206+
return testConfig;
207+
}
208+
209+
private static TestConfig ReadTestConfigEnvVariables()
210+
{
211+
var config = new TestConfig();
212+
config.user = ReadEnvVariableIfSet(config.user, "SNOWFLAKE_TEST_USER");
213+
config.password = ReadEnvVariableIfSet(config.password, "SNOWFLAKE_TEST_PASSWORD");
214+
config.account = ReadEnvVariableIfSet(config.account, "SNOWFLAKE_TEST_ACCOUNT");
215+
config.host = ReadEnvVariableIfSet(config.host, "SNOWFLAKE_TEST_HOST");
216+
config.port = ReadEnvVariableIfSet(config.port, "SNOWFLAKE_TEST_PORT");
217+
config.warehouse = ReadEnvVariableIfSet(config.warehouse, "SNOWFLAKE_TEST_WAREHOUSE");
218+
config.database = ReadEnvVariableIfSet(config.database, "SNOWFLAKE_TEST_DATABASE");
219+
config.schema = ReadEnvVariableIfSet(config.schema, "SNOWFLAKE_TEST_SCHEMA");
220+
config.role = ReadEnvVariableIfSet(config.role, "SNOWFLAKE_TEST_ROLE");
221+
config.protocol = ReadEnvVariableIfSet(config.protocol, "SNOWFLAKE_TEST_PROTOCOL");
222+
config.oktaUser = ReadEnvVariableIfSet(config.oktaUser, "SNOWFLAKE_TEST_OKTA_USER");
223+
config.oktaPassword = ReadEnvVariableIfSet(config.oktaPassword, "SNOWFLAKE_TEST_OKTA_PASSWORD");
224+
config.oktaUrl = ReadEnvVariableIfSet(config.oktaUrl, "SNOWFLAKE_TEST_OKTA_URL");
225+
config.jwtAuthUser = ReadEnvVariableIfSet(config.jwtAuthUser, "SNOWFLAKE_TEST_JWT_USER");
226+
config.pemFilePath = ReadEnvVariableIfSet(config.pemFilePath, "SNOWFLAKE_TEST_PEM_FILE");
227+
config.p8FilePath = ReadEnvVariableIfSet(config.p8FilePath, "SNOWFLAKE_TEST_P8_FILE");
228+
config.pwdProtectedPrivateKeyFilePath = ReadEnvVariableIfSet(config.pwdProtectedPrivateKeyFilePath, "SNOWFLAKE_TEST_PWD_PROTECTED_PK_FILE");
229+
config.privateKey = ReadEnvVariableIfSet(config.privateKey, "SNOWFLAKE_TEST_PK_CONTENT");
230+
config.pwdProtectedPrivateKey = ReadEnvVariableIfSet(config.pwdProtectedPrivateKey, "SNOWFLAKE_TEST_PROTECTED_PK_CONTENT");
231+
config.privateKeyFilePwd = ReadEnvVariableIfSet(config.privateKeyFilePwd, "SNOWFLAKE_TEST_PK_PWD");
232+
config.oauthToken = ReadEnvVariableIfSet(config.oauthToken, "SNOWFLAKE_TEST_OAUTH_TOKEN");
233+
config.expOauthToken = ReadEnvVariableIfSet(config.expOauthToken, "SNOWFLAKE_TEST_EXP_OAUTH_TOKEN");
234+
config.proxyHost = ReadEnvVariableIfSet(config.proxyHost, "SNOWFLAKE_TEST_PROXY_HOST");
235+
config.proxyPort = ReadEnvVariableIfSet(config.proxyPort, "SNOWFLAKE_TEST_PROXY_PORT");
236+
config.authProxyHost = ReadEnvVariableIfSet(config.authProxyHost, "SNOWFLAKE_TEST_AUTH_PROXY_HOST");
237+
config.authProxyPort = ReadEnvVariableIfSet(config.authProxyPort, "SNOWFLAKE_TEST_AUTH_PROXY_PORT");
238+
config.authProxyUser = ReadEnvVariableIfSet(config.authProxyUser, "SNOWFLAKE_TEST_AUTH_PROXY_USER");
239+
config.authProxyPwd = ReadEnvVariableIfSet(config.authProxyPwd, "SNOWFLAKE_TEST_AUTH_PROXY_PWD");
240+
config.nonProxyHosts = ReadEnvVariableIfSet(config.nonProxyHosts, "SNOWFLAKE_TEST_NON_PROXY_HOSTS");
241+
config.oauthClientId = ReadEnvVariableIfSet(config.oauthClientId, "SNOWFLAKE_TEST_OAUTH_CLIENT_ID");
242+
config.oauthClientSecret = ReadEnvVariableIfSet(config.oauthClientSecret, "SNOWFLAKE_TEST_OAUTH_CLIENT_SECRET");
243+
config.oauthScope = ReadEnvVariableIfSet(config.oauthScope, "SNOWFLAKE_TEST_OAUTH_SCOPE");
244+
config.oauthRedirectUri = ReadEnvVariableIfSet(config.oauthRedirectUri, "SNOWFLAKE_TEST_OAUTH_REDIRECT_URI");
245+
config.oauthAuthorizationUrl = ReadEnvVariableIfSet(config.oauthAuthorizationUrl, "SNOWFLAKE_TEST_OAUTH_AUTHORIZATION_URL");
246+
config.oauthTokenRequestUrl = ReadEnvVariableIfSet(config.oauthTokenRequestUrl, "SNOWFLAKE_TEST_OAUTH_TOKEN_REQUEST_URL");
247+
config.programmaticAccessToken = ReadEnvVariableIfSet(config.programmaticAccessToken, "SNOWFLAKE_TEST_PROGRAMMATIC_ACCESS_TOKEN");
248+
return config;
249+
}
250+
251+
private static string ReadEnvVariableIfSet(string defaultValue, string variableName)
252+
{
253+
var variableValue = Environment.GetEnvironmentVariable(variableName);
254+
return string.IsNullOrEmpty(variableValue) ? defaultValue : variableValue;
255+
}
256+
257+
internal static TestConfig ReadTestConfigFile(string fileName)
201258
{
202259
var reader = new StreamReader(fileName);
203260
var testConfigString = reader.ReadToEnd();
@@ -213,9 +270,7 @@ internal static TestConfig ReadTestConfig(string fileName)
213270
var testConfigs = JsonConvert.DeserializeObject<Dictionary<string, TestConfig>>(testConfigString, jsonSettings);
214271
if (testConfigs.TryGetValue("testconnection", out var testConnectionConfig))
215272
{
216-
var testConfig = testConnectionConfig;
217-
testConfig.schema = testConfig.schema + "_" + Guid.NewGuid().ToString().Replace("-", "_");
218-
return testConfig;
273+
return testConnectionConfig;
219274
}
220275
else
221276
{

Snowflake.Data.Tests/Snowflake.Data.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<ProjectReference Include="..\Snowflake.Data\Snowflake.Data.csproj" />
2929
</ItemGroup>
3030
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
31-
<Copy SourceFiles="parameters.json" DestinationFolder="$(OutDir)" />
31+
<Copy SourceFiles="parameters.json" DestinationFolder="$(OutDir)" ContinueOnError="true"/>
3232
<Copy SourceFiles="App.config" DestinationFolder="$(OutDir)" />
3333
</Target>
3434
<ItemGroup>

0 commit comments

Comments
 (0)