From 666d8076753cbbff928b34392330a7927c66381e Mon Sep 17 00:00:00 2001 From: JD Babac Date: Fri, 30 May 2025 02:20:00 +0800 Subject: [PATCH 1/2] IDEV-2133: Integrate e2e tests in CI pipeline. --- .github/workflows/test-build-publish.yml | 32 +++++++++++++++++ tests/e2e/scripts/cleanup_e2e.sh | 15 ++++++++ tests/e2e/scripts/setup_e2e.sh | 32 +++++++++++++++++ tests/e2e/scripts/test_e2e_runner.sh | 45 +++--------------------- 4 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 tests/e2e/scripts/cleanup_e2e.sh create mode 100644 tests/e2e/scripts/setup_e2e.sh mode change 100755 => 100644 tests/e2e/scripts/test_e2e_runner.sh diff --git a/.github/workflows/test-build-publish.yml b/.github/workflows/test-build-publish.yml index 7f7d497..46772b5 100644 --- a/.github/workflows/test-build-publish.yml +++ b/.github/workflows/test-build-publish.yml @@ -29,6 +29,38 @@ jobs: export TOX_SKIP_MISSING_INTERPRETERS="False"; tox -e py + e2e-tests: + runs-on: ubuntu-latest + env: + MITM_BASIC_AUTH_CONTAINER_NAME: e2e_test_mitm_basic_auth + MITM_CUSTOM_CERT_CONTAINER_NAME: e2e_test_mitm_custom_cert + DOCKER_NETWORK_NAME: e2e_test_docker_network + TEST_USER: integrations_testing + TEST_KEY: ${{ secrets.TEST_KEY }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e . + pip install -r ./requirements/development.txt + + - name: Setup E2E environment + run: | + sh ./tests/e2e/scripts/setup_e2e.sh + + - name: Run E2E tests + run: | + python -m pytest -s --capture=sys -v --cov=domaintools tests/e2e + + - name: Cleanup E2E environment + if: '!cancelled()' + run: | + sh ./tests/e2e/scripts/cleanup_e2e.sh + # run only in main and in pull request to `main` and in publish release release-build: if: | diff --git a/tests/e2e/scripts/cleanup_e2e.sh b/tests/e2e/scripts/cleanup_e2e.sh new file mode 100644 index 0000000..17e33fa --- /dev/null +++ b/tests/e2e/scripts/cleanup_e2e.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Script Requirements +# docker + +# Clean up containers +echo "Bringing down containers..." +docker stop ${MITM_BASIC_AUTH_CONTAINER_NAME} || true +docker stop ${MITM_CUSTOM_CERT_CONTAINER_NAME} || true +docker network rm ${DOCKER_NETWORK_NAME} || true + +# Clean up custom certs +echo "Removing custom certs..." +sudo rm -rf tests/e2e/mitmproxy-ca.pem +sudo rm -rf ~/.test_mitmproxy diff --git a/tests/e2e/scripts/setup_e2e.sh b/tests/e2e/scripts/setup_e2e.sh new file mode 100644 index 0000000..252b826 --- /dev/null +++ b/tests/e2e/scripts/setup_e2e.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Script Requirements +# docker + +echo "Create a bridge network for the containers to communicate" +docker network create ${DOCKER_NETWORK_NAME} + +echo "Spinning ${MITM_BASIC_AUTH_CONTAINER_NAME}" +docker run --rm -d \ + --name ${MITM_BASIC_AUTH_CONTAINER_NAME} \ + --network ${DOCKER_NETWORK_NAME} \ + -p 8080:8080 mitmproxy/mitmproxy mitmdump \ + --set proxyauth="username:pass" + +echo "Spinning ${MITM_CUSTOM_CERT_CONTAINER_NAME}" +docker run --rm -d -v ~/.test_mitmproxy:/home/mitmproxy/.mitmproxy \ + --name ${MITM_CUSTOM_CERT_CONTAINER_NAME} \ + --network ${DOCKER_NETWORK_NAME} \ + -p 8090:8090 mitmproxy/mitmproxy mitmdump \ + --set listen_port=8090 + +# Check until custom cert from mitmproxy container is copied locally +echo "Checking for valid custom cert..." +while [ ! -f ~/.test_mitmproxy/mitmproxy-ca.pem ] ; +do + sleep 2 +done +echo "Valid custom cert found!" + +# Copy valid custom cert to target dir +docker cp ${MITM_CUSTOM_CERT_CONTAINER_NAME}:/home/mitmproxy/.mitmproxy/mitmproxy-ca.pem ./tests/e2e/mitmproxy-ca.pem diff --git a/tests/e2e/scripts/test_e2e_runner.sh b/tests/e2e/scripts/test_e2e_runner.sh old mode 100755 new mode 100644 index 48c8a37..079a154 --- a/tests/e2e/scripts/test_e2e_runner.sh +++ b/tests/e2e/scripts/test_e2e_runner.sh @@ -3,50 +3,15 @@ # Script Requirements # docker -MITM_BASIC_AUTH_CONTAINER_NAME="e2e_test_mitm_basic_auth" -MITM_CUSTOM_CERT_CONTAINER_NAME="e2e_test_mitm_custom_cert" -DOCKER_NETWORK_NAME="e2e_test_docker_network" +export MITM_BASIC_AUTH_CONTAINER_NAME="e2e_test_mitm_basic_auth" +export MITM_CUSTOM_CERT_CONTAINER_NAME="e2e_test_mitm_custom_cert" +export DOCKER_NETWORK_NAME="e2e_test_docker_network" echo "Starting e2e tests..." -echo "Create a bridge network for the containers to communicate" -docker network create ${DOCKER_NETWORK_NAME} - -echo "Spinning ${MITM_BASIC_AUTH_CONTAINER_NAME}" -docker run --rm -d \ - --name ${MITM_BASIC_AUTH_CONTAINER_NAME} \ - --network ${DOCKER_NETWORK_NAME} \ - -p 8080:8080 mitmproxy/mitmproxy mitmdump \ - --set proxyauth="username:pass" - -echo "Spinning ${MITM_CUSTOM_CERT_CONTAINER_NAME}" -docker run --rm -d -v ~/.test_mitmproxy:/home/mitmproxy/.mitmproxy \ - --name ${MITM_CUSTOM_CERT_CONTAINER_NAME} \ - --network ${DOCKER_NETWORK_NAME} \ - -p 8090:8090 mitmproxy/mitmproxy mitmdump \ - --set listen_port=8090 - -# Check until custom cert from mitmproxy container is copied locally -echo "Checking for valid custom cert..." -while [ ! -f ~/.test_mitmproxy/mitmproxy-ca.pem ] ; -do - sleep 2 -done -echo "Valid custom cert found!" - -# Copy valid custom cert to target dir -cp ~/.test_mitmproxy/mitmproxy-ca.pem tests/e2e/mitmproxy-ca.pem +sh ./tests/e2e/scripts/setup_e2e.sh echo "E2E processing..." python -m pytest -s --capture=sys -v --cov=domaintools tests/e2e -# Clean up containers -echo "Bringing down containers..." -docker stop ${MITM_BASIC_AUTH_CONTAINER_NAME} || true -docker stop ${MITM_CUSTOM_CERT_CONTAINER_NAME} || true -docker network rm ${DOCKER_NETWORK_NAME} || true - -# Clean up custom certs -echo "Removing custom certs..." -rm -rf tests/e2e/mitmproxy-ca.pem -rm -rf ~/.test_mitmproxy +sh ./tests/e2e/scripts/cleanup_e2e.sh From ec13bfa9b114242f20894bcd1fbbbd87ab30c60b Mon Sep 17 00:00:00 2001 From: JD Babac Date: Sat, 31 May 2025 01:12:50 +0800 Subject: [PATCH 2/2] IDEV-2133: Add instructions in running e2e tests locally in README.md --- README.md | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 07d234c..7e06f3f 100644 --- a/README.md +++ b/README.md @@ -270,4 +270,44 @@ results = api.nod(sessionID="my-session-id", after=-7200) for partial_result in results.response() # generator that holds NOD feeds data for the past 2 hours and is expected to request multiple times # do things to partial_result -``` \ No newline at end of file +``` + + +Running E2E Tests Locally +=================== +For now, e2e tests only covers proxy and ssl testing. We are expected to broaden our e2e tests to other scenarios moving forward. +To add more e2e tests, put these in the `../tests/e2e` folder. + +## Preparation +- Create virtual environment. + ```bash + python3 -m venv venv + ``` + +- Activate virtual environment + ```bash + source venv/bin/activate + ``` + +- Install dependencies. + ```bash + pip install -r requirements/development.txt + ``` + +- From the python_api project root directory, install the package. + ```bash + pip install -e . + ``` + +- Export api credentials to use. + ```bash + export TEST_USER= + export TEST_KEY= + ``` + +## Run the end-to-end test script +- Before running the test, be sure that docker is running. +- Execute the e2e test script . + ```bash + sh tests/e2e/scripts/test_e2e_runner.sh + ```