|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Cli.Tests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Config Generation Tests for CLI. |
| 8 | +/// </summary> |
| 9 | +[TestClass] |
| 10 | +public class ConfigGeneratorTests |
| 11 | +{ |
| 12 | + private IFileSystem? _fileSystem; |
| 13 | + private RuntimeConfigLoader? _runtimeConfigLoader; |
| 14 | + |
| 15 | + [TestInitialize] |
| 16 | + public void TestInitialize() |
| 17 | + { |
| 18 | + _fileSystem = FileSystemUtils.ProvisionMockFileSystem(); |
| 19 | + |
| 20 | + _runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem); |
| 21 | + |
| 22 | + ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory(); |
| 23 | + |
| 24 | + SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>()); |
| 25 | + SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>()); |
| 26 | + } |
| 27 | + |
| 28 | + [TestCleanup] |
| 29 | + public void TestCleanup() |
| 30 | + { |
| 31 | + _fileSystem = null; |
| 32 | + _runtimeConfigLoader = null; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Tests that user provided config file is successfully generated when that file is not already present. |
| 37 | + /// </summary> |
| 38 | + [DataTestMethod] |
| 39 | + [DataRow(true, false, DisplayName = "Failed to generate config file when user provided config file is present.")] |
| 40 | + [DataRow(false, true, DisplayName = "Successfully generated config file when user provided config file is not present.")] |
| 41 | + public void TryGenerateConfig_WithUserProvidedConfig( |
| 42 | + bool isConfigFilePresent, |
| 43 | + bool isConfigGenerationSuccessful) |
| 44 | + { |
| 45 | + HandleConfigFileCreationAndDeletion(TEST_RUNTIME_CONFIG_FILE, isConfigFilePresent); |
| 46 | + Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE)); |
| 47 | + |
| 48 | + InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE); |
| 49 | + |
| 50 | + // Mocking logger to assert on logs |
| 51 | + Mock<ILogger<ConfigGenerator>> loggerMock = new(); |
| 52 | + ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object); |
| 53 | + |
| 54 | + Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!)); |
| 55 | + |
| 56 | + if (!isConfigFilePresent) |
| 57 | + { |
| 58 | + Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE)); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + // Assert on the log message to verify the failure |
| 63 | + loggerMock.Verify( |
| 64 | + x => x.Log( |
| 65 | + LogLevel.Error, |
| 66 | + It.IsAny<EventId>(), |
| 67 | + It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{TEST_RUNTIME_CONFIG_FILE} already exists.")), |
| 68 | + It.IsAny<Exception?>(), |
| 69 | + (Func<It.IsAnyType, Exception?, string>)It.IsAny<object>() |
| 70 | + ), |
| 71 | + Times.Once |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Tests that environment config file is successfully generated when that file is not already present. |
| 78 | + /// When environment variable is not set, it should generate the default config file. |
| 79 | + /// </summary> |
| 80 | + [DataTestMethod] |
| 81 | + [DataRow(true, false, "Test", "dab-config.Test.json", DisplayName = "Failed to generate the config file when environment config file is present.")] |
| 82 | + [DataRow(false, true, "Test", "dab-config.Test.json", DisplayName = "Successfully generated the config file when environment config file is not present.")] |
| 83 | + [DataRow(false, true, "", "dab-config.json", DisplayName = "Successfully generated the config file when environment config file is not present and environment variable is set as empty.")] |
| 84 | + [DataRow(false, true, null, "dab-config.json", DisplayName = "Successfully generated the config file when environment config file is not present and environment variable is not set.")] |
| 85 | + public void TryGenerateConfig_UsingEnvironmentVariable( |
| 86 | + bool isConfigFilePresent, |
| 87 | + bool isConfigGenerationSuccessful, |
| 88 | + string? environmentValue, |
| 89 | + string configFileName) |
| 90 | + { |
| 91 | + Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue); |
| 92 | + HandleConfigFileCreationAndDeletion(configFileName, isConfigFilePresent); |
| 93 | + Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(configFileName)); |
| 94 | + |
| 95 | + InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig(); |
| 96 | + |
| 97 | + // Mocking logger to assert on logs |
| 98 | + Mock<ILogger<ConfigGenerator>> loggerMock = new(); |
| 99 | + ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object); |
| 100 | + |
| 101 | + Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!)); |
| 102 | + if (!isConfigFilePresent) |
| 103 | + { |
| 104 | + Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(configFileName)); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + // Assert on the log message to verify the failure |
| 109 | + loggerMock.Verify( |
| 110 | + x => x.Log( |
| 111 | + LogLevel.Error, |
| 112 | + It.IsAny<EventId>(), |
| 113 | + It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{configFileName} already exists.")), |
| 114 | + It.IsAny<Exception?>(), |
| 115 | + (Func<It.IsAnyType, Exception?, string>)It.IsAny<object>() |
| 116 | + ), |
| 117 | + Times.Once |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// This method handles the creation and deletion of a configuration file. |
| 124 | + /// </summary> |
| 125 | + private void HandleConfigFileCreationAndDeletion(string configFilePath, bool configFilePresent) |
| 126 | + { |
| 127 | + if (!configFilePresent) |
| 128 | + { |
| 129 | + _fileSystem!.File.Delete(configFilePath); |
| 130 | + } |
| 131 | + else if (configFilePresent) |
| 132 | + { |
| 133 | + _fileSystem!.File.Create(configFilePath).Dispose(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments