|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.DataApiBuilder.Config.ObjectModel; |
| 9 | +using Microsoft.AspNetCore.TestHost; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 12 | +using Serilog; |
| 13 | +using Serilog.Core; |
| 14 | +using static Azure.DataApiBuilder.Service.Tests.Configuration.ConfigurationTests; |
| 15 | + |
| 16 | +namespace Azure.DataApiBuilder.Service.Tests.Configuration.Telemetry; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Contains tests for File Sink functionality. |
| 20 | +/// </summary> |
| 21 | +[TestClass, TestCategory(TestCategory.MSSQL)] |
| 22 | +public class FileSinkTests |
| 23 | +{ |
| 24 | + public TestContext TestContext { get; set; } |
| 25 | + |
| 26 | + private const string CONFIG_WITH_TELEMETRY = "dab-file-sink-test-config.json"; |
| 27 | + private const string CONFIG_WITHOUT_TELEMETRY = "dab-no-file-sink-test-config.json"; |
| 28 | + private static RuntimeConfig _configuration; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// This is a helper function that creates runtime config file with specified telemetry options. |
| 32 | + /// </summary> |
| 33 | + /// <param name="configFileName">Name of the config file to be created.</param> |
| 34 | + /// <param name="isFileSinkEnabled">Whether File Sink is enabled or not.</param> |
| 35 | + /// <param name="fileSinkPath">Path where logs will be sent to.</param> |
| 36 | + /// <param name="rollingInterval">Time it takes for logs to roll over to next file.</param> |
| 37 | + private static void SetUpTelemetryInConfig(string configFileName, bool isFileSinkEnabled, string fileSinkPath, RollingInterval? rollingInterval = null) |
| 38 | + { |
| 39 | + DataSource dataSource = new(DatabaseType.MSSQL, |
| 40 | + GetConnectionStringFromEnvironmentConfig(environment: TestCategory.MSSQL), Options: null); |
| 41 | + |
| 42 | + _configuration = InitMinimalRuntimeConfig(dataSource, graphqlOptions: new(), restOptions: new()); |
| 43 | + |
| 44 | + TelemetryOptions _testTelemetryOptions = new(File: new FileSinkOptions(isFileSinkEnabled, fileSinkPath, rollingInterval)); |
| 45 | + _configuration = _configuration with { Runtime = _configuration.Runtime with { Telemetry = _testTelemetryOptions } }; |
| 46 | + |
| 47 | + File.WriteAllText(configFileName, _configuration.ToJson()); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Cleans up the test environment by deleting the runtime config with telemetry options. |
| 52 | + /// </summary> |
| 53 | + [TestCleanup] |
| 54 | + public void CleanUpTelemetryConfig() |
| 55 | + { |
| 56 | + if (File.Exists(CONFIG_WITH_TELEMETRY)) |
| 57 | + { |
| 58 | + File.Delete(CONFIG_WITH_TELEMETRY); |
| 59 | + } |
| 60 | + |
| 61 | + if (File.Exists(CONFIG_WITHOUT_TELEMETRY)) |
| 62 | + { |
| 63 | + File.Delete(CONFIG_WITHOUT_TELEMETRY); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Tests if the services are correctly enabled for File Sink. |
| 69 | + /// </summary> |
| 70 | + [TestMethod] |
| 71 | + public void TestFileSinkServicesEnabled() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + SetUpTelemetryInConfig(CONFIG_WITH_TELEMETRY, true, "/dab-log-test/file-sink-file.txt"); |
| 75 | + |
| 76 | + string[] args = new[] |
| 77 | + { |
| 78 | + $"--ConfigFileName={CONFIG_WITH_TELEMETRY}" |
| 79 | + }; |
| 80 | + using TestServer server = new(Program.CreateWebHostBuilder(args)); |
| 81 | + |
| 82 | + // Additional assertions to check if File Sink is enabled correctly in services |
| 83 | + IServiceProvider serviceProvider = server.Services; |
| 84 | + LoggerConfiguration serilogLoggerConfiguration = serviceProvider.GetService<LoggerConfiguration>(); |
| 85 | + Logger serilogLogger = serviceProvider.GetService<Logger>(); |
| 86 | + |
| 87 | + // If serilogLoggerConfiguration and serilogLogger are not null, File Sink is enabled |
| 88 | + Assert.IsNotNull(serilogLoggerConfiguration, "LoggerConfiguration for Serilog should be registered."); |
| 89 | + Assert.IsNotNull(serilogLogger, "Logger for Serilog should be registered."); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Tests if the logs are flushed to the proper path when File Sink is enabled. |
| 94 | + /// </summary> |
| 95 | + /// <summary> |
| 96 | + /// Tests if the logs are flushed to the proper path when File Sink is enabled. |
| 97 | + /// </summary> |
| 98 | + [DataTestMethod] |
| 99 | + [DataRow("file-sink-test-file.txt")] |
| 100 | + [DataRow("file-sink-test-file.log")] |
| 101 | + [DataRow("file-sink-test-file.csv")] |
| 102 | + public async Task TestFileSinkSucceed(string fileName) |
| 103 | + { |
| 104 | + // Arrange |
| 105 | + SetUpTelemetryInConfig(CONFIG_WITH_TELEMETRY, true, fileName, RollingInterval.Infinite); |
| 106 | + |
| 107 | + string[] args = new[] |
| 108 | + { |
| 109 | + $"--ConfigFileName={CONFIG_WITH_TELEMETRY}" |
| 110 | + }; |
| 111 | + using TestServer server = new(Program.CreateWebHostBuilder(args)); |
| 112 | + |
| 113 | + // Act |
| 114 | + using (HttpClient client = server.CreateClient()) |
| 115 | + { |
| 116 | + HttpRequestMessage restRequest = new(HttpMethod.Get, "/api/Book"); |
| 117 | + await client.SendAsync(restRequest); |
| 118 | + } |
| 119 | + |
| 120 | + server.Dispose(); |
| 121 | + |
| 122 | + // Assert |
| 123 | + Assert.IsTrue(File.Exists(fileName)); |
| 124 | + |
| 125 | + bool containsInfo = false; |
| 126 | + string[] allLines = File.ReadAllLines(fileName); |
| 127 | + foreach (string line in allLines) |
| 128 | + { |
| 129 | + containsInfo = line.Contains("INF"); |
| 130 | + if (containsInfo) |
| 131 | + { |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + Assert.IsTrue(containsInfo); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Tests if the services are correctly disabled for File Sink. |
| 141 | + /// </summary> |
| 142 | + [TestMethod] |
| 143 | + public void TestFileSinkServicesDisabled() |
| 144 | + { |
| 145 | + // Arrange |
| 146 | + SetUpTelemetryInConfig(CONFIG_WITHOUT_TELEMETRY, false, null); |
| 147 | + |
| 148 | + string[] args = new[] |
| 149 | + { |
| 150 | + $"--ConfigFileName={CONFIG_WITHOUT_TELEMETRY}" |
| 151 | + }; |
| 152 | + using TestServer server = new(Program.CreateWebHostBuilder(args)); |
| 153 | + |
| 154 | + // Additional assertions to check if File Sink is enabled correctly in services |
| 155 | + IServiceProvider serviceProvider = server.Services; |
| 156 | + LoggerConfiguration serilogLoggerConfiguration = serviceProvider.GetService<LoggerConfiguration>(); |
| 157 | + Logger serilogLogger = serviceProvider.GetService<Logger>(); |
| 158 | + |
| 159 | + // If serilogLoggerConfiguration and serilogLogger are null, File Sink is disabled |
| 160 | + Assert.IsNull(serilogLoggerConfiguration, "LoggerConfiguration for Serilog should not be registered."); |
| 161 | + Assert.IsNull(serilogLogger, "Logger for Serilog should not be registered."); |
| 162 | + } |
| 163 | +} |
0 commit comments