mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Bug fix for https://github.com/Microsoft/azuredatastudio/issues/2923 and misc other fixes (#711)
This commit is contained in:
@@ -30,21 +30,25 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
public SourceLevels TracingLevel { get; set; } = SourceLevels.Critical;
|
||||
public bool DoNotUseTraceSource { get; set; } = false;
|
||||
|
||||
public bool AutoFlush { get; set; } = false;
|
||||
|
||||
private List<Action> pendingVerifications;
|
||||
private string testName;
|
||||
|
||||
public string CallstackMessage { get => $"Callstack=\\s*{TopFrame}"; }
|
||||
|
||||
public void Close() => Logger.Close();
|
||||
|
||||
public string LogFileName { get => logFileName ?? Logger.LogFileFullPath; set => logFileName = value; }
|
||||
public void Initialize() =>
|
||||
Logger.Initialize(TracingLevel, LogFilePath, TraceSource); // initialize the logger
|
||||
Logger.Initialize(TracingLevel, LogFilePath, TraceSource, AutoFlush); // initialize the logger
|
||||
public string LogContents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (logContents == null)
|
||||
{
|
||||
Logger.Close();
|
||||
Close();
|
||||
Assert.True(!string.IsNullOrWhiteSpace(LogFileName));
|
||||
Assert.True(LogFileName.Length > "{TraceSource}_.log".Length);
|
||||
Assert.True(File.Exists(LogFileName));
|
||||
@@ -70,24 +74,30 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
set => pendingVerifications = value;
|
||||
}
|
||||
|
||||
public void Write()
|
||||
public void Write() => Write(LogMessage);
|
||||
|
||||
public void Write(string logMessage)
|
||||
{
|
||||
// write test log
|
||||
if (DoNotUseTraceSource)
|
||||
{
|
||||
TraceSource savedTraceSource = Logger.TraceSource;
|
||||
Logger.TraceSource = null;
|
||||
Logger.Write(EventType, LogMessage);
|
||||
Logger.Write(EventType, logMessage);
|
||||
Logger.TraceSource = savedTraceSource;
|
||||
}
|
||||
else
|
||||
Logger.Write(EventType, LogMessage);
|
||||
{
|
||||
Logger.Write(EventType, logMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteWithCallstack()
|
||||
public void WriteWithCallstack() => WriteWithCallstack(LogMessage);
|
||||
|
||||
public void WriteWithCallstack(string logMessage)
|
||||
{
|
||||
// write test log with callstack
|
||||
Logger.WriteWithCallstack(EventType, LogMessage);
|
||||
Logger.WriteWithCallstack(EventType, logMessage);
|
||||
ShouldVerifyCallstack = true;
|
||||
}
|
||||
|
||||
@@ -97,7 +107,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
|
||||
public void Verify(TraceEventType eventType, string message, string callstackMessage, bool shouldVerifyCallstack = false, bool expectLogMessage = true)
|
||||
{
|
||||
Logger.Flush();
|
||||
if (!AutoFlush)
|
||||
{
|
||||
Logger.Flush();
|
||||
}
|
||||
// The Regex uses .* between the severity and the message to allow SMO to vary the content. 140 SMO has nothing there, 150 has a timestamp
|
||||
if (expectLogMessage)
|
||||
{
|
||||
@@ -138,7 +151,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void VerifyInitialization(SourceLevels expectedTracingLevel, string expectedTraceSource, string logFilePath, bool isLogFileExpectedToExist, int? testNo = null)
|
||||
{
|
||||
string FailureMessagePrefix = testNo.HasValue ? $"For Test No:{testNo.Value.ToString()}," : string.Empty;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using Xunit;
|
||||
@@ -333,6 +334,33 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ServiceHost
|
||||
TestTracingLevelChangeFromWarningToError(test);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests out AutoFlush funcitonality. The verification is two fold.
|
||||
/// 1st is to verify that the setting is persistent.
|
||||
/// 2nd that after a lot of log entries are written with AutoFlush on, explicitly flushing and closing the Tracing does not increase the file size
|
||||
/// thereby verifying that there was no leftover log entries being left behind to be flushed.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void LoggerAutolFlush()
|
||||
{
|
||||
// setup the test object
|
||||
TestLogger test = new TestLogger()
|
||||
{
|
||||
TraceSource = MethodInfo.GetCurrentMethod().Name,
|
||||
AutoFlush = true,
|
||||
TracingLevel = SourceLevels.All
|
||||
};
|
||||
test.Initialize();
|
||||
// Write 10000 lines of log
|
||||
Parallel.For(0, 100, (i) => test.Write($"Message Number:{i}, Message:{test.LogMessage}"));
|
||||
long logContentsSizeBeforeExplicitFlush = (new FileInfo(test.LogFileName)).Length;
|
||||
// Please note that Logger.Close() first flushes the logs before closing them out.
|
||||
Logger.Flush();
|
||||
long logContentsSizeAfterExplicitFlush = (new FileInfo(test.LogFileName)).Length;
|
||||
Assert.True(logContentsSizeBeforeExplicitFlush == logContentsSizeAfterExplicitFlush, "The length of log file with autoflush before and after explicit flush must be same");
|
||||
test.Cleanup();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When not use TraceSource, test to verify that upon changing TracingLevel from Error To Warning,
|
||||
/// after the change, messages of Warning as well as of Error type are present in the log.
|
||||
|
||||
@@ -47,33 +47,45 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++);
|
||||
}
|
||||
// Test 2: All defaults, -logDir as null
|
||||
// Test 2: All defaults, -autoflush-log as null
|
||||
{
|
||||
var args = new string[] { "--log-dir", null };
|
||||
var args = new string[] { "--autoflush-log", null };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++);
|
||||
VerifyCommandOptions(options, testNo++, autoFlushLog: true);
|
||||
}
|
||||
// Test 3: All defaults, -logDir as empty string
|
||||
// Test 3: All defaults, -autoflush-log as empty string
|
||||
{
|
||||
var args = new string[] { "--log-dir", string.Empty };
|
||||
var args = new string[] { "--autoflush-log", string.Empty };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++);
|
||||
VerifyCommandOptions(options, testNo++, autoFlushLog: true);
|
||||
}
|
||||
// Test 4: All defaults, -log-file as null
|
||||
// Test 4: All defaults, -tracing-level as empty string
|
||||
{
|
||||
var args = new string[] { "--log-file", null };
|
||||
var args = new string[] { "--tracing-level", string.Empty };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++, logFilePath: null);
|
||||
VerifyCommandOptions(options, testNo++, tracingLevel: string.Empty);
|
||||
}
|
||||
// Test 5: All defaults, -log-file as empty string
|
||||
// Test 5: All defaults, -tracing-level as null
|
||||
{
|
||||
var args = new string[] { "--tracing-level", null };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++, tracingLevel: null);
|
||||
}
|
||||
// Test 6: All defaults, -log-file as empty string
|
||||
{
|
||||
var args = new string[] { "--log-file", string.Empty };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++, logFilePath: string.Empty);
|
||||
}
|
||||
// Test 6: All defaults, -log-file as null
|
||||
{
|
||||
var args = new string[] { "--log-file", null };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
VerifyCommandOptions(options, testNo++, logFilePath: null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void VerifyCommandOptions(ServiceLayerCommandOptions options, int? testNo = null, string errorMessage = "", string tracingLevel = null, string logFilePath = null, bool shouldExit = false, string locale = "", string logDirectory = null)
|
||||
private static void VerifyCommandOptions(ServiceLayerCommandOptions options, int? testNo = null, string errorMessage = "", string tracingLevel = null, string logFilePath = null, bool shouldExit = false, string locale = "", bool autoFlushLog = false)
|
||||
{
|
||||
Assert.NotNull(options);
|
||||
string MsgPrefix = testNo != null ? $"TestNo:{testNo} ::" : string.Empty;
|
||||
@@ -81,12 +93,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
Assert.True(tracingLevel == options.TracingLevel, $"{MsgPrefix} options:{nameof(tracingLevel)} should be '{tracingLevel}'");
|
||||
Assert.True(logFilePath == options.LogFilePath, $"{MsgPrefix} options:{nameof(logFilePath)} should be '{logFilePath}'");
|
||||
Assert.True(shouldExit == options.ShouldExit, $"{MsgPrefix} options:{nameof(shouldExit)} should be '{shouldExit}'");
|
||||
Assert.False(string.IsNullOrWhiteSpace(options.LoggingDirectory));
|
||||
if (string.IsNullOrWhiteSpace(logDirectory))
|
||||
{
|
||||
logDirectory = Path.Combine(options.DefaultLogRoot, options.ServiceName);
|
||||
}
|
||||
Assert.True(logDirectory == options.LoggingDirectory, $"{MsgPrefix} options:{nameof(logDirectory)} should be '{logDirectory}'");
|
||||
Assert.True(autoFlushLog == options.AutoFlushLog, $"{MsgPrefix} options:{nameof(autoFlushLog)} should be '{autoFlushLog}'");
|
||||
Assert.True(options.Locale == locale, $"{MsgPrefix} options:{nameof(locale)} should be '{locale}'");
|
||||
}
|
||||
|
||||
@@ -128,24 +135,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
Assert.Equal(options.Locale, string.Empty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoggingDirectorySet()
|
||||
{
|
||||
string logDir = Directory.GetCurrentDirectory();
|
||||
var args = new string[] { "--log-dir", logDir };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
|
||||
// Asserting all options were properly set
|
||||
Assert.NotNull(options);
|
||||
Assert.False(options.ShouldExit);
|
||||
Assert.Equal(options.LoggingDirectory, logDir);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TracingLevelSet()
|
||||
{
|
||||
string expectedLevel = "Critical";
|
||||
string expectedLevel = "Information";
|
||||
var args = new string[] { "--tracing-level", expectedLevel };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
|
||||
@@ -155,11 +149,23 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
Assert.Equal(options.TracingLevel, expectedLevel);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AutoFlushLogSet()
|
||||
{
|
||||
bool expectedAutoFlush = true;
|
||||
var args = new string[] { "--autoflush-log"};
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
|
||||
// Asserting all options were properly set
|
||||
Assert.NotNull(options);
|
||||
Assert.False(options.ShouldExit);
|
||||
Assert.Equal(options.AutoFlushLog, expectedAutoFlush);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LogFilePathSet()
|
||||
{
|
||||
string expectedFilePath = Path.GetRandomFileName();
|
||||
string expectedFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
||||
var args = new string[] { "--log-file", expectedFilePath };
|
||||
ServiceLayerCommandOptions options = new ServiceLayerCommandOptions(args);
|
||||
|
||||
|
||||
@@ -66,10 +66,19 @@ namespace ScriptGenerator.Properties {
|
||||
////****** Object: Database [AdventureWorks] Script Date: 9/6/2018 12:11:50 PM ******/
|
||||
///CREATE DATABASE [AdventureWorks]
|
||||
/// CONTAINMENT = NONE
|
||||
/// ON PRIMARY
|
||||
///( NAME = N'AdventureWorks_Data', FILENAME = N'D:\sql\14.0.1000.169\sqlservr\data\AdventureWorks_Data.mdf' , SIZE = 174080KB , MAXSIZE = UNLIMITED, FILEGROWTH = 16384KB )
|
||||
/// LOG ON
|
||||
///( NAME = N'AdventureWorks_Log', FILENAME = N'D:\sql\14.0.1000.169\sqlservr\data\AdventureWorks_Log.ldf' , SIZE = 18432KB , MAXSIZE = 2048GB , FILEGROWTH = [rest of string was truncated]";.
|
||||
///GO
|
||||
///ALTER DATABASE [AdventureWorks] SET COMPATIBILITY_LEVEL = 100
|
||||
///GO
|
||||
///IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
|
||||
///begin
|
||||
///EXEC [AdventureWorks].[dbo].[sp_fulltext_database] @action = 'enable'
|
||||
///end
|
||||
///GO
|
||||
///ALTER DATABASE [AdventureWorks] SET ANSI_NULL_DEFAULT OFF
|
||||
///GO
|
||||
///ALTER DATABASE [AdventureWorks] SET ANSI_NULLS ON
|
||||
///GO
|
||||
///ALTER DATABASE [rest of string was truncated]";.
|
||||
/// </summary>
|
||||
internal static string AdventureWorks {
|
||||
get {
|
||||
|
||||
Reference in New Issue
Block a user