-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for S3 and placeholders for CF and CW
- Loading branch information
Showing
14 changed files
with
382 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ws/src/test/java/io/openraven/magpie/plugins/aws/discovery/services/BaseAWSServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.openraven.magpie.plugins.aws.discovery.services; | ||
|
||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import io.openraven.magpie.plugins.aws.discovery.AWSUtils; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.localstack.LocalStackContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient; | ||
import software.amazon.awssdk.services.cloudformation.model.*; | ||
|
||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.*; | ||
|
||
public abstract class BaseAWSServiceIT { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BaseAWSServiceIT.class); | ||
private static final int EXPOSED_EDGE_PORT = 4566; | ||
protected static final String EMPTY_STACK_TEMPLATE_PATH = "/template/empty-stack.yml"; | ||
private static final String STACK_NAME = "integration-stack-" + System.nanoTime(); | ||
|
||
protected static final Region BASE_REGION = Region.US_WEST_1; | ||
|
||
private static CloudFormationClient cfClient; | ||
|
||
protected static final ObjectMapper MAPPER = new ObjectMapper() | ||
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) | ||
.findAndRegisterModules(); | ||
|
||
protected static LocalStackContainer localStackContainer = | ||
new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) | ||
.withExposedPorts(EXPOSED_EDGE_PORT) | ||
.withEnv("DEFAULT_REGION", BASE_REGION.id()) | ||
.withEnv("DEBUG", "1") | ||
.withServices(DYNAMODB, CLOUDWATCH, S3, CLOUDFORMATION, IAM); // At the moment cannot launch services dynamically | ||
|
||
static { | ||
localStackContainer.start(); | ||
setupEnvironment(); | ||
initiateCloudFormationClient(); | ||
startStackWithResources(EMPTY_STACK_TEMPLATE_PATH); | ||
} | ||
|
||
protected static void setupEnvironment() { | ||
System.getProperties().setProperty("aws.accessKeyId", "foo"); | ||
System.getProperties().setProperty("aws.secretAccessKey", "bar"); | ||
System.getProperties().setProperty("MAGPIE_AWS_ENDPOINT", | ||
String.format("http://%s:%d", | ||
localStackContainer.getHost(), | ||
localStackContainer.getMappedPort(EXPOSED_EDGE_PORT))); | ||
} | ||
|
||
protected static void initiateCloudFormationClient() { | ||
cfClient = AWSUtils.configure(CloudFormationClient.builder(), BASE_REGION); | ||
} | ||
|
||
protected static void startStackWithResources(String templatePath) { | ||
CreateStackRequest createStackRequest = CreateStackRequest.builder() | ||
.stackName(STACK_NAME) | ||
.templateBody(getCFTemplate(templatePath)) | ||
.build(); | ||
|
||
CreateStackResponse createdStack = cfClient.createStack(createStackRequest); | ||
assertNotNull(createdStack.stackId()); | ||
LOGGER.info("Stack has been created with definition: {}", createdStack); // For transparency purpose | ||
} | ||
|
||
protected static void updateStackWithResources(String templatePath) { | ||
UpdateStackRequest updateStackRequest = UpdateStackRequest.builder() | ||
.stackName(STACK_NAME) | ||
.templateBody(getCFTemplate(templatePath)) | ||
.build(); | ||
|
||
UpdateStackResponse updateStackResponse = cfClient.updateStack(updateStackRequest); | ||
assertNotNull(updateStackResponse.stackId()); | ||
LOGGER.info("Stack has been updated with template: {}", templatePath); | ||
} | ||
|
||
private static String getCFTemplate(String resourcePath) { | ||
return new Scanner(Objects.requireNonNull(BaseAWSServiceIT.class.getResourceAsStream(resourcePath)), "UTF-8") | ||
.useDelimiter("\\A").next(); | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
...src/test/java/io/openraven/magpie/plugins/aws/discovery/services/BaseIntegrationTest.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
...src/test/java/io/openraven/magpie/plugins/aws/discovery/services/DynamoDbDiscoveryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.openraven.magpie.plugins.aws.discovery.services; | ||
|
||
import io.openraven.magpie.api.Emitter; | ||
import io.openraven.magpie.api.MagpieEnvelope; | ||
import io.openraven.magpie.api.Session; | ||
import io.openraven.magpie.plugins.aws.discovery.AWSUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
import software.amazon.awssdk.services.dynamodb.model.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DynamoDbDiscoveryIT extends BaseAWSServiceIT { | ||
|
||
private static final String TEST_TABLE = "entities"; | ||
private static final String CF_DYNAMODB_TEMPLATE_PATH = "/template/dynamo-db-template.yml"; | ||
|
||
private final DynamoDbDiscovery dynamoDbDiscovery = new DynamoDbDiscovery(); | ||
|
||
@Mock | ||
private Emitter emitter; | ||
|
||
@Captor | ||
private ArgumentCaptor<MagpieEnvelope> envelopeCapture; | ||
|
||
@Test | ||
void discoverDynamoDB() { | ||
// prepare | ||
DynamoDbClient dynamoDbClient = AWSUtils.configure(DynamoDbClient.builder(), BASE_REGION); | ||
updateStackWithResources(CF_DYNAMODB_TEMPLATE_PATH); | ||
|
||
// Execute | ||
dynamoDbDiscovery.discoverTables( | ||
MAPPER, | ||
new Session(), | ||
BASE_REGION, | ||
emitter, | ||
dynamoDbClient, | ||
"account"); | ||
|
||
// Verify | ||
Mockito.verify(emitter).emit(envelopeCapture.capture()); | ||
var contents = envelopeCapture.getValue().getContents(); | ||
|
||
assertNotNull(contents.get("documentId")); | ||
assertEquals(String.format("arn:aws:dynamodb:%s:000000000000:table/%s", BASE_REGION, TEST_TABLE), | ||
contents.get("arn").asText()); | ||
assertEquals(TEST_TABLE, contents.get("resourceName").asText()); | ||
assertEquals("AWS::DynamoDB::Table", contents.get("resourceType").asText()); | ||
assertEquals(BASE_REGION.toString(), contents.get("awsRegion").asText()); | ||
} | ||
|
||
// TODO: Global Table coverage required | ||
} |
90 changes: 0 additions & 90 deletions
90
...c/test/java/io/openraven/magpie/plugins/aws/discovery/services/DynamoDbDiscoveryTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.