Пример #1
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);
                }
            }
        }