private IUpdateClient getUpdateClientWithUpdatesToInstall(IEnumerable <string> kbIds) { var updateCollection = new UpdateCollectionClass(); foreach (var kbId in kbIds) { IUpdate update = Mock.Of <IUpdate>(); var kbIdsCollection = new StringCollectionClass(); kbIdsCollection.Add(kbId); Mock.Get(update).Setup(u => u.KBArticleIDs).Returns(kbIdsCollection); updateCollection.Add(update); } var updateClient = Mock.Of <IUpdateClient>(); Mock.Get(updateClient).Setup(uc => uc.GetAvailableUpdates()).Returns(updateCollection); var updateInstallationLogEntries = new UpdateInstallationLog(); kbIds.ToList().ForEach(kbid => updateInstallationLogEntries.Add(new UpdateInstallationLogEntry(kbid, InstallationStatus.Success, "", DateTime.UtcNow))); var result = new Result { InstallationResult = Mock.Of <IInstallationResult>(), UpdateInstallationLog = updateInstallationLogEntries }; Mock.Get(updateClient).Setup(uc => uc.InstallUpdates(It.IsAny <UpdateCollection>())).Returns(result); return(updateClient); }
public void Given_UpdatesInstalled_When_IsNotRebootNeeded_Then_RebootSystemIsNotCalled() { IUpdate update1 = Mock.Of <IUpdate>(); IUpdate update2 = Mock.Of <IUpdate>(); string updateId1 = "230b82d1-3abd-471a-a4f9-23f97fb857d9"; string updateId2 = "8497fe19-9f28-4189-b671-b95d8ba8c2d9"; Mock.Get(update1).Setup(u => u.Identity.UpdateID).Returns(updateId1); Mock.Get(update2).Setup(u => u.Identity.UpdateID).Returns(updateId2); var updateCollection = new UpdateCollectionClass(); updateCollection.Add(update1); updateCollection.Add(update2); var updateClient = Mock.Of <IUpdateClient>(); Mock.Get(updateClient).Setup(u => u.GetAvailableUpdates()).Returns(updateCollection); var installationResult = Mock.Of <IInstallationResult>(); Mock.Get(installationResult).Setup(r => r.RebootRequired).Returns(false); Mock.Get(updateClient).Setup(u => u.InstallUpdates(updateCollection)).Returns(new Result() { InstallationResult = installationResult, UpdateInstallationLog = new UpdateInstallationLog() }); var taskHandler = Mock.Of <ITaskHandler>(); var httpClient = getHttpClientWithNoExcludedUpdates(); mController = createController(null, httpClient, updateClient, null, taskHandler); bool inactive = false; mController.Inactivated += (s, e) => { inactive = true; }; mController.BeginUpdate(); while (!inactive) { Thread.Sleep(0); } Mock.Get(taskHandler).Verify(t => t.RebootSystem(), Times.Never); }
private void runUpdateProcedure() { lock (mSyncObject) { try { Logger.Debug("START runUpdateProcedure"); mInactivated = false; mStopServiceEvent.Reset(); logSystemInfo(); readLocalConfiguration(); readCentralConfiguration(); getExcludedUpdates(); if (mCentralConfiguration == null) { Logger.DebugFormat("Going idle - Customer name '{0}' was not found in the CustomerConfigurations section.", LocalConfig.CustomerName); inactivate(); return; } if (isServerInExclusionList()) { Logger.Debug("Going idle - Server is present in exclusion list."); inactivate(); return; } if (!isWithinMaintenanceTimeFrame()) { Logger.Debug("Going idle - Not within maintenance timeframe."); inactivate(); return; } trySendSavedReports(); Logger.Debug("Fetching available updates."); IUpdateCollection updates = WindowsUpdateClient.GetAvailableUpdates(); if (updates == null || updates.Count == 0) { Logger.Debug("Going idle - No updates retrieved from WUA."); inactivate(); return; } var updatesToInstall = new UpdateCollectionClass(); var excludedUpdates = new UpdateCollectionClass(); for (int i = 0; i < updates.Count; i++) { IUpdate update = updates[i]; if (!isUpdateExcluded(update)) { updatesToInstall.Add(update); } else { excludedUpdates.Add(update); } } if (updatesToInstall.Count == 0) { if (excludedUpdates.Count > 0) { var entries = UpdateInstallationLog.CreateExcludedLogEntries(excludedUpdates); var log = UpdateInstallationLog.Create(entries); reportUpdateInstallationStatus(log, true); } Logger.Debug("Going idle - No new updates found."); inactivate(); return; } Logger.Debug("Writing event log and monitoring API call."); WindowsTaskHandler.WriteEventLog(Constants.CEventLogId, Constants.CEventLogDescription); WindowsTaskHandler.CallMonitoringApi(); Logger.Debug("Installing updates."); Result result = WindowsUpdateClient.InstallUpdates(updatesToInstall); // add excluded updates to result var excludedEntries = UpdateInstallationLog.CreateExcludedLogEntries(excludedUpdates); result.UpdateInstallationLog.AddRange(excludedEntries); reportStatus(result); if (result.InstallationResult.RebootRequired) { Logger.Debug("Rebooting system."); WindowsTaskHandler.RebootSystem(); } } catch (Exception ex) { Logger.Error(ex); } finally { if (!mInactivated) { inactivate(); } Logger.Debug("FINISH runUpdateProcedure"); mStopServiceEvent.Set(); } } }
public void Start(bool showProgress) { this.ShowProgress = showProgress; Console.WriteLine("WUA_Starting"); IUpdateSession updateSession = new UpdateSessionClass(); IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher(); IUpdateDownloader updateDownloader = updateSession.CreateUpdateDownloader(); IUpdateInstaller updateInstaller = updateSession.CreateUpdateInstaller(); if (updateInstaller.IsBusy) { Console.WriteLine("WUA_IsBusy"); return; } // SEARCHING Console.WriteLine("WUA_FindingUpdates"); ISearchResult searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'"); if (searchResult.Updates.Count.Equals(0)) { Console.WriteLine("WUA_NoApplicableUpdates"); return; } // LISTING UpdateCollection updateToDownload = new UpdateCollectionClass(); for (int i = 0; i < searchResult.Updates.Count; i++) { IUpdate update = searchResult.Updates[i]; Console.WriteLine("WUA_UpdateItem:{0}|{1}", i + 1, update.Title); if (!update.IsDownloaded) { updateToDownload.Add(update); } } // DOWNLOADING if (!updateToDownload.Count.Equals(0)) { Console.WriteLine("WUA_DownloadingStarted"); updateDownloader.Updates = updateToDownload; updateDownloader.IsForced = true; updateDownloader.Priority = DownloadPriority.dpHigh; IDownloadJob job = updateDownloader.BeginDownload(this, this, updateDownloader); IDownloadResult downloaderResult = updateDownloader.EndDownload(job); Console.WriteLine("WUA_DownloadingCompleted:{0}", downloaderResult.ResultCode); } // INSTALLATION updateInstaller.Updates = searchResult.Updates; updateInstaller.IsForced = true; Console.WriteLine("WUA_InstallationStarted"); IInstallationJob installationJob = updateInstaller.BeginInstall(this, this, updateInstaller); IInstallationResult result = updateInstaller.EndInstall(installationJob); Console.WriteLine("WUA_InstallationCompleted:{0}|{1}", result.ResultCode, result.RebootRequired); // RESULT for (int i = 0; i < searchResult.Updates.Count; i++) { IUpdateInstallationResult resultItem = result.GetUpdateResult(i); Console.WriteLine("WUA_InstallationResult:{0}|{1}|{2}", i + 1, resultItem.ResultCode, resultItem.RebootRequired); } Console.WriteLine("WUA_Finish"); }
public void Start(bool showProgress) { this.ShowProgress = showProgress; Console.WriteLine("WUA_Starting"); IUpdateSession updateSession = new UpdateSessionClass(); IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher(); IUpdateDownloader updateDownloader = updateSession.CreateUpdateDownloader(); IUpdateInstaller updateInstaller = updateSession.CreateUpdateInstaller(); if (updateInstaller.IsBusy) { Console.WriteLine("WUA_IsBusy"); return; } // SEARCHING Console.WriteLine("WUA_FindingUpdates"); ISearchResult searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'"); if (searchResult.Updates.Count.Equals(0)) { Console.WriteLine("WUA_NoApplicableUpdates"); return; } // LISTING UpdateCollection updateToDownload = new UpdateCollectionClass(); for (int i = 0; i < searchResult.Updates.Count; i++) { IUpdate update = searchResult.Updates[i]; Console.WriteLine("WUA_UpdateItem:{0}|{1}", i + 1, update.Title); if (!update.IsDownloaded) { updateToDownload.Add(update); } } // DOWNLOADING if (!updateToDownload.Count.Equals(0)) { Console.WriteLine("WUA_DownloadingStarted"); updateDownloader.Updates = updateToDownload; updateDownloader.IsForced = true; updateDownloader.Priority = DownloadPriority.dpHigh; IDownloadJob job = updateDownloader.BeginDownload(this, this, updateDownloader); IDownloadResult downloaderResult = updateDownloader.EndDownload(job); Console.WriteLine("WUA_DownloadingCompleted:{0}", downloaderResult.ResultCode); } // INSTALLATION updateInstaller.Updates = searchResult.Updates; updateInstaller.IsForced = true; Console.WriteLine("WUA_InstallationStarted"); IInstallationJob installationJob = updateInstaller.BeginInstall(this, this, updateInstaller); IInstallationResult result = updateInstaller.EndInstall(installationJob); Console.WriteLine("WUA_InstallationCompleted:{0}|{1}", result.ResultCode, result.RebootRequired); // RESULT for (int i = 0; i < searchResult.Updates.Count; i++) { IUpdateInstallationResult resultItem = result.GetUpdateResult(i); Console.WriteLine("WUA_InstallationResult:{0}|{1}|{2}", i + 1, resultItem.ResultCode, resultItem.RebootRequired); } Console.WriteLine("WUA_Finish"); }
public Result InstallUpdates(IUpdateCollection updates) { //TODO: use NetBIOS name (until first .) Logger.Debug("START InstallUpdates"); var updateLog = new UpdateInstallationLog(); var uSession = new UpdateSessionClass(); var downloader = uSession.CreateUpdateDownloader(); downloader.Updates = (UpdateCollection)updates; Logger.Debug("Downloading the following updates:"); foreach (IUpdate update in updates) { Logger.DebugFormat(" KB = '{0}', Title = '{1}'", update.KBArticleIDs[0], update.Title); } downloader.Download(); var updatesToInstall = new UpdateCollectionClass(); foreach (IUpdate update in updates) { if (update.IsDownloaded) { bool addUpdate = true; if (!update.EulaAccepted) { update.AcceptEula(); } string message = string.Empty; if (update.InstallationBehavior.CanRequestUserInput) { addUpdate = false; message = Constants.CMessageUpdateCanRequestUserInput; Logger.DebugFormat("KB='{0}', Title='{1}' can request user input and it will not be installed.", update.KBArticleIDs[0], update.Title); } if (addUpdate) { updatesToInstall.Add(update); } else { Logger.DebugFormat("KB='{0}', Title='{1}' was not installed because: {2}", update.KBArticleIDs[0], update.Title, message); updateLog.Add(new UpdateInstallationLogEntry(update.KBArticleIDs[0], InstallationStatus.NotAttempted, message, DateTime.UtcNow)); } } else { Logger.DebugFormat("KB='{0}', Title='{1}' was in the list of available updates but it was not downloaded.", update.KBArticleIDs[0], update.Title); updateLog.Add(new UpdateInstallationLogEntry(update.KBArticleIDs[0], InstallationStatus.NotAttempted, Constants.CMessageUpdateNotDownloaded, DateTime.UtcNow)); } } Logger.Debug("Installing the following updates:"); foreach (IUpdate update in updatesToInstall) { var sb = new StringBuilder(); foreach (var id in update.KBArticleIDs) { sb.Append(id + ", "); } string kbArticleIds = sb.ToString(); kbArticleIds = kbArticleIds.Remove(kbArticleIds.Length - 2); Logger.DebugFormat(" KB = '{0}', Title = '{1}', KB_Articles = '{2}'", update.KBArticleIDs[0], update.Title, kbArticleIds); } IInstallationResult installationResult; if (updatesToInstall.Count == 0) { Logger.Debug("No updates to install."); installationResult = new EmptyInstallationResult(); } else { IUpdateInstaller installer = uSession.CreateUpdateInstaller(); installer.Updates = updatesToInstall; installationResult = installer.Install(); for (int i = 0; i < updatesToInstall.Count; i++) { var result = installationResult.GetUpdateResult(i); if (result.ResultCode == OperationResultCode.orcSucceeded) { updateLog.Add(new UpdateInstallationLogEntry(updatesToInstall[i].KBArticleIDs[0], InstallationStatus.Success, "", DateTime.UtcNow)); } else { updateLog.Add(new UpdateInstallationLogEntry(updatesToInstall[i].KBArticleIDs[0], InstallationStatus.Failure , "Operation result: " + result.ResultCode , DateTime.UtcNow)); } } } Logger.Debug("END InstallUpdates"); var processResult = new Result { InstallationResult = installationResult, UpdateInstallationLog = updateLog }; return(processResult); }