Skip to content

Commit 7b81e22

Browse files
committed
improve documentation
1 parent 841269e commit 7b81e22

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

powertools-e2e-tests/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
## End-to-end tests
22
This module is internal and meant to be used for end-to-end (E2E) testing of Lambda Powertools for Java.
33

4-
__Prerequisites__: an AWS account is needed as well as a local environment able to reach this account
4+
__Prerequisites__:
5+
- An AWS account is needed as well as a local environment able to reach this account
56
([credentials](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html)).
7+
- [Java 11+](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html)
8+
- [Docker](https://docs.docker.com/engine/install/)
69

7-
To execute the E2E tests, use the following command: `mvn clean verify -Pe2e`
10+
To execute the E2E tests, use the following command: `export JAVA_VERSION=11 && mvn clean verify -Pe2e`
811

912
### Under the hood
1013
This module leverages the following components:

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/testutils/Infrastructure.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@
3333

3434
import java.io.File;
3535
import java.io.IOException;
36-
import java.io.InputStream;
3736
import java.nio.file.Path;
3837
import java.util.*;
3938
import java.util.stream.Collectors;
4039

4140
import static java.util.Collections.singletonList;
4241

42+
/**
43+
* This class is in charge of bootstrapping the infrastructure for the tests.
44+
* <br/>
45+
* Tests are actually run on AWS, so we need to provision Lambda functions, DynamoDB table (for Idempotency),
46+
* CloudWatch log groups, ...
47+
* <br/>
48+
* It uses the Cloud Development Kit (CDK) to define required resources. The CDK stack is then synthesized to retrieve
49+
* the CloudFormation templates and the assets (function jars). Assets are uploaded to S3 (with the SDK `PutObjectRequest`)
50+
* and the CloudFormation stack is created (with the SDK `createStack`)
51+
*/
4352
public class Infrastructure {
4453
private static final Logger LOG = LoggerFactory.getLogger(Infrastructure.class);
4554

@@ -92,6 +101,10 @@ private Infrastructure(Builder builder) {
92101
.build();
93102
}
94103

104+
/**
105+
* Use the CloudFormation SDK to create the stack
106+
* @return the name of the function deployed part of the stack
107+
*/
95108
public String deploy() {
96109
uploadAssets();
97110
LOG.info("Deploying '" + stackName + "' on account " + account);
@@ -111,6 +124,9 @@ public String deploy() {
111124
return functionName;
112125
}
113126

127+
/**
128+
* Destroy the CloudFormation stack
129+
*/
114130
public void destroy() {
115131
LOG.info("Deleting '" + stackName + "' on account " + account);
116132
cfn.deleteStack(DeleteStackRequest.builder().stackName(stackName).build());
@@ -193,6 +209,10 @@ public Builder timeoutInSeconds(long timeoutInSeconds) {
193209
}
194210
}
195211

212+
/**
213+
* Build the CDK Stack containing the required resources (Lambda function, LogGroup, DDB Table)
214+
* @return the CDK stack
215+
*/
196216
private Stack createStackWithLambda() {
197217
Stack stack = new Stack(app, stackName);
198218
List<String> packagingInstruction = Arrays.asList(
@@ -261,12 +281,18 @@ private Stack createStackWithLambda() {
261281
return stack;
262282
}
263283

284+
/**
285+
* cdk synth to retrieve the CloudFormation template and assets directory
286+
*/
264287
private void synthesize() {
265288
CloudAssembly synth = app.synth();
266289
cfnTemplate = synth.getStackByName(stack.getStackName()).getTemplate();
267290
cfnAssetDirectory = synth.getDirectory();
268291
}
269292

293+
/**
294+
* Upload assets (mainly lambda function jars) to S3
295+
*/
270296
private void uploadAssets() {
271297
Map<String, Asset> assets = findAssets();
272298
assets.forEach((objectKey, asset) -> {
@@ -283,6 +309,10 @@ private void uploadAssets() {
283309
});
284310
}
285311

312+
/**
313+
* Reading the cdk assets.json file to retrieve the list of assets to push to S3
314+
* @return a map of assets
315+
*/
286316
private Map<String, Asset> findAssets() {
287317
Map<String, Asset> assets = new HashMap<>();
288318
try {

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/testutils/logging/InvocationLogs.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import java.util.Base64;
66
import java.util.stream.IntStream;
77

8+
/**
9+
* Logs for a specific Lambda invocation
10+
*/
811
public class InvocationLogs {
9-
private String[] logs;
10-
private String[] functionLogs;
12+
private final String[] logs;
13+
private final String[] functionLogs;
1114

1215
public InvocationLogs(String base64Logs, String requestId) {
1316
String rawLogs = new String(Base64.getDecoder().decode(base64Logs), StandardCharsets.UTF_8);

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/testutils/metrics/MetricsFetcher.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import java.util.concurrent.Callable;
2121

2222
import static java.time.Duration.ofSeconds;
23+
24+
/**
25+
* Class in charge of retrieving the actual metrics of a Lambda execution on CloudWatch
26+
*/
2327
public class MetricsFetcher {
2428
private static final Logger LOG = LoggerFactory.getLogger(MetricsFetcher.class);
2529

@@ -30,6 +34,17 @@ public class MetricsFetcher {
3034
.region(region)
3135
.build();
3236

37+
/**
38+
* Retrieve the metric values from start to end. Different parameters are required (see {@link CloudWatchClient#getMetricData} for more info).
39+
* Use a retry mechanism as metrics may not be available instantaneously after a function runs.
40+
* @param start
41+
* @param end
42+
* @param period
43+
* @param namespace
44+
* @param metricName
45+
* @param dimensions
46+
* @return
47+
*/
3348
public List<Double> fetchMetrics(Instant start, Instant end, int period, String namespace, String metricName, Map<String, String> dimensions) {
3449
List<Dimension> dimensionsList = new ArrayList<>();
3550
if (dimensions != null)

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/testutils/tracing/TraceFetcher.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
import static java.time.Duration.ofSeconds;
2828

29+
/**
30+
* Class in charge of retrieving the actual traces of a Lambda execution on X-Ray
31+
*/
2932
public class TraceFetcher {
3033

3134
private static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -36,6 +39,12 @@ public class TraceFetcher {
3639
private final String filterExpression;
3740
private final List<String> excludedSegments;
3841

42+
/**
43+
* @param start beginning of the time slot to search in
44+
* @param end end of the time slot to search in
45+
* @param filterExpression eventual filter for the search
46+
* @param excludedSegments list of segment to exclude from the search
47+
*/
3948
public TraceFetcher(Instant start, Instant end, String filterExpression, List<String> excludedSegments) {
4049
this.start = start;
4150
this.end = end;
@@ -47,6 +56,12 @@ public static Builder builder() {
4756
return new Builder();
4857
}
4958

59+
/**
60+
* Retrieve the traces corresponding to a specific function during a specific time slot.
61+
* Use a retry mechanism as traces may not be available instantaneously after a function runs.
62+
*
63+
* @return traces
64+
*/
5065
public Trace fetchTrace() {
5166
Callable<Trace> callable = () -> {
5267
List<String> traceIds = getTraceIds();
@@ -67,6 +82,11 @@ public Trace fetchTrace() {
6782
return status.getResult();
6883
}
6984

85+
/**
86+
* Retrieve traces from trace ids.
87+
* @param traceIds
88+
* @return
89+
*/
7090
private Trace getTrace(List<String> traceIds) {
7191
BatchGetTracesResponse tracesResponse = xray.batchGetTraces(BatchGetTracesRequest.builder()
7292
.traceIds(traceIds)
@@ -110,6 +130,10 @@ private void getNestedSubSegments(List<SubSegment> subsegments, Trace traceRes,
110130
});
111131
}
112132

133+
/**
134+
* Use the X-Ray SDK to retrieve the trace ids corresponding to a specific function during a specific time slot
135+
* @return a list of trace ids
136+
*/
113137
private List<String> getTraceIds() {
114138
GetTraceSummariesResponse traceSummaries = xray.getTraceSummaries(GetTraceSummariesRequest.builder()
115139
.startTime(start)

0 commit comments

Comments
 (0)