CI for AWS Amplify Console with Python unittest and Karate
Sample configuration to enable CI regular execution to Amplify Console.
AWS Amplify Console is an easy Continuous Deployment service. I can just start to use it with several settings. But, there is no configuration for Continuous Integration. As a developer, both CI/CD are necessary for happy development life.
Pre-condition
As an example of applying CI/CD configuration, will use following contents.
https://github.com/kojiisd/cognito-user-manager
This is just a sample web application to control Amazon Cognito UserPool.
GOAL
Followings are goals of this article.
- Enable to configure to execute Unit testing on Amplify Console with unittest.
- Enable to configure to execute Scenario testing on Amplify Console with Karate.
- Generates HTML report of above results and enable to be accessed from browsers. (On Amplify Console, cannot leave files generated by process on local storage.)
- Creates an amplify.yml to enable above conditions.
RESULT
Below is a result.
https://github.com/kojiisd/cognito-user-manager/blob/master/amplify.yml
Enable to configure to execute Unit testing on Amplify Console with unittest.
The key points are to edit amplify.yml but I need to prepare several things for build process.
Configure of AWS Authentication
This program can be deployed by SERVERLESS FRAMEWORK, but there is no authentication configuration on Amplify Console to use the framework. So needs to put following scripts.
- mkdir -p ~/.aws
- echo "[${SLS_AWS_ACCOUNT_ID}]" > ~/.aws/credentials
- echo "aws_access_key_id = ${ACCESS_KEY}" >> ~/.aws/credentials
- echo "aws_secret_access_key = ${SECRET_KEY}" >> ~/.aws/credentials
Install scripts for installation of Python and Pip
For HTML report generation with unittest, needs to install PyUnitReport. Default version of Python in repository is 3.4 ?
- yum update python34 -y
- yum -y install python34
- python3 — version
- curl https://bootstrap.pypa.io/get-pip.py | python3
- pip3 -V
- pip3 install -r requirements_test.txt -t libs
- python3 tests/test_user_handler.py
PyUnitReport installation is configured in requirements_test.txt.
pyunitreport==0.1.4
Installs Java and Maven for Karate execution
Karate can be executed on Maven and Gradle, in this time, I chose Maven.
- yum install -y java-1.8.0-openjdk-devel.x86_64
- yum install -y apache-maven
Install forever for Daemonizing Node.js
In this case I didn’t choose “ng e2e” command for executing Karate. To Daemonize “ng serve” process, installs forever library.
- npm install -g forever
Execute each Unit Testing and Scenario Testing
So far I finished preparations, try to execute Unit Testing and Scenario Testing. HTML reports will be generated as a result, try to upload to S3 specific bucket.
# unittest execution and upload the result to S3
- python3 tests/test_user_handler.py
- ls reports/html_report/ > file_path.txt
- aws s3 cp reports/html_report/`cat file_path.txt` s3://${REPORT_BUCKET}/index.html --profile=${SLS_AWS_ACCOUNT_ID}
:
:
# Karate execution and upload the result to S3
- mvn clean test
- ls target/
- cd target/surefire-reports/
- aws s3 cp ./ s3://${REPORT_BUCKET}/karate/ --recursive --profile=${SLS_AWS_ACCOUNT_ID}
- aws s3 cp ../karate.log s3://${REPORT_BUCKET}/karate/karate.log --profile=${SLS_AWS_ACCOUNT_ID}
The results of reporting are below. It can work well.
Conclusion
Currently AWS Amplify Console itself is for Continuous Delivery, but it can work for Continuous Integration with several customizations. As developer, not only CD but also CI is necessary for concentration of Developing. I expect Amplify Console upgrading.