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

Test Apis Workflow #480

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
213 changes: 213 additions & 0 deletions .github/workflows/test_apis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
name: Test API Server

on:
issue_comment:
types: [created, edited]
jobs:
check:
runs-on: ubuntu-latest
if: startsWith(github.event.comment.body, '/test_') && github.event.issue.pull_request != null
outputs:
PERMISSIONS: ${{ steps.user_access.outputs.PERMISSIONS }}
TESTNAME: ${{ steps.com_chk.outputs.TESTNAME }}
steps:
- name: Check Comment
id: com_chk
run: |
#!/bin/bash
# Define the list of comments
python3 --version
comments=("/test_rs" "/test_cat" "/test_fs" "/test_di" "/test_gis" "/test_lip")
# Get the comment body from the event payload
comment_body=$(jq -r ".comment.body" "$GITHUB_EVENT_PATH")
# Check if the comment matches any of the defined comments
for i in "${comments[@]}"
do
if [[ "$comment_body" == "$i" ]]; then
# The comment matches one of the defined comments, so run the desired command
echo "The comment matches \"$i\""
echo "RESULT=TRUE" >> $GITHUB_ENV
echo "TESTNAME=$(echo $i | cut -d "_" -f 2-)" >> $GITHUB_OUTPUT
break
fi
done

- name: check user access with mgs
if: ${{ env.RESULT == 'TRUE' }}
id: user_access
run: |
#!/bin/bash
# Get the permissions of the user who created the comment
PERMISSIONS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/collaborators/${{ github.event.comment.user.login }}/permission" | jq -r ".permission")
# Check if the user has write permission
if [ "$PERMISSIONS" == "write" ] || [ "$PERMISSIONS" == "admin" ]; then
echo "User has permission"
echo "PERMISSIONS=granted" >> $GITHUB_OUTPUT
else
echo "User does not have write permission"
echo "PERMISSIONS=Not granted" >> $GITHUB_OUTPUT
pr_number=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH")
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" -X POST -d "{\"body\":\"@${{ github.event.comment.user.login }} You are not authorized to trigger this workflow. Only users with write permission can trigger this workflow.\"}" "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments"
fi

test:
needs: check
runs-on: ubuntu-latest
# Check if the permission is been granted for test, only if user has write permission or admin
if: needs.check.outputs.PERMISSIONS =='granted'
abhi4578 marked this conversation as resolved.
Show resolved Hide resolved

steps:
- name: Get Pull Request context
id: extract_pr
run: |
output=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} )
echo "OWNER=$(echo $output | jq -r '.head.repo.owner.login')" >> $GITHUB_ENV
echo "REPO=$(echo $output | jq -r '.head.repo.name')" >> $GITHUB_ENV
echo "REF=$(echo $output | jq -r '.head.ref')" >> $GITHUB_ENV

- name: Checkout PR API
uses: actions/checkout@v3
with:
repository: ${{ env.OWNER }}/${{ env.REPO }}
ref: ${{ env.REF }}

- name: Install kubectl
uses: azure/setup-kubectl@v3
with:
version: 'v1.24.8'
id: install

- name: Configure kubectl
run: |
mkdir -p $HOME/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 --decode > $HOME/.kube/config

- name: Install Helm
uses: azure/setup-helm@v3
with:
version: 'v3.9.0'

- name: Configure Server
id: details
run: |
#!/bin/bash
set -x
echo ${{ needs.check.outputs.TESTNAME }}
if [[ "${{ needs.check.outputs.TESTNAME }}" == "rs" ]]; then
echo "TESTFOLDER=resource-server" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/resource-server/example-secrets/* K8s-deployment/Charts/resource-server/
echo "${{ secrets.RS_RESOURCE_VALUES }}" > K8s-deployment/Charts/resource-server/resource-values.yaml
echo -n "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > K8s-deployment/Charts/resource-server/secrets/AWS_SECRET_ACCESS_KEY
echo -n "${{ secrets.AWS_ACCESS_KEY_ID }}" > K8s-deployment/Charts/resource-server/secrets/AWS_ACCESS_KEY_ID
echo "${{ secrets.RS_CONFIG }}" | base64 --decode > K8s-deployment/Charts/resource-server/secrets/config.json
echo "${{ secrets.RS_TEST_CONFIG }}" | base64 --decode > rs_test_config.json

elif [[ "${{ needs.check.outputs.TESTNAME }}" == "cat" ]]; then
echo "TESTFOLDER=catalogue" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/catalogue/example-secrets/* K8s-deployment/Charts/catalogue/
echo "${{ secrets.CAT_RESOURCE_VALUES }}" > K8s-deployment/Charts/catalogue/resource-values.yaml
echo "${{ secrets.CAT_CONFIG }}" | base64 --decode > K8s-deployment/Charts/catalogue/secrets/config.json
echo "${{ secrets.CAT_TEST_CONFIG }}" | base64 --decode > cat_test_config.json
elif [[ "${{ needs.check.outputs.TESTNAME }}" == "fs" ]]; then
echo "TESTFOLDER=file-server" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/file-server/example-secrets/* K8s-deployment/Charts/file-server/
echo "${{ secrets.FS_RESOURCE_VALUES }}" > K8s-deployment/Charts/file-server/resource-values.yaml
echo "${{ secrets.FS_CONFIG }}" | base64 --decode > K8s-deployment/Charts/file-server/secrets/config.json
echo "${{ secrets.FS_TEST_CONFIG }}" | base64 --decode > fs_test_config.json
elif [[ "${{ needs.check.outputs.TESTNAME }}" == "gis" ]]; then
echo "TESTFOLDER=gis-interface" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/gis-interface/example-secrets/* K8s-deployment/Charts/gis-interface/
echo "${{ secrets.GIS_RESOURCE_VALUES }}" > K8s-deployment/Charts/gis-interface/resource-values.yaml
echo "${{ secrets.GIS_CONFIG }}" | base64 --decode > K8s-deployment/Charts/gis-interface/secrets/config.json
echo "${{ secrets.GIS_TEST_CONFIG }}" | base64 --decode > gis_test_config.json

elif [[ "${{ needs.check.outputs.TESTNAME }}" == "di" ]]; then
echo "TESTFOLDER=data-ingestion" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/data-ingestion/example-secrets/* K8s-deployment/Charts/data-ingestion/
echo "${{ secrets.DI_RESOURCE_VALUES }}" > K8s-deployment/Charts/data-ingestion/resource-values.yaml
echo "${{ secrets.DI_CONFIG }}" | base64 --decode > K8s-deployment/Charts/data-ingestion/secrets/config.json
echo "${{ secrets.DI_TEST_CONFIG }}" | base64 --decode > di_test_config.json

elif [[ "${{ needs.check.outputs.TESTNAME }}" == "lip" ]]; then
echo "TESTFOLDER=latest-ingestion-pipeline" >> $GITHUB_ENV
cp -r K8s-deployment/Charts/latest-ingestion-pipeline/example-secrets/* K8s-deployment/Charts/latest-ingestion-pipeline/
echo "${{ secrets.LIP_RESOURCE_VALUES }}" > K8s-deployment/Charts/latest-ingestion-pipeline/resource-values.yaml
echo "${{ secrets.LIP_CONFIG }}" | base64 --decode > K8s-deployment/Charts/latest-ingestion-pipeline/secrets/config.json
echo "${{ secrets.LIP_TEST_SCRIPT }}" | base64 --decode > test_lip.py
echo "${{ secrets.LIP_TEST_REQ }}" > requirements.txt
pip3 install -r requirements.txt
pytest test_lip.py -k test_configuration
else
echo "Invalid input"
exit 1
fi

- name: deploy
run: |
#!/bin/bash
set -x
cd K8s-deployment/Charts/${{ env.TESTFOLDER }}/
if [[ "${{ needs.check.outputs.TESTNAME }}" == "rs" ]]; then
kubectl create secret generic rs-s3-env --from-file=./secrets/AWS_ACCESS_KEY_ID --from-file=./secrets/AWS_SECRET_ACCESS_KEY -n test-apis
fi
kubectl create configmap ${{ needs.check.outputs.TESTNAME }}-env --from-env-file=./secrets/.${{ needs.check.outputs.TESTNAME }}.env -n test-apis
kubectl create secret generic ${{ needs.check.outputs.TESTNAME }}-config --from-file=./secrets/config.json -n test-apis
helm install ${{ env.TESTFOLDER }} ../${{ env.TESTFOLDER }} -f values.yaml -f resource-values.yaml -n test-apis


- name: Install Testkube
if: ${{ needs.check.outputs.TESTNAME != 'lip' }}
run: |
wget -qO - https://repo.testkube.io/key.pub | sudo apt-key add - && echo "deb https://repo.testkube.io/linux linux main" | sudo tee -a /etc/apt/sources.list && sudo apt-get update && sudo apt-get install -y testkube

- name: Test
run: |
#!/bin/bash
set -x
sleep 2.5m
if [[ "${{ needs.check.outputs.TESTNAME }}" == "lip" ]]; then
pytest test_lip.py -k test_test --html=artifacts/report.html
else
datetime=$(date +"%Y-%m-%d-%H-%M-%S")
# Creating Testkube pytest executor
testkube create executor --name pytest-executor-gh --namespace testkube --types pytest-executor-gh1/test-gh-1 --executor-type container --image docker.io/pranav0/testkube-executor-pytest:1.1.2 --command "pytest" --command "test_${{ needs.check.outputs.TESTNAME }}.py" --command "--html=/share/test/reports/report.html" --feature artifacts --content-type string
# Creating Test on Testkube using above executor
testkube create test --name "${{ needs.check.outputs.TESTNAME }}-test-$datetime" --namespace testkube --file ${{ needs.check.outputs.TESTNAME }}_test_config.json --type pytest-executor-gh1/test-gh-1 --artifact-storage-class-name azuredisk-storage-class --artifact-volume-mount-path /share --artifact-dir test/reports
# Runing the test created above
testkube run test ${{ needs.check.outputs.TESTNAME }}-test-$datetime --download-artifacts
echo "test_name=${{ needs.check.outputs.TESTNAME }}-test-$datetime" >> $GITHUB_ENV
fi

- name: Download Test reports
if: ${{ needs.check.outputs.TESTNAME != 'lip' }}
run: |
#!/bin/bash
set -x
sleep 4m
execution_output=$(tk get execution --test ${{ env.test_name }} --output json)
result_id=$(echo $execution_output | jq -r '.results[0].id')
status=$(echo $execution_output | jq -r '.results[0].status')
# Checking status of test, if status is not passed, workflow will failed
if [[ $status != passed]]; then
echo "Status of Test: $status"
exit 1
fi
kubectl testkube download artifacts $result_id

- name: Upload test reports
uses: actions/upload-artifact@v3
with:
name: Test Reports
path: artifacts/

# This step will run always to delete deployment
- name: Delete deployment
if: always()
run: |
#!/bin/bash
helm uninstall ${{ env.TESTFOLDER }} -n test-apis
kubectl delete secret --all -n test-apis
kubectl delete configmap --all -n test-apis
if: ${{ needs.check.outputs.TESTNAME != 'lip' }}
testkube delete executor pytest-executor-gh
fi
31 changes: 25 additions & 6 deletions Docker-Swarm-deployment/single-node/lip/tests/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
# Testing

A python script to do end-to-end test of latest ingestion pipeline.
It tests by ingesting a sample surat-itms-live-eta data.
## Pre-requisites
## Pre-existing Vhost
### Pre-requisites
1. Install python3.8+ and needed packages

```sh
pip install -r requirements.txt
pip install -r lip_requirements.txt
```

2. Adjust the redis and rabbitmq connection information accordingly in
python script ```latest-ingestion-pipeline-test.py```.
python script ```lip.py```.

## Test
### Test
1. To test the working of latest ingestion pipeline
```sh
python3 latest-ingestion-pipeline-test.py
python3 lip.py
```
2. The succesful output should be following
```
LIP succsefully completed end to end testing
```
## Testing with New Vhost
### Pre-requisites
1. Install python3.8+ and needed packages

```sh
pip install -r test_lip_requirements.txt
```
2. Rename `example-config.json` to `config.json`, and fill the information accordingly
```
cp example-config.json config.json
```
3. Configure the rabbitmq for test
```
pytest test_lip.py -k test_configuration
```
### Test
1. To test the working of latest ingestion pipeline
```sh
pytest test_lip.py -k test_test
```
16 changes: 16 additions & 0 deletions Docker-Swarm-deployment/single-node/lip/tests/example-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"createVhost" : "True or False, if vhost is required to create",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should n't the True or False be without the quotes

"username": "<rmq user>",
"password": "<rmq password>",
"host": "<rmq host>",
"port": "<rmq amq port>",
"hport": "<rmq https port, required only if createVhost is true >",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use proper config fields, should be clear by just reading it

  • use httpsPort instead of hport
  • use amqpsPort instead of port --> also its better to connect and test over amqps .

"vhost": "Test-Lip",
"exchange": "lip-test",
"exchange_type": "direct",
"queue_name": "redis-latest",
"route": "lip-test",
"redis_host": "<redis hostname>",
"redis_port": "<redis Port>",
"redis_password": "<Redis Password>"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pika==1.2.0
redis==4.5.4
six==1.16.0
pytest
requests
pytest-html
Loading