Skip to content

Commit

Permalink
[MAINTENANCE] Unit tests for allure report and report push lambdas (#88)
Browse files Browse the repository at this point in the history
* feat: pack unit test into docker
  • Loading branch information
a-chumagin authored Jun 9, 2023
1 parent c49aedc commit 3bda82c
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
run: |
curl http://localhost:4566/_localstack/health -i
- name: Run tests
run: make run-integration-tests QA_BUCKET=dqg-settings-local HOST=172.17.0.1
run: make run-integration-tests QA_BUCKET=dqg-settings-local HOST=172.17.0.1 test=data_test
8 changes: 6 additions & 2 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ on:
jobs:
unit-tests:
runs-on: ubuntu-latest
strategy:
matrix:
test-type: [data_test, allure_report, report_push]
steps:
- name: checkout
uses: actions/checkout@v3
- name: run data test unit tests
run: make run-unit-tests-in-docker
- name: Run unit tests
run: |
make run-unit-tests test=${{ matrix.test-type }}
46 changes: 20 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
HOST := host.docker.internal
PORT := 4566
QA_BUCKET := integration-test-bucket

INTEGRATION_TESTS_DIR := ./tests/integration_tests/test_data_tests
DATA_TEST_UNIT_TESTS_DIR := ./tests/unit_tests/data_test
DATA_TEST_UNIT_TESTS_IMG := data_test_unit_tests
DATA_TEST_INTEGRATION_TESTS_IMG := data_test_integration_tests
DATA_TEST_IMAGE_NAME := data_test
DATA_TEST_IMAGE_VERSION := latest
IMAGE_VERSION := latest

run-localstack:
docker run --rm -d -p 4566:4566 -p 4510-4559:4510-4559 localstack/localstack:1.3.1
Expand All @@ -17,27 +11,27 @@ deploy-qa-infra:
terraform init && \
terraform apply -auto-approve

build-data-test-img:
cd ./functions/data_test && \
docker build -t $(DATA_TEST_IMAGE_NAME):$(DATA_TEST_IMAGE_VERSION) .
build-lambda-img:
cd ./functions/$(test) && \
docker build -t $(test):latest .

build-data-test-tests-img: build-data-test-img
cd $(INTEGRATION_TESTS_DIR) && \
docker build --build-arg="IMAGE_NAME=$(DATA_TEST_IMAGE_NAME)" \
--build-arg="VERSION=$(DATA_TEST_IMAGE_VERSION)" \
-t $(DATA_TEST_INTEGRATION_TESTS_IMG) .
build-integration-tests-img: build-lambda-img
cd ./tests/integration_tests/$(test) && \
docker build --build-arg="IMAGE_NAME=$(test)" \
--build-arg="VERSION=$(IMAGE_VERSION)" \
-t "$(test)_integration_tests" .

run-integration-tests: build-data-test-img build-data-test-tests-img
cd $(INTEGRATION_TESTS_DIR)
run-integration-tests: build-integration-tests-img
cd ./tests/integration_tests/$(test) && \
docker run --env BUCKET=$(QA_BUCKET) \
--env S3_HOST=$(HOST) --env S3_PORT=$(PORT) $(DATA_TEST_INTEGRATION_TESTS_IMG)
--env S3_HOST=$(HOST) --env S3_PORT=$(PORT) $(test)_integration_tests

build-data-test-unit-tests-img: build-data-test-img
cd $(DATA_TEST_UNIT_TESTS_DIR) && \
docker build --build-arg="IMAGE_NAME=$(DATA_TEST_IMAGE_NAME)" \
--build-arg="VERSION=$(DATA_TEST_IMAGE_VERSION)" \
-t $(DATA_TEST_UNIT_TESTS_IMG) .
build-unit-tests-img: build-lambda-img
cd ./tests/unit_tests/$(test) && \
docker build --build-arg="IMAGE_NAME=$(test)" \
--build-arg="VERSION=$(IMAGE_VERSION)" \
-t $(test)_unit_tests .

run-unit-tests-in-docker: build-data-test-unit-tests-img
cd $(DATA_TEST_UNIT_TESTS_DIR) && \
docker run $(DATA_TEST_UNIT_TESTS_IMG)
run-unit-tests: build-unit-tests-img
cd ./tests/unit_tests/$(test) && \
docker run $(test)_unit_tests
2 changes: 1 addition & 1 deletion functions/report_push/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM public.ecr.aws/lambda/python:3.9
COPY ./*.py ./
ADD ./requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY ./*.py ./
CMD ["push_data_report.handler"]
15 changes: 11 additions & 4 deletions functions/report_push/push_data_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import random
from jira_events import auth_in_jira, get_all_issues, open_bug

cloudwatch = boto3.client('cloudwatch')
s3 = boto3.resource('s3')
sns = boto3.client('sns')

Expand Down Expand Up @@ -92,7 +91,12 @@ def handler(event, context):
created_bug_count, bug_name = create_jira_bugs_from_allure_result(
bucket, key, replaced_allure_links, suite, jira_project_key)

push_cloudwatch_metrics(suite, environment, failed, created_bug_count)
cloudwatch = boto3.client('cloudwatch')
push_cloudwatch_metrics(suite,
environment,
failed,
created_bug_count,
cloudwatch)
push_sns_message(
suite,
run_name,
Expand Down Expand Up @@ -187,7 +191,11 @@ def push_sns_message(
MessageStructure=message_structure)


def push_cloudwatch_metrics(suite, environment, failed, created_bug_count):
def push_cloudwatch_metrics(suite,
environment,
failed,
created_bug_count,
cloudwatch):
if created_bug_count > 0:
metric_data = {
'MetricName': 'bug_created_count',
Expand Down Expand Up @@ -220,7 +228,6 @@ def push_cloudwatch_metrics(suite, environment, failed, created_bug_count):
'Value': failed,
'Unit': 'Count'
}

cloudwatch.put_metric_data(
Namespace='Data-QA',
MetricData=[
Expand Down
7 changes: 7 additions & 0 deletions tests/unit_tests/allure_report/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG IMAGE_NAME=allure_report
ARG VERSION=latest
FROM ${IMAGE_NAME}:${VERSION}
RUN pip install pytest==7.3.1
ENV BUCKET=test-bucket
COPY ./test*.py ./
ENTRYPOINT ["pytest", "-qvs"]
13 changes: 13 additions & 0 deletions tests/unit_tests/allure_report/test_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from mapper import get_test_name


def test_get_test_name():
file = {
"expectation_config": {
"expectation_type": "expect_column_values_to_not_be_null",
"kwargs": {
"column": "age"
}
}
}
assert get_test_name(file) == "expect_column_values_to_not_be_null"
16 changes: 16 additions & 0 deletions tests/unit_tests/report_push/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG IMAGE_NAME=report_push
ARG VERSION=latest
FROM ${IMAGE_NAME}:${VERSION}
RUN pip install pytest==7.3.1 moto==4.1.6
ENV ENVIRONMENT=local \
BUCKET=test-bucket \
DYNAMODB_TABLE=test-table \
AWS_ACCESS_KEY_ID=test \
AWS_SECRET_ACCESS_KEY=test \
AWS_DEFAULT_REGION=us-east-1 \
SNS_BUGS_TOPIC_ARN=test-topic-arn \
JIRA_URL=https://jira.localhost \
DATAQA_JIRA_USERNAME=qauser \
DATAQA_JIRA_PASSWORD=qapassword
COPY ./test*.py ./
ENTRYPOINT ["pytest", "-qvs"]
44 changes: 44 additions & 0 deletions tests/unit_tests/report_push/test_push_data_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from push_data_report import push_cloudwatch_metrics
from moto import mock_cloudwatch
import boto3


suite = 'test_suite'
environment = 'test_environment'


@pytest.fixture
@mock_cloudwatch
def conn():
return boto3.client('cloudwatch')


@mock_cloudwatch
def test_push_cloudwatch_metrics_bug_created(conn):
# Define test input
failed = 0
created_bug_count = 3

# Call the method under test
push_cloudwatch_metrics(suite, environment, failed, created_bug_count,
conn)
metrics = conn.list_metrics()["Metrics"]
assert metrics[0]['MetricName'] == 'bug_created_count'
assert metrics[0]['Dimensions'][0]['Value'] == 'test_suite'
assert metrics[0]['Dimensions'][1]['Value'] == 'test_environment'


@mock_cloudwatch
def test_push_cloudwatch_metrics(conn):
# Define test input
failed = 5
created_bug_count = 0

# Call the method under test
push_cloudwatch_metrics(suite, environment, failed, created_bug_count,
conn)
metrics = conn.list_metrics()["Metrics"]
assert metrics[0]['MetricName'] == 'suite_failed_count'
assert metrics[0]['Dimensions'][0]['Value'] == 'test_suite'
assert metrics[0]['Dimensions'][1]['Value'] == 'test_environment'

0 comments on commit 3bda82c

Please sign in to comment.