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

feat: Asenna automaattitestaustyökalut Docker-imageksi #93

Merged
merged 1 commit into from
Jan 18, 2022
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
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