CassiniSqlFixture.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // /* **********************************************************************************
  2. // *
  3. // * Copyright (c) Sky Sanders. All rights reserved.
  4. // *
  5. // * This source code is subject to terms and conditions of the Microsoft Public
  6. // * License (Ms-PL). A copy of the license can be found in the license.htm file
  7. // * included in this distribution.
  8. // *
  9. // * You must not remove this notice, or any other, from this software.
  10. // *
  11. // * **********************************************************************************/
  12. using System;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Net;
  16. using System.Threading;
  17. using Salient.SqlServer.Testing;
  18. namespace CassiniDev.Testing.SqlServer
  19. {
  20. /// <summary>
  21. /// Made a go at spinning the server up from this process but after dealing with
  22. /// irratic behaviour regarding apartment state, platform concerns, unloaded app domains,
  23. /// and all the other issues that you can find that people struggle with I just decided
  24. /// to strictly format the console app's output and just spin up an external process.
  25. /// Seems robust so far.
  26. /// </summary>
  27. public partial class CassiniSqlFixture : DatabaseFixture, IDisposable
  28. {
  29. private bool _disposed;
  30. private bool _hostAdded;
  31. private string _hostname;
  32. private StreamWriter _input;
  33. private IPAddress _ipAddress;
  34. private Thread _outputThread;
  35. private string _rootUrl;
  36. private Process _serverProcess;
  37. public CassiniSqlFixture(string dataSource, string initialCatalog) : base(dataSource, initialCatalog)
  38. {
  39. }
  40. public CassiniSqlFixture(string dataSource, string initialCatalog, string userId, string password) : base(dataSource, initialCatalog, userId, password)
  41. {
  42. }
  43. public CassiniSqlFixture(string connectionString) : base(connectionString)
  44. {
  45. }
  46. /// <summary>
  47. /// The root URL of the running web application
  48. /// </summary>
  49. public string RootUrl
  50. {
  51. get { return _rootUrl; }
  52. }
  53. /// <summary>
  54. /// Combine the RootUrl of the running web application with the relative url
  55. /// specified.
  56. /// </summary>
  57. /// <param name="relativeUrl"></param>
  58. /// <returns></returns>
  59. public virtual Uri NormalizeUri(string relativeUrl)
  60. {
  61. relativeUrl = relativeUrl.TrimStart(new[] {'/'});
  62. string rootUrl = _rootUrl;
  63. if (!rootUrl.EndsWith("/"))
  64. {
  65. rootUrl += "/";
  66. }
  67. return new Uri(rootUrl + relativeUrl);
  68. }
  69. /// <summary>
  70. /// <para>Finds first available port in range on specified IP address.</para>
  71. /// <para>To check a specific port set start and end to same value.</para>
  72. /// </summary>
  73. /// <param name="portRangeStart"></param>
  74. /// <param name="portRangeEnd"></param>
  75. /// <param name="ipAddress"></param>
  76. /// <returns></returns>
  77. /// <exception cref="InvalidOperationException">If no port in range is available.</exception>
  78. public static ushort GetPort(ushort portRangeStart, ushort portRangeEnd, IPAddress ipAddress)
  79. {
  80. ushort port = ServiceFactory.Rules.GetAvailablePort(portRangeStart, portRangeEnd, ipAddress, true);
  81. if (port == 0)
  82. {
  83. throw new InvalidOperationException("Port is in use");
  84. }
  85. return port;
  86. }
  87. /// <summary>
  88. /// </summary>
  89. /// <param name="applicationPath">Physical path to application.</param>
  90. /// <param name="ipAddress">IP to listen on.</param>
  91. /// <param name="port">Port to listen on.</param>
  92. /// <param name="virtualPath">Optional. default value '/'</param>
  93. /// <param name="hostname">Optional unless addHostsEntry is true. In all cases is used to construct RootUrl.</param>
  94. /// <param name="addHostsEntry">If true, add hosts file entry. Requires read/write permissions to hosts file.</param>
  95. /// <param name="waitForPort">Length of time, in ms, to wait for a specific port before throwing an exception or exiting. 0 = don't wait.</param>
  96. /// <param name="timeOut">Length of time, in ms, to wait for a request before stopping the server. 0 = no timeout.</param>
  97. /// <para>If true attempts to add a temporary entry to windows hosts file comprised of ipAddress and hostname.</para>
  98. /// <para>The entry is removed when server is stopped.</para>
  99. /// <para>Throws UnauthorizedAccessException if process does not have write permissions to hosts file.</para>
  100. /// </param>
  101. public virtual void StartServer(string applicationPath, IPAddress ipAddress, ushort port, string virtualPath,
  102. string hostname, bool addHostsEntry, int waitForPort, int timeOut)
  103. {
  104. _hostAdded = addHostsEntry;
  105. _hostname = hostname;
  106. _ipAddress = ipAddress;
  107. // massage and validate arguments
  108. if (string.IsNullOrEmpty(virtualPath))
  109. {
  110. virtualPath = "/";
  111. }
  112. if (!virtualPath.StartsWith("/"))
  113. {
  114. virtualPath = "/" + virtualPath;
  115. }
  116. if (_serverProcess != null)
  117. {
  118. throw new InvalidOperationException("Server is running");
  119. }
  120. if (addHostsEntry)
  121. {
  122. if (string.IsNullOrEmpty(hostname))
  123. {
  124. throw new InvalidOperationException("Hostname is missing");
  125. }
  126. ServiceFactory.Rules.AddHostEntry(_ipAddress.ToString(), _hostname);
  127. }
  128. // use the arg object to construct command line string
  129. string commandLine = (new CommandLineArguments
  130. {
  131. Port = port,
  132. Path = string.Format("\"{0}\"", Path.GetFullPath(applicationPath).Trim('\"').TrimEnd('\\')),
  133. HostName = hostname,
  134. IPAddress = ipAddress.ToString(),
  135. VirtualPath = string.Format("\"{0}\"",virtualPath),
  136. TimeOut = timeOut,
  137. WaitForPort = waitForPort,
  138. IPMode=IPMode.Specific,
  139. PortMode=PortMode.Specific
  140. }).ToString();
  141. _serverProcess = new Process();
  142. _serverProcess.StartInfo = new ProcessStartInfo
  143. {
  144. UseShellExecute = false,
  145. ErrorDialog = false,
  146. CreateNoWindow = true,
  147. RedirectStandardOutput = true,
  148. RedirectStandardInput = true,
  149. FileName = "CassiniDev-console.exe",
  150. Arguments = commandLine,
  151. WorkingDirectory = Environment.CurrentDirectory
  152. };
  153. // we are going to monitor each line of the output until we get a start or error signal
  154. // and then just ignore the rest
  155. string line = null;
  156. _serverProcess.Start();
  157. _outputThread = new Thread(() =>
  158. {
  159. string l = _serverProcess.StandardOutput.ReadLine();
  160. while (l != null)
  161. {
  162. if (l.StartsWith("started:") || l.StartsWith("error:"))
  163. {
  164. line = l;
  165. }
  166. l = _serverProcess.StandardOutput.ReadLine();
  167. }
  168. });
  169. _outputThread.Start();
  170. // use StandardInput to send the newline to stop the server when required
  171. _input = _serverProcess.StandardInput;
  172. // block until we get a signal
  173. while (line == null)
  174. {
  175. Thread.Sleep(10);
  176. }
  177. if (!line.StartsWith("started:"))
  178. {
  179. throw new Exception(string.Format("Could not start server: {0}", line));
  180. }
  181. // line is the root url
  182. _rootUrl = line.Substring(line.IndexOf(':') + 1);
  183. }
  184. /// <summary>
  185. /// <para>Stops the server, if running and removes hosts entry if added.</para>
  186. /// </summary>
  187. public virtual void StopServer()
  188. {
  189. StopServer(100);
  190. }
  191. /// <summary>
  192. /// <para>Stops the server, if running and removes hosts entry if added.</para>
  193. /// </summary>
  194. public virtual void StopServer(int delay)
  195. {
  196. Thread.Sleep(delay);
  197. if (_serverProcess != null)
  198. {
  199. try
  200. {
  201. _input.WriteLine();
  202. _serverProcess.WaitForExit(10000);
  203. if (_hostAdded)
  204. {
  205. ServiceFactory.Rules.RemoveHostEntry(_ipAddress.ToString(), _hostname);
  206. }
  207. Thread.Sleep(10);
  208. }
  209. catch
  210. {
  211. }
  212. finally
  213. {
  214. _serverProcess.Dispose();
  215. _serverProcess = null;
  216. }
  217. }
  218. }
  219. #region IDisposable
  220. public void Dispose()
  221. {
  222. if (!_disposed)
  223. {
  224. if (_serverProcess != null)
  225. {
  226. StopServer();
  227. }
  228. }
  229. _disposed = true;
  230. GC.SuppressFinalize(this);
  231. }
  232. ~CassiniSqlFixture()
  233. {
  234. Dispose();
  235. }
  236. #endregion
  237. }
  238. }