mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-18 01:25:41 -05:00
New tool to store SQL connection configs locally (#218)
* added a new tool to store SQL connections locally. Modified the peek definition tests to create test database before running test * fixed failing test QueryExecutionPlanInvalidParamsTest * Fixes based on code review comments * fixed failing test GetSignatureHelpReturnsNotNullIfParseInfoInitialized
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -16,9 +16,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
public class ConnectionSetting
|
||||
{
|
||||
[JsonProperty("mssql.connections")]
|
||||
public List<ConnectionProfile> Connections { get; set; }
|
||||
public List<InstanceInfo> Connections { get; set; }
|
||||
|
||||
public ConnectionProfile GetConnectionProfile(string profileName, string serverName)
|
||||
public InstanceInfo GetConnectionProfile(string profileName, string serverName)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(profileName) && Connections != null)
|
||||
{
|
||||
@@ -33,52 +33,94 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The model to deserializing the connections inside settings.json
|
||||
/// The model to de-serializing the connections inside settings.json
|
||||
/// </summary>
|
||||
public class ConnectionProfile
|
||||
public class InstanceInfo
|
||||
{
|
||||
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:";
|
||||
public InstanceInfo(string versionKey)
|
||||
{
|
||||
ConnectTimeout = 15;
|
||||
VersionKey = versionKey;
|
||||
}
|
||||
|
||||
[JsonProperty("server")]
|
||||
public string ServerName { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string Database { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string User { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string Password { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string ProfileName { get; set; }
|
||||
|
||||
public TestServerType ServerType { get; set; }
|
||||
|
||||
public AuthenticationType AuthenticationType { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string RemoteSharePath { get; set; }
|
||||
|
||||
public string formatCredentialId(string itemType = "Profile")
|
||||
public int ConnectTimeout { get; set; }
|
||||
|
||||
public string VersionKey { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public string ConnectTimeoutAsString
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ServerName))
|
||||
get { return ConnectTimeout.ToString(); }
|
||||
set
|
||||
{
|
||||
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));
|
||||
int temp;
|
||||
if (int.TryParse(value, out temp))
|
||||
{
|
||||
this.ConnectTimeout = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.ConnectTimeout = 15;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string MachineName
|
||||
{
|
||||
get
|
||||
{
|
||||
string serverName = ServerName;
|
||||
int index = ServerName.IndexOf('\\');
|
||||
if (index > 0)
|
||||
{
|
||||
serverName = ServerName.Substring(0, index);
|
||||
}
|
||||
if (StringComparer.OrdinalIgnoreCase.Compare("(local)", serverName) == 0
|
||||
|| StringComparer.OrdinalIgnoreCase.Compare(".", serverName) == 0)
|
||||
{
|
||||
serverName = Environment.MachineName;
|
||||
}
|
||||
return serverName;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string InstanceName
|
||||
{
|
||||
get
|
||||
{
|
||||
string name = null;
|
||||
int index = ServerName.IndexOf('\\');
|
||||
if (index > 0)
|
||||
{
|
||||
name = ServerName.Substring(index + 1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// 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.Test.Common
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const string SqlConectionSettingsEnvironmentVariable = "SettingsFileName";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// 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.Runtime.InteropServices;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
public static class FileUtils
|
||||
{
|
||||
public static string UserRootFolder
|
||||
{
|
||||
get
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("USERPROFILE");
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string VsCodeSettingsFileName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("APPDATA") + @"\Code\User\settings.json";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME") + @"/Library/Application Support/Code/User/settings.json";
|
||||
}
|
||||
else
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME") + @"/.config/Code/User/settings.json";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string TestServerNamesDefaultFileName
|
||||
{
|
||||
get
|
||||
{
|
||||
string testServerFileName = "testServerNames.json";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("APPDATA") + @"\\" + testServerFileName;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME") + @"/" + testServerFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("HOME") + @"/" + testServerFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
@@ -25,19 +24,20 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
/// <summary>
|
||||
/// Create the test db if not already exists
|
||||
/// </summary>
|
||||
public static async Task<SqlTestDb> CreateNew(TestServerType serverType, bool doNotCleanupDb = false, string databaseName = null, string query = null)
|
||||
public static SqlTestDb CreateNew(TestServerType serverType, bool doNotCleanupDb = false, string databaseName = null, string query = null)
|
||||
{
|
||||
SqlTestDb testDb = new SqlTestDb();
|
||||
using (TestServiceDriverProvider testService = new TestServiceDriverProvider())
|
||||
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
databaseName = databaseName ?? GetUniqueDBName("");
|
||||
string createDatabaseQuery = Scripts.CreateDatabaseQuery.Replace("#DatabaseName#", databaseName);
|
||||
await testService.RunQuery(serverType, MasterDatabaseName, createDatabaseQuery);
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Verified test database '{0}' is created", databaseName));
|
||||
TestServiceProvider.Instance.RunQuery(serverType, MasterDatabaseName, createDatabaseQuery);
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test database '{0}' is created", databaseName));
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
await testService.RunQuery(serverType, databaseName, query);
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Verified test database '{0}' SQL types are created", databaseName));
|
||||
TestServiceProvider.Instance.RunQuery(serverType, databaseName, query);
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test database '{0}' SQL types are created", databaseName));
|
||||
}
|
||||
testDb.DatabaseName = databaseName;
|
||||
testDb.ServerType = serverType;
|
||||
@@ -47,6 +47,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
return testDb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the test db if not already exists
|
||||
/// </summary>
|
||||
public static SqlTestDb CreateNew(TestServerType serverType, string query = null)
|
||||
{
|
||||
return CreateNew(serverType, false, null, query);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a mangled name that unique based on Prefix + Machine + Process
|
||||
/// </summary>
|
||||
@@ -59,18 +67,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
namePrefix, safeMachineName, Guid.NewGuid().ToString().Replace("-", ""));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
public void Cleanup()
|
||||
{
|
||||
if(!DoNotCleanupDb)
|
||||
if (!DoNotCleanupDb)
|
||||
{
|
||||
using (TestServiceDriverProvider testService = new TestServiceDriverProvider())
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
string dropDatabaseQuery = string.Format(CultureInfo.InvariantCulture,
|
||||
(ServerType == TestServerType.Azure ? Scripts.DropDatabaseIfExistAzure : Scripts.DropDatabaseIfExist), DatabaseName);
|
||||
testService.RunQuery(ServerType, MasterDatabaseName, dropDatabaseQuery).Wait();
|
||||
string dropDatabaseQuery = string.Format(CultureInfo.InvariantCulture,
|
||||
(ServerType == TestServerType.Azure ? Scripts.DropDatabaseIfExistAzure : Scripts.DropDatabaseIfExist), DatabaseName);
|
||||
TestServiceProvider.Instance.RunQuery(ServerType, MasterDatabaseName, dropDatabaseQuery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// 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 Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
public sealed class TestConfigPersistenceHelper
|
||||
{
|
||||
private static string DefaultSettingFileName = Path.Combine(FileUtils.UserRootFolder, "sqlConnectionSettings.json");
|
||||
private static TestCredentialService credentialService = TestCredentialService.Instance;
|
||||
|
||||
public static bool Write(IEnumerable<InstanceInfo> instances)
|
||||
{
|
||||
try
|
||||
{
|
||||
ConnectionSetting connectionSetting = new Common.ConnectionSetting()
|
||||
{
|
||||
Connections = new List<InstanceInfo>(instances)
|
||||
};
|
||||
|
||||
//Remove the passwords and store in credential store and then store the copy without passwords in the file
|
||||
foreach (var instance in connectionSetting.Connections)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(instance.Password))
|
||||
{
|
||||
|
||||
if (!credentialService.SaveCredential(instance))
|
||||
{
|
||||
Console.WriteLine("Failed to store the password for server: " + instance.ServerName);
|
||||
}
|
||||
|
||||
instance.Password = null; //Make sure the password is not stored in sqlConnectionSettings.json
|
||||
instance.AuthenticationType = AuthenticationType.SqlLogin;
|
||||
}
|
||||
else
|
||||
{
|
||||
instance.AuthenticationType = AuthenticationType.Integrated;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("The SQL connection instances will be written to " + DefaultSettingFileName);
|
||||
string jsonContent = JsonConvert.SerializeObject(connectionSetting);
|
||||
|
||||
if (File.Exists(DefaultSettingFileName))
|
||||
{
|
||||
Console.WriteLine("The file " + DefaultSettingFileName + " already exists and it will be overwritten.");
|
||||
|
||||
}
|
||||
File.WriteAllText(DefaultSettingFileName, jsonContent);
|
||||
Environment.SetEnvironmentVariable(Constants.SqlConectionSettingsEnvironmentVariable, DefaultSettingFileName);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to store the instances.", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static IEnumerable<TestServerIdentity> InitTestServerNames()
|
||||
{
|
||||
try
|
||||
{
|
||||
string testServerNamesFileContent = GetTestServerNamesFileContent();
|
||||
if (!string.IsNullOrEmpty(testServerNamesFileContent))
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<IList<TestServerIdentity>>(testServerNamesFileContent);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Enumerable.Empty<TestServerIdentity>();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Enumerable.Empty<TestServerIdentity>();
|
||||
}
|
||||
}
|
||||
|
||||
internal static ConnectionSetting InitSetting()
|
||||
{
|
||||
try
|
||||
{
|
||||
string settingsFileContents = GetSettingFileContent();
|
||||
ConnectionSetting setting = Newtonsoft.Json.JsonConvert.DeserializeObject<ConnectionSetting>(settingsFileContents);
|
||||
Console.WriteLine("Connection Settings loaded successfully");
|
||||
return setting;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to load the connection settings. error: " + ex.Message);
|
||||
return new ConnectionSetting();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the location of testServerNames.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
|
||||
/// the location of vs code testServerNames.json
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string GetTestServerNamesFileContent()
|
||||
{
|
||||
var testServerNameFilePath = Environment.GetEnvironmentVariable("TestServerNamesFile");
|
||||
|
||||
if (string.IsNullOrEmpty(testServerNameFilePath))
|
||||
{
|
||||
testServerNameFilePath = FileUtils.TestServerNamesDefaultFileName;
|
||||
}
|
||||
string testServerNamesFileContent = string.IsNullOrEmpty(testServerNameFilePath) ? string.Empty : File.ReadAllText(testServerNameFilePath);
|
||||
|
||||
return testServerNamesFileContent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the location of setting.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
|
||||
/// the location of vs code settings.json
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string GetSettingFileContent()
|
||||
{
|
||||
var settingsFileName = Environment.GetEnvironmentVariable(Constants.SqlConectionSettingsEnvironmentVariable);
|
||||
|
||||
if (string.IsNullOrEmpty(settingsFileName))
|
||||
{
|
||||
if (File.Exists(DefaultSettingFileName))
|
||||
{
|
||||
settingsFileName = DefaultSettingFileName;
|
||||
Console.WriteLine(DefaultSettingFileName + " SQL connection instances are not configured. Will try to get connections from VS code settings.json");
|
||||
}
|
||||
else
|
||||
{
|
||||
//If the SQL connection settings is not set use the VS code one
|
||||
settingsFileName = FileUtils.VsCodeSettingsFileName;
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(settingsFileName))
|
||||
{
|
||||
Console.WriteLine("SQL connection instances are not configured. Run dotnet run Microsoft.SqlTools.ServiceLayer.TestEnvConfig from the command line to configure");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("SQL Connection settings are loaded from: " + settingsFileName);
|
||||
}
|
||||
|
||||
string settingsFileContents = string.IsNullOrEmpty(settingsFileName) ? string.Empty : File.ReadAllText(settingsFileName);
|
||||
|
||||
return settingsFileContents;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,136 +4,179 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Credentials;
|
||||
using Microsoft.SqlTools.ServiceLayer.Credentials.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TestDriver.Driver;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Service to get connection profiles from the configured settings
|
||||
/// to get the credentials, test driver will be used if available otherwise the credential service will be called directly
|
||||
/// credential service will be used to get the credentials
|
||||
/// </summary>
|
||||
public class TestConnectionProfileService
|
||||
{
|
||||
private readonly IEnumerable<TestServerIdentity> _testServers = TestServersLazyInstance.Value;
|
||||
private readonly ConnectionSetting _setting = ConnectionSettingLazyInstance.Value;
|
||||
private static string _connectionSettingsFilename;
|
||||
private static ConcurrentDictionary<TestServerType, ConnectionProfile> _connectionProfilesCache = new ConcurrentDictionary<TestServerType, ConnectionProfile>();
|
||||
private static Dictionary<string, InstanceInfo> connectionProfilesCache = new Dictionary<string, InstanceInfo>();
|
||||
private static TestConnectionProfileService instance = new TestConnectionProfileService();
|
||||
|
||||
public TestConnectionProfileService(ServiceTestDriver driver)
|
||||
public const string DefaultSql2005InstanceKey = "defaultSql2005";
|
||||
public const string DefaultSql2008InstanceKey = "defaultSql2008";
|
||||
public const string DefaultSql2011InstanceKey = "defaultSql2011";
|
||||
public const string DefaultSql2012Pcu1InstanceKey = "defaultSql2012pcu1";
|
||||
public const string DefaultSql2014InstanceKey = "defaultSql2014";
|
||||
public const string DefaultSqlAzureInstanceKey = "defaultSqlAzure";
|
||||
public const string DefaultServerlessInstanceKey = "defaultServerless";
|
||||
public const string DefaultSqlPdwInstanceKey = "defaultSqlPdw";
|
||||
public const string DefaultSqlAzureV12InstanceKey = "defaultSqlAzureV12";
|
||||
public const string DefaultSql2016InstanceKey = "defaultSql2016";
|
||||
public const string DefaultSqlvNextInstanceKey = "defaultSqlvNext";
|
||||
|
||||
private TestConnectionProfileService()
|
||||
{
|
||||
Driver = driver;
|
||||
LoadInstanceSettings();
|
||||
}
|
||||
|
||||
public TestConnectionProfileService()
|
||||
public static TestConnectionProfileService Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Lazy<IEnumerable<TestServerIdentity>> TestServersLazyInstance =
|
||||
new Lazy<IEnumerable<TestServerIdentity>>(InitTestServerNames);
|
||||
|
||||
private static readonly Lazy<ConnectionSetting> ConnectionSettingLazyInstance =
|
||||
new Lazy<ConnectionSetting>(InitSetting);
|
||||
|
||||
private ServiceTestDriver Driver { get; set; }
|
||||
|
||||
private ConnectionProfile GetConnectionProfile(TestServerType serverType)
|
||||
public static InstanceInfo DefaultSql2012Pcu1
|
||||
{
|
||||
ConnectionProfile connectionProfile = null;
|
||||
get { return GetInstance(DefaultSql2012Pcu1InstanceKey); }
|
||||
}
|
||||
|
||||
//Get the server or profile name for given type to use for database connection
|
||||
TestServerIdentity serverIdentity = _testServers != null ? _testServers.FirstOrDefault(x => x.ServerType == serverType) : null;
|
||||
public static InstanceInfo DefaultSql2014
|
||||
{
|
||||
get { return GetInstance(DefaultSql2014InstanceKey); }
|
||||
}
|
||||
|
||||
//Search for the connection info in settings.json
|
||||
if (serverIdentity == null)
|
||||
{
|
||||
//If not server name found, try to find the connection info for given type
|
||||
connectionProfile = _setting != null && _setting.Connections != null ? _setting.Connections.FirstOrDefault(x => x.ServerType == serverType) : null;
|
||||
if (connectionProfile == null && _setting != null && _setting.Connections != null)
|
||||
{
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture,
|
||||
"Cannot find any connection profile for server type '{0}'. "
|
||||
+ " Make sure the serverType attribute is set in {1}. " +
|
||||
"Or run CreateTestServerNameSettings.cmd to create a template for the server names", serverType, _connectionSettingsFilename));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Find the connection info for specific server name or profile name
|
||||
connectionProfile = _setting != null ? _setting.GetConnectionProfile(serverIdentity.ProfileName, serverIdentity.ServerName) : null;
|
||||
public static InstanceInfo DefaultSqlAzure
|
||||
{
|
||||
get { return GetInstance(DefaultSqlAzureInstanceKey); }
|
||||
}
|
||||
|
||||
}
|
||||
public static InstanceInfo DefaultSqlAzureV12
|
||||
{
|
||||
get { return GetInstance(DefaultSqlAzureV12InstanceKey); }
|
||||
}
|
||||
|
||||
Assert.True(connectionProfile != null, "Cannot find any connection profile for server type " + serverType.ToString());
|
||||
|
||||
return connectionProfile;
|
||||
public static InstanceInfo DefaultSql2016
|
||||
{
|
||||
get { return GetInstance(DefaultSql2016InstanceKey); }
|
||||
}
|
||||
|
||||
public static InstanceInfo DefaultSqlvNext
|
||||
{
|
||||
get { return GetInstance(DefaultSqlvNextInstanceKey); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns database connection parameters for given server type
|
||||
/// Returns the SQL connection info for given version key
|
||||
/// </summary>
|
||||
public async Task<ConnectParams> GetConnectionParametersAsync(TestServerType serverType = TestServerType.OnPrem, string databaseName = null)
|
||||
public static InstanceInfo GetInstance(string key)
|
||||
{
|
||||
ConnectionProfile connectionProfile;
|
||||
if (!_connectionProfilesCache.TryGetValue(serverType, out connectionProfile))
|
||||
{
|
||||
connectionProfile = GetConnectionProfile(serverType);
|
||||
InstanceInfo instanceInfo;
|
||||
connectionProfilesCache.TryGetValue(key, out instanceInfo);
|
||||
Assert.True(instanceInfo != null, string.Format(CultureInfo.InvariantCulture, "Cannot find any instance for version key: {0}", key));
|
||||
return instanceInfo;
|
||||
}
|
||||
|
||||
if (connectionProfile != null)
|
||||
{
|
||||
//If the password is empty, get the credential using the service
|
||||
if (connectionProfile.AuthenticationType == AuthenticationType.SqlLogin && string.IsNullOrEmpty(connectionProfile.Password))
|
||||
{
|
||||
Credential credential = await ReadCredentialAsync(connectionProfile.formatCredentialId());
|
||||
connectionProfile.Password = credential.Password;
|
||||
}
|
||||
_connectionProfilesCache.GetOrAdd(serverType, connectionProfile);
|
||||
}
|
||||
}
|
||||
|
||||
if (connectionProfile != null)
|
||||
public ConnectParams GetConnectionParameters(string key = DefaultSql2016InstanceKey, string databaseName = null)
|
||||
{
|
||||
InstanceInfo instanceInfo = GetInstance(key);
|
||||
if (instanceInfo != null)
|
||||
{
|
||||
ConnectParams connenctParam = CreateConnectParams(connectionProfile, serverType, databaseName);
|
||||
|
||||
ConnectParams connenctParam = CreateConnectParams(instanceInfo, key, databaseName);
|
||||
|
||||
return connenctParam;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request a Read Credential for given credential id
|
||||
/// Returns database connection parameters for given server type
|
||||
/// </summary>
|
||||
private async Task<Credential> ReadCredentialAsync(string credentialId)
|
||||
public ConnectParams GetConnectionParameters(TestServerType serverType = TestServerType.OnPrem, string databaseName = null)
|
||||
{
|
||||
var credentialParams = new Credential();
|
||||
credentialParams.CredentialId = credentialId;
|
||||
string key = ConvertServerTypeToVersionKey(serverType);
|
||||
return GetConnectionParameters(key, databaseName);
|
||||
}
|
||||
|
||||
ServiceTestDriver driver = Driver;
|
||||
if (driver == null)
|
||||
/// <summary>
|
||||
/// Forces the InstanceManager to load/reload it's instance list
|
||||
/// </summary>
|
||||
internal void LoadInstanceSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
TestServiceProvider.InitializeTestServices();
|
||||
return await CredentialService.Instance.ReadCredentialAsync(credentialParams);
|
||||
connectionProfilesCache = new Dictionary<string, InstanceInfo>();
|
||||
IEnumerable<TestServerIdentity> testServers = TestConfigPersistenceHelper.InitTestServerNames();
|
||||
ConnectionSetting settings = TestConfigPersistenceHelper.InitSetting();
|
||||
if (settings == null)
|
||||
{
|
||||
Console.WriteLine("DBTestInstance not configured. Run 'dotnet run Microsoft.SqlTools.ServiceLayer.TestEnvConfig from the command line to configure");
|
||||
}
|
||||
|
||||
if (testServers != null && settings != null)
|
||||
{
|
||||
foreach (var serverIdentity in testServers)
|
||||
{
|
||||
var instance = settings != null ? settings.GetConnectionProfile(serverIdentity.ProfileName, serverIdentity.ServerName) : null;
|
||||
if (instance.ServerType == TestServerType.None)
|
||||
{
|
||||
instance.ServerType = serverIdentity.ServerType;
|
||||
AddInstance(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (settings != null)
|
||||
{
|
||||
foreach (var instance in settings.Connections)
|
||||
{
|
||||
AddInstance(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
catch(Exception ex)
|
||||
{
|
||||
return await Driver.SendRequest(ReadCredentialRequest.Type, credentialParams);
|
||||
Assert.True(false, "Fail to load the SQL connection instances. error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddInstance(InstanceInfo instance)
|
||||
{
|
||||
if (instance != null && (instance.ServerType != TestServerType.None || !string.IsNullOrEmpty(instance.VersionKey)))
|
||||
{
|
||||
TestServerType serverType = instance.ServerType == TestServerType.None ? TestServerType.OnPrem : instance.ServerType; //Default to onPrem
|
||||
string versionKey = string.IsNullOrEmpty(instance.VersionKey) ? ConvertServerTypeToVersionKey(serverType) : instance.VersionKey;
|
||||
if (!connectionProfilesCache.ContainsKey(versionKey))
|
||||
{
|
||||
//If the password is empty, get the credential using the service
|
||||
if (instance.AuthenticationType == AuthenticationType.SqlLogin && string.IsNullOrEmpty(instance.Password))
|
||||
{
|
||||
Credential credential = TestCredentialService.Instance.ReadCredential(instance);
|
||||
instance.Password = credential.Password;
|
||||
}
|
||||
connectionProfilesCache.Add(versionKey, instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string ConvertServerTypeToVersionKey(TestServerType serverType)
|
||||
{
|
||||
return serverType == TestServerType.OnPrem ? DefaultSql2016InstanceKey : DefaultSqlAzureV12InstanceKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a connection parameters object
|
||||
/// </summary>
|
||||
private ConnectParams CreateConnectParams(ConnectionProfile connectionProfile, TestServerType serverType, string databaseName)
|
||||
private ConnectParams CreateConnectParams(InstanceInfo connectionProfile, string key, string databaseName)
|
||||
{
|
||||
ConnectParams connectParams = new ConnectParams();
|
||||
connectParams.Connection = new ConnectionDetails();
|
||||
@@ -146,7 +189,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
connectParams.Connection.DatabaseName = databaseName;
|
||||
}
|
||||
if (serverType == TestServerType.Azure)
|
||||
if (key == DefaultSqlAzureInstanceKey || key == DefaultSqlAzureV12InstanceKey)
|
||||
{
|
||||
connectParams.Connection.ConnectTimeout = 30;
|
||||
connectParams.Connection.Encrypt = true;
|
||||
@@ -154,112 +197,5 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
}
|
||||
return connectParams;
|
||||
}
|
||||
|
||||
private static IEnumerable<TestServerIdentity> InitTestServerNames()
|
||||
{
|
||||
try
|
||||
{
|
||||
string testServerNamesFileContent = GetTestServerNamesFileContent();
|
||||
if (!string.IsNullOrEmpty(testServerNamesFileContent))
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<IList<TestServerIdentity>>(testServerNamesFileContent);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Enumerable.Empty<TestServerIdentity>();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to load the database connection server name settings. error: " + ex.Message);
|
||||
return Enumerable.Empty<TestServerIdentity>();
|
||||
}
|
||||
}
|
||||
|
||||
private static ConnectionSetting InitSetting()
|
||||
{
|
||||
try
|
||||
{
|
||||
string settingsFileContents = GetSettingFileContent();
|
||||
ConnectionSetting setting = Newtonsoft.Json.JsonConvert.DeserializeObject<ConnectionSetting>(settingsFileContents);
|
||||
Console.WriteLine("Connection Settings loaded successfully");
|
||||
return setting;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to load the connection settings. error: " + ex.Message);
|
||||
return new ConnectionSetting();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the location of testServerNames.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
|
||||
/// the location of vs code testServerNames.json
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string GetTestServerNamesFileContent()
|
||||
{
|
||||
var testServerNameFilePath = Environment.GetEnvironmentVariable("TestServerNamesFile");
|
||||
string testServerFileName = "testServerNames.json";
|
||||
|
||||
if (string.IsNullOrEmpty(testServerNameFilePath))
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
testServerNameFilePath = Environment.GetEnvironmentVariable("APPDATA") + @"\\" + testServerFileName;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
testServerNameFilePath = Environment.GetEnvironmentVariable("HOME") + @"/" + testServerFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
testServerNameFilePath = Environment.GetEnvironmentVariable("HOME") + @"/" + testServerFileName;
|
||||
}
|
||||
}
|
||||
string testServerNamesFileContent = string.IsNullOrEmpty(testServerNameFilePath) ? string.Empty : File.ReadAllText(testServerNameFilePath);
|
||||
|
||||
return testServerNamesFileContent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the location of setting.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
|
||||
/// the location of vs code settings.json
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string GetSettingFileContent()
|
||||
{
|
||||
var 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";
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(settingsFilename))
|
||||
{
|
||||
Console.WriteLine("Cannot find any connection settings. Please run CreateConnectionSettings.cmd to generate a template for the connection settings.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Connection settings: " + settingsFilename);
|
||||
_connectionSettingsFilename = settingsFilename;
|
||||
}
|
||||
|
||||
string settingsFileContents = string.IsNullOrEmpty(settingsFilename) ? string.Empty : File.ReadAllText(settingsFilename);
|
||||
|
||||
return settingsFileContents;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// 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.Globalization;
|
||||
using Microsoft.SqlTools.ServiceLayer.Credentials;
|
||||
using Microsoft.SqlTools.ServiceLayer.Credentials.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
public class TestCredentialService
|
||||
{
|
||||
private CredentialService credentialService = TestServiceProvider.Instance.CredentialService;
|
||||
|
||||
private static TestCredentialService instance = new TestCredentialService();
|
||||
|
||||
/// <summary>
|
||||
/// The singleton instance of the service
|
||||
/// </summary>
|
||||
public static TestCredentialService Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private const string MSSQL_CRED_PREFIX = "Microsoft.SqlTools";
|
||||
private const string TEST_CRED_PREFIX = "SqlToolsTestInstance";
|
||||
private const string CRED_SEPARATOR = "|";
|
||||
private const string CRED_SERVER_PREFIX = "server:";
|
||||
private const string CRED_DB_PREFIX = "db:";
|
||||
private const string CRED_USER_PREFIX = "user:";
|
||||
private const string CRED_ITEMTYPE_PREFIX = "itemtype:";
|
||||
|
||||
/// <summary>
|
||||
/// Read Credential for given instance Info. Tries the test credential id and if no password found
|
||||
/// will try the MSSQL credential id
|
||||
/// </summary>
|
||||
public Credential ReadCredential(InstanceInfo connectionProfile)
|
||||
{
|
||||
var credentialParams = new Credential();
|
||||
credentialParams.CredentialId = FormatCredentialIdForTest(connectionProfile);
|
||||
Credential credential = credentialService.ReadCredential(credentialParams);
|
||||
if (credential == null || string.IsNullOrEmpty(credential.Password))
|
||||
{
|
||||
credentialParams.CredentialId = FormatCredentialIdForMsSql(connectionProfile);
|
||||
credential = credentialService.ReadCredential(credentialParams);
|
||||
}
|
||||
|
||||
return credential;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stored the credential to credential store using the test prefix
|
||||
/// </summary>
|
||||
public bool SaveCredential(InstanceInfo connectionProfile)
|
||||
{
|
||||
Credential credential = new Credential(FormatCredentialIdForTest(connectionProfile), connectionProfile.Password);
|
||||
return credentialService.SaveCredential(credential);
|
||||
}
|
||||
|
||||
private string FormatCredentialIdForMsSql(InstanceInfo connectionProfile, string itemType = "Profile")
|
||||
{
|
||||
return FormatCredentialId(connectionProfile, itemType, MSSQL_CRED_PREFIX);
|
||||
}
|
||||
|
||||
private string FormatCredentialIdForTest(InstanceInfo connectionProfile, string itemType = "Profile")
|
||||
{
|
||||
return FormatCredentialId(connectionProfile, itemType, TEST_CRED_PREFIX);
|
||||
}
|
||||
|
||||
private string FormatCredentialId(InstanceInfo connectionProfile, string itemType = "Profile", string credPrefix = TEST_CRED_PREFIX)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(connectionProfile.ServerName))
|
||||
{
|
||||
List<string> cred = new List<string>();
|
||||
cred.Add(credPrefix);
|
||||
AddToList(itemType, CRED_ITEMTYPE_PREFIX, cred);
|
||||
AddToList(connectionProfile.ServerName, CRED_SERVER_PREFIX, cred);
|
||||
AddToList(connectionProfile.Database, CRED_DB_PREFIX, cred);
|
||||
AddToList(connectionProfile.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
get
|
||||
{
|
||||
return (testConnectionService = testConnectionService ?? new TestConnectionProfileService(Driver));
|
||||
return (testConnectionService = testConnectionService ?? TestConnectionProfileService.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,23 +101,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
WriteToFile(ownerUri, query);
|
||||
}
|
||||
|
||||
/*
|
||||
DidOpenTextDocumentNotification openParams = new DidOpenTextDocumentNotification
|
||||
{
|
||||
TextDocument = new TextDocumentItem
|
||||
{
|
||||
Uri = ownerUri,
|
||||
LanguageId = "enu",
|
||||
Version = 1,
|
||||
Text = query
|
||||
}
|
||||
};
|
||||
|
||||
await RequestOpenDocumentNotification(openParams);
|
||||
|
||||
Thread.Sleep(500);
|
||||
*/
|
||||
|
||||
return await Connect(serverType, ownerUri, databaseName, timeout);
|
||||
}
|
||||
|
||||
@@ -127,7 +110,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
public async Task<bool> Connect(TestServerType serverType, string ownerUri, string databaseName = null, int timeout = 15000)
|
||||
{
|
||||
|
||||
var connectParams = await GetConnectionParametersAsync(serverType, databaseName);
|
||||
var connectParams = GetConnectionParameters(serverType, databaseName);
|
||||
|
||||
bool connected = await Connect(ownerUri, connectParams, timeout);
|
||||
Assert.True(connected, "Connection is successful");
|
||||
@@ -215,9 +198,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
/// <summary>
|
||||
/// Returns database connection parameters for given server type
|
||||
/// </summary>
|
||||
public async Task<ConnectParams> GetConnectionParametersAsync(TestServerType serverType, string databaseName = null)
|
||||
public ConnectParams GetConnectionParameters(TestServerType serverType, string databaseName = null)
|
||||
{
|
||||
return await TestConnectionService.GetConnectionParametersAsync(serverType, databaseName);
|
||||
return TestConnectionService.GetConnectionParameters(serverType, databaseName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -339,6 +322,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
await ConnectForQuery(serverType, query, queryTempFile.FilePath, databaseName);
|
||||
var queryResult = await CalculateRunTime(() => RunQueryAndWaitToComplete(queryTempFile.FilePath, query, 50000), false);
|
||||
Assert.NotNull(queryResult);
|
||||
Assert.NotNull(queryResult.BatchSummaries);
|
||||
|
||||
await Disconnect(queryTempFile.FilePath);
|
||||
}
|
||||
|
||||
@@ -4,13 +4,20 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Credentials;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
{
|
||||
@@ -19,39 +26,140 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
||||
/// </summary>
|
||||
public class TestServiceProvider
|
||||
{
|
||||
private TestServiceProvider()
|
||||
{
|
||||
InitializeTestServices();
|
||||
}
|
||||
|
||||
private static object _lockObject = new object();
|
||||
private static TestServiceProvider _instance = new TestServiceProvider();
|
||||
|
||||
|
||||
public static TestServiceProvider Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public CredentialService CredentialService
|
||||
{
|
||||
get
|
||||
{
|
||||
return CredentialService.Instance;
|
||||
}
|
||||
}
|
||||
|
||||
public TestConnectionProfileService ConnectionProfileService
|
||||
{
|
||||
get
|
||||
{
|
||||
return TestConnectionProfileService.Instance;
|
||||
}
|
||||
}
|
||||
|
||||
public WorkspaceService<SqlToolsSettings> WorkspaceService
|
||||
{
|
||||
get
|
||||
{
|
||||
return WorkspaceService<SqlToolsSettings>.Instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a query by calling the services directly (not using the test driver)
|
||||
/// </summary>
|
||||
public void RunQuery(TestServerType serverType, string databaseName, string queryText)
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
ConnectionInfo connInfo = InitLiveConnectionInfo(serverType, databaseName, queryTempFile.FilePath);
|
||||
Query query = new Query(queryText, connInfo, new QueryExecutionSettings(), GetFileStreamFactory(new Dictionary<string, byte[]>()));
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
private ConnectionInfo InitLiveConnectionInfo(TestServerType serverType, string databaseName, string scriptFilePath)
|
||||
{
|
||||
ConnectParams connectParams = ConnectionProfileService.GetConnectionParameters(serverType, databaseName);
|
||||
|
||||
string ownerUri = scriptFilePath;
|
||||
var connectionService = ConnectionService.Instance;
|
||||
var connectionResult = connectionService.Connect(new ConnectParams()
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectParams.Connection
|
||||
});
|
||||
|
||||
connectionResult.Wait();
|
||||
|
||||
ConnectionInfo connInfo = null;
|
||||
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||
Assert.NotNull(connInfo);
|
||||
return connInfo;
|
||||
}
|
||||
|
||||
private static IFileStreamFactory GetFileStreamFactory(Dictionary<string, byte[]> storage)
|
||||
{
|
||||
Mock<IFileStreamFactory> mock = new Mock<IFileStreamFactory>();
|
||||
mock.Setup(fsf => fsf.CreateFile())
|
||||
.Returns(() =>
|
||||
{
|
||||
string fileName = Guid.NewGuid().ToString();
|
||||
storage.Add(fileName, new byte[8192]);
|
||||
return fileName;
|
||||
});
|
||||
mock.Setup(fsf => fsf.GetReader(It.IsAny<string>()))
|
||||
.Returns<string>(output => new ServiceBufferFileStreamReader(new MemoryStream(storage[output])));
|
||||
mock.Setup(fsf => fsf.GetWriter(It.IsAny<string>()))
|
||||
.Returns<string>(output => new ServiceBufferFileStreamWriter(new MemoryStream(storage[output]), 1024, 1024));
|
||||
|
||||
return mock.Object;
|
||||
}
|
||||
|
||||
private static bool hasInitServices = false;
|
||||
|
||||
public static void InitializeTestServices()
|
||||
private static void InitializeTestServices()
|
||||
{
|
||||
if (TestServiceProvider.hasInitServices)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TestServiceProvider.hasInitServices = true;
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (TestServiceProvider.hasInitServices)
|
||||
{
|
||||
return;
|
||||
}
|
||||
TestServiceProvider.hasInitServices = true;
|
||||
|
||||
const string hostName = "SQ Tools Test Service Host";
|
||||
const string hostProfileId = "SQLToolsTestService";
|
||||
Version hostVersion = new Version(1, 0);
|
||||
const string hostName = "SQ Tools Test Service Host";
|
||||
const string hostProfileId = "SQLToolsTestService";
|
||||
Version hostVersion = new Version(1, 0);
|
||||
|
||||
// set up the host details and profile paths
|
||||
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
|
||||
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
|
||||
// set up the host details and profile paths
|
||||
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
|
||||
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
|
||||
|
||||
// Grab the instance of the service host
|
||||
ServiceHost serviceHost = ServiceHost.Instance;
|
||||
// Grab the instance of the service host
|
||||
ServiceHost serviceHost = ServiceHost.Instance;
|
||||
|
||||
// Start the service
|
||||
serviceHost.Start().Wait();
|
||||
// Start the service
|
||||
serviceHost.Start().Wait();
|
||||
|
||||
// Initialize the services that will be hosted here
|
||||
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
|
||||
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
|
||||
ConnectionService.Instance.InitializeService(serviceHost);
|
||||
CredentialService.Instance.InitializeService(serviceHost);
|
||||
QueryExecutionService.Instance.InitializeService(serviceHost);
|
||||
// Initialize the services that will be hosted here
|
||||
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
|
||||
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
|
||||
ConnectionService.Instance.InitializeService(serviceHost);
|
||||
CredentialService.Instance.InitializeService(serviceHost);
|
||||
QueryExecutionService.Instance.InitializeService(serviceHost);
|
||||
|
||||
serviceHost.Initialize();
|
||||
serviceHost.Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user