Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Cli.Tests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void TestInitialize()
_cliLogger = loggerFactory.CreateLogger<Program>();
SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>());
SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>());

Environment.SetEnvironmentVariable($"connection-string", TEST_CONNECTION_STRING);
}

[TestCleanup]
Expand All @@ -50,7 +52,7 @@ public void TestCleanup()
public Task TestInitForCosmosDBNoSql()
{
string[] args = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "cosmosdb_nosql",
"--connection-string", "localhost:5000", "--cosmosdb_nosql-database",
"--connection-string", TEST_ENV_CONN_STRING, "--cosmosdb_nosql-database",
"graphqldb", "--cosmosdb_nosql-container", "planet", "--graphql-schema", TEST_SCHEMA_FILE, "--cors-origin", "localhost:3000,www.nolocalhost.com:80" };
Program.Execute(args, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);

Expand Down Expand Up @@ -107,7 +109,10 @@ public void TestInitializingRestAndGraphQLGlobalSettings()
string[] args = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql", "--rest.path", "/rest-api", "--rest.disabled", "--graphql.path", "/graphql-api" };
Program.Execute(args, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);

Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? runtimeConfig));
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(
TEST_RUNTIME_CONFIG_FILE,
out RuntimeConfig? runtimeConfig,
replaceEnvVar: true));

Assert.IsNotNull(runtimeConfig);
Assert.AreEqual(DatabaseType.MSSQL, runtimeConfig.DataSource.DatabaseType);
Expand All @@ -124,7 +129,8 @@ public void TestInitializingRestAndGraphQLGlobalSettings()
[TestMethod]
public void TestAddEntity()
{
string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--host-mode", "development", "--database-type", "mssql", "--connection-string", "localhost:5000", "--auth.provider", "StaticWebApps" };
string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--host-mode", "development", "--database-type",
"mssql", "--connection-string", TEST_ENV_CONN_STRING, "--auth.provider", "StaticWebApps" };
Program.Execute(initArgs, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);

Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? runtimeConfig));
Expand All @@ -140,6 +146,7 @@ public void TestAddEntity()

Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? addRuntimeConfig));
Assert.IsNotNull(addRuntimeConfig);
Assert.AreEqual(TEST_ENV_CONN_STRING, addRuntimeConfig.DataSource.ConnectionString);
Assert.AreEqual(1, addRuntimeConfig.Entities.Count()); // 1 new entity added
Assert.IsTrue(addRuntimeConfig.Entities.ContainsKey("todo"));
Entity entity = addRuntimeConfig.Entities["todo"];
Expand Down Expand Up @@ -374,7 +381,7 @@ public Task TestConfigGeneratedAfterAddingEntityWithSourceWithDefaultType()
public void TestUpdateEntity()
{
string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type",
"mssql", "--connection-string", "localhost:5000" };
"mssql", "--connection-string", TEST_ENV_CONN_STRING };
Program.Execute(initArgs, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);

Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? runtimeConfig));
Expand Down Expand Up @@ -416,6 +423,7 @@ public void TestUpdateEntity()

Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? updateRuntimeConfig));
Assert.IsNotNull(updateRuntimeConfig);
Assert.AreEqual(TEST_ENV_CONN_STRING, updateRuntimeConfig.DataSource.ConnectionString);
Assert.AreEqual(2, updateRuntimeConfig.Entities.Count()); // No new entity added

Assert.IsTrue(updateRuntimeConfig.Entities.ContainsKey("todo"));
Expand Down
16 changes: 10 additions & 6 deletions src/Cli.Tests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public void TestSystemEnvironmentVariableIsUsedInAbsenceOfEnvironmentFile()
[TestMethod]
public void TestStartWithEnvFileIsSuccessful()
{
BootstrapTestEnvironment("CONN_STRING=test_connection_string");
string expectedEnvVarName = "CONN_STRING";
BootstrapTestEnvironment(expectedEnvVarName + "=test_connection_string", expectedEnvVarName);

// Trying to start the runtime engine
using Process process = ExecuteDabCommand(
Expand All @@ -148,10 +149,11 @@ public void TestStartWithEnvFileIsSuccessful()
/// I feel confident that the overarching scenario is covered through other testing
/// so disabling temporarily while we investigate should be acceptable.
/// </summary>
[TestMethod, Ignore]
[TestMethod]
public async Task FailureToStartEngineWhenEnvVarNamedWrong()
{
BootstrapTestEnvironment("COMM_STRINX=test_connection_string");
string expectedEnvVarName = "WRONG_CONN_STRING";
BootstrapTestEnvironment("COMM_STRINX=test_connection_string", expectedEnvVarName);

// Trying to start the runtime engine
using Process process = ExecuteDabCommand(
Expand All @@ -160,11 +162,12 @@ public async Task FailureToStartEngineWhenEnvVarNamedWrong()
);

string? output = await process.StandardError.ReadLineAsync();
StringAssert.Contains(output, "Environmental Variable, CONN_STRING, not found.", StringComparison.Ordinal);
StringAssert.Contains(output, "Environmental Variable, "
+ expectedEnvVarName + ", not found.", StringComparison.Ordinal);
process.Kill();
}

private static void BootstrapTestEnvironment(string envFileContents)
private static void BootstrapTestEnvironment(string envFileContents, string connStringEnvName)
{
// Creating environment variable file
File.Create(".env").Close();
Expand All @@ -174,7 +177,8 @@ private static void BootstrapTestEnvironment(string envFileContents)
File.Delete(TEST_RUNTIME_CONFIG_FILE);
}

string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql", "--connection-string", "@env('CONN_STRING')" };
string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql",
"--connection-string", "@env('" + connStringEnvName + "')" };
Program.Main(initArgs);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Cli.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public static class TestHelper
// Config file name for tests
public const string TEST_RUNTIME_CONFIG_FILE = "dab-config-test.json";

public const string TEST_CONNECTION_STRING = "testconnectionstring";
public const string TEST_ENV_CONN_STRING = "@env('connection-string')";

// test schema for cosmosDB
public const string TEST_SCHEMA_FILE = "test-schema.gql";
public const string DAB_DRAFT_SCHEMA_TEST_PATH = "https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch/dab.draft.schema.json";
Expand Down Expand Up @@ -39,6 +42,7 @@ public static Process ExecuteDabCommand(string command, string flags)
StartInfo =
{
FileName = @"./Microsoft.DataApiBuilder",
CreateNoWindow = true,
Arguments = $"{command} {flags}",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
Expand Down
3 changes: 2 additions & 1 deletion src/Cli/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,8 @@ public static bool TryStartEngineWithOptions(StartOptions options, FileSystemRun
loader.UpdateBaseConfigFileName(runtimeConfigFile);

// Validates that config file has data and follows the correct json schema
if (!loader.TryLoadKnownConfig(out RuntimeConfig? deserializedRuntimeConfig))
// Replaces all the environment variables while deserializing when starting DAB.
if (!loader.TryLoadKnownConfig(out RuntimeConfig? deserializedRuntimeConfig, replaceEnvVar: true))
{
_logger.LogError("Failed to parse the config file: {configFile}.", runtimeConfigFile);
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/Cli/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public static void Export(ExportOptions options, ILogger logger, FileSystemRunti
return;
}

if (!loader.TryLoadConfig(runtimeConfigFile, out RuntimeConfig? runtimeConfig) || runtimeConfig is null)
if (!loader.TryLoadConfig(
runtimeConfigFile,
out RuntimeConfig? runtimeConfig,
replaceEnvVar: true) || runtimeConfig is null)
{
logger.LogError("Failed to read the config file: {runtimeConfigFile}.", runtimeConfigFile);
return;
Expand Down
133 changes: 0 additions & 133 deletions src/Config/Converters/EntityGraphQLOptionsConverter.cs

This file was deleted.

Loading