-
Notifications
You must be signed in to change notification settings - Fork 292
Add support for .env file #1497
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
Merged
abhishekkumams
merged 25 commits into
main
from
dev/abhishekkuma/add_support_for_env_file
Jun 9, 2023
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8773e01
adding test
abhishekkumams a01e75b
adding unit test
abhishekkumams 67b16e7
fix formatting
abhishekkumams 97b2e39
removing redundant change
abhishekkumams 939f726
removing redundant change
abhishekkumams dad8aaa
Merge branch 'main' into dev/abhishekkuma/add_support_for_env_file
abhishekkumams efd36de
Merge branch 'main' into dev/abhishekkuma/add_support_for_env_file
abhishekkumams 3fac17e
Merge branch 'main' into dev/abhishekkuma/add_support_for_env_file
abhishekkumams ef95732
fixing nits
abhishekkumams 246aef0
adding a new Test file for environment related tests in CLI
abhishekkumams 6d9f724
Merge branch 'main' of https://github.com/Azure/data-api-builder into…
abhishekkumams 2b63b07
Update src/Cli.Tests/EnvironmentTests.cs
abhishekkumams 4079a01
Merge branch 'main' into dev/abhishekkuma/add_support_for_env_file
abhishekkumams 4702ce6
resolve nits
abhishekkumams ca16456
resolving merge conflicts
abhishekkumams 6e0d5a9
adding new tests
abhishekkumams c0ef3cd
fix formatting
abhishekkumams ea3fbe3
updating env file lookup
abhishekkumams ac6a089
Merge branch 'main' into dev/abhishekkuma/add_support_for_env_file
abhishekkumams 35d2413
Merge branch 'main' of https://github.com/Azure/data-api-builder into…
abhishekkumams e1bf19c
Merge branch 'dev/abhishekkuma/add_support_for_env_file' of https://g…
abhishekkumams 74d9554
adding new test
abhishekkumams d11a199
adding summary
abhishekkumams 366f9ac
fixing nits
abhishekkumams 029ad35
fixing nits
abhishekkumams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Cli.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Contains test involving environment variables. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class EnvironmentTests | ||
| { | ||
| public const string TEST_ENV_VARIABLE = "DAB_TEST_ENVIRONMENT"; | ||
| /// <summary> | ||
| /// Test to verify that environment variable setup in the system is picked up correctly | ||
| /// when no .env file is present. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void TestEnvironmentVariableIsConsumedCorrectly() | ||
| { | ||
| string jsonWithEnvVariable = @"{""envValue"": ""@env('DAB_TEST_ENVIRONMENT')""}"; | ||
|
|
||
| // No environment File, No environment variable set in the system | ||
| Assert.AreEqual(null, Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); | ||
|
|
||
| // Configuring environment variable in the system | ||
| Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); | ||
|
|
||
| // Test environment variable is correctly resolved in the config file | ||
| string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable); | ||
| Assert.IsNotNull(resolvedJson); | ||
| Assert.IsTrue(JToken.DeepEquals( | ||
| JObject.Parse(@"{""envValue"": ""TEST""}"), | ||
| JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); | ||
|
|
||
| // removing Environment variable from the System | ||
| Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, null); | ||
| Assert.ThrowsException<DataApiBuilderException>(() => | ||
| RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable), | ||
| $"Environmental Variable, {TEST_ENV_VARIABLE}, not found."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test creates a .env file and adds a variable and verifies that the variable is | ||
| /// correctly consumed. | ||
| /// For this test there were no existing environment variables. The values are picked up | ||
| /// directly from the `.env` file. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void TestEnvironmentFileIsConsumedCorrectly() | ||
| { | ||
| string jsonWithEnvVariable = @"{""envValue"": ""@env('DAB_TEST_ENVIRONMENT')""}"; | ||
|
|
||
| // No environment File, No environment variable set in the system | ||
| Assert.IsNull(Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); | ||
|
|
||
| // Creating environment variable file | ||
| File.Create(".env").Close(); | ||
| File.WriteAllText(".env", $"{TEST_ENV_VARIABLE}=DEVELOPMENT"); | ||
| DotNetEnv.Env.Load(); | ||
|
|
||
| // Test environment variable is picked up from the .env file and is correctly resolved in the config file. | ||
| Assert.AreEqual("DEVELOPMENT", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); | ||
| string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable); | ||
| Assert.IsNotNull(resolvedJson); | ||
| Assert.IsTrue(JToken.DeepEquals( | ||
| JObject.Parse(@"{""envValue"": ""DEVELOPMENT""}"), | ||
| JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test setups a environment variable in the system and also creates a .env file containing | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// same variable with different value to show the value stored in .env file is given | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// precedence over the system variable. | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| [TestMethod] | ||
| public void TestPrecedenceOfEnvironmentFileOverExistingVariables() | ||
| { | ||
| // The variable set in the .env file takes precedence over the environment value set in the system. | ||
| Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); | ||
|
|
||
| // Creating environment variable file | ||
| File.Create(".env").Close(); | ||
| File.WriteAllText(".env", $"{TEST_ENV_VARIABLE}=DEVELOPMENT"); | ||
| DotNetEnv.Env.Load(); // It contains value DEVELOPMENT | ||
| Assert.AreEqual("DEVELOPMENT", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); | ||
|
|
||
| // If a variable is not present in the .env file then the system defined variable would be used if defined. | ||
| Environment.SetEnvironmentVariable("HOSTING_TEST_ENVIRONMENT", "PHOENIX_TEST"); | ||
| string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables( | ||
| @"{ | ||
| ""envValue"": ""@env('DAB_TEST_ENVIRONMENT')"", | ||
| ""hostingEnvValue"": ""@env('HOSTING_TEST_ENVIRONMENT')"" | ||
| }" | ||
| ); | ||
| Assert.IsNotNull(resolvedJson); | ||
| Assert.IsTrue(JToken.DeepEquals( | ||
| JObject.Parse( | ||
| @"{ | ||
| ""envValue"": ""DEVELOPMENT"", | ||
| ""hostingEnvValue"": ""PHOENIX_TEST"" | ||
| }"), | ||
| JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); | ||
|
|
||
| // Removing the .env file it will then use the value of system environment variable. | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test to verify that no error is thrown if .env file is not present, and existing system variables is used. | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| [TestMethod] | ||
| public void TestSystemEnvironmentVariableIsUsedInAbsenceOfEnvironmentFile() | ||
| { | ||
| Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); | ||
| Assert.IsFalse(File.Exists(".env")); | ||
| DotNetEnv.Env.Load(); // No error is thrown | ||
| Assert.AreEqual("TEST", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test to verify that if the environment variables are not resolved correctly, runtime engine will not start. | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Here, in the first scenario, engine failed to start because the variable defined in the environment file | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// is typed incorrectly and do not match with the one present in the config. | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| [DataRow("COMM_STRINX=test_connection_string", true, DisplayName = "Incorrect Variable name used in the environment file.")] | ||
| [DataRow("CONN_STRING=test_connection_string", false, DisplayName = "Correct Variable name used in the environment file.")] | ||
| [DataTestMethod] | ||
| public void TestFailureToStartWithUnresolvedJsonConfig( | ||
| string environmentFileContent, | ||
| bool isFailure | ||
| ) | ||
| { | ||
| // Creating environment variable file | ||
| File.Create(".env").Close(); | ||
| File.WriteAllText(".env", environmentFileContent); | ||
| if (File.Exists(TEST_RUNTIME_CONFIG_FILE)) | ||
| { | ||
| File.Delete(TEST_RUNTIME_CONFIG_FILE); | ||
| } | ||
|
|
||
| string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql", "--connection-string", "@env('CONN_STRING')" }; | ||
| Program.Main(initArgs); | ||
|
|
||
| // Trying to start the runtime engine | ||
| using Process process = ExecuteDabCommand( | ||
| "start", | ||
| $"-c {TEST_RUNTIME_CONFIG_FILE}" | ||
| ); | ||
|
|
||
| string? output = process.StandardOutput.ReadToEnd(); | ||
| Assert.IsNotNull(output); | ||
|
|
||
| if (isFailure) | ||
| { | ||
| // Failed to resolve the environment variables in the config. | ||
| Assert.IsFalse(output.Contains("Starting the runtime engine...")); | ||
| Assert.IsTrue(output.Contains("Error: Failed due to: Environmental Variable, CONN_STRING, not found.")); | ||
| Assert.IsTrue(output.Contains("Error: Failed to start the engine.")); | ||
abhishekkumams marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| // config resolved correctly. | ||
| Assert.IsTrue(output.Contains("Starting the runtime engine...")); | ||
| } | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void CleanUp() | ||
| { | ||
| if (File.Exists(".env")) | ||
| { | ||
| File.Delete(".env"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.