diff --git a/.gitignore b/.gitignore index ac879508e..cf611a134 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules # Jest test report -report.xml +report.xml # Jest code coverage output coverage/ @@ -9,3 +9,9 @@ coverage/ # AWS SAM .aws-sam/ samconfig.toml + +cf-output.txt +docker-vars.env + +# VSCode +.vscode \ No newline at end of file diff --git a/backend-api/Dockerfile.test b/backend-api/Dockerfile.test new file mode 100644 index 000000000..edeea3163 --- /dev/null +++ b/backend-api/Dockerfile.test @@ -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"] diff --git a/backend-api/run-tests-locally.sh b/backend-api/run-tests-locally.sh new file mode 100644 index 000000000..d9f70b307 --- /dev/null +++ b/backend-api/run-tests-locally.sh @@ -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 diff --git a/backend-api/run-tests.sh b/backend-api/run-tests.sh new file mode 100644 index 000000000..582a1b413 --- /dev/null +++ b/backend-api/run-tests.sh @@ -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 diff --git a/sonar-project.properties b/sonar-project.properties index d3c76dcde..435072d95 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -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