private IErrorIndex getIndex(ErrorIndexSettings settings, StackHashSqlConfiguration sqlSettings) { IErrorIndex index; if (settings.Type == ErrorIndexType.Xml) { index = new XmlErrorIndex(settings.Folder, settings.Name); index = new ErrorIndexCache(index); } else { sqlSettings.InitialCatalog = settings.Name; index = new SqlErrorIndex(sqlSettings, settings.Name, settings.Folder); } return(index); }
// // You can use the following additional attributes as you write your tests: // // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } // #endregion public void copyIndex(bool createDestinationIndexLocally, bool switchIndex, StackHashTestIndexData testData) { try { m_Utils.RegisterForNotifications(true, m_Utils.ApplicationGuid); m_Utils.CreateAndSetNewContext(ErrorIndexType.Xml); // Create a context and give it a non-default name. m_Utils.DeleteIndex(0); GetStackHashPropertiesResponse getResp = m_Utils.GetContextSettings(); Assert.AreEqual(ErrorIndexStatus.NotCreated, getResp.Settings.ContextCollection[0].ErrorIndexSettings.Status); m_Utils.ActivateContext(0); // Create the index. m_Utils.CreateTestIndex(0, testData); getResp = m_Utils.GetContextSettings(); Assert.AreEqual(ErrorIndexStatus.Created, getResp.Settings.ContextCollection[0].ErrorIndexSettings.Status); String originalIndexPath = getResp.Settings.ContextCollection[0].ErrorIndexSettings.Folder; String originalIndexName = getResp.Settings.ContextCollection[0].ErrorIndexSettings.Name; String destIndexName = "UnitTestsCopyIndexName"; String destIndexPath = "c:\\stackhashunittests\\copyindexpath\\"; // Make sure the destination folder does not exist. String fullDestPath = Path.Combine(destIndexPath, destIndexName); if (Directory.Exists(fullDestPath)) { PathUtils.DeleteDirectory(fullDestPath, true); } ErrorIndexSettings destSettings = new ErrorIndexSettings() { Folder = destIndexPath, Name = destIndexName, Type = ErrorIndexType.SqlExpress }; StackHashSqlConfiguration sqlConfig = new StackHashSqlConfiguration() { ConnectionString = String.Format(TestSettings.DefaultConnectionString, destIndexName), InitialCatalog = destIndexName, MaxPoolSize = 100, MinPoolSize = 2, ConnectionTimeout = 20, }; if (createDestinationIndexLocally) { StackHashSqlControl.InstallerInterface installerInterface = new StackHashSqlControl.InstallerInterface(TestSettings.DefaultConnectionString + "Initial Catalog=MASTER;", destSettings.Name, destSettings.Folder); installerInterface.Connect(); installerInterface.CreateDatabase(true); installerInterface.Disconnect(); SqlConnection.ClearAllPools(); } m_Utils.DeactivateContext(0); m_Utils.CopyIndex(0, destSettings, sqlConfig, switchIndex, 60000); m_Utils.ActivateContext(0); Assert.AreEqual(true, Directory.Exists(Path.Combine(originalIndexPath, originalIndexName))); Assert.AreEqual(true, Directory.Exists(fullDestPath)); Assert.AreNotEqual(null, m_Utils.CopyCompleteAdminReport); Assert.AreEqual(null, m_Utils.CopyCompleteAdminReport.LastException); Assert.AreEqual(StackHashAsyncOperationResult.Success, m_Utils.CopyCompleteAdminReport.ResultData); getResp = m_Utils.GetContextSettings(); if (switchIndex) { Assert.AreEqual(ErrorIndexStatus.Created, getResp.Settings.ContextCollection[0].ErrorIndexSettings.Status); Assert.AreEqual(destSettings.Folder.ToUpperInvariant(), getResp.Settings.ContextCollection[0].ErrorIndexSettings.Folder.ToUpperInvariant()); Assert.AreEqual(destSettings.Name.ToUpperInvariant(), getResp.Settings.ContextCollection[0].ErrorIndexSettings.Name.ToUpperInvariant()); } else { Assert.AreEqual(ErrorIndexStatus.Created, getResp.Settings.ContextCollection[0].ErrorIndexSettings.Status); Assert.AreEqual(originalIndexPath.ToUpperInvariant(), getResp.Settings.ContextCollection[0].ErrorIndexSettings.Folder.ToUpperInvariant()); Assert.AreEqual(originalIndexName.ToUpperInvariant(), getResp.Settings.ContextCollection[0].ErrorIndexSettings.Name.ToUpperInvariant()); } } finally { m_Utils.DeactivateContext(0); m_Utils.DeleteIndex(0); } }
/// <summary> /// An index will be created in sourceFolder\SourceIndex called SourceIndex. /// It will then be moved to destFolder\DestIndex. /// If defaultDatabaseLocation is specified then only the cab files will be moved and not the SQL database. /// </summary> private void runMoveTask(String settingsFolder, String sourceErrorIndexFolder, String sourceErrorIndexName, String destErrorIndexFolder, String destErrorIndexName, bool defaultDatabaseLocation, StackHashTestIndexData testIndexData) { String scriptFolder = settingsFolder + "\\Scripts"; SqlCommands sqlCommands = new SqlCommands(m_ProviderFactory, s_MasterConnectionString, s_MasterConnectionString, 1); // Create the source database folders and settings. if (sqlCommands.DatabaseExists(destErrorIndexName)) { try { sqlCommands.DeleteDatabase(destErrorIndexName); } catch {; } } if (sqlCommands.DatabaseExists(sourceErrorIndexName)) { try { sqlCommands.DeleteDatabase(sourceErrorIndexName); } catch {; } } if (Directory.Exists(settingsFolder)) { PathUtils.DeleteDirectory(settingsFolder, true); } if (Directory.Exists(destErrorIndexFolder)) { PathUtils.DeleteDirectory(destErrorIndexFolder, true); } if (Directory.Exists(sourceErrorIndexFolder)) { PathUtils.DeleteDirectory(sourceErrorIndexFolder, true); } if (!Directory.Exists(sourceErrorIndexFolder)) { Directory.CreateDirectory(sourceErrorIndexFolder); } if (!Directory.Exists(settingsFolder)) { Directory.CreateDirectory(settingsFolder); } if (!Directory.Exists(scriptFolder)) { Directory.CreateDirectory(scriptFolder); } if (!Directory.Exists(destErrorIndexFolder)) { Directory.CreateDirectory(destErrorIndexFolder); } try { // Create a settings manager and a new context. SettingsManager settingsManager = new SettingsManager(settingsFolder + "\\ServiceSettings.XML"); StackHashContextSettings contextSettings = settingsManager.CreateNewContextSettings(); contextSettings.ErrorIndexSettings = new ErrorIndexSettings(); contextSettings.ErrorIndexSettings.Folder = sourceErrorIndexFolder; contextSettings.ErrorIndexSettings.Name = sourceErrorIndexName; contextSettings.ErrorIndexSettings.Type = ErrorIndexType.SqlExpress; contextSettings.SqlSettings = StackHashSqlConfiguration.Default; contextSettings.SqlSettings.ConnectionString = s_ConnectionString; contextSettings.SqlSettings.InitialCatalog = sourceErrorIndexName; ScriptManager scriptManager = new ScriptManager(scriptFolder); string licenseFileName = string.Format("{0}\\License.bin", settingsFolder); LicenseManager licenseManager = new LicenseManager(licenseFileName, s_ServiceGuid); licenseManager.SetLicense(s_LicenseId); // Create a dummy controller to record the callbacks. BugTrackerManager bugTrackerManager = new BugTrackerManager(new String[0]); // Create a dummy controller to record the callbacks. ControllerContext controllerContext = new ControllerContext(contextSettings, scriptManager, new Windbg(), settingsManager, true, null, licenseManager); // Hook up to receive admin reports. controllerContext.AdminReports += new EventHandler <AdminReportEventArgs>(this.OnAdminReport); // Progress reports don't come through the controller context - they come straight through the contoller so create a dummy. Controller controller = new Controller(); Reporter reporter = new Reporter(controller); controller.AdminReports += new EventHandler <AdminReportEventArgs>(this.OnAdminReport); // ****************************************** // CREATE THE SOURCE INDEX // ****************************************** // Delete any old index first. SqlConnection.ClearAllPools(); try { controllerContext.DeleteIndex(); } catch {; } // Activate the context and the associated index - this will create the index if necessary. controllerContext.Activate(null, defaultDatabaseLocation); String[] databaseFiles = Directory.GetFiles(Path.Combine(sourceErrorIndexFolder, sourceErrorIndexName), "*.mdf"); Assert.AreEqual(defaultDatabaseLocation, databaseFiles.Length == 0); controllerContext.CreateTestIndex(testIndexData); Guid guid = new Guid(); StackHashClientData clientData = new StackHashClientData(guid, "GuidName", 1); // ****************************************** // MOVE TO DESTINATION // ****************************************** // Deactivate before the move. controllerContext.Deactivate(); StackHashSqlConfiguration sqlConfig = new StackHashSqlConfiguration(s_ConnectionString, destErrorIndexName, 1, 100, 15, 100); // Move the index. controllerContext.RunMoveIndexTask(clientData, destErrorIndexFolder, destErrorIndexName, sqlConfig); // Wait for the move task to complete. waitForMoveCompleted(60000 * 20); Assert.AreEqual(2, m_AdminReports.Count); Assert.AreEqual(null, m_AdminReports[0].Report.LastException); Assert.AreEqual(0, m_AdminReports[0].Report.ContextId); Assert.AreEqual(StackHashAdminOperation.ErrorIndexMoveStarted, m_AdminReports[0].Report.Operation); Assert.AreEqual(0, m_AdminReports[1].Report.ContextId); Assert.AreEqual(StackHashAdminOperation.ErrorIndexMoveCompleted, m_AdminReports[1].Report.Operation); Assert.AreEqual(null, m_AdminReports[1].Report.LastException); Assert.AreEqual(StackHashServiceErrorCode.NoError, m_AdminReports[1].Report.ServiceErrorCode); if ((testIndexData.NumberOfCabs > 0) && (sourceErrorIndexFolder[0] != destErrorIndexFolder[0])) { Assert.AreEqual(true, m_MoveAdminReports.Count > 0); } controllerContext.AdminReports -= new EventHandler <AdminReportEventArgs>(this.OnAdminReport); ErrorIndexSettings destIndexData = new ErrorIndexSettings() { Folder = destErrorIndexFolder, Name = destErrorIndexName, Type = ErrorIndexType.SqlExpress }; IErrorIndex index1 = getIndex(destIndexData, sqlConfig); try { index1.Activate(); // Make a single call just to ensure the database is still in tact. StackHashProductCollection products = index1.LoadProductList(); Assert.AreEqual(testIndexData.NumberOfProducts, products.Count); } finally { index1.Deactivate(); index1.Dispose(); SqlConnection.ClearAllPools(); } } finally { SqlConnection.ClearAllPools(); if (sqlCommands.DatabaseExists(destErrorIndexName)) { try { sqlCommands.DeleteDatabase(destErrorIndexName); } catch {; } } if (sqlCommands.DatabaseExists(sourceErrorIndexName)) { try { sqlCommands.DeleteDatabase(sourceErrorIndexName); } catch {; } } SqlConnection.ClearAllPools(); if (Directory.Exists(sourceErrorIndexFolder)) { PathUtils.SetFilesWritable(sourceErrorIndexFolder, true); PathUtils.DeleteDirectory(sourceErrorIndexFolder, true); } if (Directory.Exists(destErrorIndexFolder)) { PathUtils.SetFilesWritable(destErrorIndexFolder, true); PathUtils.DeleteDirectory(destErrorIndexFolder, true); } if (Directory.Exists(settingsFolder)) { PathUtils.SetFilesWritable(settingsFolder, true); PathUtils.DeleteDirectory(settingsFolder, true); } } }