Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DCMAW-9786] Add dev platform assets to enable testing in codebuild #68

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
node_modules

# Jest test report
report.xml
report.xml

# Jest code coverage output
coverage/

# AWS SAM
.aws-sam/
samconfig.toml

cf-output.txt
docker-vars.env

# VSCode
.vscode
24 changes: 24 additions & 0 deletions backend-api/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:iron-alpine

WORKDIR /backend-api

# Create a new user 'test' to avoid running the app as a root
RUN adduser --disabled-password test && chown test .

COPY package.json package-lock.json ./
RUN npm install --ignore-scripts


## Update container, install awscli and check awscli is installed correctly
RUN apk upgrade && apk update; apk add --no-cache bash aws-cli && aws --version

# Copy the CloudFormation infrastructure template and application source code to the working directory
COPY template.yaml /
COPY template.yaml src openApiSpecs infra-tests eslint.config.mjs jest.config.ts tsconfig.json ./

# Give user, 'test', permissions to execute test script and switch the user to 'test'
COPY run-tests.sh /
RUN chmod 005 /run-tests.sh
USER test

ENTRYPOINT ["/run-tests.sh"]
35 changes: 35 additions & 0 deletions backend-api/run-tests-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
set -eu

stack_name=${1:-mob-async-backend}

echo "Running test against ${stack_name}"

rm -rf docker-vars.env

export AWS_DEFAULT_REGION="eu-west-2"
TEST_REPORT_DIR="results"
ENVIRONMENT="dev"

aws cloudformation describe-stacks \
--stack-name "$stack_name" \
--query 'Stacks[0].Outputs[].{key: OutputKey, value: OutputValue}' \
--output text >cf-output.txt

eval $(awk '{ printf("export CFN_%s=\"%s\"\n", $1, $2) }' cf-output.txt)
awk '{ printf("CFN_%s=\"%s\"\n", $1, $2) }' cf-output.txt >>docker-vars.env

{
echo TEST_REPORT_DIR="$TEST_REPORT_DIR"
echo TEST_REPORT_ABSOLUTE_DIR="/results"
echo TEST_ENVIRONMENT="$ENVIRONMENT"
echo SAM_STACK_NAME="$stack_name"
} >>docker-vars.env

docker build --file Dockerfile.test --tag testcontainer .

docker run --rm --interactive --tty \
--user root \
--env-file docker-vars.env \
--volume "$(pwd):/results" \
testcontainer
29 changes: 29 additions & 0 deletions backend-api/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
set -eu

cd /backend-api

mkdir -pv results

if npm run test:unit --ci --silent; then
cp -rf results $TEST_REPORT_ABSOLUTE_DIR
else
cp -rf results $TEST_REPORT_ABSOLUTE_DIR
exit 1
fi

# if [[ "$ENVIRONMENT" == "dev" ]] || [[ "$ENVIRONMENT" == "build" ]]; then
# if npm run test:e2e --ci --silent; then
# cp -rf results $TEST_REPORT_ABSOLUTE_DIR
# else
# cp -rf results $TEST_REPORT_ABSOLUTE_DIR
# exit 1
# fi
# elif [[ "$ENVIRONMENT" == "staging" ]]; then
# if npm run test:stage --ci --silent; then
# cp -rf results $TEST_REPORT_ABSOLUTE_DIR
# else
# cp -rf results $TEST_REPORT_ABSOLUTE_DIR
# exit 1
# fi
# fi
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sonar.projectKey=mobile-id-check-async
sonar.organization=govuk-one-login

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=backend-api/src
sonar.sources=backend-api/src,backend-api/Dockerfile.test
sonar.javascript.lcov.reportPaths=backend-api/coverage/lcov.info
sonar.language=ts
sonar.exclusions=**/*.test.ts,**/*.test.js,**/testUtils/**,**/tests/mocks.ts
Expand Down