public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Check_Exists_Throws_Error()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);
            const int PollingIntervalInMinutes = 60;

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();
            mockDeployment
                .Expect(d => d.CheckExists(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
                .Repeat.Once()
                .Throw(new Exception("Error."));

            // Act
            mockRepository.ReplayAll();
            ScheduledHorizontalScaleForecastWorker deploymentCreateForecastWorker = new ScheduledHorizontalScaleForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new HorizontalScale { InstanceCount = 1, RoleName = RoleName } },
                new[] { new ScheduleDay { DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                TreatWarningsAsError,
                Mode.Auto,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_But_Not_On_A_Scheduled_Day()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);

            // Get a day that is not today.
            DayOfWeek notToday = DayOfWeek.Sunday;
            if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
            {
                notToday = DayOfWeek.Monday;
            }

            const int PollingIntervalInMinutes = 60;

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();

            // Act
            mockRepository.ReplayAll();
            ScheduledHorizontalScaleForecastWorker deploymentCreateForecastWorker = new ScheduledHorizontalScaleForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new HorizontalScale { InstanceCount = 1, RoleName = RoleName } },
                new[] { new ScheduleDay { DayOfWeek = notToday, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                TreatWarningsAsError,
                Mode.Auto,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Deployment_Exists_And_Requires_Scaling_Out()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);
            const int RequiredInstanceCount = 2;
            const int PollingIntervalInMinutes = 60;
            OperationResult operationResult = new OperationResult
                {
                    Code = "Test",
                    HttpStatusCode = HttpStatusCode.OK,
                    Id = Guid.NewGuid(),
                    Message = string.Empty,
                    Status = OperationStatus.Succeeded
                };

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();
            mockDeployment
                .Expect(d => d.CheckExists(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
                .Repeat.Once()
                .Return(true);
            mockDeployment
                .Expect(d => d.HorizontallyScale(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot, new[] { new HorizontalScale { RoleName = RoleName, InstanceCount = RequiredInstanceCount } }, TreatWarningsAsError, Mode.Auto))
                .Repeat.Once()
                .Return(RequestId);
            mockOperation
                .Expect(o => o.StatusCheck(this.subscriptionId, CertificateThumbprint, RequestId))
                .Repeat.Once()
                .Return(operationResult);

            // Act
            mockRepository.ReplayAll();
            ScheduledHorizontalScaleForecastWorker deploymentCreateForecastWorker = new ScheduledHorizontalScaleForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new HorizontalScale { InstanceCount = RequiredInstanceCount, RoleName = RoleName } },
                new[] { new ScheduleDay { DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                TreatWarningsAsError,
                Mode.Auto,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();
            deploymentCreateForecastWorker.DoWork(); // Call DoWork twice to check the polling window works.

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Deployment_Exists_And_Second_Call_Not_Within_Polling_Interval()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);
            const int RequiredInstanceCount = 2;

            // Set polling window to zero so the second call to "DoWork" is not within the first polling window.
            const int PollingIntervalInMinutes = 0;

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();
            mockDeployment
                .Expect(d => d.CheckExists(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
                .Repeat.Twice()
                .Return(true);
            mockDeployment
                .Expect(d => d.HorizontallyScale(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot, new[] { new HorizontalScale { RoleName = RoleName, InstanceCount = RequiredInstanceCount } }, TreatWarningsAsError, Mode.Auto))
                .Repeat.Twice()
                .Return(null);

            // Act
            mockRepository.ReplayAll();
            ScheduledHorizontalScaleForecastWorker deploymentCreateForecastWorker = new ScheduledHorizontalScaleForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new HorizontalScale { InstanceCount = RequiredInstanceCount, RoleName = RoleName } },
                new[] { new ScheduleDay { DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                TreatWarningsAsError,
                Mode.Auto,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();
            Thread.Sleep(10);
            deploymentCreateForecastWorker.DoWork(); // Call DoWork twice to check the polling window works.

            // Assert
            mockRepository.VerifyAll();
        }
        internal static ScheduledHorizontalScaleForecastWorker[] GetScheduledHorizontalScaleForecastWorkers()
        {
            IConfigurationSource configurationSource = GetConfigurationSource();
            ArrayList list = new ArrayList();
            foreach (ScheduledHorizontalScaleConfiguration scheduledHorizontalScaleConfiguration in configurationSource.GetWindowsAzureScheduledHorizontalScaleConfigurations())
            {
                SubscriptionConfiguration subscriptionConfiguration = configurationSource.GetWindowsAzureSubscriptionConfiguration(scheduledHorizontalScaleConfiguration.SubscriptionConfigurationId);
                foreach (ScheduleDefinitionConfiguration scheduleDefinitionConfiguration in scheduledHorizontalScaleConfiguration.Schedules)
                {
                    HorizontalScale[] horizontalScales = GetHorizontalScalesFromHorizontalScaleConfiguration(scheduledHorizontalScaleConfiguration.HorizontalScales.ToArray());
                    ScheduleDay[] scheduleDays = GetScheduleDaysFromScheduleConfiguration(scheduleDefinitionConfiguration);
                    ScheduledHorizontalScaleForecastWorker scheduledHorizontalScaleForecastWorker = new ScheduledHorizontalScaleForecastWorker(
                        new Deployment(),
                        new Operation(),
                        subscriptionConfiguration.SubscriptionId,
                        subscriptionConfiguration.CertificateThumbprint,
                        scheduledHorizontalScaleConfiguration.ServiceName,
                        scheduledHorizontalScaleConfiguration.DeploymentSlot,
                        horizontalScales,
                        scheduleDays,
                        scheduledHorizontalScaleConfiguration.TreatWarningsAsError,
                        scheduledHorizontalScaleConfiguration.Mode,
                        scheduledHorizontalScaleConfiguration.PollingIntervalInMinutes);
                    list.Add(scheduledHorizontalScaleForecastWorker);
                }
            }

            return (ScheduledHorizontalScaleForecastWorker[])list.ToArray(typeof(ScheduledHorizontalScaleForecastWorker));
        }