From a64f60af581fa7557ce4af314dc71062cb6a988c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Haapama=CC=88ki?= Date: Mon, 17 Jan 2022 13:21:56 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Asenna=20automaattitestausty=C3=B6kalut?= =?UTF-8?q?=20Docker-imageksi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++++ deployment/bin/setupEnvironment.ts | 45 +++++++++++++++++++++++++--- package.json | 6 +++- test/suites/lib/setup.robot | 21 +++++++++++++ test/suites/login_tests/mytest.robot | 20 +++++++++++++ tools/robot/Dockerfile | 22 ++++++++++++++ tools/robot/run_suite.sh | 13 ++++++++ tools/robot/updateRobotImage.sh | 11 +++++++ 8 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 test/suites/lib/setup.robot create mode 100644 test/suites/login_tests/mytest.robot create mode 100644 tools/robot/Dockerfile create mode 100755 tools/robot/run_suite.sh create mode 100755 tools/robot/updateRobotImage.sh diff --git a/.gitignore b/.gitignore index 11c4eccb6..a32e75443 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,8 @@ build .version /.env.test /coverage + +/test/.report + +/robotenv.py +__pycache__ diff --git a/deployment/bin/setupEnvironment.ts b/deployment/bin/setupEnvironment.ts index a729cf7be..8b7d5c28c 100644 --- a/deployment/bin/setupEnvironment.ts +++ b/deployment/bin/setupEnvironment.ts @@ -87,6 +87,11 @@ export type HassuSSMParameters = { VelhoAuthenticationUrl: string; CognitoURL: string; + + basicAuthenticationUsername: string; + basicAuthenticationPassword: string; + + FrontendDomainName: string; }; async function readParametersForEnv(environment: string, region: Region): Promise { @@ -121,15 +126,28 @@ function writeEnvFile(fileName: string, variables: { [p: string]: string }) { let envFile = "# This file is automatically generated\n"; for (const key in variables) { if (variables.hasOwnProperty(key) && variables[key]) { - envFile += key + "=" + variables[key] + "\n"; + envFile += `${key}=${variables[key]}\n`; } } fs.writeFileSync(fileName, envFile); } -export async function getEnvironmentVariablesFromSSM() { - const variables = await readParametersForEnv(BaseConfig.infraEnvironment, Region.EU_WEST_1); +function writePyEnvFile(fileName: string, variables: { [p: string]: string }) { + let envFile = "# This file is automatically generated\n"; + for (const key in variables) { + if (variables.hasOwnProperty(key) && variables[key]) { + envFile += `${key}="${variables[key]}"\n`; + } + } + + fs.writeFileSync(fileName, envFile); +} + +export async function getEnvironmentVariablesFromSSM(variables?: HassuSSMParameters) { + if (!variables) { + variables = await readParametersForEnv(BaseConfig.infraEnvironment, Region.EU_WEST_1); + } return { COGNITO_URL: variables.CognitoURL, @@ -153,11 +171,13 @@ async function main() { const searchStackOutputs = await readSearchStackOutputs(); const backendStackOutputs = await readBackendStackOutputs(); const frontendStackOutputs = await readFrontendStackOutputs(); + const variables = await readParametersForEnv(BaseConfig.infraEnvironment, Region.EU_WEST_1); + const environmentVariables = await getEnvironmentVariablesFromSSM(variables); writeEnvFile(".env.test", { SEARCH_DOMAIN: searchStackOutputs.SearchDomainOutput, FRONTEND_DOMAIN_NAME: frontendStackOutputs.CloudfrontPrivateDNSName, - ...(await getEnvironmentVariablesFromSSM()), + ...environmentVariables, }); writeEnvFile(".env.local", { @@ -165,6 +185,23 @@ async function main() { INTERNAL_BUCKET_NAME: Config.internalBucketName, FRONTEND_DOMAIN_NAME: frontendStackOutputs.CloudfrontPrivateDNSName, }); + + let server; + if (BaseConfig.isPermanentEnvironment()) { + server = + "https://" + + variables.basicAuthenticationUsername + + ":" + + variables.basicAuthenticationPassword + + "@" + + variables.FrontendDomainName; + } else { + // For local testing + server = "http://host.docker.internal:3000"; + } + writePyEnvFile("robotenv.py", { + SERVER: server, + }); } main().catch((e) => { diff --git a/package.json b/package.json index ab6c22744..63fd057d5 100644 --- a/package.json +++ b/package.json @@ -182,7 +182,11 @@ "get-next-version": "npx semantic-release --no-ci --dryRun", "release": "npx semantic-release --no-ci", "createVelhoProjekti": "ts-node --project=tsconfig.backend.json -r dotenv/config ./backend/bin/createVelhoProjekti dotenv_config_path=.env.test", - "deleteVelhoProjekti": "ts-node --project=tsconfig.backend.json -r dotenv/config ./backend/bin/deleteVelhoProjekti dotenv_config_path=.env.test" + "deleteVelhoProjekti": "ts-node --project=tsconfig.backend.json -r dotenv/config ./backend/bin/deleteVelhoProjekti dotenv_config_path=.env.test", + "robot:build": "./tools/robot/updateRobotImage.sh", + "robot:pull": "cross-env docker pull 283563576583.dkr.ecr.eu-west-1.amazonaws.com/hassu-robot:latest", + "robot": "cross-env docker run --rm --add-host=host.docker.internal:host-gateway -v $(pwd):/work 283563576583.dkr.ecr.eu-west-1.amazonaws.com/hassu-robot:latest", + "ecrLogin": "aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 283563576583.dkr.ecr.eu-west-1.amazonaws.com" }, "browserslist": { "production": [ diff --git a/test/suites/lib/setup.robot b/test/suites/lib/setup.robot new file mode 100644 index 000000000..f6474f5c4 --- /dev/null +++ b/test/suites/lib/setup.robot @@ -0,0 +1,21 @@ +*** Settings *** +Library SeleniumLibrary timeout=10 implicit_wait=1.5 run_on_failure=Capture Page Screenshot +Library XvfbRobot + +Variables ../../../robotenv.py + +*** Variables *** + +${TMP_PATH} /tmp + +*** Keywords *** +Avaa selain + Start Virtual Display 1920 1080 + ${options} Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver + Call Method ${options} add_argument --no-sandbox + ${prefs} Create Dictionary download.default_directory=${TMP_PATH} + Call Method ${options} add_experimental_option prefs ${prefs} + Create Webdriver Chrome chrome_options=${options} + +Selain on avattu kansalaisen etusivulle + GoTo ${SERVER} diff --git a/test/suites/login_tests/mytest.robot b/test/suites/login_tests/mytest.robot new file mode 100644 index 000000000..fc395205a --- /dev/null +++ b/test/suites/login_tests/mytest.robot @@ -0,0 +1,20 @@ +*** Settings *** +Resource ../lib/setup.robot + +Suite Setup Avaa selain + +Test Teardown Run Keyword If Test Failed Capture Page Screenshot + +Suite Teardown Close All Browsers + +*** Variables *** + +*** Test Cases *** +Avaa kansalaisen etusivu + Selain on avattu kansalaisen etusivulle + Sivun otsikon pitäisi olla Hassu + +*** Keywords *** +Sivun otsikon pitäisi olla ${expected_title} + ${title}= Get Title + Should Be Equal ${expected_title} ${title} diff --git a/tools/robot/Dockerfile b/tools/robot/Dockerfile new file mode 100644 index 000000000..ad3a0df15 --- /dev/null +++ b/tools/robot/Dockerfile @@ -0,0 +1,22 @@ +FROM python:3.10.0a6-buster + +RUN apt-get update \ + && apt-get install -y xvfb wget ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 \ + libatspi2.0-0 libcups2 libdbus-1-3 libgbm1 libgtk-3-0 libnspr4 libnss3 \ + libxcomposite1 libxkbcommon0 libxrandr2 xdg-utils ntpdate openssl + +RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \ + && dpkg -i google-chrome*.deb \ + && rm google-chrome*.deb + +RUN python3 -m pip install robotframework && pip install robotframework-requests \ + && pip install xvfbwrapper && pip install robotframework-xvfb && pip install certifi && pip install asn1crypto \ + && pip install bcrypt && pip install robotframework-sshlibrary && pip install cryptography && pip install pyOpenSSL \ + && pip install idna && pip install requests[security] && pip install robotframework-seleniumlibrary + +RUN pip install webdrivermanager +RUN webdrivermanager firefox chrome --linkpath /usr/local/bin + +CMD ["/work/tools/robot/run_suite.sh"] + + diff --git a/tools/robot/run_suite.sh b/tools/robot/run_suite.sh new file mode 100755 index 000000000..f4380198f --- /dev/null +++ b/tools/robot/run_suite.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +# Run an individual test suite if the TEST_SUITE environmental variable is set. +if [ -z "$TEST_SUITE" ]; then + TEST_SUITE="" +fi + +CMD="robot --console verbose --outputdir /work/test/.report /work/test/suites/$TEST_SUITE" + +echo ${CMD} + +``${CMD}`` diff --git a/tools/robot/updateRobotImage.sh b/tools/robot/updateRobotImage.sh new file mode 100755 index 000000000..86abc4202 --- /dev/null +++ b/tools/robot/updateRobotImage.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 283563576583.dkr.ecr.eu-west-1.amazonaws.com + +IMAGE_TAG=hassu-robot +REPO_TAG=283563576583.dkr.ecr.eu-west-1.amazonaws.com/$IMAGE_TAG:latest +aws ecr create-repository --repository-name $IMAGE_TAG || true +docker pull $REPO_TAG || true +docker build --progress=plain --cache-from $REPO_TAG -t $IMAGE_TAG:latest -f ./tools/robot/Dockerfile . +docker tag $IMAGE_TAG:latest $REPO_TAG +docker push $REPO_TAG