diff --git a/test/Microsoft.SqlTools.ServiceLayer.TestDriver/Utility/ConnectionTestUtils.cs b/test/Microsoft.SqlTools.ServiceLayer.TestDriver/Utility/ConnectionTestUtils.cs new file mode 100644 index 00000000..f48c86e8 --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.TestDriver/Utility/ConnectionTestUtils.cs @@ -0,0 +1,131 @@ +// +// 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; +using System.Linq; +using System.Runtime.InteropServices; +using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; +using Newtonsoft.Json.Linq; + +namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility +{ + /// + /// Contains useful utility methods for testing connections + /// + public class ConnectionTestUtils + { + private static readonly Lazy azureTestServerConnection = + new Lazy(() => GetConnectionFromVsCodeSettings("***REMOVED***")); + + 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 sqlDataToolsAzureConnection = + new Lazy(() => GetConnectionFromVsCodeSettings("***REMOVED***")); + + public static ConnectParams SqlDataToolsAzureConnection + { + get { return sqlDataToolsAzureConnection.Value; } + } + + private static readonly Lazy dataToolsTelemetryAzureConnection = + new Lazy(() => GetConnectionFromVsCodeSettings("***REMOVED***")); + + public static ConnectParams DataToolsTelemetryAzureConnection + { + get { return dataToolsTelemetryAzureConnection.Value; } + } + + /// + /// Create a connection parameters object + /// + 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; + } + + /// + /// Retrieve connection parameters from the vscode settings file + /// + public static ConnectParams GetConnectionFromVsCodeSettings(string serverName) + { + try + { + string 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); + + 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); + } + } + } +}