Skip to content

Commit

Permalink
feat: Asenna automaattitestaustyökalut Docker-imageksi (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
haapamakim committed Jan 18, 2022
1 parent de9eaf8 commit e65198a
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ build
.version
/.env.test
/coverage

/test/.report

/robotenv.py
__pycache__
45 changes: 41 additions & 4 deletions deployment/bin/setupEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HassuSSMParameters> {
Expand Down Expand Up @@ -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,
Expand All @@ -153,18 +171,37 @@ 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", {
REACT_APP_API_URL: backendStackOutputs.AppSyncAPIURL,
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) => {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
21 changes: 21 additions & 0 deletions test/suites/lib/setup.robot
Original file line number Diff line number Diff line change
@@ -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}
20 changes: 20 additions & 0 deletions test/suites/login_tests/mytest.robot
Original file line number Diff line number Diff line change
@@ -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}
22 changes: 22 additions & 0 deletions tools/robot/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]


13 changes: 13 additions & 0 deletions tools/robot/run_suite.sh
Original file line number Diff line number Diff line change
@@ -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}``
11 changes: 11 additions & 0 deletions tools/robot/updateRobotImage.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e65198a

Please sign in to comment.