|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Cli.Tests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Contains test involving environment variables. |
| 8 | +/// </summary> |
| 9 | +[TestClass] |
| 10 | +public class EnvironmentTests |
| 11 | +{ |
| 12 | + public const string TEST_ENV_VARIABLE = "DAB_TEST_ENVIRONMENT"; |
| 13 | + /// <summary> |
| 14 | + /// Test to verify that environment variable setup in the system is picked up correctly |
| 15 | + /// when no .env file is present. |
| 16 | + /// </summary> |
| 17 | + [TestMethod] |
| 18 | + public void TestEnvironmentVariableIsConsumedCorrectly() |
| 19 | + { |
| 20 | + string jsonWithEnvVariable = @"{""envValue"": ""@env('DAB_TEST_ENVIRONMENT')""}"; |
| 21 | + |
| 22 | + // No environment File, No environment variable set in the system |
| 23 | + Assert.AreEqual(null, Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); |
| 24 | + |
| 25 | + // Configuring environment variable in the system |
| 26 | + Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); |
| 27 | + |
| 28 | + // Test environment variable is correctly resolved in the config file |
| 29 | + string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable); |
| 30 | + Assert.IsNotNull(resolvedJson); |
| 31 | + Assert.IsTrue(JToken.DeepEquals( |
| 32 | + JObject.Parse(@"{""envValue"": ""TEST""}"), |
| 33 | + JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); |
| 34 | + |
| 35 | + // removing Environment variable from the System |
| 36 | + Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, null); |
| 37 | + Assert.ThrowsException<DataApiBuilderException>(() => |
| 38 | + RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable), |
| 39 | + $"Environmental Variable, {TEST_ENV_VARIABLE}, not found."); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// This test creates a .env file and adds a variable and verifies that the variable is |
| 44 | + /// correctly consumed. |
| 45 | + /// For this test there were no existing environment variables. The values are picked up |
| 46 | + /// directly from the `.env` file. |
| 47 | + /// </summary> |
| 48 | + [TestMethod] |
| 49 | + public void TestEnvironmentFileIsConsumedCorrectly() |
| 50 | + { |
| 51 | + string jsonWithEnvVariable = @"{""envValue"": ""@env('DAB_TEST_ENVIRONMENT')""}"; |
| 52 | + |
| 53 | + // No environment File, no environment variable set in the system |
| 54 | + Assert.IsNull(Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); |
| 55 | + |
| 56 | + // Creating environment variable file |
| 57 | + File.Create(".env").Close(); |
| 58 | + File.WriteAllText(".env", $"{TEST_ENV_VARIABLE}=DEVELOPMENT"); |
| 59 | + DotNetEnv.Env.Load(); |
| 60 | + |
| 61 | + // Test environment variable is picked up from the .env file and is correctly resolved in the config file. |
| 62 | + Assert.AreEqual("DEVELOPMENT", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); |
| 63 | + string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables(jsonWithEnvVariable); |
| 64 | + Assert.IsNotNull(resolvedJson); |
| 65 | + Assert.IsTrue(JToken.DeepEquals( |
| 66 | + JObject.Parse(@"{""envValue"": ""DEVELOPMENT""}"), |
| 67 | + JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// This test setups an environment variable in the system and also creates a .env file containing |
| 72 | + /// the same variable with different value. In such a case, the value stored for variable in .env file is given |
| 73 | + /// precedence over the value specified in the system. |
| 74 | + /// </summary> |
| 75 | + [TestMethod] |
| 76 | + public void TestPrecedenceOfEnvironmentFileOverExistingVariables() |
| 77 | + { |
| 78 | + // The variable set in the .env file takes precedence over the environment value set in the system. |
| 79 | + Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); |
| 80 | + |
| 81 | + // Creating environment variable file |
| 82 | + File.Create(".env").Close(); |
| 83 | + File.WriteAllText(".env", $"{TEST_ENV_VARIABLE}=DEVELOPMENT"); |
| 84 | + DotNetEnv.Env.Load(); // It contains value DEVELOPMENT |
| 85 | + Assert.AreEqual("DEVELOPMENT", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); |
| 86 | + |
| 87 | + // If a variable is not present in the .env file then the system defined variable would be used if defined. |
| 88 | + Environment.SetEnvironmentVariable("HOSTING_TEST_ENVIRONMENT", "PHOENIX_TEST"); |
| 89 | + string? resolvedJson = RuntimeConfigPath.ParseConfigJsonAndReplaceEnvVariables( |
| 90 | + @"{ |
| 91 | + ""envValue"": ""@env('DAB_TEST_ENVIRONMENT')"", |
| 92 | + ""hostingEnvValue"": ""@env('HOSTING_TEST_ENVIRONMENT')"" |
| 93 | + }" |
| 94 | + ); |
| 95 | + Assert.IsNotNull(resolvedJson); |
| 96 | + Assert.IsTrue(JToken.DeepEquals( |
| 97 | + JObject.Parse( |
| 98 | + @"{ |
| 99 | + ""envValue"": ""DEVELOPMENT"", |
| 100 | + ""hostingEnvValue"": ""PHOENIX_TEST"" |
| 101 | + }"), |
| 102 | + JObject.Parse(resolvedJson)), "JSON resolved with environment variable correctly"); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Test to verify that no error is thrown if .env file is not present, and existing system variables are used. |
| 107 | + /// </summary> |
| 108 | + [TestMethod] |
| 109 | + public void TestSystemEnvironmentVariableIsUsedInAbsenceOfEnvironmentFile() |
| 110 | + { |
| 111 | + Environment.SetEnvironmentVariable(TEST_ENV_VARIABLE, "TEST"); |
| 112 | + Assert.IsFalse(File.Exists(".env")); |
| 113 | + DotNetEnv.Env.Load(); // No error is thrown |
| 114 | + Assert.AreEqual("TEST", Environment.GetEnvironmentVariable(TEST_ENV_VARIABLE)); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Test to verify that if the environment variables are not resolved correctly, the runtime engine will not start. |
| 119 | + /// Here, in the first scenario, engine fails to start because the variable defined in the environment file |
| 120 | + /// is typed incorrectly and does not match the one present in the config. |
| 121 | + /// </summary> |
| 122 | + [DataRow("COMM_STRINX=test_connection_string", true, DisplayName = "Incorrect Variable name used in the environment file.")] |
| 123 | + [DataRow("CONN_STRING=test_connection_string", false, DisplayName = "Correct Variable name used in the environment file.")] |
| 124 | + [DataTestMethod] |
| 125 | + public void TestFailureToStartWithUnresolvedJsonConfig( |
| 126 | + string environmentFileContent, |
| 127 | + bool isFailure |
| 128 | + ) |
| 129 | + { |
| 130 | + // Creating environment variable file |
| 131 | + File.Create(".env").Close(); |
| 132 | + File.WriteAllText(".env", environmentFileContent); |
| 133 | + if (File.Exists(TEST_RUNTIME_CONFIG_FILE)) |
| 134 | + { |
| 135 | + File.Delete(TEST_RUNTIME_CONFIG_FILE); |
| 136 | + } |
| 137 | + |
| 138 | + string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql", "--connection-string", "@env('CONN_STRING')" }; |
| 139 | + Program.Main(initArgs); |
| 140 | + |
| 141 | + // Trying to start the runtime engine |
| 142 | + using Process process = ExecuteDabCommand( |
| 143 | + "start", |
| 144 | + $"-c {TEST_RUNTIME_CONFIG_FILE}" |
| 145 | + ); |
| 146 | + |
| 147 | + string? output = process.StandardOutput.ReadToEnd(); |
| 148 | + Assert.IsNotNull(output); |
| 149 | + |
| 150 | + if (isFailure) |
| 151 | + { |
| 152 | + // Failed to resolve the environment variables in the config. |
| 153 | + Assert.IsFalse(output.Contains("Starting the runtime engine...")); |
| 154 | + Assert.IsTrue(output.Contains("Error: Failed due to: Environmental Variable, CONN_STRING, not found.")); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + // config resolved correctly. |
| 159 | + Assert.IsTrue(output.Contains("Starting the runtime engine...")); |
| 160 | + Assert.IsFalse(output.Contains("Error: Failed due to: Environmental Variable, CONN_STRING, not found.")); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + [TestCleanup] |
| 165 | + public void CleanUp() |
| 166 | + { |
| 167 | + if (File.Exists(".env")) |
| 168 | + { |
| 169 | + File.Delete(".env"); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments