Skip to content

Commit

Permalink
JavaScript (v3): Update Dockerfile to correctly build for, and run, i…
Browse files Browse the repository at this point in the history
…nteg tests. (#5589)

* JavaScript (v3): Update scripts location in dockerfile and log all test output
* JavaScript (v3): Update docker file to correctly run integ tests.
  • Loading branch information
cpyle0819 authored Nov 1, 2023
1 parent 1c12c39 commit 5c372a3
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 76 deletions.
1 change: 0 additions & 1 deletion javascriptv3/.dockerignore

This file was deleted.

25 changes: 16 additions & 9 deletions javascriptv3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# syntax=docker/dockerfile:1
FROM node:18
FROM node:20

# Update image
RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy source code
COPY . /javascriptv3

# Perform build steps
RUN npm install -C /javascriptv3

# Set non-root user
RUN useradd -m automation && \
chown -R automation:automation /javascriptv3/
chown -R automation:automation /home/automation
USER automation:automation

# Add config
RUN mkdir /home/automation/.aws && \
echo "[default]\nregion = us-east-1" > /home/automation/.aws/config

# Copy source code
COPY --chown=automation:automation ./javascriptv3 /home/automation/javascriptv3

# Copy resources
COPY --chown=automation:automation ./resources /home/automation/resources
COPY --chown=automation:automation ./python/example_code/glue/flight_etl_job_script.py /home/automation/python/example_code/glue/flight_etl_job_script.py
COPY --chown=automation:automation ./workflows /home/automation/workflows

# Set default command
CMD ["./scripts/run_tests.sh", "integration"]
# `npm i` needs to be run in the container. Otherwise it causes a dependency issue: https://github.com/evanw/esbuild/issues/1646#issuecomment-1238080595
CMD npm i --prefix /home/automation/javascriptv3 && npm run --prefix /home/automation/javascriptv3 integration-test
2 changes: 2 additions & 0 deletions javascriptv3/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/node_modules
*/package-lock.json
27 changes: 8 additions & 19 deletions javascriptv3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,13 @@ Cross-service examples are located in the [_cross-services folder_](./example_co

You can run tests for a specific service, or for every service in this repository. Choose whether to run unit tests, integration tests, or both.

- To run both unit and integration tests for all services, run the following from this directory:
- To run unit tests, use the following command:

`npm test`

- To run only unit tests, use the "unit" argument:
- To run integration tests, use the following command:

`npm test unit`

- To run only integration tests, use the "integration" argument:

`npm test integration`
`npm run integration-test`

- To run tests for a specific service, follow the instructions in the service's README.

Expand All @@ -59,27 +55,20 @@ If you run tests using the preceding commands, output will be stored in `unit_te

## Docker image (Beta)

This example is available in a container image
hosted on [Amazon Elastic Container Registry (ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html). This image will be pre-loaded
with all JavaScript v3 examples with dependencies pre-resolved, allowing you to explore
these examples in an isolated environment.
This example is available in a container image hosted on [Amazon Elastic Container Registry (ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html). This image will be pre-loaded with all JavaScript v3 examples with dependencies pre-resolved. It is used for running tests.

- [SDK for JavaScript v3 image](https://gallery.ecr.aws/b4v4v1s0/javascriptv3)

### Build the Docker image

1. Install and run Docker on your machine.
2. Navigate to the same directory as this readme.
3. Run `docker build -t <image_name> .` and replace `image_name` with a name for the image.
2. Navigate to the root directory of this repository.
3. Run `docker build -t <image_name> -f javascriptv3/Dockerfile .` and replace `<image_name>` with a name for the image.

### Launch the Docker container

1. Run `docker run -it -v ~/.aws/credentials:/root/.aws/credentials <image_name>`. `-it` launches an
interactive terminal. `-v ~/.aws...` is optional but recommended. It will mount your local credentials
file to the container.
2. The terminal initiates a bash instance at the root of the container. Run `cd javascriptv3` and then you
can run tests from here by following the steps in the [Tests](#tests) section. Run examples by navigating
to a service folder and following the README instructions there.
1. Run `docker run -it -v /Users/corepyle/.aws/credentials:/home/automation/.aws/credentials <image_name>`. `-it` launches an interactive terminal. `-v ~/.aws...` is optional but recommended. It will mount your local credentials file to the container.
2. The Dockerfile is configured to automatically run integration tests when the container is run.

## Contribute

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi } from "vitest";
import { describe, it, expect } from "vitest";

import { main } from "../hello.js";
import { getUniqueName } from "@aws-sdk-examples/libs/utils/util-string.js";
Expand All @@ -10,10 +10,8 @@ describe("hello test", () => {
tableSetupTeardown(tableName, [{ AttributeName: "Id", AttributeType: "N" }]);

it("should list tables", async () => {
const spy = vi.spyOn(console, "log");
const { TableNames } = await main();

await main();

expect(spy).toHaveBeenCalledWith(`${tableName}`);
expect(TableNames.includes(tableName)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
SetQueueAttributesCommand,
} from "@aws-sdk/client-sqs";

import { retry } from "@aws-sdk-examples/libs/utils/util-timers.js";
import { retry, wait } from "@aws-sdk-examples/libs/utils/util-timers.js";

import { putEvents } from "../actions/put-events.js";
import { putRule } from "../actions/put-rule.js";
Expand Down Expand Up @@ -77,9 +77,11 @@ test("target should receive message", async () => {

// Ensure the queue allows the rule to send messages.
await addQueuePolicy(Clients.SQSClient, queueArn, RuleArn, queueUrl);
await wait(15);

// Put the target.
await putTarget(ruleName, queueArn, targetId);
await wait(15);

// Put the event.
await putEvents("eventbridge.integration.test", "greeting", []);
Expand Down Expand Up @@ -162,14 +164,20 @@ async function addQueuePolicy(sqsClient, queueArn, ruleArn, queueUrl) {
* @param {SQSClient} sqsClient
* @param {string} queueUrl
*/
async function getMessagesFromQueue(sqsClient, queueUrl) {
const { Messages } = await sqsClient.send(
new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
WaitTimeSeconds: 20,
}),
);

return Messages;
function getMessagesFromQueue(sqsClient, queueUrl) {
return retry({ intervalInMs: 0, maxRetries: 3 }, async () => {
const { Messages } = await sqsClient.send(
new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
WaitTimeSeconds: 20,
}),
);

if (!Messages || Messages.length === 0) {
throw new Error("No messages received.");
}

return Messages;
});
}
3 changes: 2 additions & 1 deletion javascriptv3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "root",
"private": true,
"scripts": {
"test": "scripts/run_tests.sh",
"test": "npm run test --workspaces --if-present -- --reporter verbose",
"integration-test": "npm run integration-test --workspaces --if-present -- --reporter verbose",
"lint": "lint-staged",
"ci-lint": "eslint"
},
Expand Down
30 changes: 0 additions & 30 deletions javascriptv3/scripts/run_tests.sh

This file was deleted.

0 comments on commit 5c372a3

Please sign in to comment.