// This sample, non-production-ready project that demonstrates how to detect when an Amazon Elastic Beanstalk // platform's base AMI has been updated and starts an EC2 Image Builder Pipeline to automate the creation of a golden image. // © 2021 Amazon Web Services, Inc. or its affiliates. All Rights Reserved. // This AWS Content is provided subject to the terms of the AWS Customer Agreement available at // http://aws.amazon.com/agreement or other written agreement between Customer and either // Amazon Web Services, Inc. or Amazon Web Services EMEA SARL or both. // SPDX-License-Identifier: MIT-0 namespace BeanstalkImageBuilderPipeline.UnitTests { using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Lambda.Core; using BeanstalkImageBuilderPipeline.Repositories; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; [TestClass] public sealed class AmiMonitorFixture { private AmiMonitor _amiMonitor; private Mock> _mockLogger; private Mock _mockBeanstalkRepo; private Mock _mockSsmRepo; private Mock _mockLambdaContext; [TestInitialize] public void TestSetup() { var services = new ServiceCollection(); _mockLogger = new Mock>(); _mockBeanstalkRepo = new Mock(); _mockSsmRepo = new Mock(); _mockLambdaContext = new Mock(); services.AddScoped(_ => _mockLogger.Object); services.AddScoped(_ => _mockBeanstalkRepo.Object); services.AddScoped(_ => _mockSsmRepo.Object); _amiMonitor = new AmiMonitor(services.BuildServiceProvider()); } [TestMethod] public async Task GivenRequest_WhenLambdaIsInvoked_ThenLoggingScopeContainsAwsRequestId() { await _amiMonitor.Handler(null, _mockLambdaContext.Object); _mockLogger.Verify(logger => logger.BeginScope(It.Is>(d => d["AwsRequestId"] == _mockLambdaContext.Object.AwsRequestId))); } [TestMethod] public async Task GivenUnhandledExceptionOccurs_WhenLambdaIsInvoked_ThenExceptionIsBubbledUp() { _mockBeanstalkRepo.Setup(r => r.GetLatestAmiVersionAsync(null)) .Throws(); await Assert.ThrowsExceptionAsync(() => _amiMonitor.Handler(null, _mockLambdaContext.Object)); } [TestMethod] public async Task GivenPlatformHasHvmAmi_WhenLambdaIsInvoked_ThenSsmParameterIsUpdated() { string expectedAmiId = "ami-"; _mockBeanstalkRepo.Setup(r => r.GetLatestAmiVersionAsync(null)) .ReturnsAsync(expectedAmiId); await _amiMonitor.Handler(null, _mockLambdaContext.Object); _mockSsmRepo.Verify(r => r.UpdateParameterAsync(It.IsAny(), expectedAmiId), Times.Once(), "Parameter should be updated with latest AMI ID."); } [TestMethod] public async Task GivenAmiIsNotFoundForPlatform_WhenLambdaIsInvoked_ThenSsmParameterIsNotUpdated() { _mockBeanstalkRepo.Setup(m => m.GetLatestAmiVersionAsync(It.IsAny())) .ReturnsAsync(string.Empty); await _amiMonitor.Handler(null, _mockLambdaContext.Object); _mockSsmRepo.Verify(r => r.UpdateParameterAsync(It.IsAny(), It.IsAny()), Times.Never(), "Parameter should not be updated unless AMI was successfully retrieved."); } } }