public async Task GetResouceAsync_ReturnsCorrectResource() { // arrange var group = new AutoScalingGroup { AutoScalingGroupName = "ASG Name" }; var firstPage = new DescribeAutoScalingGroupsResponse { AutoScalingGroups = new List <AutoScalingGroup> { group } }; var client = CreateAutoScalingClientStub(firstPage); // act var sut = new AutoScalingGroupSource(client); var result = await sut.GetResourceAsync(group.AutoScalingGroupName); // assert Assert.That(result, Is.EqualTo(group)); }
public async Task GetResourcesAsync_MultiplePages_AllFetchedAndReturned() { // arrange var firstPage = new DescribeAutoScalingGroupsResponse { AutoScalingGroups = new List <AutoScalingGroup> { new AutoScalingGroup { AutoScalingGroupName = "Asg1" } } }; var secondPage = new DescribeAutoScalingGroupsResponse { AutoScalingGroups = new List <AutoScalingGroup> { new AutoScalingGroup { AutoScalingGroupName = "Asg2" } } }; var client = CreateAutoScalingClientStub(firstPage, secondPage); // act var sut = new AutoScalingGroupSource(client); var result = await sut.GetResourceNamesAsync(); // assert Assert.That(result.Count, Is.EqualTo(2)); Assert.That(result.First(), Is.EqualTo(firstPage.AutoScalingGroups.Single().AutoScalingGroupName)); Assert.That(result.Skip(1).First(), Is.EqualTo(secondPage.AutoScalingGroups.Single().AutoScalingGroupName)); }
public async Task GetResourcesAsync_EmptyResult_EmptyListReturned() { // arrange var firstPage = new DescribeAutoScalingGroupsResponse { AutoScalingGroups = new List <AutoScalingGroup>() }; var client = CreateAutoScalingClientStub(firstPage); // act var sut = new AutoScalingGroupSource(client); var result = await sut.GetResourceNamesAsync(); // assert Assert.That(result.Count, Is.EqualTo(0)); }
public async Task UsesDelayedScalingThresholdFromCloudWatch() { // arrange var stack = new FakeStackDeployer(); var autoScalingClient = FakeAwsClients.CreateAutoScalingClientForGroups(new[] { new AutoScalingGroup() { AutoScalingGroupName = "group-delay-20" }, new AutoScalingGroup() { AutoScalingGroupName = "group-delay-100" } }); var source = new AutoScalingGroupSource(autoScalingClient); var creator = new CloudFormationAlarmCreator(stack, new ConsoleAlarmLogger(true)); var config = ConfigHelper.CreateBasicConfiguration("test", "group-suffix", new AlertingGroupServices() { AutoScaling = new AwsServiceAlarms <AutoScalingResourceConfig>() { Resources = new List <ResourceThresholds <AutoScalingResourceConfig> >() { new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-delay-20", Options = new AutoScalingResourceConfig() { InstanceCountIncreaseDelayMinutes = 20 } }, new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-delay-100" } }, Options = new AutoScalingResourceConfig() { InstanceCountIncreaseDelayMinutes = 100 } } }); var now = DateTime.Parse("2018-01-26"); var fakeTime = new Mock <ICurrentTimeProvider>(); fakeTime.Setup(f => f.UtcNow).Returns(now); var cloudWatch = new Mock <IAmazonCloudWatch>(); SetupCloudWatchDesiredMetric(cloudWatch, 100 * 60, now, "group-delay-100", 90); SetupCloudWatchDesiredMetric(cloudWatch, 20 * 60, now, "group-delay-20", 80); var provider = new AutoScalingGroupAlarmDataProvider(cloudWatch.Object, fakeTime.Object); var sut = IoCHelper.CreateSystemUnderTest( source, provider, provider, WatchmanServiceConfigurationMapper.MapAutoScaling, creator, ConfigHelper.ConfigLoaderFor(config) ); // act await sut.LoadAndGenerateAlarms(RunMode.GenerateAlarms); // assert var alarms = stack .Stack("Watchman-test") .AlarmsByDimension("AutoScalingGroupName"); var alarm100 = alarms["group-delay-100"].Single(a => a.Properties["AlarmName"].ToString().Contains("InService")); var alarm20 = alarms["group-delay-20"].Single(a => a.Properties["AlarmName"].ToString().Contains("InService")); var defaultAlarmThreshold = 0.5m; Assert.That((decimal)alarm100.Properties["Threshold"], Is.EqualTo(90 * defaultAlarmThreshold)); Assert.That((decimal)alarm20.Properties["Threshold"], Is.EqualTo(80 * defaultAlarmThreshold)); }
public async Task TimesSuppliedToCloudWatchAreUtc() { // arrange var stack = new FakeStackDeployer(); var autoScalingClient = FakeAwsClients.CreateAutoScalingClientForGroups(new[] { new AutoScalingGroup() { AutoScalingGroupName = "group-1", DesiredCapacity = 40 } }); var source = new AutoScalingGroupSource(autoScalingClient); var creator = new CloudFormationAlarmCreator(stack, new ConsoleAlarmLogger(true)); var config = ConfigHelper.CreateBasicConfiguration("test", "group-suffix", new AlertingGroupServices() { AutoScaling = new AwsServiceAlarms <AutoScalingResourceConfig>() { Resources = new List <ResourceThresholds <AutoScalingResourceConfig> >() { new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-1", Options = new AutoScalingResourceConfig() { InstanceCountIncreaseDelayMinutes = 5 } } } } }); var cloudWatch = new Mock <IAmazonCloudWatch>(); cloudWatch .Setup(c => c.GetMetricStatisticsAsync(It.IsAny <GetMetricStatisticsRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new GetMetricStatisticsResponse() { Datapoints = new List <Datapoint>() { new Datapoint() { Minimum = 5 } } }); var provider = new AutoScalingGroupAlarmDataProvider(cloudWatch.Object, new CurrentTimeProvider()); var sut = IoCHelper.CreateSystemUnderTest( source, provider, provider, WatchmanServiceConfigurationMapper.MapAutoScaling, creator, ConfigHelper.ConfigLoaderFor(config) ); // act await sut.LoadAndGenerateAlarms(RunMode.GenerateAlarms); // assert cloudWatch.Verify(x => x.GetMetricStatisticsAsync( It.Is <GetMetricStatisticsRequest>( r => r.StartTime.Kind == DateTimeKind.Utc && r.EndTime.Kind == DateTimeKind.Utc ), It.IsAny <CancellationToken>()) ); }
public async Task UsesDesiredInstancesForThresholdByDefault() { // arrange var stack = new FakeStackDeployer(); var autoScalingClient = FakeAwsClients.CreateAutoScalingClientForGroups(new[] { new AutoScalingGroup() { AutoScalingGroupName = "group-1", DesiredCapacity = 40 } }); var source = new AutoScalingGroupSource(autoScalingClient); var creator = new CloudFormationAlarmCreator(stack, new ConsoleAlarmLogger(true)); var config = ConfigHelper.CreateBasicConfiguration("test", "group-suffix", new AlertingGroupServices() { AutoScaling = new AwsServiceAlarms <AutoScalingResourceConfig>() { Resources = new List <ResourceThresholds <AutoScalingResourceConfig> >() { new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-1" } } } }); var fakeTime = new Mock <ICurrentTimeProvider>(); var cloudWatch = new Mock <IAmazonCloudWatch>(); var provider = new AutoScalingGroupAlarmDataProvider(cloudWatch.Object, fakeTime.Object); var sut = IoCHelper.CreateSystemUnderTest( source, provider, provider, WatchmanServiceConfigurationMapper.MapAutoScaling, creator, ConfigHelper.ConfigLoaderFor(config) ); // act await sut.LoadAndGenerateAlarms(RunMode.GenerateAlarms); // assert var alarms = stack .Stack("Watchman-test") .AlarmsByDimension("AutoScalingGroupName"); var alarm = alarms["group-1"].Single(a => a.Properties["AlarmName"].ToString().Contains("InService")); var defaultAlarmThreshold = 0.5m; Assert.That((decimal)alarm.Properties["Threshold"], Is.EqualTo(40 * defaultAlarmThreshold)); }
public async Task ContinuesWhenAlarmGenerationFailsForOneAlertingGroup() { // arrange var stack = new FakeStackDeployer(); var autoScalingClient = FakeAwsClients.CreateAutoScalingClientForGroups(new[] { new AutoScalingGroup() { AutoScalingGroupName = "group-1", DesiredCapacity = 40 }, new AutoScalingGroup() { AutoScalingGroupName = "group-2", DesiredCapacity = 10 } }); var source = new AutoScalingGroupSource(autoScalingClient); var creator = new CloudFormationAlarmCreator(stack, new ConsoleAlarmLogger(true)); var config1 = new AlertingGroup() { Name = "group-1", AlarmNameSuffix = "suffix-1", Targets = new List <AlertTarget>() { new AlertEmail("*****@*****.**") }, Services = new AlertingGroupServices() { AutoScaling = new AwsServiceAlarms <AutoScalingResourceConfig>() { Resources = new List <ResourceThresholds <AutoScalingResourceConfig> >() { new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-1", Options = new AutoScalingResourceConfig() { // this will trigger the failure InstanceCountIncreaseDelayMinutes = 5 } } } } } }; var config2 = new AlertingGroup() { Name = "group-2", AlarmNameSuffix = "suffix-2", Targets = new List <AlertTarget>() { new AlertEmail("*****@*****.**") }, Services = new AlertingGroupServices() { AutoScaling = new AwsServiceAlarms <AutoScalingResourceConfig>() { Resources = new List <ResourceThresholds <AutoScalingResourceConfig> >() { new ResourceThresholds <AutoScalingResourceConfig>() { Name = "group-2" } } } } }; var cloudWatch = new Mock <IAmazonCloudWatch>(); cloudWatch .Setup(c => c.GetMetricStatisticsAsync(It.IsAny <GetMetricStatisticsRequest>(), It.IsAny <CancellationToken>())) .ThrowsAsync(new Exception("something bad")); var provider = new AutoScalingGroupAlarmDataProvider(cloudWatch.Object, new CurrentTimeProvider()); var sut = IoCHelper.CreateSystemUnderTest( source, provider, provider, WatchmanServiceConfigurationMapper.MapAutoScaling, creator, ConfigHelper.ConfigLoaderFor(config1, config2) ); Exception caught = null; // act try { await sut.LoadAndGenerateAlarms(RunMode.GenerateAlarms); } catch (Exception ex) { caught = ex; } // assert Assert.That(stack.StackWasDeployed("Watchman-group-1"), Is.EqualTo(false)); Assert.That(stack.StackWasDeployed("Watchman-group-2"), Is.EqualTo(true)); Assert.That(caught, Is.Not.Null); }