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);

            // Set polling window to zero so the second call to "DoWork" is not within the first polling window.
            const int       PollingIntervalInMinutes = 0;
            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.Twice()
            .Return(true);
            mockDeployment
            .Expect(d => d.DeleteRequest(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
            .Repeat.Twice()
            .Return(DeleteRequestId);
            mockOperation
            .Expect(o => o.StatusCheck(this.subscriptionId, CertificateThumbprint, DeleteRequestId))
            .Repeat.Twice()
            .Return(operationResult);

            // Act
            mockRepository.ReplayAll();
            DeploymentDeleteForecastWorker deploymentDeleteForecastWorker = new DeploymentDeleteForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new ScheduleDay {
                            DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime
                        } },
                PollingIntervalInMinutes);

            deploymentDeleteForecastWorker.DoWork();
            Thread.Sleep(10);
            deploymentDeleteForecastWorker.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();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Deployment_Exists_And_HorizontallyScale_Throws_An_Error()
        {
            MockRepository mockRepository = new MockRepository();

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

            // 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()
            .Return(true);
            mockDeployment
            .Expect(d => d.HorizontallyScale(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot, new[] { new HorizontalScale {
                                                                                                                                  RoleName = RoleName, InstanceCount = RequiredInstanceCount
                                                                                                                              } }, TreatWarningsAsError, Mode.Auto))
            .Repeat.Once()
            .Throw(new WebException("Error."));

            // 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();

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Deployment_Exists_And_Delete_Throws_An_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()
            .Return(false);
            mockDeployment
            .Expect(d => d.CreateRequest(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot, DeploymentName, this.packageUrl, Label, ConfigurationFilePath, StartDeployment, TreatWarningsAsError))
            .Repeat.Once()
            .Throw(new WebException("Error."));

            // Act
            mockRepository.ReplayAll();
            DeploymentCreateForecastWorker deploymentCreateForecastWorker = new DeploymentCreateForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new ScheduleDay {
                            DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime
                        } },
                DeploymentName,
                this.packageUrl,
                Label,
                ConfigurationFilePath,
                StartDeployment,
                TreatWarningsAsError,
                PollingIntervalInMinutes);

            deploymentCreateForecastWorker.DoWork();

            // Assert
            mockRepository.VerifyAll();
        }
 private static void SetupHorizontallyScaleWithStatusCheck(IDeployment mockDeployment, IOperation mockOperation, string serviceName, string deploymentSlot, HorizontalScale[] horizontalScales, string requestId)
 {
     mockDeployment
     .Expect(d => d.CheckExists(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
     .Repeat.Once()
     .Return(true);
     mockDeployment
     .Expect(d => d.HorizontallyScale(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot, horizontalScales, true, Mode.Auto))
     .Repeat.Once()
     .Return(requestId);
     mockOperation
     .Expect(o => o.StatusCheck(SubscriptionId, CertificateThumbprint, requestId))
     .Repeat.Once()
     .Return(new OperationResult {
         Status = OperationStatus.Succeeded
     });
 }
 private static void SetupDeleteRequestWithStatusCheck(IDeployment mockDeployment, IOperation mockOperation, string serviceName, string deploymentSlot, string requestId)
 {
     mockDeployment
     .Expect(d => d.CheckExists(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
     .Repeat.Once()
     .Return(true);
     mockDeployment
     .Expect(d => d.DeleteRequest(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
     .Repeat.Once()
     .Return(requestId);
     mockOperation
     .Expect(o => o.StatusCheck(SubscriptionId, CertificateThumbprint, requestId))
     .Repeat.Once()
     .Return(new OperationResult {
         Status = OperationStatus.Succeeded
     });
 }
        public void UnitTestDoWork_With_One_Role_Having_Instance_Count_Too_High_And_Second_Role_Having_Instance_Size_Too_Large()
        {
            MockRepository   mockRepository   = new MockRepository();
            WhiteListService whiteListService = new WhiteListService {
                Name = "wls1"
            };
            WhiteListRole whiteListRole = new WhiteListRole {
                Name = "wlr1", MaxInstanceSize = InstanceSize.ExtraSmall, MaxInstanceCount = 1
            };

            whiteListService.Roles.Add(whiteListRole);
            WhiteListService[] whiteListServices = new[] { whiteListService };

            // Arrange
            ISubscription mockSubscription = mockRepository.StrictMock <ISubscription>();

            mockSubscription
            .Expect(s => s.ListHostedServices())
            .Repeat.Once()
            .Return(new[] { "bls1", "wls1" });
            mockSubscription
            .Expect(s => s.SubscriptionId)
            .Repeat.Any()
            .Return(SubscriptionId);
            mockSubscription
            .Expect(s => s.CertificateThumbprint)
            .Repeat.Any()
            .Return(CertificateThumbprint);
            ISubscription[] subscriptions  = new[] { mockSubscription };
            IDeployment     mockDeployment = mockRepository.StrictMock <IDeployment>();
            IOperation      mockOperation  = mockRepository.StrictMock <IOperation>();

            // Delete the black listed service from Staging and Production
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Staging, "deleteRequest1");
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Production, "deleteRequest2");

            // Get the instance size of White List Role 1 from Staging, all okay.
            mockDeployment
            .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
            .Repeat.Once()
            .Return(InstanceSize.ExtraSmall.ToString());

            // Get the instance count of White List Role 1 from Staging, horizontally scale as a result.
            mockDeployment
            .Expect(d => d.GetInstanceCount(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
            .Repeat.Once()
            .Return(2);
            SetupHorizontallyScaleWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Staging, new[] { new HorizontalScale {
                                                                                                                             RoleName = "wlr1", InstanceCount = 1
                                                                                                                         } }, "scaleRequest1");

            // Get the instance size of the White List Role 1 from Production, delete as a result.
            mockDeployment
            .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Production, "wlr1"))
            .Repeat.Once()
            .Return(InstanceSize.ExtraLarge.ToString());
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Production, "deleteRequest3");

            // Act
            mockRepository.ReplayAll();
            WhiteListForecastWorker whiteListForecastWorker = new WhiteListForecastWorker(subscriptions, mockDeployment, mockOperation, whiteListServices, 1);

            whiteListForecastWorker.DoWork();
            whiteListForecastWorker.DoWork(); // Call twice to test the polling interval (nothing should be invoked the second time).

            // Assert
            mockRepository.VerifyAll();
        }
 private static void SetupHorizontallyScaleWithStatusCheck(IDeployment mockDeployment, IOperation mockOperation, string serviceName, string deploymentSlot, HorizontalScale[] horizontalScales, string requestId)
 {
     mockDeployment
         .Expect(d => d.CheckExists(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
         .Repeat.Once()
         .Return(true);
     mockDeployment
         .Expect(d => d.HorizontallyScale(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot, horizontalScales, true, Mode.Auto))
         .Repeat.Once()
         .Return(requestId);
     mockOperation
         .Expect(o => o.StatusCheck(SubscriptionId, CertificateThumbprint, requestId))
         .Repeat.Once()
         .Return(new OperationResult { Status = OperationStatus.Succeeded });
 }
 private static void SetupDeleteRequestWithStatusCheck(IDeployment mockDeployment, IOperation mockOperation, string serviceName, string deploymentSlot, string requestId)
 {
     mockDeployment
         .Expect(d => d.CheckExists(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
         .Repeat.Once()
         .Return(true);
     mockDeployment
         .Expect(d => d.DeleteRequest(SubscriptionId, CertificateThumbprint, serviceName, deploymentSlot))
         .Repeat.Once()
         .Return(requestId);
     mockOperation
         .Expect(o => o.StatusCheck(SubscriptionId, CertificateThumbprint, requestId))
         .Repeat.Once()
         .Return(new OperationResult { Status = OperationStatus.Succeeded });
 }
        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();
        }