EC2DestroyNodeStrategy implements DestroyNodeStrategy { @Override public NodeMetadata destroyNode(String id) { String[] parts = AWSUtils.parseHandle(id); String region = parts[0]; String instanceId = parts[1]; releaseAnyPublicIpForInstanceInRegion(instanceId, region); destroyInstanceInRegion(instanceId, region); return getNode.getNode(id); } @Inject protected  EC2DestroyNodeStrategy(EC2Client client, GetNodeMetadataStrategy getNode,
            @Named("ELASTICIP") LoadingCache<RegionAndName, String> elasticIpCache); @Override NodeMetadata destroyNode(String id);  }
@SuppressWarnings("unchecked") @Test public void testDestroyNodeTerminatesInstanceAndReturnsRefreshedNode() throws Exception { EC2Client client = createMock(EC2Client.class); InstanceClient instanceClient = createMock(InstanceClient.class); GetNodeMetadataStrategy getNode = createMock(GetNodeMetadataStrategy.class); LoadingCache<RegionAndName, String> elasticIpCache = createMock(LoadingCache.class); NodeMetadata node = createMock(NodeMetadata.class); expect(client.getInstanceServices()).andReturn(instanceClient).atLeastOnce(); expect(instanceClient.terminateInstancesInRegion("region", "i-blah")).andReturn(null); expect(getNode.getNode("region/i-blah")).andReturn(node); replay(client); replay(getNode); replay(instanceClient); replay(elasticIpCache); EC2DestroyNodeStrategy destroyer = new EC2DestroyNodeStrategy(client, getNode, elasticIpCache); assertEquals(destroyer.destroyNode("region/i-blah"), node); verify(client); verify(getNode); verify(instanceClient); verify(elasticIpCache); }
@SuppressWarnings("unchecked") @Test public void testDestroyNodeDisassociatesAndReleasesIpThenTerminatesInstanceAndReturnsRefreshedNode() throws Exception { EC2Client client = createMock(EC2Client.class); GetNodeMetadataStrategy getNode = createMock(GetNodeMetadataStrategy.class); LoadingCache<RegionAndName, String> elasticIpCache = createMock(LoadingCache.class); ElasticIPAddressClient ipClient = createMock(ElasticIPAddressClient.class); InstanceClient instanceClient = createMock(InstanceClient.class); NodeMetadata node = createMock(NodeMetadata.class); expect(elasticIpCache.get(new RegionAndName("region", "i-blah"))).andReturn("1.1.1.1"); expect(client.getElasticIPAddressServices()).andReturn(ipClient).atLeastOnce(); ipClient.disassociateAddressInRegion("region", "1.1.1.1"); ipClient.releaseAddressInRegion("region", "1.1.1.1"); elasticIpCache.invalidate(new RegionAndName("region", "i-blah")); expect(client.getInstanceServices()).andReturn(instanceClient).atLeastOnce(); expect(instanceClient.terminateInstancesInRegion("region", "i-blah")).andReturn(null); expect(getNode.getNode("region/i-blah")).andReturn(node); replay(client); replay(getNode); replay(elasticIpCache); replay(instanceClient); replay(ipClient); EC2DestroyNodeStrategy destroyer = new EC2DestroyNodeStrategy(client, getNode, elasticIpCache); destroyer.autoAllocateElasticIps = true; assertEquals(destroyer.destroyNode("region/i-blah"), node); verify(client); verify(getNode); verify(elasticIpCache); verify(instanceClient); verify(ipClient); }
@SuppressWarnings("unchecked") @Test public void testDestroyNodeSafeOnCacheMissThenTerminatesInstanceAndReturnsRefreshedNode() throws Exception { EC2Client client = createMock(EC2Client.class); GetNodeMetadataStrategy getNode = createMock(GetNodeMetadataStrategy.class); LoadingCache<RegionAndName, String> elasticIpCache = createMock(LoadingCache.class); ElasticIPAddressClient ipClient = createMock(ElasticIPAddressClient.class); InstanceClient instanceClient = createMock(InstanceClient.class); NodeMetadata node = createMock(NodeMetadata.class); expect(elasticIpCache.get(new RegionAndName("region", "i-blah"))).andThrow(new CacheLoader.InvalidCacheLoadException(null)); expect(client.getInstanceServices()).andReturn(instanceClient).atLeastOnce(); expect(instanceClient.terminateInstancesInRegion("region", "i-blah")).andReturn(null); expect(getNode.getNode("region/i-blah")).andReturn(node); replay(client); replay(getNode); replay(elasticIpCache); replay(instanceClient); replay(ipClient); EC2DestroyNodeStrategy destroyer = new EC2DestroyNodeStrategy(client, getNode, elasticIpCache); destroyer.autoAllocateElasticIps = true; assertEquals(destroyer.destroyNode("region/i-blah"), node); verify(client); verify(getNode); verify(elasticIpCache); verify(instanceClient); verify(ipClient); }
@SuppressWarnings("unchecked") @Test public void testDestroyNodeSafeOnCacheExecutionExceptionThenTerminatesInstanceAndReturnsRefreshedNode() throws Exception { EC2Client client = createMock(EC2Client.class); GetNodeMetadataStrategy getNode = createMock(GetNodeMetadataStrategy.class); LoadingCache<RegionAndName, String> elasticIpCache = createMock(LoadingCache.class); ElasticIPAddressClient ipClient = createMock(ElasticIPAddressClient.class); InstanceClient instanceClient = createMock(InstanceClient.class); NodeMetadata node = createMock(NodeMetadata.class); expect(elasticIpCache.get(new RegionAndName("region", "i-blah"))).andThrow(new ExecutionException(null)); expect(client.getInstanceServices()).andReturn(instanceClient).atLeastOnce(); expect(instanceClient.terminateInstancesInRegion("region", "i-blah")).andReturn(null); expect(getNode.getNode("region/i-blah")).andReturn(node); replay(client); replay(getNode); replay(elasticIpCache); replay(instanceClient); replay(ipClient); EC2DestroyNodeStrategy destroyer = new EC2DestroyNodeStrategy(client, getNode, elasticIpCache); destroyer.autoAllocateElasticIps = true; assertEquals(destroyer.destroyNode("region/i-blah"), node); verify(client); verify(getNode); verify(elasticIpCache); verify(instanceClient); verify(ipClient); }