mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-17 01:25:40 -05:00
New test common project for database connections using the settings.json (#210)
* moved test driver tests and test common classes to separate projects
This commit is contained in:
@@ -1,184 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains useful utility methods for testing connections
|
||||
/// </summary>
|
||||
public class ConnectionTestUtils
|
||||
{
|
||||
public static IEnumerable<TestServerIdentity> TestServers = InitTestServerNames();
|
||||
public static Setting Setting = InitSetting();
|
||||
|
||||
private static readonly Lazy<ConnectParams> azureTestServerConnection =
|
||||
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
||||
|
||||
private static IEnumerable<TestServerIdentity> InitTestServerNames()
|
||||
{
|
||||
try
|
||||
{
|
||||
string testServerNamesFilePath = Environment.GetEnvironmentVariable("TestServerNamesFile");
|
||||
if (!string.IsNullOrEmpty(testServerNamesFilePath))
|
||||
{
|
||||
string jsonFileContent = File.ReadAllText(testServerNamesFilePath);
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<IList<TestServerIdentity>>(jsonFileContent);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Enumerable.Empty<TestServerIdentity>();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to load the database connection server name settings. error: " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Setting InitSetting()
|
||||
{
|
||||
try
|
||||
{
|
||||
string settingsFileContents = GetSettingFileContent();
|
||||
Setting setting = Newtonsoft.Json.JsonConvert.DeserializeObject<Setting>(settingsFileContents);
|
||||
|
||||
return setting;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to load the connection settings. error: " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ConnectParams AzureTestServerConnection
|
||||
{
|
||||
get { return azureTestServerConnection.Value; }
|
||||
}
|
||||
|
||||
public static ConnectParams LocalhostConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ConnectParams()
|
||||
{
|
||||
Connection = new ConnectionDetails()
|
||||
{
|
||||
DatabaseName = "master",
|
||||
ServerName = "localhost",
|
||||
AuthenticationType = "Integrated"
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static ConnectParams InvalidConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ConnectParams()
|
||||
{
|
||||
Connection = new ConnectionDetails()
|
||||
{
|
||||
DatabaseName = "master",
|
||||
ServerName = "localhost",
|
||||
AuthenticationType = "SqlLogin",
|
||||
UserName = "invalid",
|
||||
Password = ".."
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Lazy<ConnectParams> sqlDataToolsAzureConnection =
|
||||
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
||||
|
||||
public static ConnectParams SqlDataToolsAzureConnection
|
||||
{
|
||||
get { return sqlDataToolsAzureConnection.Value; }
|
||||
}
|
||||
|
||||
private static readonly Lazy<ConnectParams> dataToolsTelemetryAzureConnection =
|
||||
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
||||
|
||||
private static string GetSettingFileContent()
|
||||
{
|
||||
string settingsFilename;
|
||||
settingsFilename = Environment.GetEnvironmentVariable("SettingsFileName");
|
||||
if (string.IsNullOrEmpty(settingsFilename))
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
settingsFilename = Environment.GetEnvironmentVariable("APPDATA") + @"\Code\User\settings.json";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
settingsFilename = Environment.GetEnvironmentVariable("HOME") + @"/Library/Application Support/Code/User/settings.json";
|
||||
}
|
||||
else
|
||||
{
|
||||
settingsFilename = Environment.GetEnvironmentVariable("HOME") + @"/.config/Code/User/settings.json";
|
||||
}
|
||||
}
|
||||
string settingsFileContents = File.ReadAllText(settingsFilename);
|
||||
|
||||
return settingsFileContents;
|
||||
}
|
||||
|
||||
public static ConnectParams DataToolsTelemetryAzureConnection
|
||||
{
|
||||
get { return dataToolsTelemetryAzureConnection.Value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a connection parameters object
|
||||
/// </summary>
|
||||
public static ConnectParams CreateConnectParams(string server, string database, string username, string password)
|
||||
{
|
||||
ConnectParams connectParams = new ConnectParams();
|
||||
connectParams.Connection = new ConnectionDetails();
|
||||
connectParams.Connection.ServerName = server;
|
||||
connectParams.Connection.DatabaseName = database;
|
||||
connectParams.Connection.UserName = username;
|
||||
connectParams.Connection.Password = password;
|
||||
connectParams.Connection.AuthenticationType = "SqlLogin";
|
||||
return connectParams;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve connection parameters from the vscode settings file
|
||||
/// </summary>
|
||||
public static ConnectParams GetConnectionFromVsCodeSettings(string serverName)
|
||||
{
|
||||
try
|
||||
{
|
||||
string settingsFileContents = GetSettingFileContent();
|
||||
|
||||
JObject root = JObject.Parse(settingsFileContents);
|
||||
JArray connections = (JArray)root["mssql.connections"];
|
||||
|
||||
var connectionObject = connections.Where(x => x["server"].ToString() == serverName).First();
|
||||
|
||||
return CreateConnectParams( connectionObject["server"].ToString(),
|
||||
connectionObject["database"].ToString(),
|
||||
connectionObject["user"].ToString(),
|
||||
connectionObject["password"].ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Unable to load connection " + serverName + " from the vscode settings.json. Ensure the file is formatted correctly.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
public class SelfCleaningTempFile : IDisposable
|
||||
{
|
||||
private bool disposed;
|
||||
|
||||
public SelfCleaningTempFile()
|
||||
{
|
||||
FilePath = Path.GetTempFileName();
|
||||
}
|
||||
|
||||
public string FilePath { get; private set; }
|
||||
|
||||
#region IDisposable Implementation
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed && disposing)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(FilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine($"Failed to cleanup {FilePath}");
|
||||
}
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// The model for deserializing settings.json
|
||||
/// </summary>
|
||||
public class Setting
|
||||
{
|
||||
[JsonProperty("mssql.connections")]
|
||||
public List<ConnectionProfile> Connections { get; set; }
|
||||
|
||||
public ConnectionProfile GetConnentProfile(string profilerName, string serverName)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(profilerName))
|
||||
{
|
||||
var byPrfileName = Connections.FirstOrDefault(x => x.ProfileName == profilerName);
|
||||
if (byPrfileName != null)
|
||||
{
|
||||
return byPrfileName;
|
||||
}
|
||||
}
|
||||
return Connections.FirstOrDefault(x => x.ServerName == serverName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The model to deserializing the connections inside settings.json
|
||||
/// </summary>
|
||||
public class ConnectionProfile
|
||||
{
|
||||
public const string CRED_PREFIX = "Microsoft.SqlTools";
|
||||
public const string CRED_SEPARATOR = "|";
|
||||
public const string CRED_SERVER_PREFIX = "server:";
|
||||
public const string CRED_DB_PREFIX = "db:";
|
||||
public const string CRED_USER_PREFIX = "user:";
|
||||
public const string CRED_ITEMTYPE_PREFIX = "itemtype:";
|
||||
|
||||
[JsonProperty("server")]
|
||||
public string ServerName { get; set; }
|
||||
public string Database { get; set; }
|
||||
|
||||
public string User { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
|
||||
public string ProfileName { get; set; }
|
||||
|
||||
public TestServerType ServerType { get; set; }
|
||||
|
||||
|
||||
public string formatCredentialId(string itemType = "Profile")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ServerName))
|
||||
{
|
||||
List<string> cred = new List<string>();
|
||||
cred.Add(CRED_PREFIX);
|
||||
AddToList(itemType, CRED_ITEMTYPE_PREFIX, cred);
|
||||
AddToList(ServerName, CRED_SERVER_PREFIX, cred);
|
||||
AddToList(Database, CRED_DB_PREFIX, cred);
|
||||
AddToList(User, CRED_USER_PREFIX, cred);
|
||||
return string.Join(CRED_SEPARATOR, cred.ToArray());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private void AddToList(string item, string prefix, List<string> list)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item))
|
||||
{
|
||||
list.Add(string.Format(CultureInfo.InvariantCulture, "{0}{1}", prefix, item));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
public class TestResult
|
||||
{
|
||||
public double ElapsedTime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// The model to deserialize the server names json
|
||||
/// </summary>
|
||||
public class TestServerIdentity
|
||||
{
|
||||
public string ServerName { get; set; }
|
||||
public string ProfileName { get; set; }
|
||||
|
||||
public TestServerType ServerType { get; set; }
|
||||
}
|
||||
|
||||
public enum TestServerType
|
||||
{
|
||||
None,
|
||||
Azure,
|
||||
OnPrem
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Timer to calculate the test run time
|
||||
/// </summary>
|
||||
public class TestTimer
|
||||
{
|
||||
private static string resultFolder = InitResultFolder();
|
||||
|
||||
private static string InitResultFolder()
|
||||
{
|
||||
string resultFodler = Environment.GetEnvironmentVariable("ResultFolder");
|
||||
if (string.IsNullOrEmpty(resultFodler))
|
||||
{
|
||||
string assemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
|
||||
resultFodler = Path.GetDirectoryName(assemblyLocation);
|
||||
}
|
||||
return resultFodler;
|
||||
}
|
||||
|
||||
public TestTimer()
|
||||
{
|
||||
Start();
|
||||
}
|
||||
|
||||
public bool PrintResult { get; set; }
|
||||
|
||||
public void Start()
|
||||
{
|
||||
StartDateTime = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void End()
|
||||
{
|
||||
EndDateTime = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void EndAndPrint([CallerMemberName] string testName = "")
|
||||
{
|
||||
End();
|
||||
if (PrintResult)
|
||||
{
|
||||
var currentColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test Name: {0} Run time in milliSeconds: {1}", testName, TotalMilliSeconds));
|
||||
Console.ForegroundColor = currentColor;
|
||||
string resultContent = Newtonsoft.Json.JsonConvert.SerializeObject(new TestResult { ElapsedTime = TotalMilliSeconds });
|
||||
string fileName = testName + ".json";
|
||||
string resultFilePath = string.IsNullOrEmpty(resultFolder) ? fileName : Path.Combine(resultFolder, fileName);
|
||||
File.WriteAllText(resultFilePath, resultContent);
|
||||
Console.WriteLine("Result file: " + resultFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public double TotalMilliSeconds
|
||||
{
|
||||
get
|
||||
{
|
||||
return (EndDateTime - StartDateTime).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
|
||||
public double TotalMilliSecondsUntilNow
|
||||
{
|
||||
get
|
||||
{
|
||||
return (DateTime.UtcNow - StartDateTime).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime StartDateTime { get; private set; }
|
||||
public DateTime EndDateTime { get; private set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user