@Inject public CreateCloudFrontSecurityGroupUpdaterLambdaOperation(final CloudFormationService cloudFormationService, final EnvironmentMetadata environmentMetadata, @Named(CF_OBJECT_MAPPER) final ObjectMapper cloudformationObjectMapper, AWSLambda awsLambda, AmazonS3 amazonS3) { this.cloudFormationService = cloudFormationService; this.cloudformationObjectMapper = cloudformationObjectMapper; this.environmentMetadata = environmentMetadata; this.awsLambda = awsLambda; this.amazonS3 = amazonS3; final Region region = Region.getRegion(Regions.US_EAST_1); AmazonCloudFormation amazonCloudFormation = new AmazonCloudFormationClient(); amazonCloudFormation.setRegion(region); amazonSNS = new AmazonSNSClient(); amazonSNS.setRegion(region); }
private void deleteFailedCreate(final String stackName, AmazonCloudFormation cf, int statusPollingIntervalMs) { { // delete an application in ROLLBACK_COMPLETE status ListStacksResult r = cf.listStacks(); r.getStackSummaries() // .stream() // .filter(x -> x.getStackName().equals(stackName)) // .limit(1) // .filter(x -> StackStatus.ROLLBACK_COMPLETE.toString().equals(x.getStackStatus())) // .forEach(x -> { log.info("Deleting stack with status " + x.getStackStatus()); // cf.deleteStack(new DeleteStackRequest().withStackName(stackName)); waitForCompletion(cf, stackName, statusPollingIntervalMs, log); }); } }
private void displayStatusHistory(final String stackName, AmazonCloudFormation cf) { { // list history of application log.info("------------------------------"); log.info("Stack history - " + stackName); log.info("------------------------------"); ListStacksResult r = cf.listStacks(); r.getStackSummaries() // .stream() // .filter(x -> x.getStackName().equals(stackName)) // .forEach(x -> { log.info("id=" + x.getStackId()); log.info(" status=" + x.getStackStatus()); log.info(" created=" + x.getCreationTime()); log.info(" update=" + x.getLastUpdatedTime()); log.info(" deleted=" + x.getDeletionTime()); }); log.info(""); } }
private void displayEvents(final String stackName, AmazonCloudFormation cf, long sinceTime) { // list history of application log.info("------------------------------"); log.info("Event history - " + stackName); log.info("------------------------------"); DescribeStackEventsResult r = cf.describeStackEvents(new DescribeStackEventsRequest().withStackName(stackName)); r.getStackEvents() // .stream() // .sorted((a, b) -> a.getTimestamp().compareTo(b.getTimestamp())) // .filter(x -> x.getTimestamp().getTime() >= sinceTime - TimeUnit.MINUTES.toMillis(1)) // .forEach(x -> { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddd HH:mm:ss"); log.info(sdf.format(x.getTimestamp()) + " " + x.getResourceStatus() + " " + x.getResourceType()); if (x.getResourceStatusReason() != null) { log.info(" reason=" + x.getResourceStatusReason()); if (x.getResourceProperties() != null) { log.info(" properties=\n"); log.info(Util.formatJson(x.getResourceProperties())); } } }); }
public CloudFormationOutput getStackOutputs(AmazonCloudFormation stackbuilder, String stackName) { DescribeStacksRequest wait = new DescribeStacksRequest(); wait.setStackName(stackName); List<Stack> stacks = getCloudFormationClient().describeStacks(wait).getStacks(); CloudFormationOutput cloudFormationOutput = new CloudFormationOutput(); for (Stack stack : stacks) { if (stack.getStackName().equals(stackName)) { stack.getOutputs().forEach(output -> { if (output.getOutputKey().equals(LAMBDA_EXECUTION_IAM_RESOURCE_NAME)) { cloudFormationOutput.setLambdaExecutionRole(output.getOutputValue()); } if (output.getOutputKey().equals(LAMBDA_EXECUTION_NAME)) { cloudFormationOutput.setLambdaFunctionArn(output.getOutputValue()); } }); return cloudFormationOutput; } } throw new RuntimeException("Unknown Cloudformation error. Try deploying."); }
public CfSignalResourceBundle() { cloudFormationSupplier = (cfSignalResourceConfig) -> { AmazonCloudFormation amazonCloudFormation = internalCloudFormation.get(); if (amazonCloudFormation != null) { return amazonCloudFormation; } return internalCloudFormation.updateAndGet((unused) -> { AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(); String awsRegion = cfSignalResourceConfig.getAwsRegion(); Region region; if (Strings.isNullOrEmpty(awsRegion)) { region = Regions.getCurrentRegion(); } else { region = Region.getRegion(Regions.fromName(awsRegion)); } amazonCloudFormationClient.setRegion(region); return amazonCloudFormationClient; }); }; }
@Test public void getObject_stackWithTagsDefined_createTagsMap() throws Exception { //Arrange AmazonCloudFormation cloudFormation = mock(AmazonCloudFormation.class); StackNameProvider stackNameProvider = mock(StackNameProvider.class); when(stackNameProvider.getStackName()).thenReturn("testStack"); when(cloudFormation.describeStacks(new DescribeStacksRequest().withStackName("testStack"))). thenReturn(new DescribeStacksResult().withStacks(new Stack().withTags( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2") ))); StackResourceUserTagsFactoryBean factoryBean = new StackResourceUserTagsFactoryBean(cloudFormation, stackNameProvider); //Act factoryBean.afterPropertiesSet(); Map<String, String> factoryBeanObject = factoryBean.getObject(); //Assert assertEquals("value1", factoryBeanObject.get("key1")); assertEquals("value2", factoryBeanObject.get("key2")); }
@Test public void parseInternal_stackConfigurationWithExternallyConfiguredCloudFormationClient_returnsConfiguredStackWithExternallyConfiguredClient() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-withCustomCloudFormationClient.xml", getClass())); AmazonCloudFormation amazonCloudFormationMock = beanFactory.getBean(AmazonCloudFormation.class); when(amazonCloudFormationMock.listStackResources(new ListStackResourcesRequest().withStackName("test"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); when(amazonCloudFormationMock.describeStacks(new DescribeStacksRequest().withStackName("test"))). thenReturn(new DescribeStacksResult().withStacks(new Stack())); //Act StackResourceRegistry stackResourceRegistry = beanFactory.getBean(StackResourceRegistry.class); //Assert assertNotNull(stackResourceRegistry); assertFalse(beanFactory.containsBeanDefinition(getBeanName(AmazonCloudFormationClient.class.getName()))); verify(amazonCloudFormationMock, times(1)).listStackResources(new ListStackResourcesRequest().withStackName("test")); beanFactory.getBean("customStackTags"); verify(amazonCloudFormationMock, times(1)).describeStacks(new DescribeStacksRequest().withStackName("test")); }
@Test public void resourceIdResolver_stackConfiguration_resourceIdResolverBeanExposed() { // Arrange GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("IntegrationTestStack"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-staticStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); // Act ResourceIdResolver resourceIdResolver = applicationContext.getBean(ResourceIdResolver.class); // Assert assertThat(resourceIdResolver, is(not(nullValue()))); }
@Test public void stackResourceRegistry_stackConfigurationWithStaticName_stackResourceRegistryBeanExposedUnderStaticStackName() throws Exception { // Arrange GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("IntegrationTestStack"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-staticStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); // Act StackResourceRegistry staticStackNameProviderBasedStackResourceRegistry = applicationContext.getBean("IntegrationTestStack", StackResourceRegistry.class); // Assert assertThat(staticStackNameProviderBasedStackResourceRegistry, is(not(nullValue()))); }
@Test public void resourceIdResolverResolveToPhysicalResourceId_stackConfigurationWithStaticNameAndLogicalResourceIdOfExistingResourceProvided_returnsPhysicalResourceId() { // Arrange GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("IntegrationTestStack"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries( new StackResourceSummary().withLogicalResourceId("EmptyBucket").withPhysicalResourceId("integrationteststack-emptybucket-foo"))); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-staticStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); ResourceIdResolver resourceIdResolver = applicationContext.getBean(ResourceIdResolver.class); // Act String physicalResourceId = resourceIdResolver.resolveToPhysicalResourceId("EmptyBucket"); // Assert assertThat(physicalResourceId, startsWith("integrationteststack-emptybucket-")); }
@Test public void resourceIdResolverResolveToPhysicalResourceId_logicalResourceIdOfNonExistingResourceProvided_returnsLogicalResourceIdAsPhysicalResourceId() { // Arrange GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("IntegrationTestStack"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-staticStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); ResourceIdResolver resourceIdResolver = applicationContext.getBean(ResourceIdResolver.class); // Act String physicalResourceId = resourceIdResolver.resolveToPhysicalResourceId("nonExistingLogicalResourceId"); // Assert assertThat(physicalResourceId, is("nonExistingLogicalResourceId")); }
@Before public void beforeEachTestRuns() { AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain(); AmazonEC2 ec2Client = EnvironmentSetupForTests.createEC2Client(); AmazonElasticLoadBalancing awsElbClient = EnvironmentSetupForTests.createELBClient(); AmazonCloudFormation cfnClient = EnvironmentSetupForTests.createCFNClient(); AmazonRDS awsRdsClient = EnvironmentSetupForTests.createRDSClient(); CloudClient cloudClient = new CloudClient(ec2Client, new DefaultAwsRegionProviderChain()); LoadBalancerClient elbClient = new LoadBalancerClient(awsElbClient); VpcRepository vpcRepository = new VpcRepository(cloudClient); CloudFormationClient cloudFormationClient = new CloudFormationClient(cfnClient); cloudRepository = new CloudRepository(cloudClient); ResourceRepository cfnRepository = new CfnRepository(cloudFormationClient, cloudRepository, "CfnAssist"); elbRepository = new ELBRepository(elbClient, vpcRepository, cfnRepository); rdsClient = new RDSClient(awsRdsClient); }
public static StackNameAndId createTemporarySimpleStack(AmazonCloudFormation cfnClient, String vpcId, String arn) throws IOException { CreateStackRequest createStackRequest = new CreateStackRequest(); createStackRequest.setStackName(TEMPORARY_STACK); File file = new File(FilesForTesting.SIMPLE_STACK); createStackRequest.setTemplateBody(FileUtils.readFileToString(file , Charset.defaultCharset())); Collection<Parameter> parameters = new LinkedList<>(); parameters.add(createParam("env", EnvironmentSetupForTests.ENV)); parameters.add(createParam("vpc", vpcId)); if (!arn.isEmpty()) { Collection<String> notificationARNs = new LinkedList<>(); notificationARNs.add(arn); logger.debug("Adding arn subscription "+ arn); createStackRequest.setNotificationARNs(notificationARNs); } createStackRequest.setParameters(parameters); CreateStackResult result = cfnClient.createStack(createStackRequest); return new StackNameAndId(TEMPORARY_STACK, result.getStackId()); }
@Bean public AmazonCloudFormation amazonCloudFormationClient(final AWSCredentialsProvider awsCredentialsProvider, final ClientConfiguration awsClientConfig, final Region awsRegion) { return AmazonCloudFormationClientBuilder.standard() .withCredentials(awsCredentialsProvider) .withClientConfiguration(awsClientConfig) .withRegion(awsRegion.getName()) .build(); }
/** * Binds all the Amazon services used. */ @Override protected void configure() { final Region region = Region.getRegion(Regions.fromName(regionName)); bind(AmazonEC2.class).toInstance(createAmazonClientInstance(AmazonEC2Client.class, region)); bind(AmazonCloudFormation.class).toInstance(createAmazonClientInstance(AmazonCloudFormationClient.class, region)); bind(AmazonIdentityManagement.class).toInstance(createAmazonClientInstance(AmazonIdentityManagementClient.class, region)); bind(AWSKMS.class).toInstance(createAmazonClientInstance(AWSKMSClient.class, region)); bind(AmazonS3.class).toInstance(createAmazonClientInstance(AmazonS3Client.class, region)); bind(AmazonAutoScaling.class).toInstance(createAmazonClientInstance(AmazonAutoScalingClient.class, region)); bind(AWSSecurityTokenService.class).toInstance(createAmazonClientInstance(AWSSecurityTokenServiceClient.class, region)); bind(AWSLambda.class).toInstance(createAmazonClientInstance(AWSLambdaClient.class, region)); bind(AmazonSNS.class).toInstance(createAmazonClientInstance(AmazonSNSClient.class, region)); }
@Test public void testSkip() { testConfig.cfSignalResourceConfig.setSkip(true); // purposefully null this to test for NPE testConfig.cfSignalResourceConfig.setStackName(null); AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); new CfSignalResourceBundle(amazonCloudFormation).run(testConfig, environment); verifyZeroInteractions(amazonCloudFormation, lifecycleEnvironment); verify(testConfig).getCfSignalResourceConfig(); }
@Test public void testRunNoAWS() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); testConfig.cfSignalResourceConfig.setEc2InstanceId(""); new CfSignalResourceBundle(amazonCloudFormation).run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verifyZeroInteractions(lifecycleEnvironment, amazonCloudFormation); }
@Test public void testRunAWS() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); new CfSignalResourceBundle(amazonCloudFormation).run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verify(lifecycleEnvironment).addLifeCycleListener(any(CfSignalResourceBundle.CfSignalResourceLifcycleListener.class)); }
@Test public void lifecycleListenerDoesNotSignalOutsideCloudFormationUpdate() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); final String uniqueId = "i-123"; testConfig.cfSignalResourceConfig.setEc2InstanceId(uniqueId); CfSignalResourceBundle cfSignalResourceBundle = new CfSignalResourceBundle(amazonCloudFormation); cfSignalResourceBundle.run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verify(lifecycleEnvironment).addLifeCycleListener(listenerArgumentCaptor.capture()); when(amazonCloudFormation.describeStackResource(any())).thenReturn( new DescribeStackResourceResult().withStackResourceDetail( new StackResourceDetail().withResourceStatus(ResourceStatus.UPDATE_COMPLETE))); listenerArgumentCaptor.getValue().lifeCycleStarted(mock(LifeCycle.class)); verify(amazonCloudFormation) .describeStackResource( argThat( allOf( hasProperty("stackName", equalTo(testConfig.getCfSignalResourceConfig().getStackName())), hasProperty("logicalResourceId", equalTo(testConfig.getCfSignalResourceConfig().getAsgResourceName()))))); verify(amazonCloudFormation, never()).signalResource(any()); assertThat(cfSignalResourceBundle.getInternalCloudFormation(), nullValue()); }
@Bean public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) { if (StringUtils.hasText(this.annotationAttributes.getString("stackName"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.annotationAttributes.getString("stackName"))); } else { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEc2)); } }
@Test public void stackResourceRegistry_stackConfigurationWithoutStaticName_stackResourceRegistryBeanExposedUnderGeneratedName() throws Exception { // Arrange HttpServer server = MetaDataServer.setupHttpServer(); HttpContext httpContext = server.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("foo")); GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId("foo"))). thenReturn(new DescribeStackResourcesResult().withStackResources(new StackResource().withStackName("test"))); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("test"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-autoDetectStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); // Act StackResourceRegistry autoDetectingStackNameProviderBasedStackResourceRegistry = applicationContext.getBean("org.springframework.cloud.aws.core.env.stack.config.StackResourceRegistryFactoryBean#0", StackResourceRegistry.class); // Assert assertThat(autoDetectingStackNameProviderBasedStackResourceRegistry, is(not(nullValue()))); server.removeContext(httpContext); }
@Test public void resourceIdResolverResolveToPhysicalResourceId_stackConfigurationWithoutStaticNameAndLogicalResourceIdOfExistingResourceProvided_returnsPhysicalResourceId() throws Exception { // Arrange HttpServer server = MetaDataServer.setupHttpServer(); HttpContext httpContext = server.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("foo")); GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class); when(amazonCloudFormation.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId("foo"))). thenReturn(new DescribeStackResourcesResult().withStackResources(new StackResource().withStackName("test"))); when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("test"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries( new StackResourceSummary().withLogicalResourceId("EmptyBucket").withPhysicalResourceId("integrationteststack-emptybucket-foo"))); applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-autoDetectStackName.xml", getClass())); applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation); applicationContext.refresh(); ResourceIdResolver resourceIdResolver = applicationContext.getBean(ResourceIdResolver.class); // Act String physicalResourceId = resourceIdResolver.resolveToPhysicalResourceId("EmptyBucket"); // Assert assertThat(physicalResourceId, startsWith("integrationteststack-emptybucket-")); server.removeContext(httpContext); }
@Test public void testAllTestCfnFilesAreValid() throws IOException, InterruptedException { AmazonCloudFormation cfnClient = EnvironmentSetupForTests.createCFNClient(); CloudFormationClient cloudClient = new CloudFormationClient(cfnClient); File folder = new File("src/cfnScripts"); assertTrue(folder.exists()); validateFolder(cloudClient, folder); }
StackReadyWaitCondition(AmazonCloudFormation cloudFormation, String stackName) { super(cloudFormation, stackName); }
public ChangeSetCreateCompleteWaitCondition(AmazonCloudFormation cloudFormation, String changeSetArn) { this.cloudFormation = cloudFormation; this.changeSetArn = changeSetArn; }
StackDeletedWaitCondition(AmazonCloudFormation cloudFormation, String stackName) { super(cloudFormation, stackName); }
CloudFormationPollingService(AmazonCloudFormation cloudFormation, StatusPollingService pollingService) { this.cloudFormation = cloudFormation; this.pollingService = pollingService; }
public CloudFormationPollingService(AmazonCloudFormation cloudFormation) { this(cloudFormation, new StatusPollingService()); }
StackStatusWaitCondition(AmazonCloudFormation cloudFormation, String stackName) { this.cloudFormation = cloudFormation; this.stackName = stackName; }
public DeployService(SamConfig config, AmazonCloudFormation cloudFormation) { this(config, new CloudformationService(cloudFormation), new CloudFormationPollingService(cloudFormation), new TemplateService()); }
public CloudformationService(AmazonCloudFormation cloudFormation) { this.cloudFormation = cloudFormation; }
@Inject public CloudFormationService(final AmazonCloudFormation cloudFormationClient, final EnvironmentMetadata environmentMetadata) { this.cloudFormationClient = cloudFormationClient; this.environmentMetadata = environmentMetadata; }
@Inject public CfSignalResourceBundle(AmazonCloudFormation amazonCloudFormation) { checkNotNull(amazonCloudFormation); cloudFormationSupplier = (config) -> amazonCloudFormation; }
@VisibleForTesting /*package-private*/ AmazonCloudFormation getInternalCloudFormation() { return internalCloudFormation.get(); }
@VisibleForTesting /*package-private*/ AmazonCloudFormation getCloudFormation(final CfSignalResourceConfig config) { return cloudFormationSupplier.apply(config); }
@Test public void lifecycleListenerSignalsSuccess() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); final String uniqueId = "i-123"; testConfig.cfSignalResourceConfig.setEc2InstanceId(uniqueId); CfSignalResourceBundle cfSignalResourceBundle = new CfSignalResourceBundle(amazonCloudFormation); cfSignalResourceBundle.run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verify(lifecycleEnvironment).addLifeCycleListener(listenerArgumentCaptor.capture()); when(amazonCloudFormation.describeStackResource(any())).thenReturn( new DescribeStackResourceResult().withStackResourceDetail( new StackResourceDetail().withResourceStatus(ResourceStatus.UPDATE_IN_PROGRESS))); LifeCycle event = mock(LifeCycle.class); listenerArgumentCaptor.getValue().lifeCycleStarted(event); verify(amazonCloudFormation) .describeStackResource( argThat( allOf( hasProperty("stackName", equalTo(testConfig.getCfSignalResourceConfig().getStackName())), hasProperty("logicalResourceId", equalTo(testConfig.getCfSignalResourceConfig().getAsgResourceName()))))); verify(amazonCloudFormation) .signalResource( argThat( allOf( hasProperty("status", equalTo(ResourceSignalStatus.SUCCESS.toString())), hasProperty("stackName", equalTo(testConfig.getCfSignalResourceConfig().getStackName())), hasProperty("logicalResourceId", equalTo(testConfig.getCfSignalResourceConfig().getAsgResourceName())), hasProperty("uniqueId", equalTo(uniqueId))))); assertThat(cfSignalResourceBundle.getInternalCloudFormation(), nullValue()); }
@Test public void lifecycleListenerSignalsFailureOnStartup() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); final String uniqueId = "i-123"; testConfig.cfSignalResourceConfig.setEc2InstanceId(uniqueId); CfSignalResourceBundle cfSignalResourceBundle = new CfSignalResourceBundle(amazonCloudFormation); cfSignalResourceBundle.run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verify(lifecycleEnvironment).addLifeCycleListener(listenerArgumentCaptor.capture()); when(amazonCloudFormation.describeStackResource(any())).thenReturn( new DescribeStackResourceResult().withStackResourceDetail( new StackResourceDetail().withResourceStatus(ResourceStatus.CREATE_IN_PROGRESS))); LifeCycle event = mock(LifeCycle.class); when(event.isStopping()).thenReturn(Boolean.FALSE); when(event.isStopped()).thenReturn(Boolean.FALSE); listenerArgumentCaptor.getValue().lifeCycleFailure(event, new Throwable("testing")); verify(amazonCloudFormation) .describeStackResource( argThat( allOf( hasProperty("stackName", equalTo(testConfig.getCfSignalResourceConfig().getStackName())), hasProperty("logicalResourceId", equalTo(testConfig.getCfSignalResourceConfig().getAsgResourceName()))))); verify(amazonCloudFormation) .signalResource( argThat( allOf( hasProperty("status", equalTo(ResourceSignalStatus.FAILURE.toString())), hasProperty("stackName", equalTo(testConfig.getCfSignalResourceConfig().getStackName())), hasProperty("logicalResourceId", equalTo(testConfig.getCfSignalResourceConfig().getAsgResourceName())), hasProperty("uniqueId", equalTo(uniqueId))))); assertThat(cfSignalResourceBundle.getInternalCloudFormation(), nullValue()); }
@Test public void lifecycleListenerDoesNotSignalFailureOnShutdown() throws Exception { AmazonCloudFormation amazonCloudFormation = mock(AmazonCloudFormation.class); final String uniqueId = "i-123"; testConfig.cfSignalResourceConfig.setEc2InstanceId(uniqueId); new CfSignalResourceBundle(amazonCloudFormation).run(testConfig, environment); verify(testConfig).getCfSignalResourceConfig(); verify(lifecycleEnvironment).addLifeCycleListener(listenerArgumentCaptor.capture()); LifeCycle event = mock(LifeCycle.class); when(event.isStopping()).thenReturn(Boolean.TRUE); when(event.isStopped()).thenReturn(Boolean.FALSE); verifyZeroInteractions(amazonCloudFormation); }
protected AmazonCloudFormation amazonCloudFormationClient() { AmazonCloudFormation amazonCloudFormation = new AmazonCloudFormationClient(amazonCredentialsProvider()); amazonCloudFormation.setRegion(getRegion()); return amazonCloudFormation; }