public void Prepare_should_add_steps_to_update_scheduled_tasks_when_settings_has_changed() { // arrange const string exePath = "exe path has changed"; SchedulerAppTask schedulerAppTask = _projectInfo.SchedulerAppTasks.Second(); ScheduledTaskDetails runningTaskDetails = GetTaskDetails( schedulerAppTask, exePath, true, false, new ScheduledTaskRepetition( schedulerAppTask.Repetition.Interval, schedulerAppTask.Repetition.Duration, schedulerAppTask.Repetition.StopAtDurationEnd)); _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>())) .Returns(runningTaskDetails); _passwordCollectorFake .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns("password"); // act _deployTask.Prepare(); // assert Assert.AreEqual(4, _deployTask.SubTasks.Count(x => x is UpdateSchedulerTaskDeploymentStep)); }
public void Prepare_should_collect_password_when_tasks_settings_has_changed() { // arrange const string exePath = "exe path has changed"; SchedulerAppTask schedulerAppTask = _projectInfo.SchedulerAppTasks.First(); ScheduledTaskDetails runningTaskDetails = GetTaskDetails( schedulerAppTask, exePath, true, false, new ScheduledTaskRepetition( schedulerAppTask.Repetition.Interval, schedulerAppTask.Repetition.Duration, schedulerAppTask.Repetition.StopAtDurationEnd)); _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>())) .Returns(runningTaskDetails); _passwordCollectorFake .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns("password"); // act _deployTask.Prepare(); // assert _passwordCollectorFake.Verify( x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(2)); }
public void Prepare_should_fail_if_task_is_running() { // arrange SchedulerAppTask schedulerAppTask = _projectInfo.SchedulerAppTasks.Second(); ScheduledTaskDetails runningTaskDetails = GetTaskDetails( schedulerAppTask, Path.Combine( _environmentInfo.SchedulerAppsBaseDirPath, _projectInfo.SchedulerAppDirName, schedulerAppTask.ExecutableName), true, true, new ScheduledTaskRepetition( schedulerAppTask.Repetition.Interval, schedulerAppTask.Repetition.Duration, schedulerAppTask.Repetition.StopAtDurationEnd)); _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>())) .Returns(runningTaskDetails); // act assert Assert.Throws <DeploymentTaskException>(() => _deployTask.Prepare()); }
private static ScheduledTaskDetails GetTaskDetails(SchedulerAppTask schedulerAppTask, string exeAbsolutePath, bool isEnabled, bool isRunning, ScheduledTaskRepetition repetition) { return (new ScheduledTaskDetails( schedulerAppTask.Name, isEnabled, isRunning, DateTime.Now, DateTime.Now, exeAbsolutePath, schedulerAppTask.ScheduledHour, schedulerAppTask.ScheduledMinute, schedulerAppTask.ExecutionTimeLimitInMinutes, repetition)); }
public void Prepare_should_collect_password_only_once_when_tasks_settings_has_changed_and_username_is_the_same() { // arrange SchedulerAppTask schedulerAppTask1 = _projectInfo.SchedulerAppTasks.First(); SchedulerAppTask schedulerAppTask2 = _projectInfo.SchedulerAppTasks.Second(); EnvironmentInfo environmentInfo = DeploymentDataGenerator.GetEnvironmentInfo( new[] { new EnvironmentUser(schedulerAppTask1.UserId, "user_name"), new EnvironmentUser(schedulerAppTask2.UserId, "user_name"), }); _environmentInfoRepositoryFake .Setup(x => x.FindByName(It.IsAny <string>())) .Returns(environmentInfo); const string exePath = "exe path has changed"; ScheduledTaskDetails runningTaskDetails = GetTaskDetails( schedulerAppTask1, exePath, true, false, new ScheduledTaskRepetition( schedulerAppTask1.Repetition.Interval, schedulerAppTask1.Repetition.Duration, schedulerAppTask1.Repetition.StopAtDurationEnd)); _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>())) .Returns(runningTaskDetails); _passwordCollectorFake .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns("password"); // act _deployTask.Prepare(); // assert _passwordCollectorFake.Verify( x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(1)); }
private TaskSettingsCompareResult CompareTaskSettings(ScheduledTaskDetails taskDetails, SchedulerAppTask schedulerAppTask, EnvironmentInfo environmentInfo) { if (taskDetails == null) { return(new TaskSettingsCompareResult(new List <SettingDifference>())); } string taskExecutablePath = GetTaskExecutablePath(schedulerAppTask, environmentInfo); var compareResultBuilder = new TaskSettingsCompareResultBuilder(); compareResultBuilder .CompareValues("Name", taskDetails.Name, schedulerAppTask.Name) .CompareValues("ScheduledHour", taskDetails.ScheduledHour, schedulerAppTask.ScheduledHour) .CompareValues("ScheduledMinute", taskDetails.ScheduledMinute, schedulerAppTask.ScheduledMinute) .CompareValues("ExecutionTimeLimitInMinutes", taskDetails.ExecutionTimeLimitInMinutes, schedulerAppTask.ExecutionTimeLimitInMinutes) .CompareValues("Repetition.Interval", taskDetails.Repetition.Interval, schedulerAppTask.Repetition.Interval) .CompareValues("Repetition.Duration", taskDetails.Repetition.Duration, schedulerAppTask.Repetition.Duration) .CompareValues("Repetition.StopAtDurationEnd", taskDetails.Repetition.StopAtDurationEnd, schedulerAppTask.Repetition.StopAtDurationEnd) .CompareValues("ExeAbsolutePath", taskDetails.ExeAbsolutePath, taskExecutablePath); return(compareResultBuilder.GetResult()); }
private string GetTaskExecutablePath(SchedulerAppTask schedulerAppTask, EnvironmentInfo environmentInfo) { string targetDirPath = GetTargetDirPath(environmentInfo); return(Path.Combine(targetDirPath, schedulerAppTask.ExecutableName)); }
private void AddTaskConfigurationSteps(EnvironmentInfo environmentInfo, string schedulerServerTasksMachineName, SchedulerAppTask schedulerAppTask, ScheduledTaskDetails taskDetails = null) { TaskSettingsCompareResult settingsCompareResult = CompareTaskSettings(taskDetails, schedulerAppTask, environmentInfo); bool hasSettingsChanged = settingsCompareResult.AreEqual == false; bool taskExists = taskDetails != null; if (taskExists) { LogSettingsDifferences(schedulerAppTask.Name, settingsCompareResult); } else { PostDiagnosticMessage(string.Format("Scheduler task [{0}] doesn't exist.", schedulerAppTask.Name), DiagnosticMessageType.Trace); } EnvironmentUser environmentUser = environmentInfo.GetEnvironmentUser(schedulerAppTask.UserId); string environmentUserPassword = null; if (!taskExists || hasSettingsChanged) { // collect password if not already collected if (!_collectedPasswordsByUserName.TryGetValue(environmentUser.UserName, out environmentUserPassword)) { environmentUserPassword = PasswordCollectorHelper.CollectPasssword( _passwordCollector, DeploymentInfo.DeploymentId, environmentInfo, schedulerServerTasksMachineName, environmentUser, OnDiagnosticMessagePosted); _collectedPasswordsByUserName.Add(environmentUser.UserName, environmentUserPassword); } } string taskExecutablePath = GetTaskExecutablePath(schedulerAppTask, environmentInfo); if (!taskExists) { // create a step for scheduling a new app AddSubTask( new CreateSchedulerTaskDeploymentStep( schedulerServerTasksMachineName, schedulerAppTask.Name, taskExecutablePath, environmentUser.UserName, environmentUserPassword, schedulerAppTask.ScheduledHour, schedulerAppTask.ScheduledMinute, schedulerAppTask.ExecutionTimeLimitInMinutes, schedulerAppTask.Repetition, _taskScheduler)); } else if (hasSettingsChanged) { // create a step for updating an existing scheduler app AddSubTask( new UpdateSchedulerTaskDeploymentStep( schedulerServerTasksMachineName, schedulerAppTask.Name, taskExecutablePath, environmentUser.UserName, environmentUserPassword, schedulerAppTask.ScheduledHour, schedulerAppTask.ScheduledMinute, schedulerAppTask.ExecutionTimeLimitInMinutes, schedulerAppTask.Repetition, _taskScheduler)); } }
public void SetUp() { _projectInfoRepositoryFake = new Mock <IProjectInfoRepository>(); _environmentInfoRepositoryFake = new Mock <IEnvironmentInfoRepository>(); _artifactsRepositoryFake = new Mock <IArtifactsRepository>(); _taskSchedulerFake = new Mock <ITaskScheduler>(); _passwordCollectorFake = new Mock <IPasswordCollector>(); _directoryAdapterFake = new Mock <IDirectoryAdapter>(); _fileAdapterFake = new Mock <IFileAdapter>(); _zipFileAdapterFake = new Mock <IZipFileAdapter>(); _projectInfo = ProjectInfoGenerator.GetSchedulerAppProjectInfo(); SchedulerAppTask schedulerAppTask1 = _projectInfo.SchedulerAppTasks.First(); SchedulerAppTask schedulerAppTask2 = _projectInfo.SchedulerAppTasks.Second(); _environmentInfo = DeploymentDataGenerator.GetEnvironmentInfo( new[] { new EnvironmentUser(schedulerAppTask1.UserId, "user_name_1"), new EnvironmentUser(schedulerAppTask2.UserId, "user_name_2"), }); _projectInfoRepositoryFake.Setup(pir => pir.FindByName(It.IsAny <string>())) .Returns(_projectInfo); string exeAbsolutePath1 = Path.Combine( _environmentInfo.SchedulerAppsBaseDirPath, _projectInfo.SchedulerAppDirName, schedulerAppTask1.ExecutableName); var scheduledTaskRepetition1 = new ScheduledTaskRepetition( schedulerAppTask1.Repetition.Interval, schedulerAppTask1.Repetition.Duration, schedulerAppTask1.Repetition.StopAtDurationEnd); ScheduledTaskDetails taskDetails1 = GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, true, false, scheduledTaskRepetition1); ScheduledTaskDetails taskDetailsDisabled1 = GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, false, false, scheduledTaskRepetition1); int timesCalled11 = 0; _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask1.Name)) .Returns(() => { timesCalled11++; if (timesCalled11 == 1) { return(taskDetails1); } if (timesCalled11 == 2) { return(taskDetailsDisabled1); } throw new Exception("Unexpected number of calls!"); }); int timesCalled21 = 0; _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask1.Name)) .Returns(() => { timesCalled21++; if (timesCalled21 == 1) { return(taskDetails1); } if (timesCalled21 == 2) { return(taskDetailsDisabled1); } throw new Exception("Unexpected number of calls!"); }); string exeAbsolutePath2 = Path.Combine( _environmentInfo.SchedulerAppsBaseDirPath, _projectInfo.SchedulerAppDirName, schedulerAppTask2.ExecutableName); var scheduledTaskRepetition2 = new ScheduledTaskRepetition( schedulerAppTask2.Repetition.Interval, schedulerAppTask2.Repetition.Duration, schedulerAppTask2.Repetition.StopAtDurationEnd); ScheduledTaskDetails taskDetails2 = GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, true, false, scheduledTaskRepetition2); ScheduledTaskDetails taskDetailsDisabled2 = GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, false, false, scheduledTaskRepetition2); int timesCalled12 = 0; _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask2.Name)) .Returns(() => { timesCalled12++; if (timesCalled12 == 1) { return(taskDetails2); } if (timesCalled12 == 2) { return(taskDetailsDisabled2); } throw new Exception("Unexpected number of calls!"); }); int timesCalled22 = 0; _taskSchedulerFake .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask2.Name)) .Returns(() => { timesCalled22++; if (timesCalled22 == 1) { return(taskDetails2); } if (timesCalled22 == 2) { return(taskDetailsDisabled2); } throw new Exception("Unexpected number of calls!"); }); _environmentInfoRepositoryFake .Setup(x => x.FindByName(It.IsAny <string>())) .Returns(_environmentInfo); _directoryAdapterFake .Setup(x => x.Exists(It.IsAny <string>())) .Returns(true); _deployTask = new DeploySchedulerAppDeploymentTask( _projectInfoRepositoryFake.Object, _environmentInfoRepositoryFake.Object, _artifactsRepositoryFake.Object, _taskSchedulerFake.Object, _passwordCollectorFake.Object, _directoryAdapterFake.Object, _fileAdapterFake.Object, _zipFileAdapterFake.Object); _deployTask.Initialize(DeploymentInfoGenerator.GetSchedulerAppDeploymentInfo()); }